Home Code Sending SHT31 data to thingspeak using an Arduino Uno

Sending SHT31 data to thingspeak using an Arduino Uno

by shedboy71

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 Thingspeak. To achieve this we will need an Arduino Uno with an Ethernet shield fitted and then we will connect the SHt31sensor to this.

We covered the SHT31 in the Arduino and SHT31 module so we will focus on the thingspeak part and arduino code here

You will now need to create a new account at thingspeak – https://thingspeak.com. Once done create a new channel and add two new fields called temperature and humidity. You can see this in a screen capture of my simple channel, notice the ChannelID you will need that in your code later. You can also fill in other fields such as Name, description and there are a few others as well. The key ones are Field1 and Field2 – this effectively is the data you send to thingspeak

thingspeak1

Layout

This layout shows the SHT31 connected to the ethernet shield

ethernet-and-sht31_bb

Code

You will need to install the thingspeak library first of all. 2 options here

In the Arduino IDE, choose Sketch/Include Library/Manage Libraries. Click the ThingSpeak Library from the list, and click the Install button.

— or —

  1. Download the ZIP file from https://github.com/mathworks/thingspeak-arduino to your machine.
  2. In the Arduino IDE, choose Sketch/Include Library/Add Zip Library
  3. Navigate to the ZIP file, and click Open

 

 

Here is the code, its basically a merge of a basic SHt31 example and the thingspeak WriteVoltage example with a lot of the hardware specific code removed – so it only works for an Arduino Uno and an Ethernet shield whereas the WriteVoltaghe example can cater for various pieces of hardware such as an ESP8266, WiFi shield, Arduino Yun

[codesyntax lang=”cpp”]

#define USE_ETHERNET_SHIELD

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

EthernetClient client;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
//change the following for your channel and api key
unsigned long myChannelNumber = 11111;
const char * myWriteAPIKey = "api key in here";

void setup() 
{
 //start the serial
 //Serial.begin(9600);
 //network stuff
 Ethernet.begin(mac);
 ThingSpeak.begin(client);
 //start the sensor
 if (! sht31.begin(0x44)) 
 {
 //Serial.println("Couldn't find SHT31");
 while (1) delay(1);
 }
}

void loop() 
{

 // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
 // pieces of information in a channel. Here, we write to field 1 and 2
 ThingSpeak.setField(1,sht31.readTemperature());
 ThingSpeak.setField(2,sht31.readHumidity());
 ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); 
 delay(20000); // ThingSpeak will only accept updates every 15 seconds.
}

[/codesyntax]

 

Testing

Navigate to your thingspeak channel and take a look at the private view, all going well you should see a nice graph of data. You can actually get code and embed this on another site. You can also play about with colours and more

Here is a screen capture of mine

thingspeaksht31

 

 

 

Further Steps

There are obviously alternatives to thingspeak which we will look at and we are only scratching the surface using two fields when 8 are available. This example used a specific hardware combination and we will look at others in future articles such as a Wifi shield

 

Links

https://thingspeak.com/

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

 

Share

You may also like

Leave a Comment