Temperature IoT

 

        This project allows you to check the temperature from a DS18B20 temperature sensor on a mobile App.

 

    Complexity

Project  Temperature Monitor
Time to build  1 hour
Cost $30
Arduino Code download
Cloud Device Code N/A
Mobile App Code download

 

    Hardware

        Here you have the list of the components required to build the hardware. We assume you have a computer, a router and a network cable. If you purchase the Arduino board from the specified source it comes with the USB cable. Otherwise you might need to provide a USB cable too. You can find better options for hardware. Be careful what components you buy. For Ethernet boards there are many versions with different chipsets but only the standard Arduino Shield or equivalent works with the software provided here. The prices are just a guide. These are the values we found when we created this project.

  
Picture Name Price Source

Arduino Uno

 $13 Link
Arduino Ethernet Shield $10 Link
Temperature Sensor (DS18B20) $3 Link
Single Port Female to Female Jumper Wires $3 Link
Single Port Male to Male Jumper Wires $2 Link

 

        We recommend to buy an Arduino board with a smaller USB connector (mini or micro USB) because the Ethernet Shield will not insert properly most of the time if you do not use original Arduino boards.

        Here you have a picture with the components we used:

 

 

     Specifications

       This section shows the pinout of the components or provide a link to the specifications of the component.

 

Component Name Pins
Arduino Uno See: http://arduino.cc/en/Main/arduinoBoardUno
Arduino Ethernet See: http://arduino.cc/en/Main/ArduinoEthernetShield
Temperature Sensor
Pin Description
Gnd Ground
Data Communication Channel (One Wire)
5V 5V

 

     Wiring

        This section shows you how to connect the components together. First you have a pin-out connection and below there are pictures showing how it's done on the real boards.

 

Temperature Sensor Pin ArduinoUno
GND GND
5V +5V
Data D8

 

Arduino Ethernet ArduinoUno
Just insert the Ethernet Shield into Arduino Uno
 

       Here you have pictures with the wiring

 

 

 

 

        The schematic is

 

 

    Software

        To run the system you need two software components. One is the software to run the hardware (Arduino Software) and the other one is the mobile App (Mobile App).

 

     Arduino Software

        To run the software you need the Arduino development environment. You need an external Library that is not included by default with Arduino IDE.

Library Location Author Comments
One Wire Link Check Site  

 

        You have to load the software provided here and configure it. There is only one value you need to change in the code.

 

Field Description
device_id

This is a unique id you have to create for your hardware device to identify it from others in the cloud. Use this www.guidgenerator.com to generate GUID (Global Unique ID)

String devId      = "b5ebf7a7-e63a-4660-8f83-cf1e5d39cf5b";
auth

This is the authentication string requred for basic authentication of the http requests. Create a string from <email_address>:<API_Password> and encode it base64. The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password"

String auth    = "dXNlcjpwYXNzd29yZA==";

 

       And here you have the sketch (download)

 

#include <OneWire.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xED, 0xBE, 0xEF, 0xFE, 0xD1 };        // mac address for ethernet board
EthernetClient eth;                                         // the Ethernet client

char server[]  = "www.ic2pro.com";                          // Wiring Cloud host name
int port       = 80;
String devId   = "111-222-333";                             // Device ID. CREATE YOUR OWN GUID; Use this http://www.guidgenerator.com/
String auth    = "dXNlcjpwYXNzd29yZA==";                    // Authentication credentials Create a string from <email_address>:<API_Password> and encode it base64
                                                            // The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password"

OneWire  ds(8);                                             // Temperature sensor is connected to pin 8 (Signal(DS18B20) -> D8)


// Update TEMPERATURE signal on the Wiring Cloud
void sendTemperature(String temperature)
{
  Serial.println("Temperature = " + temperature);
  Serial.println("Send Temperature...");
  if (eth.connect(server, port)) {                                                          // initialize HTTP connection on port 80
    eth.println("GET http://" + String(server) + ":" + String(port) + "/Wire/connector/set?id=" + devId + "&TEMPERATURE=" + temperature+ " HTTP/1.1");   // submit HTTP request
    eth.println("Authorization: Basic " + auth);
    eth.println("Connection: close");
    eth.println();
    Serial.println("Temperature sent to server.");
  }
  else {
    Serial.println("Connection Error");
  }
  eth.stop();
}


// retrieve temperature from DS18B20 sensor
String getTemperature()
{
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius;
  char scelsius[10];

  if ( !ds.search(addr)) {                                            // find chip
    if ( !ds.search(addr)) {
      ds.reset_search();
      delay(250);
      return "ERR1";
    }
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {                            // check CRC
    return "ERR2";
  }

  switch (addr[0]) {                                                  // test if it's a proper chip
    case 0x10: type_s = 1; break;
    case 0x28: type_s = 0; break;
    case 0x22: type_s = 0; break;
    default:
      Serial.println("Device is not a DS18x20 family device.");       // Not a proper chip 
      return "ERR3";
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);                                                  // initiate conversion

  delay(1000); // maybe 750ms is enough, maybe not

  ds.reset();
  ds.select(addr);
  ds.write(0xBE);

  for (byte i = 0; i < 9; i++) {                                     // read data
    data[i] = ds.read();
  }

  
  int16_t raw = (data[1] << 8) | data[0];                            // Convert the data to temperature
  if (type_s) {
    raw = raw << 3;
    if (data[7] == 0x10) {
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7;                                // based on the resolution make the conversion 
    else if (cfg == 0x20) raw = raw & ~3;
    else if (cfg == 0x40) raw = raw & ~1;
  }
  celsius = (float)raw / 16.0;
  dtostrf(celsius, 5, 2, scelsius);
  return scelsius;                                                   // return value in Celsius
}

void setup() {
  Serial.begin(9600);
  Serial.println("Start...");

  Ethernet.begin(mac);                                           // start the Ethernet connection:
  delay(1000);                                                   // give the Ethernet shield a second to initialize:
}

void loop() {
    sendTemperature(getTemperature());
    delay(5000);                                                  // send temperature only every 2 seconds
}

    

Mobile App

 

        The mobile application to control the security system is built with "App Inventor" (ai2.appinventor.mit.edu). If you just want to import the .aia file you can download it here. Otherwise you have to create next components.

 

Component Name Description
Label TemperatureMsg A text that shows the temperature.
Clock Timer A timer that will send regular requests to get the temperature from the Wiring Cloud.
Web APIGetTemperature

The HTTP request that will be sent to retrieve the temperature value. Set URL to:

http://www.ic2pro.com/Wire/connector/get?id=<device_id>&TEMPERATURE

don't forget to put the device_id you set in the Arduino Code

        In the code you have to set a proper authentication string. Replace the string "Basic dXNlcjpwYXNzd29yZA==" with your authentication string. Create a string from <email_address>:<API_Password> and encode it base64. The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password". Make sure you append "Basic " at the beginning of it.

        And this is the logic you have to create in App Inventor. Once that is done you just build the APK and install it on your Android phone.

 

    Final Product

       Here you have a picture of the "final" product, hardware and screenshots from App.