MATLAB Answers

Not getting reading multiple senaors data from thingspeak

8 views (last 30 days)
Md. Najmul Hasan
Md. Najmul Hasan on 22 Mar 2021
Edited: BEHNAM MOBARAKI . on 25 May 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 (2)

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.

BEHNAM MOBARAKI .
BEHNAM MOBARAKI . on 24 May 2021
Edited: BEHNAM MOBARAKI . on 24 May 2021
Hi.
I am sending temperature data from 2 sensor installed on NODEMCU ESP8266. (I am using I2C comunication). Each sensor data is being sent to a single field of a channnel. the problem is that both fields are showing the tempreature of the first sensor. Could you please check the below code and let me know the problem?
// Connect GND of HC05 to GND of Arduino
// Connect VCC of HC05 to 5V of Arduino
// Connect TXD of HC05 to pin 10 of Arduino
// Connect RXD of HC05 to pin 11 of Arduino
#include <SPI.h> //Load SPI comunication library
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <SoftwareSerial.h> // import the serial library
#include <ESP8266WiFi.h>
Adafruit_MLX90614 mlx1 = Adafruit_MLX90614();
Adafruit_MLX90614 mlx2 = Adafruit_MLX90614();
Adafruit_MLX90614 mlx3 = Adafruit_MLX90614();
String apiKey = "S9GZAP184MGDRTJKKGX"; // Enter your Write API key from ThingSpeak
const char *ssid = "ijgxsdta"; // replace with your wifi ssid and wpa2 key
const char *pass = "dfasfee55";
const char* server = "api.thingspeak.com";
#define TCAADDR0 0x5A // you need to conec nothing
WiFiClient client;
//OPENING THE FIRST MUX IN THE PORTS
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR0);
Wire.write(1 << i);
Wire.endTransmission();
}
////////////CLOSING THE FIRST MUX IN THE PORTS
void endtcaselect(){
Wire.beginTransmission(TCAADDR0);
Wire.write(0); // no channel selected
Wire.endTransmission();
}
void setup() {
// put your setup code here, to run once:
Serial.println("Adafruit MLX90614 test");
Serial.begin(115200);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
//*************INITIALIZING FIRST SENSOR MUX1*******************************
endtcaselect();
tcaselect(0);
mlx1.begin();
endtcaselect();
//*************INITIALIZING 2nd SENSOR MUX1*******************************
tcaselect(1);
mlx2.begin();
endtcaselect();
}
void loop() {
// tcaselect(3);
// Send Day-of-Week
Serial.print("HEAT-SET--6 ");
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String( float (mlx1.readAmbientTempC()));
postStr +="&field2=";
postStr += String( float (mlx2.readAmbientTempC()));
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
//*************INITIALIZING 2nd SENSOR MUX1*******************************
endtcaselect();
tcaselect(0);
Serial.print("Amb-1 = ");
Serial.print(mlx1.readAmbientTempC());
endtcaselect();
tcaselect(1);
Serial.print(" Amb-2 = ");
Serial.print(mlx2.readAmbientTempC());
}
client.stop();
Serial.println("Waiting...");
delay(60000); // delay 1 minute
}
  2 Comments
BEHNAM MOBARAKI .
BEHNAM MOBARAKI . on 25 May 2021
Dear Christopher. Many thanks for the response. I am using I2C communication for connection of the two sensors to the NODEMCU ESP8266. Therefore, I utilized multiplexer TCA9548 and connected the two sensors to the pins number (0) and (1) of the multiplexer. In the last few lines of the NODEMCU code (two Bold lines) I am defining the pin number that sensors are connected (see the below few lines of the code plz). In the serial monitor of the Arduini IDE everything is fine and the output of the two sensors are clear. But in the fields of the channel no. I am not sure if the two fields of the ThingSpeak distingush these two sensors or not?!!!
endtcaselect();
tcaselect(0);
Serial.print("Amb-1 = ");
Serial.print(mlx1.readAmbientTempC());
endtcaselect();
tcaselect(1);
Serial.print(" Amb-2 = ");
Serial.print(mlx2.readAmbientTempC());
}

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

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

Start Hunting!

Translated by