Arduino

Arduino


This is a quick example using Arduino Zero to send data to Tago. For the connectivity board, we selected the shield Atmel WiFi101. Learn about the Arduino Zero and how to get started.

In this example, let’s send the temperature reading from the Arduino to Tago. We will visualize the temperature in the dashboard, and use the actions capability to send e-mails when the temperature reaches a threshold.

Diagram

The temperature sensor used in this tutorial is the TMP36 that comes in a TO-92 package. The output of the TMP36 is a voltage that will be read by the Analog pin 0 of the Arduino Zero. As the Arduino Zero is a 3.3V board, we will use the following formula to convert the reading to temperature in Celsius.

Voltage at pin in milliVolts = (reading from ADC) * (3300/1024)

This formula converts the number 0-1023 from the ADC into 0-3300mV (= 3.3V) Then, to convert millivolts into temperature, use this formula:

Centigrade temperature = [(analog voltage in mV) - 500] / 10

Adding the Device

Access your Admin , navigate to the Devices module, and you'll find a '+ Add Device' button in the top-right corner. Click on this button to add a device.

The device you'll be adding in this case is the Arduino board. Please locate its connector before proceeding. When prompted, give the device the name 'dev01'. After naming the device, click the 'Save' button to confirm your configuration.

For each device, you have to define a data storage type​ to store its data. Once you create the device, a Bucket will be created with the same name.

All devices should use a valid token when accessing TagoIO. This token is automatically generated when a device is created. Go to the Tokens section of the device, click the reveal button and copy the token to be added in the Arduino code later.


Building the Dashboard

Let’s build a simple dashboard to visualize the data sent by your Arduino. Click Dashboards on the left side bar, click the + button then type the name of your dashboard, and click on Save. Once you have saved your dashboard, you can add widgets to show the variables of your devices. Click on + top right button and pick the widget Dial.

Note: If you do not have a device or if your device does not have any variables associated with it, check out our videos on adding variables and adding devices.

Start the configuration of this widget by adding the variable to be displayed. Under Variables in Main Configuration, click on the search area then type the device and the variable name that will be sent by the device. In this case the device being used has a variable called temperature. Add a title for your widget, click Save, and your widget will be ready!


Great! As soon as your device start sending data, the values will be shown on this dial.

Sending e-mail

Now, let’s add an action to send an e-mail notification when the sensor overheat. First, create an action for the device:

Configure the action to send the e-mail, enter with the destination e-mail address in the ‘To’ field, and the Subject. Tago can include dynamic variables in the body of the message! For example, using $VALUE$ in the message, we can send the last temperature value with the text. An e-mail body written as: Hi, the temperature is $VALUE$, could in fact send an e-mail like: Hi, the temperature is 26.5



To make sure that you will receive only one notification each time the temperature passes the threshold, we will define values to Set and Reset the trigger. It will create a hysteresis function to prevent the system from sending e-mails continuously. Basically, we just need to configure Set Trigger and Reset Trigger as showed below. You can change the threshold values later, but now, let’s send an e-mail when the temperature goes over 50C and reset the trigger when it goes back to less than 30C.

Sending data from Arduino

Your setup is ready at Tago! Now, you just need to code your Arduino to send the data to Tago.

When communicating with devices, Tago uses the JSON format. For example, to send a variable called temperature with a value of 26 C, the device just needs to POST the data:

{
    "variable": "temperature",
    "value": 26,
    "unit": "C"
}

Here for the Arduino, we give a C code example which use HTTP format connecting through port 80 (non-secure) to simplify the example. To send the data to Tago correctly, the code should simply prepare a string that will represent the json block above: “variable”:”temperature”, “value”:26,”unit”:”C”.

Arduino Code

/*
  Send data to Tago - Wifi101

 This sketch connects to the TAGO server and post a data
 using an Arduino Wifi 101 shield.

 Circuit:
 * WiFi shield attached to pins SPI pins and pin 7

 Based on the the Wifi Web Client from
 http://arduino.cc/en/Tutorial/WifiWebClientRepeating.
 */

#include <SPI.h>
#include <WiFi101.h>

char ssid[] = "## YOUR NETWORK HERE ##";      //  your network SSID (name)
char pass[] = "## YOUR NETWORK PASSWORD HERE ##";   // your network password
String Device_Token = "## INSERT THE TOKEN FOR YOUR DEVICE HERE ##";
int keyIndex = 0;         // your network key Index number (needed only for WEP)

int sensorPin = A0;       // select the input pin for the analog input
int rawvoltage = 0;       // variable to store the value coming from the sensor
float sensorValue = 0;
String value_string = "";

int status = WL_IDLE_STATUS;

// Initialize the Wifi client library
WiFiClient client;

// server address:
char server[] = "api.tago.io";

unsigned long lastConnectionTime = 0;            // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 2L * 1000L; // delay between updates, in milliseconds

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ;                     // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  // you're connected now, so print out the status:
  printWifiStatus();
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if TWO seconds have passed since your last connection,
  // then connect again and send data:

  if (millis() - lastConnectionTime > postingInterval) {
    // read the value from the sensor:
    rawvoltage = analogRead(sensorPin);
    // converting that reading to voltage, for 3.3v voltage
    float voltage = rawvoltage * 3.3;
    voltage /= 1024.0;
    // converting to Celsius
    float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                                  //to degrees ((voltage - 500mV) times 100)

    int i = (int) temperatureC;                   //convert data format from float to int
    value_string =String(i);                      //end of conversion, to finally get it in the String format(Celsius)
    Serial.println(value_string);

    // then, send data to Tago
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
    // close any connection before send a new request.
    // This will free the socket on the WiFi shield
    client.stop();

    Serial.println("\nStarting connection to server...");
    // if you get a connection, report back via serial:
    String PostData = String("{\"variable\":\"temperature\", \"value\":") + String(value_string)+ String(",\"unit\":\"C\"}");
    String Dev_token = String("Device-Token: ")+ String(Device_Token);
    if (client.connect(server,80)) {                      // we will use non-secured connnection (HTTP) for tests
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("POST /data? HTTP/1.1");
    client.println("Host: api.tago.io");
    client.println("_ssl: false");                        // for non-secured connection, use this option "_ssl: false"
    client.println(Dev_token);
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Running the application

Open your dashboard, and run the code in your Arduino board. Notice that the widget displays the value in real time. Try to heat the sensor to reach a temperature higher than 50C. You should then receive an e-mail from Tago. Cool down the sensor below 30C, and try again! If you have any issue or question about this application, access our Community .

Conclusion

That was a complete example that showed how easy and quick is to set the ecosystem around Tago and your device. To extract more from Tago, check out our next tutorials. There you will be able to send and receive data from Tago, run scripts in the Analysis and combine data.


    • Related Articles

    • Thinxtra Xkit for Sigfox

      This tutorial explains how to connect the Xkit from Thinxtra with TagoIO. This Sigfox board can be connected to a PC, Mac, Arduino, Raspberry PI, and other platforms. Here is a diagram of Xkit board's pinout, you can click here for more details. Xkit ...