Failing to send data to ThingSpeak

Hi, everybody! Can anyone help me to find the reason why my code fails to send data to ΤhingSpeak? I checked the API Keys and they are right.
Sorry for the greek characters in the code!
My code :

#include <ThingSpeak.h>// Η βιβλιοθήκη του ThingSpeak.
#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>
#include <Servo.h> // Η βιβλιοθήκη του σερβοκινητήρα.
#include "DHT.h"
#include "MQ7.h"
//Για τον αισθητήρα DHT11.
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
//Για τον φωτισμό.
#define DELAY 5000 // Καθυστέρηση μεταξύ μετρήσεων σε ms για τον φωτισμό.
#define VIN 5 // Τάση 5V.
#define R 10000 //Αντίσταση 10.000 Ω.
//Για τον αισθητήρα MQ7.
int MQ7pin = A0;
int MQ7Reading;
int MQ7led = 3;
//Για τον αισθητήρα Soil.
int val;
int soil_dig= 8;
int soil_analog = A1;
//Μεταβλητές για τον φωτισμό και για τον υπολογισμό lux.
const int sensorPin = A3; // Το pin στο οποίο συνδέεται ο αισθητήρας.
const int led = 12;
const int greenled = 10;
const int redled = 4;
const long onduration = 10000;
const long offduration = 30000;
int ledstate = HIGH;
long remembertime = 0;
int sensorVal; // Αναλογική τιμή από τον αισθητήρα.
int lux; //Τιμές για τα lux

int valvePin = 13; // Το pin στο οποίο συνδέεται το ρελέ, ώστε να ενεργοποιεί την ηλεκτροβάνα.
//Για τον αισθητήρα Ultrasonic.
#define echoPin 5 // Το echoPin.
#define trigPin 6 // To trigPin.
long duration; // Η διάρκεια που κάνει το ηχητικό κύμα να ταξιδέψει.
int distance; // Ο υπολογισμός της ταχύτητας του ηχητικού κύματος.
//Για τον αισθητήρα Rain.
const int rain_D = 2;//Ψηφιακή τιμή αισθητήρα βροχής.
const int rain_A = A2;//Αναλογική τιμή αισθητήρα βροχής.
int rain_val;// Μεταβλητή για την αποθήκευση της τιμής του αισθητήρα βροχής.
int r;// Μεταβλητή για την αποθήκευση της τιμής του αισθητήρα βροχής.
Servo servo; // Το όνομα του σερβοκινητήρα.
int servoPin = 11; // Το pin στο οποίο συνδέεται ο σερβοκινητήρας.
#define pwm 9 // Το pin στο οποίο συνδέεται ο ανεμιστήρας.

//Σύνδεση
String readString; 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //H mac address.
byte ip[] = {192,168,2,121};
EthernetClient client;
unsigned long myChannelNumber = ********; //Ο αριθμός του καναλιού.
const char * myWriteAPIKey = "***********";  //Το ΑΡΙ Κey του καναλιού.
 const int updateInterval = 16000;
void setup() {
   
  Serial.begin(9600);
  ThingSpeak.begin(client);// Σύνδεση στο cloud του ThingSpeak.

  dht.begin();
  pinMode(MQ7led, OUTPUT);// Θέτει το led του MQ7 ως OUTPUT.
  pinMode(trigPin, OUTPUT); // Θέτει το trigPin ως OUTPUT.
  pinMode(echoPin, INPUT); // Θέτει το echoPin ως INPUT.
  pinMode(rain_D, INPUT); // Θέτει το rain_D ως INPUT.
  pinMode(rain_A, INPUT); // Θέτει το rain_A ως INPUT.
  pinMode(servoPin, OUTPUT);// Θέτει το servoPin ως OUTPUT.
  servo.attach(servoPin);// Σύνδεση σε pin της μεταβλητής του σερβοκινητήρα.
  analogWrite(pwm, 255);// Γράφει μία αναλογική τιμή (PWM pin) σε ένα pin.
  pinMode(soil_analog, INPUT); // Θέτει το soil_analog ως INPUT.
  pinMode(soil_dig, INPUT);// Θέτει το soil_dig ως INPUT.
  pinMode(valvePin, OUTPUT); //Θέτει το 13 ως OUTPUT pin, ώστε να στέλνει σήμα στο relay.
  pinMode(led, OUTPUT);  // Θέτει το ledPin ως output.
  digitalWrite(led,ledstate);
  pinMode(sensorPin, INPUT);// Θέτει το sensorPin ως INPUT.
  pinMode(greenled, OUTPUT);// Θέτει το greenled ως OUTPUT.
  pinMode(redled, OUTPUT);// Θέτει το redled ως OUTPUT.

}
void loop() {
  
  Serial.println(F("======================================"));
  Serial.println(F("Συνθήκες στο Θερμοκήπιο :"));
  //DHT11
  float t = dht.readTemperature();// Η θερμοκρασία σε °C.
  float h = dht.readHumidity(); // Η υγρασία του χώρου.
  float f = dht.readTemperature(true); //Η θερμοκρασία σε °F.
  // Υπολογισμός του heat index, ο οποίος πρέπει να μετατραπεί σε Fahrenheit.
  float hi = dht.computeHeatIndex(f, h);
  Serial.print(F("Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(F(" and "));
  Serial.print(f);
  Serial.print(F("°F\t"));
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F(" %\t"));
  Serial.print(F("Heat index: "));
  Serial.print(hi);
  Serial.println(F(" °F"));

  // Έλεγχος της θερμοκρασίας , ώστε να ενεργοποιείται ο ανεμιστήρας όποτε χρειάζεται.
  if (t == 26)
  {
    analogWrite(pwm, 51);
    Serial.print(F("Ταχύτητα ανεμιστήρα: 20%   "));
    delay(1000);
  }
  else if (t == 30)
  {
    analogWrite(pwm, 204);
    Serial.print(F("Ταχύτητα ανεμιστήρα: 80%   "));
    delay(1000);
  }
  else if (t > 40 )
  {
    analogWrite(pwm, 255);
    Serial.print(F("Ταχύτητα ανεμιστήρα: 100%   "));
    delay(1000);
    Serial.print(F("\t"));
  }
  
  
  
  //MQ7
  float g = analogRead(MQ7pin);
  Serial.print(F("MQ7: "));
  Serial.println(g);

    // Έλεγχος της τιμής του MQ7, ώστε να ανάβει το led που αναπαριστά ανεμιστήρα.
   if ( g < 200)
  {
    Serial.println(F("O ανεμιστήρας κλειστός - Φυσιολογική ποσότητα CO στον χώρο εργασίας."));
    digitalWrite(MQ7led, LOW); 
  }
  else if (201 < g && g < 220)
  {
  Serial.println(F("Η μέγιστη φυσιολογική ποσότητα CO για χώρους εργασίας ξεπεράστηκε!!!- Λειτουργία εξαερισμού στο 50%."));
  digitalWrite(MQ7led,HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(MQ7led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);   
  }
   if (g > 220)
  {
  Serial.println(F("Eπικίνδυνα αέρια CO !!!- Λειτουργία εξαερισμού στο 100%."));
  digitalWrite(MQ7led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(5000);
  }
  //Soil Humidity
  int soil_analog_value = analogRead(soil_analog);
  soil_analog_value = map(soil_analog_value, 0, 1023, 230, 0);//Συνάρτηση map για μετρατροπή τιμής σε %.
  val = digitalRead(soil_dig);
  Serial.print(F("Soil moisture (Αναλογική τιμή) : "));
  Serial.print(soil_analog_value);
  Serial.print(F("\t"));

  
  //Έλεγχος υγρασίας εδάφους για αυτόματο πότισμα.
  if (soil_analog_value < 50)
  {
    digitalWrite(valvePin, LOW); //Όταν η τιμή γίνει LOW,τότε και το relay πρέπει να λάβει τιμή LOW.
    Serial.println(F("relay on"));
  }
  else
  {
    digitalWrite(valvePin, HIGH); //Όταν η τιμή γίνει HIGH, τότε και το relay πρέπει να λάβει τιμή HIGH.
    Serial.println(F("relay off"));
  }
 
  
  //Ultrasonic Sensor
  // Αρχικοποίηση του trigPin.
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Το trigPin σε HIGH (ACTIVE) για 10 μs.
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  //Διαβάζει το echoPin, επιστρέφει το χρόνο του ηχητικού κύματος σε μs.
  duration = pulseIn(echoPin, HIGH);
  // Υπολογισμός απόστασης.
  distance = (duration - 10) * 0.034 / 2;
  Serial.println("");
  Serial.print(F("Distance: "));
  Serial.print(distance);
  Serial.print(F(" cm"));
//Rain Sensor
  rain_val = digitalRead(rain_D);
  r = analogRead (rain_A);
  r = map (r,0,1023,0,230);
  Serial.println(F(""));
  Serial.print(F("Rain (Αναλογική τιμή) :"));
  Serial.print(r);
  Serial.print(F("\t"));
  Serial.print(F("Rain (Ψηφιακή τιμή) :"));
  Serial.print(rain_val);

  //Έλεγχος για λειτουργία του σερβοκινητήρα.
  if ((rain_val != HIGH) && (distance < 9)) {
    servo.write(0);
  }
  if ((rain_val != HIGH) && (distance > 9)) {
    servo.write(90);
  }
  else {
      servo.write(0);
   }
 
  //Μέτρηση τιμής της φωτοαντίστασης και μετατροπή σε lux.
  sensorVal = analogRead(sensorPin);
  lux=sensorRawToPhys(sensorVal);
  Serial.print("Raw value from sensor= ");
  Serial.println(sensorVal); //Τύπωση αναλογικής τιμής.
  Serial.print("Physical value from sensor = ");
  Serial.print(lux); //Τύπωση αναλογικής τιμής.
  Serial.println(" lumen"); 

//Συνθήκες για το άνοιγμα των led και των κουρτινών σκίασης σε διάφορες γωνίες.
   if (ledstate == HIGH){
    if((millis() - remembertime) >= onduration){
    ledstate = LOW;
    remembertime = millis();
    }
   }
   if (ledstate == LOW){
    if((millis() - remembertime) >= offduration){
    ledstate = HIGH;
    remembertime = millis();
   }
   }
  
  //Lux.
   if (lux < 100){
   digitalWrite(led, HIGH);
   if (lux < 200){
    digitalWrite(greenled, ledstate);
   if (lux < 300){
    digitalWrite(redled, ledstate);
   }}}
   if(lux > 350){
    digitalWrite(redled, LOW);
    if(lux > 450){
    digitalWrite(greenled, LOW);
    if (lux>600){
    digitalWrite(led, LOW); 

 }}}
   else 
   digitalWrite(led,ledstate);
   
 
 //Τα δεδομένα για το ThingSpeak, τα οποία στέλνονται στα αριθμημένα fields.
  ThingSpeak.writeField(myChannelNumber, 1, t, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 2, h, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 3, g, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 4, soil_analog_value, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 5, distance, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 6, r, myWriteAPIKey);
 delay(16000); 
}
int sensorRawToPhys(int raw){
  // Conversion rule
  float Vout = float(raw) * (VIN / float(1023));// Conversion analog to voltage
  float RLDR = (R * (VIN - Vout))/Vout; // Conversion voltage to resistance
  int phys=500/(RLDR/1000); // Conversion resitance to lumen
  return phys;
 
}

That method returns a result code. It might help to check that.

Did you check that your Arduino has Internet access?

Yes, my Arduino has Internet access. But I can't understand what method should I use to send my data. I see examples on the Internet and many people use the same method.

Can you explain to me what do you mean by saying that the method I use returns a result code?
I don't have a lot of experience with Αrduino and some guidance should be helpful.

Thanks in advance!

ThingSpeak.setField(1,t);
ThingSpeak.setField(2,h);
int x = ThingSpeak.writeField(myChannelNumber, 1, t, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }

I used a different method, but still the data are not posting to ThingSpeak.
Any ideas with the different script?

Look at the example here to send multiple fields to one channel: Why y nodemcu code for PIR is not working? - #59 by 6v6gt

Also, depending on which thingspeak subscription you have, you are restricted on the rate at which you can send a transaction. With the free one, you may only send 1 transaction every 15 seconds and anything else is rejected.

I tried it, but it sends nothing to ThingSpeak...

I can't help much but you did notice that if you use
ThingSpeak.setField

then you have to use
ThingSpeak.writeFields() // note the 's' at the end

to send the transaction and clear the intermediate data setup by setfield() ?

If you have tried that, then I suggest you set up a very simple program which just sends 2 or 3 fields to a channel and get that working first. It may be a problem with your thingspeak account.

edit
ThingSpeak.writeFields() not ThingSpeak.setFields() now corrected above.

Thank you again, I did it and I had no response.
After that I tried something different.

ThingSpeak.setField(1,t);
 ThingSpeak.setField(2,h);
 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));
  }
 
delay(16000);
 

This code returned an error which is this one :
"Problem updating channel. HTTP error code -302".

So now I have to find out how to fix this error.
Are you familiar with such errors?

Show the definition and initialization of h and t.
The thingspeak error codes are here: GitHub - mathworks/thingspeak-arduino: ThingSpeak Communication Library for Arduino, ESP8266 and ESP32
Scroll nearly to the end.

//DHT11
  float t = dht.readTemperature();
  float h = dht.readHumidity(); 

I get these two variables from DHT11 sensor. They don't need initialization as I have read.

Also, I found the error from the link you posted , but unfortunately it doesn't say how to fix it.

I have created another version of this program, but the issue is that the code laggs, even though the data are sending. You can see it here :https://forum.arduino.cc/t/failing-to-send-data-to-thingspeak/998906

Maybe these 2 variables have invalid data in them.

Just to get something in thinkspeak to test it and see if the -302 error vanishes, do something like:

ThingSpeak.setField(1, 45.1 );  // test
ThingSpeak.setField(2, 92.8 );  // test
 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));
  }
 
delay(16000);

If that works, suspect the values you get back from the dht. functions.

Yes, that was a possibility, but still I am not able to send any data.
Is there another way to post my data?

Try entering this into your browser URL field:

https://api.thingspeak.com/update.json?api_key=abcdefg&field1=123

but first replace abcdefg with your API key.

If successful, you should get something that looks like this (I used 999, not 123):

image

If that works, you'll know that it is indeed possible to post data to your Thingspeak channel. If it doesn't...

1 Like


I think it is successful. Once I've run it, imediatelly the number of entries changed in my channel. (It was 10901, before I enter the URL. )
How can I fix it in order to send the data from the code?

I would set your current code aside for a while and try to get the WriteSingleField Thingspeak Ethernet example code working. It should be in the examples in your Arduino IDE but here it is on Github: thingspeak-arduino/examples/ArduinoEthernet/WriteSingleField at master · mathworks/thingspeak-arduino · GitHub

1 Like

OP cross-posted to the ThingSpeak forum, where it probably belongs:

Interesting. In post #10 here the OP claimed to have something partly working but did not mention it on the Thingspeak forum. I now see that the link to that "working" example is simply a link to this same thread.

  1. Where is this partly working example?
  2. A good start in problem solving is a tiny program which demonstrates the problem: How to create a Minimal, Reproducible Example - Help Center - Stack Overflow and suggestions to show one should not be ignored.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.