Why I don't get thingspeak update from both DHT11 with 4 field

9 views (last 30 days)
I get the data from my 2x dth11 (2x temp and 2x humidity) sensor and no error with this code, but thingspeak receved no data and i get only one loop.
Any one see what wrong?
#include <Ethernet.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include "DHT.h"
#define DHT1PIN 10 // what pin we're connected to
#define DHT2PIN 9
// Uncomment whatever type you're using!
#define DHT1TYPE DHT11 // DHT 11
#define DHT2TYPE DHT11 // DHT 11
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xXX, 0xXX, 0xXX };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, X, XXX);
IPAddress myDns(192, 168, X, X);
EthernetClient client;
unsigned long myChannelNumber = XXXXXX;
const char * myWriteAPIKey = "XXXXXXXXXXXXXXX";
// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
void setup() {
Ethernet.init(10); // Most Arduino Ethernet hardware
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
// give the Ethernet shield a second to initialize:
ThingSpeak.begin(client); // Initialize ThingSpeak
Serial.println("DHT11 Fonctionnel");
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float t1 = dht1.readTemperature();
float h1 = dht1.readHumidity();
float t2 = dht2.readTemperature();
float h2 = dht2.readHumidity();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t1) || isnan(h1)) {
Serial.println("Failed to read from DHT #1");
} else {
Serial.print("Temperature entrée: ");
Serial.print(" *C ");
Serial.print(" Humidity entrée: ");
Serial.println(" %\t");
if (isnan(t2) || isnan(h2)) {
Serial.println("Failed to read from DHT #2");
} else {
Serial.print("Temperature sortie: ");
Serial.print(" *C ");
Serial.print(" Humidity sortie: ");
Serial.println(" %\t");
// set the fields with the values
ThingSpeak.setField(1, dht1.readTemperature()); delay(10000);
ThingSpeak.setField(2, dht1.readHumidity()); delay(10000);
ThingSpeak.setField(3, dht2.readTemperature()); delay(10000);
ThingSpeak.setField(4, dht2.readHumidity()); delay(10000);
// write to the ThingSpeak channel
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Mise a jour du Canal.");
Serial.println("Problem updating channel. HTTP error code " + String(x));
// change the values
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

Answers (1)

Vinod on 27 Nov 2021
What have you tried to debug the issue? Have you tried printing the measured readings to your serial port before updating your thingspeak channel? Try
Before #include "Thingspeak.h" and look in the serial monitor for the messages to debug what is going on.
Vinod on 1 Dec 2021
From your log, I don't see the connection to ThingSpeak happening at all. I'd strongly recommend looking at your connection. Perhaps the Ethernet connection requires some additional configuration?

Sign in to comment.


More Answers in the  ThingSpeak Community

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!