Error while uploading sensors data to ThingSpeak!!

Hey guys, how you doing? Hope that everything goes well!

So I'm trying to upload data to my channel to ThingSpeak (Moisture sensor, Light sensor "TSL2561", and Temperature & Humidity sensor "DHT22") with an arduino uno with ESP8266-01.

Here are my connection for ESP

GND >> GND
RXD >> Pin 3
TXF >> Pin 2
CH_PD & VCC >> 3.3v

And the sensors connection are good! I guss!!

Here is the code I'm using

#include<SoftwareSerial.h>


SoftwareSerial esp8266(2, 3);
bool isDebug = false;


String writeAPIKey = ""; // Here you will write you API Write Code

//-- IoT Information
#define SSID ""; //Here your wirless name
#define PASS "" // Your Password
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
String GET = "GET /update?key="+writeAPIKey;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  esp8266.begin(115200); 
  connectWiFi();

}
String res;
void loop() {
  // put your main code here, to run repeatedly:
  //YOUR LOGIC

  //-------------------
     updateTS(x,y,z);

}

void updateTS( String T, String L , String H)
{
  // ESP8266 Client
  String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
  cmd += IP;
  cmd += "\",80";
  sendData(cmd,false);
  delay(2000);
  if ( esp8266.find( "Error" ) )
  {
    Serial.print( "RECEIVED: Error\nExit1" );
    return;
  }
  //Here you will repeat for each filed in your channel

  cmd = GET + "&field1=" + T + "&field2=" + L + "&field3=" + H + "\r\n";
  esp8266.print( "AT+CIPSEND=" );
  esp8266.println( cmd.length() );
  if (esp8266.find( ">" ) )
  {
    Serial.print(">");
    Serial.print(cmd);
    esp8266.print(cmd);
  }
  else
  {
    sendData( "AT+CIPCLOSE",false);//close TCP connection
  }
  if ( esp8266.find("OK") )
  {
    Serial.println( "RECEIVED: OK" );
  }
  else
  {
    Serial.println( "RECEIVED: Error\nExit2" );
  }
}
String sendData(String cmd, bool isDebug) {
  String resp = "";
  esp8266.println(cmd);
  while (esp8266.available()) {
    char c = esp8266.read();
    resp += c;

  }
  if (isDebug) {
    Serial.print(resp);
  }
  return resp;

}

boolean connectWiFi()
{
  sendData("AT+CWMODE=1", true); //WiFi STA mode - if '3' it is both client and AP
  delay(2000);
  //Connect to Router with AT+CWJAP="SSID","Password";
  // Check if connected with AT+CWJAP?
  String cmd = "AT+CWJAP=\""; // Join accespoint
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  sendData(cmd, true);
  delay(5000);
  if (esp8266.find("OK"))
  {
    //esp8266.println("RECEIVED: OK");
    Serial.println("Connected");
    return true;
  }
  else
  {
    // debug.println("RECEIVED: Error");

    Serial.println("ERROR HERE");

    return false;
  }

  cmd = "AT+CIPMUX=0";// Set Single connection
  sendData(cmd, false);
  if ( Serial.find( "Error") )
  {
    // debug.print( "RECEIVED: Error" );
    return false;
  }
  Serial.println("CONNECTED");
}

So I try to run the sketch and I get that error

Arduino: 1.8.2 (Windows 8.1), Board: "Arduino/Genuino Uno"

C:\Users\Yuu\Documents\Arduino\sketch_may17a\sketch_may17a.ino: In function 'void loop()':

sketch_may17a:30: error: 'x' was not declared in this scope

updateTS(x,y,z);

^

sketch_may17a:30: error: 'y' was not declared in this scope

updateTS(x,y,z);

^

sketch_may17a:30: error: 'z' was not declared in this scope

updateTS(x,y,z);

^

exit status 1
'x' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

So anyone can help?! Thanks in advance :slight_smile:

You never initialise x, y or z. What information are they supposed to contain?

Well they suppose to contain data values of these sensors (Moisture, TSL2561, and DHT22)

Well they suppose to contain data values of these sensors (Moisture, TSL2561, and DHT22)

So, why haven't you declared x, y, and z? Why haven't you assigned meaningful data to the variables with meaningless names?

I did it and the sketch run without errors

String res;
void loop() {
  // put your main code here, to run repeatedly:
  //YOUR LOGIC

  //-------------------
   String x;
   String y;
   String z;
     updateTS(x,y,z);

}

void updateTS( String T, String L , String H)

But it didn't send any data to ThingSpeak, I don't know what's the problem tho?

I don't know what's the problem tho?

What IS in x? What IS in y? What IS in z? Is that what you REALLY think that thingsqueak can plot for you?

These are variables set for the reading of sensors!! I don't wanna to set a static values, I need it to plot real-time sensor's values.Sir

I'm new to that, so can you explain how to fix that?!

These are variables set for the reading of sensors!

Where do you read the sensors? Does reading any of the sensors result in a String? Why are the names useless?

   updateTS(temperature, humidity, moisture, light);

would make sense.

   updateTS(x, y, z);

does not, regardless of whether you counted correctly, or not.

Of course, you still need to properly declare temperature, humidity, moisture, and light, and initialize them with meaningful data.

Hmmmm how can I read a sensor?! and how to declare them and initializing them with meaningful data?

No reading of them should be a float

void loop() {
  
   float temperature;
   float humidity;
   float moisture;
   float light;
     updateTS(moisture, light, temperature, humidity);

}

void updateTS( float M, float L , float T, float H)
{
  // ESP8266 Client
  String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
  cmd += IP;
  cmd += "\",80";
  sendData(cmd,false);
  delay(2000);
  if ( esp8266.find( "Error" ) )
  {
    Serial.print( "RECEIVED: Error\nExit1" );
    return;
  }
  //Here you will repeat for each filed in your channel

  cmd = GET + "&field1=" + M + "&field2=" + L + "&field3=" + T +"&field4=" + H + "\r\n";

So what next?!

You must read sensors before you use updateTS.

Include DHT.h library, create object with pin and type of sensor, then read temp and humidity.

So I'm trying to upload data to my channel to ThingSpeak (Moisture sensor, Light sensor "TSL2561", and Temperature & Humidity sensor "DHT22") with an arduino uno with ESP8266-01.

Here you have a nice example using DHT22.

Same with TSL2561 example and library

Moisture finally, link.
Choice an analog pin to read it. For instance, A0.
Only this code to read it

// define as global 
const byte sensorValue = A0;

// inside loop
sensorValue = analogRead(sensorPin);

Read all sensors and THEN send info using updateTS(bla bla bla)

So the code will be like that ?!

#include <TSL2561.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <DHT.h>
#include<SoftwareSerial.h>

#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);


const byte sensorValue = A0;

SoftwareSerial esp8266(2, 3);
bool isDebug = false;


String writeAPIKey = ""; // Here you will write you API Write Code

//-- IoT Information
#define SSID ""; //Here your wirless name
#define PASS "" // Your Password
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
String GET = "GET /update?key="+writeAPIKey;


void setup() {
  // put your setup code here, to run once:
  dht.begin();
  Serial.begin(9600);
  esp8266.begin(115200);
  connectWiFi();

}
String res;
void loop() {

  sensorValue = analogRead(0);

   float temperature = dht.readTemperature;
   float humidity = dht.readHumidity;
   float moisture;
   float light;
     updateTS(moisture, light, temperature, humidity);

}

void updateTS( float M, float L , float T, float H)
{
  // ESP8266 Client
  String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
  cmd += IP;
  cmd += "\",80";
  sendData(cmd,false);
  delay(2000);
  if ( esp8266.find( "Error" ) )
  {
    Serial.print( "RECEIVED: Error\nExit1" );
    return;
  }
  //Here you will repeat for each filed in your channel

  cmd = GET + "&field1=" + M + "&field2=" + L + "&field3=" + T +"&field4=" + H + "\r\n";
  esp8266.print( "AT+CIPSEND=" );
  esp8266.println( cmd.length() );
  if (esp8266.find( ">" ) )
  {
    Serial.print(">");
    Serial.print(cmd);
    esp8266.print(cmd);
  }
  else
  {
    sendData( "AT+CIPCLOSE",false);//close TCP connection
  }
  if ( esp8266.find("OK") )
  {
    Serial.println( "RECEIVED: OK" );
  }
  else
  {
    Serial.println( "RECEIVED: Error\nExit2" );
  }
}
String sendData(String cmd, bool isDebug) {
  String resp = "";
  esp8266.println(cmd);
  while (esp8266.available()) {
    char c = esp8266.read();
    resp += c;

  }
  if (isDebug) {
    Serial.print(resp);
  }
  return resp;

}

boolean connectWiFi()
{
  sendData("AT+CWMODE=1", true); //WiFi STA mode - if '3' it is both client and AP
  delay(2000);
  //Connect to Router with AT+CWJAP="SSID","Password";
  // Check if connected with AT+CWJAP?
  String cmd = "AT+CWJAP=\""; // Join accespoint
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  sendData(cmd, true);
  delay(5000);
  if (esp8266.find("OK"))
  {
    //esp8266.println("RECEIVED: OK");
    Serial.println("Connected");
    return true;
  }
  else
  {
    // debug.println("RECEIVED: Error");

    Serial.println("ERROR HERE");

    return false;
  }

  cmd = "AT+CIPMUX=0";// Set Single connection
  sendData(cmd, false);
  if ( Serial.find( "Error") )
  {
    // debug.print( "RECEIVED: Error" );
    return false;
  }
  Serial.println("CONNECTED");
}

But I get this error message again :roll_eyes:

Arduino: 1.8.2 (Windows 8.1), Board: "Arduino/Genuino Uno"

C:\Users\Yuu\Documents\Arduino\sketch_may17a\sketch_may17a.ino: In function 'void loop()':

sketch_may17a:38: error: assignment of read-only variable 'sensorValue'

sensorValue = analogRead(0);

^

sketch_may17a:40: error: cannot convert 'DHT::readTemperature' from type 'float (DHT::)(bool, bool)' to type 'float'

float temperature = dht.readTemperature;

^

sketch_may17a:41: error: cannot convert 'DHT::readHumidity' from type 'float (DHT::)(bool)' to type 'float'

float humidity = dht.readHumidity;

^

exit status 1
assignment of read-only variable 'sensorValue'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Is there any good reason why you've started yet another topic (now deleted) on this subject?

Think carefully about your answer.

Maybe I asked in wrong place, or maybe someone else gonna see it a help me to solve it!
And for sure because I still stuck here and no one tell me what to do :confused:

Got to work thru your code problems.
First one:

sketch_may17a:38: error: assignment of read-only variable 'sensorValue'

sensorValue = analogRead(0);

^

You're trying to read A0 and store the result as sensorValue.
However, you made sensorValue a constant value with this line:

const byte sensorValue = A0;

So the sketch cannot put a new reading into it.
change that line to this instead
int sensorValue; // will hold 10-bit result of analogRead(0);

I don't know what to do about the float errors.

Thanks sir it works but still get this errors

Arduino: 1.8.2 (Windows 8.1), Board: "Arduino/Genuino Uno"

C:\Users\Yuu\Documents\Arduino\sketch_may17a\sketch_may17a.ino: In function 'void loop()':

sketch_may17a:40: error: cannot convert 'DHT::readTemperature' from type 'float (DHT::)(bool, bool)' to type 'float'

float temperature = dht.readTemperature;

^

sketch_may17a:41: error: cannot convert 'DHT::readHumidity' from type 'float (DHT::)(bool)' to type 'float'

float humidity = dht.readHumidity;

^

exit status 1
cannot convert 'DHT::readTemperature' from type 'float (DHT::)(bool, bool)' to type 'float'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

What's the problem with float tho?

float humidity = dht.readHumidity;

you call a function like this:

float humidity = dht.readHumidity();

Wow, it works without any errors. Thanks a lot :slight_smile:

But still have problem, my channel on ThingSpeak don't show any data. What's wrong do you think I have?

My code

#include <TSL2561.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <DHT.h>
#include<SoftwareSerial.h>

#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);



SoftwareSerial esp8266(2, 3);
bool isDebug = false;


String writeAPIKey = "HVQJ5ZI88C3PFB46"; // Here you will write you API Write Code

//-- IoT Information
#define SSID "Vaffaculo"; //Here your wirless name
#define PASS "Vaffaculo1395" // Your Password
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
String GET = "GET /update?key="+writeAPIKey;


void setup() {
  // put your setup code here, to run once:
  dht.begin();
  Serial.begin(9600);
  esp8266.begin(115200);
  connectWiFi();

}
String res;
void loop() {
float sensorValue;

  sensorValue = analogRead(0);

   float temperature = dht.readTemperature();
   float humidity = dht.readHumidity();
   float moisture;
   float light;
     updateTS(moisture, light, temperature, humidity);

}

void updateTS( float M, float L , float T, float H)
{
  // ESP8266 Client
  String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
  cmd += IP;
  cmd += "\",80";
  sendData(cmd,false);
  delay(2000);
  if ( esp8266.find( "Error" ) )
  {
    Serial.print( "RECEIVED: Error\nExit1" );
    return;
  }
  //Here you will repeat for each filed in your channel

  cmd = GET + "&field1=" + M + "&field2=" + L + "&field3=" + T +"&field4=" + H + "\r\n";
  esp8266.print( "AT+CIPSEND=" );
  esp8266.println( cmd.length() );
  if (esp8266.find( ">" ) )
  {
    Serial.print(">");
    Serial.print(cmd);
    esp8266.print(cmd);
  }
  else
  {
    sendData( "AT+CIPCLOSE",false);//close TCP connection
  }
  if ( esp8266.find("OK") )
  {
    Serial.println( "RECEIVED: OK" );
  }
  else
  {
    Serial.println( "RECEIVED: Error\nExit2" );
  }
}
String sendData(String cmd, bool isDebug) {
  String resp = "";
  esp8266.println(cmd);
  while (esp8266.available()) {
    char c = esp8266.read();
    resp += c;

  }
  if (isDebug) {
    Serial.print(resp);
  }
  return resp;

}

boolean connectWiFi()
{
  sendData("AT+CWMODE=1", true); //WiFi STA mode - if '3' it is both client and AP
  delay(2000);
  //Connect to Router with AT+CWJAP="SSID","Password";
  // Check if connected with AT+CWJAP?
  String cmd = "AT+CWJAP=\""; // Join accespoint
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  sendData(cmd, true);
  delay(5000);
  if (esp8266.find("OK"))
  {
    //esp8266.println("RECEIVED: OK");
    Serial.println("Connected");
    return true;
  }
  else
  {
    // debug.println("RECEIVED: Error");

    Serial.println("ERROR HERE");

    return false;
  }

  cmd = "AT+CIPMUX=0";// Set Single connection
  sendData(cmd, false);
  if ( Serial.find( "Error") )
  {
    // debug.print( "RECEIVED: Error" );
    return false;
  }
  Serial.println("CONNECTED");
}

precagriculture:
But still have problem, my channel on ThingSpeak don't show any data. What's wrong do you think I have?

I don't speak Thing, sorry.

Yoda:
Do. Or do not. There is no try.

Any recommendation where I can find speak thing tho? :slight_smile: