Cannot get real sensor values from Thingspeak
Hi,
I am experiencing trouble in sending the actual sensor values to Thingspeak. I use a code template that I downloaded from a github repository https://github.com/mathworks/thingspeak-arduino/tree/master/examples/ESP32/WriteMultipleFields. The main issue is that I don't know how to fill out the source code to get real sensor values instead of random values.
I am using a DHT sensor, from which I want to measure both the temperature and the humidity. Here is what it looks like in the serial monitor after uploading the code to the ESP32 dev board.
Here is my code that I amended from the source template, and uploaded to the ESP32 board.
#include "DHT.h" #include <WiFi.h> #include "ThingSpeak.h"
// The wire is in pin 14 as required by the DHT-sensor, so it is written here: #define DHTPIN 14
// We use a DHT11-sensor #define DHTTYPE DHT110
const char* ssid = "Koti_9BE9"; // your network SSID (name) const char* password = "XXXXXXX"; // your network password
WiFiClient client;
// We write one (1) as the channel number. Assumably in that order in which they are in the MyChannels-page. unsigned long myChannelNumber = 1; const char * myWriteAPIKey = "NW34MMOB5J1NGRL2";
// Timer variables unsigned long lastTime = 0; unsigned long timerDelay = 10000; // This specifies what kind of delay we want between measurements. DHT11 // The fastest measurement interval of DHT sensors is 2000 ms (2 seconds).
// An object from where values are brought from the DHT library DHT dht(DHTPIN, DHTTYPE);
// Initialize our values. Writing down the values that need to be measured from DHT-sensors: // humidity h, temperature t, and temperature in fahrenheit-scale f float h = dht.readHumidity(); float t = dht.readTemperature(); float f = dht.readTemperature(true); String myStatus = "";
void setup() {
Serial.begin(115200);
Serial.println(F("DHT11 test!"));
dht.begin();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); }
void loop() {
delay(20000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
} if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
Many thanks for any advice.
Lauri
4 个评论
时间降序Thank you for your reply!
How should I name my sensor variables? Can I figure it out by looking at the library of the sensor in question? In this DHT sensor case I should probably check them from DHT_U.h (DHT Unified Sensor) library, at home/(name)/.arduino/libraries. I looked for a proper variable declaration from the file, and after trial and error I found dht.temperature().getEvent(&event); and dht.humidity().getEvent(&event); to be valid, i.e. not unknown nor erroneous according to the Arduino IDE compiler. However, with these variables I seem to get only number ones (1) as sensor values to my Thingspeak channel (check the image). So I am still trying to find sensor variables that give me the real, measured sensor values.
Below is my edited WriteMultipleFields (added the DHT11-sensor, my ssid, password, write api key, channel number, and edited the sensor variables from random(0,100) to dht.temperature().getEvent(&event); and dht.humidity().getEvent(&event);.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <WiFi.h>
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
/* Pin number for DHT11 data pin */
#define DHTPIN 14
//type of sensor in use:
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
char ssid[] = "mokkula"; // your network SSID (name)
char pass[] = "XXXXXXXX"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = '123456';
const char * myWriteAPIKey = "letters";
// Initialize our values
//sensor_t sensor;
sensors_event_t event;
float number1 = dht.temperature().getEvent(&event);
float number2 = dht.humidity().getEvent(&event);
//int number3 = random(0,100);
//int number4 = random(0,100);
String myStatus = "";
void setup() {
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// set the fields with the values
ThingSpeak.setField(1, number1);
ThingSpeak.setField(2, number2);
//ThingSpeak.setField(3, number3);
//ThingSpeak.setField(4, number4);
// figure out the status message
if(number1 > number2){
myStatus = String("field1 is greater than field2");
}
else if(number1 < number2){
myStatus = String("field1 is less than field2");
}
else{
myStatus = String("field1 equals field2");
}
// set the status
ThingSpeak.setStatus(myStatus);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
// change the values
number1++;
if(number1 > 99){
number1 = 0;
}
number2 = random(0,100);
//number3 = random(0,100);
//number4 = random(0,100);
delay(20000); // Wait 20 seconds to update the channel again
}
You need to use setField(filednum,value) first. In the ThingSpeak library, look at the write multiple fields example in the thingspeak examples under esp32 for the correct syntax.