A Temperature Sensing and Networked Arduino

For our device we started with a Borderless Electronics Arduino Leonardo compatible board though pretty much any Arduino board will work.

Arduiono Compat

For the Ethernet connectivity, we added a clone of the Arduino Ethernet Shield. The Ethernet Shield passes through all the I/O connections of the Arduino board and is labelled in the same way.

Board Plus Ethershield

To do pick up temperature data we used a TMP36 temperature sensor. It’s an inexpensive, if crude, sensor but its very simple to get temperature data from. It’s so simple that it’s used in one of the tutorials at Adafruit which also has detailed information on the TMP36, how to wire it up and how to wire it so it is more precise. For our needs, we attached the +V pin of the TMP36 to the Ardunio’s 5V socket, the Ground pin to the GND socket and the middle pin, where the voltage varies according to the temperature, to the Arduino’s Analog socket 5. To make it a little more stable, we mounted the TMP36 on a small breadboard like so:

Board With Sensor

All that was left, at least hardware wise was to plug in the power through, in this case, a micro-USB cable and the Ethernet cable.

Board Running

We move now onto the software. The code we use will need to be modified slightly to suit your network. We’ve used hardwired IP addresses and MAC addresses for the Ethernet connection and MQTT connection. These values are early on in the code and appear by default as:

byte mac[]    = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };  
byte server[] = { 192, 168, 111, 50 };  
byte ip[]     = { 192, 168, 111, 240 };  

Change the ip to a safe static address on your network and change the server value to the address of the machine you plan to run the Mosquitto broker on.

The code breaks up into two blocks. In the setup function…

EthernetClient ethClient;  
PubSubClient client(server, 1883, callback, ethClient);

void setup() {  
  Ethernet.begin(mac, ip);
  if (client.connect("arduinoClient")) {
    client.publish("demo/status/arduino01", "online");
    lastTemperature = 0;
    lastTime = 0;
  }
}

The program initializes the Ethernet connection and then asks the pre-configured PubSubClient, as the MQTT library advertises itself as, to connect to the server with a client identity of “arduinoClient”. If you plan on deploying more than one of these devices, this is another thing you’ll need to make unique for each device. The setup function then publishes a message to demo/status/arduino01 to announce its online. The Arduino will clear some variables and enter the loop function. By design, the loop function is constantly called while the Arduino is powered up.

void loop() {  
  int reading = analogRead(sensorPin);
  int temperature = ((reading * 0.004882) - 0.50) * 100;
  if (temperature != lastTemperature) {
    if (millis() & gt;
      (lastTime + 1000)) {
      sprintf(buffer, "%d", temperature);
      client.publish("demo/device/arduino01", buffer);
      lastTemperature = temperature;
      lastTime = millis();
    }
  }
  client.loop();
}

All this loop does is read the sensor (sensorPin is set to 5, where we plugged the TMP36 in) and convert the reading into an integer value in centigrade which represents the temperature. Only if the reading has changed and only if a second has elapsed since a previous reported reading does it go on to format the integer temperature as a string and then publish it over MQTT to the demo/device/arduino01 topic. It then sets the last temperature to what it just read and the last reading time to now. Whatever happens, the code always then executes client.loop() which lets the MQTT library receive incoming messages and handle housekeeping.

The full code is available in a GitHub Gist.