Why I don't get thingspeak update from both DHT11 with 4 field
3 次查看(过去 30 天)
显示 更早的评论
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
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
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 ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
ThingSpeak.begin(client); // Initialize ThingSpeak
Serial.println("DHT11 Fonctionnel");
dht1.begin();
dht2.begin();
}
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(t1);
Serial.print(" *C ");
Serial.print(" Humidity entrée: ");
Serial.print(h1);
Serial.println(" %\t");
}
if (isnan(t2) || isnan(h2)) {
Serial.println("Failed to read from DHT #2");
} else {
Serial.print("Temperature sortie: ");
Serial.print(t2);
Serial.print(" *C ");
Serial.print(" Humidity sortie: ");
Serial.print(h2);
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.");
}
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
}
0 个评论
回答(1 个)
Vinod
2021-11-27
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
#define PRINT_DEBUG_MESSAGES true
Before #include "Thingspeak.h" and look in the serial monitor for the messages to debug what is going on.
2 个评论
Vinod
2021-12-1
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?
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read Data from Channel 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!