SHT11 to Thingspeak

Hello All,

I would like some some assistance getting my SHT11 sensor data sent to my local Thingspeak set-up.

I see examples of sending to the official Thingspeak however none to a local server.

I am defiantly a total noob.

I have 3 other adruino’s set-up sending data to my local server, One uno and Ethernet shield, One yun and one pro mini with Ethernet shield. All of them are using one form of SHT sensors (SHT10, and 2 SHT75)

Below is the code i have started with, It is the stock code on the Particle Libraries. The code prints to serial with no issues so I know the sensor works. Once I get a basic set-up working to my local server I will be able to modify and add additional sensors myself. I will also be swapping out all the arduino’s for Photon’s due to their size and low power usage.

Any help at all would be greatly appreciated.

// This #include statement was automatically added by the Spark IDE.
#include "SHT1x/SHT1x.h"

// Specify data and clock connections and instantiate SHT1x object
#define dataPin  D0
#define clockPin D1
SHT1x sht1x(dataPin, clockPin);

void setup()
{
    Serial.begin(9600); // Open serial connection to report values to host
    while(!Serial.available()) SPARK_WLAN_Loop();
    Serial.println("Starting up");
}

void loop()
{
    float temp_c;
    float temp_f;
    float humidity;
    
    // Read values from the sensor
    temp_c = sht1x.readTemperatureC();
    temp_f = sht1x.readTemperatureF();
    humidity = sht1x.readHumidity();
    
    // Print the values to the serial port
    Serial.print("Temperature: ");
    Serial.print(temp_c, DEC);
    Serial.print("C / ");
    Serial.print(temp_f, DEC);
    Serial.print("F. Humidity: ");
    Serial.print(humidity);
    Serial.println("%");
    
    delay(1000);
}

Hello Again all, I seem to have gotten it working, The code is a bit dirty because I am far from a coder however I will post it for anyone who wants it.

This code will work on a local Thingspeak server running webric on port 80. You will need to change the host name from “uno.thingspeak.com” to whatever your servers address is.

Hope it helps.
If anyone has any suggestions on cleaning the code up i am all ears. More sensors to come!

    // This #include statement was automatically added by the Particle IDE.
#include "SHT1x/SHT1x.h"

// This #include statement was automatically added by the Particle IDE.
#include "ThingSpeak/ThingSpeak.h"


#include "math.h"

// Thinkspeak channel information
String writeAPIKey = "------API KEY HERE----";
String channelID = "---ID HERE---";
// TCP socket initialize
TCPClient client;

// Specify data and clock connections and instantiate SHT1x object
#define dataPin  D0
#define clockPin D1
SHT1x sht1x(dataPin, clockPin);

void setup()
{
    Serial.begin(9600);
    delay(10000);
    Serial.println("===Starting===");
}

void loop()
{
    float temp_c;
    float temp_f;
    float humidity;
    
    // Read values from the sensor
    temp_c = sht1x.readTemperatureC();
    temp_f = sht1x.readTemperatureF();
    humidity = sht1x.readHumidity();

    if(Spark.connected())
    {
    {

  // Must convert data to Strings, make sure you use capital "S" in Strings
  ThingSpeakUpdate("field1="+String(temp_c)+"&field2="+String(humidity));

  // I put this delay in place so we don't flood Thingspeak but you should really use a timer, delays screw with the sparkcloud connection some times.
  delay(15000);
   }
    }

}
/*------------------------------------------------
Sends sensor data to Thingspeak
Inputs: String, data to be entered for each field
Returns: 
------------------------------------------------*/
void ThingSpeakUpdate(String tsData)
{
    Serial.println("Date string: " + tsData);

    Serial.println("...Connecting to Thingspeak");

    // Connecting and sending data to Thingspeak
    if(client.connect("uno.thingspeak.com", 80))
    {
        Serial.println("...Connection succesful, updating datastreams");

        client.print("POST /update HTTP/1.1\n");
        client.print("Host: uno.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(tsData.length());
        client.print("\n\n");

        client.println(tsData); //the ""ln" is important here.

        // This delay is pivitol without it the TCP client will often close before the data is fully sent
        delay(200);

        Serial.println("Thingspeak update sent.");
    }
    else{
        // Failed to connect to Thingspeak
        Serial.println("Unable to connect to Thingspeak.");
    }

    if(!client.connected()){
        client.stop();
    }
    client.flush();
    client.stop();
}