MATLAB Answers

Not getting reading multiple senaors data from thingspeak

17 views (last 30 days)
Md. Najmul Hasan
Md. Najmul Hasan on 22 Mar 2021
i am using arduino mega and esp 01.i am getting reading on serial monitor.but thingspeak shows 0 for 5 sensors data fields.where is the problem in this code please help me.
Esp 01 code-
#include "ThingSpeak.h" #include ESP8266WiFi.h
char ssid[] = "xxxxxxx"; // SSID here char pass[] = "xxxxxxxx"; // Passowrd here
unsigned long Channel_ID = xxxxxx; // Channel ID const char * myWriteAPIKey = "xxxxxxxx"; // 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; String value = ""; int value_1 = 0, value_2 = 0, value_3 = 0, value_4 = 0, value_5 = 0; 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(); if (value[0] == '*') { if (value[11] == '#') { value_1 = ((value[1] - 0x30) * 10 + (value[2] - 0x30)); value_2 = ((value[3] - 0x30) * 10 + (value[4] - 0x30)); value_3 = ((value[5] - 0x30) * 10 + (value[6] - 0x30)); value_4 = ((value[7] - 0x30) * 10 + (value[8] - 0x30)); value_5 = ((value[9] - 0x30) * 10 + (value[10] - 0x30)); }
}
}
}
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); value = ""; }
arduino code-
#include dht.h #define soilWet 56 #define soilDry 50
const int LM35 = A0; // Soil Temperature pin
// lowest and highest sensor readings (Rain Detection) const int sensorMin = 0; // sensor minimum const int sensorMax = 1024; // sensor maximum
#define sensorPower 5 // Motor pin
int obstaclePin = 10; // IR sensor pin 7 int hasObstacle = HIGH; // High means no obstacle
const int trigger = 34; const int echo = 35; long T; float distanceCM;
dht DHT; #define DHT11_PIN 30 int humi,temp;
void setup(){ pinMode(sensorPower, OUTPUT); // Initially keep the Motor OFF digitalWrite(sensorPower, HIGH);
pinMode(obstaclePin, INPUT);
pinMode(trigger, OUTPUT); pinMode(echo, INPUT); Serial.begin(9600); }
void loop() { int moisture = (100 - ((analogRead(A1)/1023.00)*100));
digitalWrite(trigger, LOW); delay(10); digitalWrite(trigger, HIGH); delayMicroseconds(10); digitalWrite(trigger, LOW); T = pulseIn(echo, HIGH); distanceCM = T * 0.034; distanceCM = distanceCM / 2;
int ADC; float tempS; ADC = analogRead(LM35); tempS = (ADC * 4.88); tempS = (tempS / 10);
int sensorReading = analogRead(A2); // Rain detection pin A2 int range = map(sensorReading, sensorMin, sensorMax, 0, 3); // Rain detection
switch (range) { case 0: // Sensor getting wet Serial.println("Rain Status : *Raining***"); break; case 1: // Sensor getting wet Serial.println("Rain Status : *Rain Warning*"); break; case 2: // Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below. Serial.println("Rain Status : Not Raining"); break; }
DHT.read11(DHT11_PIN); humi=DHT.humidity; temp=DHT.temperature;
Serial.print("Air Temperature = "); Serial.print(temp); Serial.println("°C "); Serial.print("Air Humidity = "); Serial.print(humi); Serial.println(" %"); Serial.print("Distance = "); Serial.print(distanceCM); Serial.println("cm"); Serial.print("Soil Temperature = "); Serial.print(tempS); Serial.println("°C"); Serial.print("Soil Moisture = "); Serial.print(moisture); Serial.println(" %");
if (moisture < soilWet) { Serial.println("Status: Soil is too wet"); } else if (moisture >= soilWet && moisture < soilDry) { Serial.println("Status: Soil moisture is perfect"); } else { Serial.println("Status: Soil is too dry - time to water!"); }
if (moisture < 35) { digitalWrite(sensorPower, HIGH); // turn ON motor Serial.println("Motor is ON"); } if (moisture > 35 && moisture < 37) { digitalWrite(sensorPower, HIGH); //turn ON motor Serial.println("Motor is ON"); } if (moisture > 38) { digitalWrite(sensorPower, LOW); // turn OFF motor Serial.println("Motor is OFF"); }
hasObstacle = digitalRead(obstaclePin); //Reads the output of the obstacle sensor from the 7th PIN of the Digital section of the arduino
if (hasObstacle == LOW) //LOW means something is ahead
{
Serial.println("Something is ahead !!!");
}
else {
Serial.println("Path is clear");
}
Serial.println("--------------------------------------------------------------------------"); Serial.println("");
delay(3000); }

Answers (1)

Christopher Stapels
Christopher Stapels on 22 Mar 2021
I'd start by trying to write one data from the ESP.
Just hard code something and make sure it writes to ThingSpeak. Take all the other writes out.
ThingSpeak.writeField(Channel_ID, Field_Number_2, 123, myWriteAPIKey);
Move the delay after the upload in the loop function so it doesnt get called too often.
upload();
delay(15000);
When you are ready to put the five reads back in, look at the write multiple fields example (in the arduino library examples) so you can use ThingSpeak,writeFields() after ThingSpeak.addField. You wont have to have all the delay in you upload function. You can write up to 8 fields in one command. (as long as they are in teh same channel.

Community Treasure Hunt

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

Start Hunting!