thingspeak values for real time data of my IOT weather station are displaying as zero.

14 Ansichten (letzte 30 Tage)
I am making an IOT based online and lcd based off;ine weather station. I have uploaded a code to my arduino and ait works absolutely fine displaying all values on the lcd. but when i am using my esp8266 the code gets uploaded but the values on the charts in thingspeak is all zero even after 90 entries.
  3 Kommentare
Ayman Mulla
Ayman Mulla am 17 Mai 2023
This is the code uploaded to arduino
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <dht.h>
#include <Wire.h>
#include <BMP180.h>
dht DHT;
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial mySerial(10, 11);
BMP180 myBMP(BMP180_ULTRAHIGHRES);
#define DHT11_PIN A0
#define mq135_pin A2
#define LDR A1
#define rain_sensor_pin A3 // Pin connected to the rain sensor
void ReadDHT(void);
void ReadBMP(void);
void Readlight(void);
void ReadAir(void);
void ReadRain(void); // New function to read the rain sensor
void send_data(void);
bool BMP_flag = 0;
bool DHT_flag = 0;
void setup()
{
mySerial.begin(115200);
pinMode(mq135_pin, INPUT);
pinMode(LDR, INPUT);
pinMode(rain_sensor_pin, INPUT); // Set the rain sensor pin as input
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" IoT Weather ");
lcd.setCursor(0, 1);
lcd.print("Monitor System");
delay(1500);
}
void loop()
{
ReadDHT();
ReadBMP();
Readlight();
ReadAir();
ReadRain(); // Read the rain sensor data
send_data();
}
void ReadDHT(void)
{
lcd.clear();
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
DHT_flag = 1;
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(DHT.temperature, 1);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(DHT.humidity, 1);
lcd.print(" %");
break;
case DHTLIB_ERROR_CONNECT:
lcd.setCursor(0, 0);
lcd.print("NO DHT11 SENSOR");
lcd.setCursor(0, 1);
lcd.print(" FOUND! ");
break;
case DHTLIB_ERROR_CHECKSUM:
case DHTLIB_ERROR_TIMEOUT:
case DHTLIB_ERROR_ACK_L:
case DHTLIB_ERROR_ACK_H:
default:
DHT_flag = 1;
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(DHT.temperature, 1);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(DHT.humidity, 1);
lcd.print(" %");
break;
}
delay(5000);
}
void ReadBMP(void)
{
lcd.clear();
if (myBMP.begin() != true)
{
lcd.setCursor(0, 0);
lcd.print(" BMP180 SENSOR ");
lcd.setCursor(0, 1);
lcd.print(" NOT FOUND ");
BMP_flag = 0;
delay(5000);
}
else
{
BMP_flag = 1;
lcd.setCursor(0, 0);
lcd.print("Pa(Grnd):");
lcd.print(myBMP.getPressure());
lcd.setCursor(0, 1);
lcd.print("Pa(sea) :");
lcd.print(myBMP.getSeaLevelPressure(115));
}
delay(5000);
}
void Readlight(void)
{
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("LIGHT :");
lcd.print(map(analogRead(LDR), 0, 1024, 0, 99));
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("****************");
delay(5000);
}
void ReadAir(void)
{
int airqlty = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("AIR QUALITY:");
airqlty = analogRead(mq135_pin);
lcd.print(map(analogRead(mq135_pin), 0, 1024, 99, 0));
lcd.print("%");
lcd.setCursor(0, 1);
if (airqlty <= 180)
lcd.print("GOOD!");
else if (airqlty > 180 && airqlty <= 225)
lcd.print("POOR");
else if (airqlty > 225 && airqlty <= 300)
lcd.print("VERY BAD");
else
lcd.print("TOXIC");
delay(5000);
}
void ReadRain(void)
{
int rainValue = analogRead(rain_sensor_pin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RAIN SENSOR:");
lcd.setCursor(0, 1);
lcd.print("Value: ");
lcd.print(rainValue);
delay(5000);
}
void send_data()
{
mySerial.print('*'); // Starting char
if (DHT_flag == 1)
{
mySerial.print(DHT.temperature, 0); // 2 digit data
mySerial.print(DHT.humidity, 0); // 2 digit data
}
else
{
mySerial.print("0000"); // Send dummy data
}
if (BMP_flag == 1)
{
mySerial.print(myBMP.getPressure()); // 5 digit data
}
else
{
mySerial.print("00000"); // Send dummy data
}
mySerial.print(map(analogRead(LDR), 0, 1024, 0, 99)); // 2 digit data
mySerial.print(map(analogRead(mq135_pin), 0, 1024, 99, 0)); // 2 digit data
mySerial.print(analogRead(rain_sensor_pin)); // Send rain sensor value
mySerial.println('#'); // Ending char
}
this is the code uploaded to esp8266 wifi module
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
//------- WI-FI details ----------//
char ssid[] = "Afiaayman"; // SSID here
char pass[] = "Malamahitinai"; // Password here
//--------------------------------//
//----------- Channel details ----------------//
unsigned long Channel_ID = 2018764; // Your Channel ID
const char *myWriteAPIKey = "Q06WMKAOOY24Q6R5"; // Your write API key
//-------------------------------------------//
const int Field_Number_1 = 1;
const int Field_Number_2 = 2;
const int Field_Number_3 = 3;
const int Field_Number_4 = 4;
const int Field_Number_5 = 5;
const int Field_Number_6 = 6; // New field number for value_6
String value = "";
int value_1 = 0, value_2 = 0, value_3 = 0, value_4 = 0, value_5 = 0, value_6 = 0; // New variable value_6
WiFiClient client;
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
internet();
}
void loop()
{
internet();
if (Serial.available() > 0)
{
delay(100);
while (Serial.available() > 0)
{
value = Serial.readString();
Serial.println(value);
if (value[0] == '*')
{
if (value.length() >= 18 && value[17] == '#')
{
value_1 = ((value[1] - '0') * 10 + (value[2] - '0'));
value_2 = ((value[3] - '0') * 10 + (value[4] - '0'));
value_3 = ((value[5] - '0') * 10000 + (value[6] - '0') * 1000 + (value[7] - '0') * 100 + (value[8] - '0') * 10 + (value[9] - '0'));
value_4 = ((value[10] - '0') * 10 + (value[11] - '0'));
value_5 = ((value[12] - '0') * 10 + (value[13] - '0'));
value_6 = ((value[15] - '0') * 10 + (value[16] - '0'));
}
}
}
}
upload();
}
void internet()
{
if (WiFi.status() != WL_CONNECTED)
{
while (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass);
delay(5000);
}
}
}
void upload()
{
ThingSpeak.writeField(Channel_ID, Field_Number_1, value_1, myWriteAPIKey);
delay(15000);
ThingSpeak.writeField(Channel_ID, Field_Number_2, value_2, myWriteAPIKey);
delay(15000);
ThingSpeak.writeField(Channel_ID, Field_Number_3, value_3, myWriteAPIKey);
delay(15000);
ThingSpeak.writeField(Channel_ID, Field_Number_4, value_4, myWriteAPIKey);
delay(15000);
ThingSpeak.writeField(Channel_ID, Field_Number_5, value_5, myWriteAPIKey);
delay(15000);
ThingSpeak.writeField(Channel_ID, Field_Number_6, value_6, myWriteAPIKey); // Write value_6 to ThingSpeak
delay(15000);
value = "";
}
Christopher Stapels
Christopher Stapels am 17 Mai 2023
Bearbeitet: Christopher Stapels am 17 Mai 2023
Can you edit the code just to show the parts where you communicate to ThingSpeak? Also there is a code button in the editor that will make it wasier to read the code.Would you consider using that as well, pelase?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Christopher Stapels
Christopher Stapels am 18 Mai 2023
In the upload() section, I would suggest you try hard coding values instead of value_1, etc. Like this
ThingSpeak.writeField(Channel_ID, Field_Number_1, 321, myWriteAPIKey);
Then see if that number shows up in ThingSpeak correctly. If so, then your variables arent being set correctly.
Also, you dont have to publish each field individually. You will get null values in all the other fields if you do.
Then you will publish all values in a single write action, and they will all have the same timestamp. Your field charts will look much nicer and you will save a lot of writing time during which your processor could take a small nap.
And can you please edit the code above just to show the parts where you interact with thingspeak.
  3 Kommentare
Christopher Stapels
Christopher Stapels am 18 Mai 2023
Thanks! Were you able to try hard coding the values in the write and writing multiple fields in one call?
Ayman Mulla
Ayman Mulla am 19 Mai 2023
I tried a different thing. What I did was I added Serial.begin(9600) in my arduino code and for esp I kept it mySerial.begin(115200).
void setup()
{
Serial.begin(9600);
mySerial.begin(115200);
pinMode(mq135_pin, INPUT);
pinMode(LDR, INPUT);
pinMode(rain_sensor_pin, INPUT); // Set the rain sensor pin as input
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" IoT Weather ");
lcd.setCursor(0, 1);
lcd.print("Monitor System");
delay(1500);
}
After this while uploading the arduino code I changed the buad rate to 9600 and the data started to appear on the thingspeak. However whatever the value of first entry for temperature and humidity it stays the same through out in thingspeak even if the lcd displays a change in temperature. Where as the air quality and light value are floating and flactuating very high and low no where near to the actual value. The pressure and rain sensor values are correct and varying accordingly.

Melden Sie sich an, um zu kommentieren.

Communitys

Weitere Antworten in  ThingSpeak Community

Kategorien

Mehr zu Write Data to Channel finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by