Writing RAM efficient Arduino code

Developing the software for the wind stations I often run into problems and random restarts of the Arduino. Soon I realised it was due to that I ran out of RAM, 2k does not last that long, specifically when every debug string you put in consumes RAM. I found out about PROGMEM but I did not find so much more information on how to use it so instead I focused on getting things to work. But now when I re-write the whole communication code I plan to do it right!

So have looked a bit more into it and found the great article Putting constant data into program memory (PROGMEM) by Nick Gammon. It explains the whole problem as well as give many practical tips on how to write RAM efficient Arduino code.

And another recommendation is to use the latest Arduino IDE, the 1.5.8 Beta. It uses much newer compiler etc as described in this post. Also when compiling it generates not only information about how much of the storage space you sketch uses but also how much RAM is used at start. Like this:
Sketch uses 9,412 bytes (30%) of program storage space. Maximum is 30,720 bytes.
Global variables use 1,126 bytes (54%) of dynamic memory, leaving 922 bytes for local variables. Maximum is 2,048 bytes.

Remember that this only indicates the situation at start so do you have functions that use large arrays that will not be shown there.

Debugging HTTP communication over GSM

I’m writing a HTTP client for the Arduino. The debugging options on the Arduino are quite limited. And in general doing low level HTTP programming a tool like SockSpy is invaluable. Though for traffic to reach SockSpy it must be connected to a public TCP IP port. With the common use of firewalls and NATed networks this is generally not the case. How do you solve that?

If you have access to a public server on the internet, SSH is your salvation! Setup a reverse SSH tunnel from your server(yelloworb.com in my case, port 3000) to your local machine(port 2000), then configure SockSpy to connect back to the server(if thats the web server you try to talk HTTP with). Like this:
ssh -R 3000:localhost:2000 yelloworb.com
sockspy 2000 www.server.com 80

And change your code to talk to your server instead(yelloworb.com and port 3000 in my case).

To get the port forwarding working you need to enable GatewayPorts in the sshd configuration. Read here how to do that. The SSHd manpages has more useful information.

How to get PostgreSQL to work with Rails3 on Mac OSX snowleopard

I’m about to host a web app on Heroku and they are using PostgreSQL for database. Followed the instructions and added gem ‘pg’ to my Gemfile and made bundle install but it failed:
Installing pg (0.11.0) with native extensions /Users/kalle/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:552:in `rescue in block in build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

Thought I didn’t have PostgreSQL installed so installed it via MacPorts:
sudo port install postgresql90

but still got the same error! After a bit of googling I found out it was due to that the bin folder was not in the path. So by doing this it worked:
export PATH=/opt/local/lib/postgresql90/bin/:${PATH}
env ARCHFLAGS="-arch x86_64" bundle install

Vindmätarbygge – framgångar

Sist jag rapporterade om projektet hade jag bara lyckats kompilera NewSoftSerial men sedan dess har jag lyckats köra det och efter ytterligare en del omkodning bla pga Minins minimala mängd RAM så fungerar det nästan!

Minin har bara 1k RAM så jag kan inte läsa in hela HTML svaret i minnet för det kan mkt väl vara större än så. Eftersom jag bara är intresserad att just nu rapportera in svar och att det går bra skrev jag en ny POST funktion som läser rad efter rad och kollar om en av dem innehåller HTTP/1.1 200 OK så jag vet att det gick bra. Man skulle kunna göra en utökning av GET med som bara returnerar själva XML delen som ett REST anrop ger tillbaka, allt för att nyttja så minimalt med RAM som möjligt.

Dock har jag ett problem kvar, efter att läst en del data så bootar Arduinon om! Jag har lyckats lokalisera det till när jag gör läsning från porten så gissar att NewSoftSerial inte klarar sig så bra i mycket minnes begränsat utrymme. Någon som har bra tips om hur man lätt debuggar dessa typer av problem? Bara göra massa print känns för osäkert då det kan uppstå timing problem etc…

Vindmätarbygge – serieport

Arduino Mini Pro har bara en hårdvarubaserad serial port. GSM Arduino lib som jag tänker använda kräver en för att prata med Telit kretsen men samtidigt vill man gärna ha på debuggingen och den är bunden till den enda hårdvaruporten som finns. Problem!

Det finns ju ett Arduino lib för mjukvaruportar men det är en hel del som inte stöds i det. Men dock finns NewSoftSerial som har stöd för det mesta så det borde inte vara så svårt att skriva om GSM Arduino lib att använda den serieporten istället. Så det blev kvällens lilla projekt.

Så nu har jag en ny version av det som jag kallar Soft GSM Arduino lib. Dock krävde det att jag fick hacka om NewSoftSerial lite också så att den blev än mer kompatibel med vanliga hårdvaruserieportarna. Dock inte hunnit med att testa något alls, men det kompilerar iaf:)

Nu är det dags att sova, god natt!

Vindmätarbygge – vindgivare

Som jag skrev igår skickade jag mail till flera svenska återförsäljare. Fick ett par svar idag och tänkte bara snabbt sammanfatta läget.

Safecast – skickade ett långt svar men hade inget konkret svar men var intresserade och ville veta mer. Diskussionen fortsätter och skickade dem ett längre mail precis.

Svenska Termoinstrument – svarade att de representerar två tyska tillverkare av vindgivare (Adolf Thies GmbH resp Lufft GmbH ) vars givare endast är avsedda för industrin (vindkraft, fastighetsautomation etc.). Några lågprisgivare under 1500 kr finns tyvärr inte i deras sortiment.

Livedata – deras billigaste givare låg på ca 5000 kr, långt över vad jag hoppats på. De får gå bort.

FDS Mätteknik har jag inte hört något från. Dock fick jag en kommentar från Magnus som byggt WindWiz på mitt förra inlägg att ClasOhlson har en trådkopplad givare som reservdel till en annan väderstation. 440 kr kostar vindriktning och vindhastighetsgivare ihop. Beställt!