Sending SHT31 sensor data to sparkfun using an Arduino

In this example we will take the SHt31, read the temperature and humidity and send this data to an online IoT platform, in this case we will use sparkfun data. To achieve this we will need an Arduino Uno with an Ethernet shield fitted and then we will connect the SHt31 sensor to this.

You will now need to create a new stream at sparkfun – https://data.sparkfun.com/streams/make . There are various fileds to fill in on the form but the key ones are Fields in this example the fields we use are humidity and temperature, after you have clicked the create button you will go to another page which contains all of your keys, url and other important information.

Here is a screen capture of the data, I have blanked my keys. you can also see the Fields listed, these are key because they have to match the Arduino code

sparkfun

 

Layout

This layout shows the SHT31 connected to the ethernet shield

ethernet-and-sht31_bb

 

Code

This code came from sparkfun and also included an example for an SHT31. Once you get sensor examples working via serial output its fairly easy to then send data to IoT platforms

You will also need to add the adafruit sht31 library – https://github.com/adafruit/Adafruit_SHT31

I mentioned the fields earlier, if you didn’t want to use the same fields as me then change the fields highlighted in bold in the following lines in the code.

client.print(“&humidity=”);
client.print(“&temperature=”);

 

[codesyntax lang=”cpp”]

 #include <SPI.h>
#include <Ethernet.h>
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

char sparkfunDataServer[] = "data.sparkfun.com";
#define PUBLIC_KEY  "public key here " //paste your SparkFun public_key
#define PRIVATE_KEY "private key here" //paste your SparkFun private_key

#define SPARKFUN_UPDATE_TIME 60000         //Update SparkFun data server every 60000 ms (1 minute).

#define TIMEOUT 1000 //1 second timout

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xAE, 0xCD };
IPAddress ip(192, 168, 1, 250); //Your local IP if DHCP fails.
IPAddress dnsServerIP(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// Initialize the Ethernet client.
EthernetClient client;

Adafruit_SHT31 sht31 = Adafruit_SHT31();

unsigned long timer1 = 0;
unsigned long timer2 = 0;

int failedResponse = 0;

void setup()
{

  //Initiallize the serial port.
  Serial.begin(9600);
  Serial.println("-= SparkFun data client =-\n");
  if (! sht31.begin(0x44)) 
  {
    //Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0)
  {
  //  Serial.println("Failed to configure Ethernet using DHCP");
    // DHCP failed, so use a fixed IP address:
    Ethernet.begin(mac, ip, dnsServerIP, gateway, subnet);
  }
  Serial.print("LocalIP:\t\t");
  Serial.println(Ethernet.localIP());
  Serial.print("SubnetMask:\t\t");
  Serial.println(Ethernet.subnetMask());
  Serial.print("GatewayIP:\t\t");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP:\t\t");
  Serial.println(Ethernet.dnsServerIP());
}
  

void loop()
{
  //Update sparkfun data server every 60 seconds.
  if(millis() > timer1 + SPARKFUN_UPDATE_TIME)
  {
      timer1 = millis();
      sendToSparkfunDataServer(); //Send data to sparkfun data server.
  }
}


void sendToSparkfunDataServer()
{ 
    //Establish a TCP connection to sparkfun server.
    if (client.connect(sparkfunDataServer, 80))
    {
        //if the client is connected to the server...
        if(client.connected())
        {
            Serial.println("Sending data to SparkFun...\n");
            // send the HTTP PUT request:
            client.print("GET /input/");
            client.print(PUBLIC_KEY);
            client.print("?private_key=");
            client.print(PRIVATE_KEY);
            client.print("&humidity=");
            client.print(sht31.readHumidity()); 
            client.print("&temperature="); 
            client.println(sht31.readTemperature());
         
            delay(1000); //Give some time to Sparkfun server to send the response to ENC28J60 ethernet module.
            timer2 = millis();
            while((client.available() == 0)&&(millis() < timer2 + TIMEOUT)); //Wait here until server respond or timer2 expires. 
            
            // if there are incoming bytes available
            // from the server, read them and print them:
            while(client.available() > 0)
            {
                char inData = client.read();
                Serial.print(inData);
            }      
            Serial.println("\n");   
            client.stop(); //Disconnect the client from server.  
         }
     } 
}

[/codesyntax]

 

Testing

open the serial monitor and you should details of the IP address, then you should see communication to sparkfun like this

Sending data to SparkFun…

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Headers: X-Requested-With, Phant-Private-Key
Content-Type: text/plain
X-Rate-Limit-Limit: 300
X-Rate-Limit-Remaining: 299
X-Rate-Limit-Reset: 1477954006.879
Date: Mon, 31 Oct 2016 22:31:46 GMT
Connection: close
Set-Cookie: SERVERID=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Cache-control: private

1 success

The key information is the 1 success, that as you may have guessed means its worked

Navigate to your sparkfun stream and you should see data on the screen, you may need to refresh the browser a few times as the example code only sends data every 1 minute.

Here is a sample of my data

humidity temperature timestamp
54.19 26.62 2016-10-31T22:28:45.385Z
54.51 26.59 2016-10-31T22:27:45.982Z
54.21 26.69 2016-10-31T22:26:44.874Z
54.19 26.65 2016-10-31T22:25:43.889Z
54.05 26.66 2016-10-31T22:24:43.490Z
54.02 26.63 2016-10-31T22:23:43.304Z
54.00 26.69 2016-10-31T22:22:46.424Z
54.09 26.69 2016-10-31T22:21:41.902Z
54.14 26.67 2016-10-31T22:20:41.402Z

 

The stream also allows you to export in a few formats such as csv, mysql and json. So quite handy that.

You also get a public URL, this one is https://data.sparkfun.com/streams/QG5REdgY6qF44zwpVnlK

 

Links

https://data.sparkfun.com

1PCS/LOT SHT31 Temperature & SHT31-D Humidity Sensor module Breakout Weather for Arduino