Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Friday, 4 July 2014

Sniff 101 - the Weather station demo!

It's not much of a claim to say I'm the most experience Sniff programmer! Sniff is a new project, and we're still figuring out what it can do. For example we'll soon be releasing code for the sPhone - a mobile phone written in Sniff on Arduino. You can control equipment by phoning it up or texting it, and have it send out status reports to your phone where ever you are. We think that's pretty exciting, and I can't wait to show that off here...

However, most of you are probably a lot newer to Sniff. While showing off advanced examples is great fun, I thought it would be useful to go back to one of the first things we ever built in Sniff - the first demo where we got really excited, that maybe we were on to something that would make physical computing really accessible.

The weather station project is really easy, but produces great results, and is bizarrely addictive: I had it running on my desk for several months, and was constantly checking in to see what the temperature and humidity was.

We'll assume you've got an Arduino! The only other thing you need is a DHT11 temperature and humidity sensor. These cost £0.99 on ebay. If you're only buying one or two, then I'd recommend you push the boat out and get the "deluxe" DHT22, which costs about £3, as its much more accurate, but the DHT11 works identically, so if you just want to get something working (and need a dozen or so!) then get the cheap ones.

To wire them up, place them on their back (grill holes up), and connect the left pin to 5v, the next pin to the arduino's A1 pin and the right pin to ground. This will leave the third pin unconnected - it doesn't do anything. My preferred way of connecting these (and anything else) is to connect them to a 3pin dupont female header wired as 0v, 5V, signal. (the simplest and cheapest way to do this is to get a female-female cable, and cut it in half).  This is a pretty standard pinout, so you can plug them into breakout boards like the sensor shield, or anything that takes 3pin Grove connectors - You've now build your own equivalent of an £8 grove temperature/humitidy "brick" for about £1.50. 


Plug that into your Arduino - I've chosen to plug it into A1, but any free data pin will work fine. Now we need to code it:

make thermometer dht11 device A1
make humidity number
make temperature number

when start
.forever
..tell thermometer to "read"
..
..say join "Temperature:" [ temperature ]
..say join "Humidity:" [ humidity ]
..

..wait 5 secs

And that's it! First off we make a dht device on pin A1. This is going to use the variables humidity and temperature, so we make these too. Sniff programs are started by broadcasting the start message, so when the arduino powers on, it goes into the forever loop, asking the DHT device to "read". This does the hard work of talking to the hardware, and puts the results into the variables we've prepared.

Then we just print out the results. Say sends the results back to the computer, so you can see them in the Arduino Serial Monitor, or other terminal software (On Pi you can just type "cat </dev/ttyACM0"). Sniff can only print out a single string, so we need to join the label text ("Temperature:") with the result (the variable temperature). However remember Sniff is a typed  language, so the number 35 isn't the same as the string "35". Putting the variable in square brackets is the Sniff notation to say that we want to turn a number into a string.

Once we've printed out the results, we wait 5 seconds - in part this is just so we don't swamp the computer with results (after all air temperature doesn't change that quickly!). However the DHT's have limits on how fast they can measure the air conditions, so 5 seconds is a reasonable waiting time.


That's great but what if we want to actually put this thing somewhere - we don't really want to be carrying around a computer screen! There's an earlier post about all the different display devices you can connect, but the simplest is an LCD/keypad shield. It just plugs in and you're good to go - no wiring required. They cost about £5, so even if you're buying an Arduino, LCD shield and DHT, you can still bring this thing home for less that £15, and all of the parts are reusable for future experiments and projects.


The one disadvantage of the keypad shield is that it covers the Arduino's pins, so they're harder to get to. However it does (usually!) have holes for the pins available, so you can solder on extra header pins to the LCD shield to get access to the Arduino again. However a sensor shield gives you access to the Analog pins, so using them is probably easier (A sensor shield costs about £3, and makes hooking things up a lot easier - especially if you've wired them to a 0v/5v/S dupont header).

To draw something on the LCD we just make an LCD device and tell it to "show" a message:

make lcd device
make message string

when start
.set message to "Hello World"
.tell lcd to "show"

Easy! Now combining that with our earlier code:

make thermometer dht11 device A1
make humidity number
make temperature number

make lcd device
make message string

when start
.forever
..tell dht to "read"
..
..set message to join "Temperature:" [ temperature ]
..tell lcd to "show"
..
..set message to join "Humidity:" [ humidity ]
..tell lcd to "show"
..
..wait 5 secs
A functioning weather station in a few lines of simple Sniff code. You can use this as a jumping off point for all sorts of bigger projects. You could add an SD card, and log data to that. You could add a tft screen and plot the temperature over time (and calculate the dew point, and plot that too!). Adding Ethernet and you could share the data on a website.

You can also easily record more types of data - the BMP180 air pressure sensor (which also has a thermometer build in) is only a couple of pounds, and connects directly. Similarly Maplin sell an anemometer to record wind speed, and a rainfall meter for about £3 each. Both of these are easy to integrate.

Right now its 21 degrees and 35% humidity - that means its a nice warm, sunny day!

No comments:

Post a Comment