So, the next stage is to read the temperature from a sensor and submit that to the database. The network code is pretty standard, based as it is on the previous post, so it’s the sensor bit that’s important.
I started tonight’s session by making a mood lamp. I bought an RGB LED (CPC code SC11728); this looks like a chip but is three LEDs in one package. Based on previous code, it was fairly easy to randomise an RGB value and set it, then wait a few seconds, re-randomise and fade smoothly. Amazing what cool stuff you can make with very little effort and know-how 🙂
Project-wise, I was looking at the Dallas DS18B20 (CPC code SC10426). This is a digital thermistor that sits on the Dallas One-Wire bus. Previously, this was something I’d only seen when configuring a Linux kernel; now, it’s time to use it.
The DS18B20 can be powered in two ways: regularly or parasitic. The latter means you need to run one fewer wire to the component but can cause a 750ms delay in reading the temperature. That’s not such an issue for me, so I went with parasitic mode as my book instructed me. The book also told me to use a 4.7K pull-up resistor; however, I didn’t have one of those so I used two 10Ks in parallel instead (R = 1/(1/R1 + 1/R2)). I figured 5K would be close enough; I have some 4.7Ks on order (Rapid code 63-1376).
The circuit diagram looks like this:
Multiple One Wire components (of which the DS18B20 seems the most common) can be strung together on one wire, which is ace because the Arduino has limited pin availability. You just connect all the ground pins and all the data pins together and hey presto! However, the accumulated lag could be irritating so I’d probably go with regular power rather than parasitic.
The Arduino IDE doesn’t come with One Wire or Dallas Temperature libraries by default; however, they’re readily available with a quick Google and installation was a case of unzipping them into the right place. The library provides methods for reading serial numbers of every device on the bus, reading the temperature, converting to degrees C or F, all the stuff you need. Decent enough.
So, a little bit of C++ to read the device and then existing network code to submit to the DB, which logs the reading and records the time against it. It also notes the “check-in”, which might be useful for diagnostics later. Pertinent code fragments as follows:
const int oneWirePin = 3;
const int temperaturePrecision = 12;
sensors.setResolution( thermometer, temperaturePrecision);
Network::SubmitReading( zoneId, sensors.getTempC(thermometer) );
Was pleasantly surprised to see that the thermistor didn’t require any calibration – it just worked! By holding it between finger and thumb, I could see the temperature rising. Job done.
Apart from the “productising”, that’s effectively the remote sensor part done 🙂 Or could be. There’s still the option for a Raspberry Pi variant using the Pi’s GPIO pins. That might be useful; for example, the lounge or bedroom media players could double as temperature sensors. For now, though, the Arduino version is just fine 🙂