#include <ThingSpeak.h>
#include <Arduino.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <DHT.h> // Including library for dht
#include <WiFiClient.h>
String apiKey = "YWEFRSBO8QVAN6FY"; // Enter your Write API key from ThingSpeak
const char *ssid = "Mattath-Fi"; // replace with your wifi ssid and wpa2 key
const char pass = "12345678";
const char server = "api.thingspeak.com ";
#define DHTPIN 0 //pin where the dht11 is connected
const int sensor_pin = A0;
DHT dht(DHTPIN, DHT11);
unsigned long myTime = 0;
//const long interval = 60000;
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
dht.begin();
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");
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
float m;
m = ( 100.00 - ( (analogRead(sensor_pin)/1023.00) * 100.00 ) );
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from Sensors!");
return;
}
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(m);
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);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.print("%, Soil Moisture:");
Serial.print(m);
Serial.println("%. Send to Thingspeak.");
}
delay(1000);
client.stop();
Serial.println("Waiting...");
unsigned long currentMillis = millis();
*if (currentMillis - previousMillis > interval)
{
Serial.println("Anomaly Occured...");
}
delay(1000);
}
This is my code to generate the dataset from A DHT11 Sensor and a Soil Moisture Sensor. By uploading the code its starts generating the data and can be seen in the serial monitor. What I want is if the data is not generated for about 1 minute ie the "Waiting" message is been showing in the Serial Monitor for more than 1 minute, a new message has to be displayed in the serial monitor that "Anomaly Occurred". I am stuck with it. Can anybody really help me out? Trying for a long now........
Thanks......
Use of code tags:
#include <ThingSpeak.h>
#include <Arduino.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <DHT.h> // Including library for dht
#include <WiFiClient.h>
String apiKey = "YWEFRSBO8QVAN6FY"; // Enter your Write API key from ThingSpeak
const char *ssid = "Mattath-Fi"; // replace with your wifi ssid and wpa2 key
const char *pass = "12345678";
const char* server = "api.thingspeak.com";
#define DHTPIN 0 //pin where the dht11 is connected
const int sensor_pin = A0;
DHT dht(DHTPIN, DHT11);
unsigned long myTime = 0;
//const long interval = 60000;
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
dht.begin();
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");
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
float m;
m = ( 100.00 - ( (analogRead(sensor_pin)/1023.00) * 100.00 ) );
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from Sensors!");
return;
}
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(m);
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);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.print("%, Soil Moisture:");
Serial.print(m);
Serial.println("%. Send to Thingspeak.");
}
delay(1000);
client.stop();
Serial.println("Waiting...");
unsigned long currentMillis = millis();
*if (currentMillis - previousMillis > interval)
{
Serial.println("Anomaly Occured...");
}
delay(1000);
}
this wont work
unsigned long currentMillis = millis();
*if (currentMillis - previousMillis > interval)
{
Serial.println("Anomaly Occured...");
}
You see how you set the time to wait and keep resetting the time to wait before you've ever waited the time.
BUT the millis code becomes irrelevant with all the other delays.
See using millis
and
doing several tings at the same time
Edit the OP and put your code in code tags, please?
Expect the dhtthingy to reguarlly produce nan's
I'd not print
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from Sensors!");
return;
}
each time you get a nan, serial printing is a time consuming thingy. best left when you really need the info.
if (isnan(h) || isnan(t))
{
nanWarningCOunt++;
if ( nanWarningCOunt ==10)
{ Serial.println("Failed to read from Sensors!"); //10 nans in a row error
nanWarningCOunt=0;
} else{
nanWarningCount=0;
}
}
drmpf
March 25, 2021, 7:26pm
5
You need to do two things,
i) move to a multi-tasking design, see my tutorial on Multi-tasking in Arduino
ii) use timers instead of delays to 'fire' your tasks at the correct time, see my tutorial on How to write Timers and Delays in Arduino
In your case the timer that 'fires' the msg will be reset by the task that generates the data so it won't 'fire' as long as there has been data generated in the last min
system
Closed
July 23, 2021, 7:26pm
6
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.