Hello,
I am trying to set up the SHT10 sensor with ThingSpeak and having some issues. I ran the tutorials and all worked ok but I cannot seem to log any data from the sensor to ThingSpeak. I have placed my code below for reference. Please help and yes I am new to coding so my apologies in advance.
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>
/**
* ReadSHT1xValues
*
* Read temperature and humidity values from an SHT1x-series (SHT10,
* SHT11, SHT15) sensor.
* www.practicalarduino.com
*/
#include "SHT1x.h"
//Connections:
// SHT_RED ELECTRON_3V3
// SHT_BLACK ELECTRON_GND
// SHT_YELLOW ELECTRON_D1 CLOCK
// SHT_GREEN ELECTRON_D0 DATA
// Specify data and clock connections and instantiate SHT1x object
#define dataPin D0
#define clockPin D1
SHT1x sht1x(dataPin, clockPin);
// ThingSpeak integration
#define THINGSPEAK_ENABLED 0
#define THINGSPEAK_CHANNEL 311390
#define THINGSPEAK_API_KEY "O6CPGHVMYQX3K584"
// ThingSpeak
#if THINGSPEAK_ENABLED
#include "ThingSpeak/ThingSpeak.h"
#endif
#define MAX_TEMP 120.0
#define MIN_TEMP -10.0
#define MIN_HUMIDITY 0.0
#define publish_delay 16000
unsigned int lastPublish = 0;
uint32_t previousMillis = 0;
const uint32_t interval = 1000; //milliseconds
bool state = 0;
int t_hPublish(String command);
int tempPublish(String command); // So I can use a function to call the temp reading
int humidityPublish(String command); // So I can use a function to call the humidity reading
void setup()
{
Particle.function("temp", tempPublish); // So I can use a function to call the temp reading
Particle.function("humidity", humidityPublish); // So I can use a function to call the humidity reading
Serial.begin(9600); // Open serial connection to report values to host
Serial.println("Starting up");
pinMode(D7,OUTPUT);
// Subscribe to the integration response event
Particle.subscribe("hook-response/thingSpeakWrite_", myHandler, MY_DEVICES);
#if THINGSPEAK_ENABLED
ThingSpeak.begin(tcpClient);
#endif
}
void myHandler(const char *event, const char *data) {
// Handle the integration response
}
void loop()
{
float temp_c;
float temp_f;
float humidity;
#if THINGSPEAK_ENABLED
// set all 3 fields first
ThingSpeak.setField(1, tempPublish);
ThingSpeak.setField(2, humidityPublish);
// send all fields at once
ThingSpeak.writeFields(THINGSPEAK_CHANNEL, THINGSPEAK_API_KEY);
#endif
uint32_t currentMillis = millis();
// read sensor at set intervals
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
state =! state;
digitalWrite(D7,state);
// Read values from the sensor
temp_c = sht1x.readTemperatureC();
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();
//check to see if the measurements are out of bound
if(((temp_f > MAX_TEMP) || (temp_f < MIN_TEMP)) ||
((humidity < MIN_HUMIDITY)))
{
String t_h_reading = "{\"temp\":" + String(temp_f,2) + ",\"humidity\":" + String(humidity,2) + "}";
//publish an alert event along with the readings
Particle.publish("alert", t_h_reading, 60, PRIVATE);
}
//I put this in so that I can get temp & humidity readings every 15 min.
{
String t_h_reading = "{\"temp\":" + String(sht1x.readTemperatureF(),2) + ",\"humidity\":" + String(sht1x.readHumidity(),2) + "}";
Particle.publish("t_h", t_h_reading, PRIVATE);
delay(900000);
}
}
}
int tempPublish(String command) // sending a new function to call for the temperature only
{
String temp_reading = "{\"temp\":" + String(sht1x.readTemperatureF(),2) + "}";
Particle.publish("temp", temp_reading, PRIVATE);
return 1;
}
int humidityPublish(String command) // sending a new function to call for the humidity only
{
String humidity_reading = "{\"humidity\":" + String(sht1x.readHumidity(),2) + "}";
Particle.publish("humidity", humidity_reading, PRIVATE);
return 1;
}