Arduino Tutorial

Connecting a Temperature Sensor

Introduction

This tutorial introduces the basics of how to quickly deploy an Internet of Things project with a working example of a Connected Product.

The example illustrates how to connect a temperature sensor to the CloudPlugs service through a Arduino board using the internal Ethernet controller.

You will need to open an account at CloudPlugs so you can create a prototype in your account. The example shows how to configure the Arduino with a connectivity library to connect to CloudPlugs. Once all the software is configured and the sensor is connected, the Arduino will upload and publish data every minute.

Requirements

You will need to set up your Arduino and sensor hardware, your CloudPlugs account and your Arduino software so you can monitor published data from the CloudPlugs web desktop.

Hardware

You can use any of the Arduino boards with Ethernet controller listed below to build the project. In addition, you will need a temperature sensor like the TMP35/36/37.

Here is an image of the circuit that needs to be wired.

Boards

  • Arduino Mega + Ethernet Shield;
  • Arduino Mega ADK + Ethernet Shield;

Components

Software

  1. Use the CloudPlugs Arduino library.
  2. Use the CloudPlugs Web Desktop to configure your device in the cloud.

Connecting the the Arduino and the sensor using the Python Library

Here are a few simple steps you need to follow to connect the sensor and to start publishing data as a device Prototype:

  1. Become familiar with the process for installing Arduino libraries and follow any guide to learn how to connect the Ethernet Shield to the compatible Arduino board.
  2. Create an account at www.cloudplugs.com.
  3. Login to the CloudPlugs platform either through the website or through http://platform.cloudplugs.com.
  4. Create a Prototype.
  5. Write down or copy to your clipboard the Prototype’s Plug-ID displayed in General->Device Profile.
  6. Download the CloudPlugs Arduino Library and use an example that matches your device. In your Arduino software go to Files > Examples and choose CloudPlugs.ino from the CloudPlugs library folder.
  7. Add the source code shown below to your Arduino sketch.
  1. Change AUTH_PLUGID to the Plug-ID provided by the platform and listed in the Prototype->General->Device Profile->General Properties panel.
  2. Either change the authentication password AUTH_PASS to a new password if you set it on the platform Prototype->General->Device Profile->Permissions->Set password, or leave the default which is your CloudPlugs login password (only in this case set the AUTH_MASTER variable to True).
  1. Connect your hardware.
  1. Connect the TMP36 to the Arduino ports (optionally wiring it with a breadboard).
  1. Power to the 5V port and ground to GND.
  2. Analog input to analog port A0.
  1. Connect the Arduino board to your network using an ethernet cable. You can power it using a micro USB cable connected to your computer or using the board’s external power adapter.
  1. Temperature data will be published every minute in the Published Data panel under Prototype->Publish data. To change temperature values easily, lightly press the sensor between your fingers.
  2. Congratulations! you have successfully connected a Thing (temp sensor) through your Arduino to the cloud.

Source Code

Use the following source code.

#include "SPI.h"
#include "Ethernet.h"
#include "CloudPlugs.h"

CloudPlugs client;
char* AUTH_PLUGID = "dev-00112233445566778899AABB"; // Enter your Plug-ID here
char* AUTH_PASS = "password";  // Enter your authentication password for the device here
boolean AUTH_MASTER = false;   // change to true, if you want to authenticate the device using your login password.

void setup() {
    Serial.begin(9600);
    // setup your Ethernet connection via DHCP
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

    while (Ethernet.begin(mac) != 1) {
        Serial.println("Error getting IP address via DHCP, trying again...");
        delay(500);
    }
    client.setAuth(AUTH_PLUGID, AUTH_PASS, AUTH_MASTER); // CloudPlugs Authentication
}

String response;
boolean res = false;

void loop() {
    int sensorValue;
    float celsius;
    float voltage;
    char msgBody[256];
    char float_buffer[20]; 

    sensorValue = analogRead(0);               // Reading data from TMP36 sensor
    voltage = (sensorValue * 5) / 1,024;       // millivolts from Analog port
    celsius = (voltage - 500)/10;              // Celsius Degrees;
    dtostrf(celsius, 2, 2, float_buffer);

    sprintf(msgBody, "{\"data\":{\"temperature\":\"%s\"}}", float_buffer); // composing JSON obj 
    res = client.publishData("weather", msgBody, response); // publishing data 
    Serial.println(res);
    delay(60000); // once per minute 
}
Still need help? Get in touch!
Last updated on 26th Mar 2019