The use of the ESP8266 in the world of IoT

User avatar
By yngnnr
#80671 Hi, I am doing a mini project on water flow monitoring using Thingspeak as a cloud server. The idea here is to use a water flow sensor and nodeMCU and post real time data on Thingspeak. During the course, I have encountered lot of problems pertaining to frequent crashes on the serial monitor with the final statement called wdt reset. when I tried to troubleshoot, I found the statement called Exception(0) which means that there is an illegal instruction somewhere in the code which I cannot identify. Im in need of serious help. The following code is pasted below.



#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>


volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
flow_frequency++;
}

/* Set these to your desired credentials. */
const char *ssid = "ajay"; //ENTER YOUR WIFI SETTINGS <<<<<<<<<
const char *password = "password";

//Web address to read from
const char *host = "api.thingspeak.com";
String apiKey = "BE9WF4VXT4OTL2ZP"; //ENTER YOUR API KEY <<<<<<<<<<<
//=======================================================================
// Power on setup
//=======================================================================

void setup() {

Serial.begin(115200);
Serial.setDebugOutput(true); //Enable Serial Debug of ESP
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
Serial.begin(115200);
attachInterrupt(0, flow, RISING); // Setup Interrupt
sei(); // Enable interrupts;
currentTime = millis();
cloopTime = currentTime;

WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
//WiFi.mode(WIFI_AP_STA); //Both hotspot and client are enabled
//WiFi.mode(WIFI_AP); //Only Access point

WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");

Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP


}
//=======================================================================
// Main Program Loop
//=======================================================================


void loop() {
int h = l_hour;
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
flow_frequency = 0; // Reset Counter


}

WiFiClient client;
const int httpPort = 80; //Port 80 is commonly used for www
//---------------------------------------------------------------------
//Connect to host, host(web site) is define at top
if(!client.connect(host, httpPort)){
Serial.println("Connection Failed");
delay(300);
return; //Keep retrying until we get connected
}

//---------------------------------------------------------------------
//Make GET request as pet HTTP GET Protocol format
String ADCData;

ADCData = String(l_hour); //String to interger conversion
String Link = "GET https://api.thingspeak.com/update?api_k ... P&field1=0"; //Requeste webpage
Link = Link + ADCData;
Link = Link + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n";
client.print(Link);
delay(1000);

//---------------------------------------------------------------------
//Wait for server to respond with timeout of 5 Seconds
int timeout=0;
while((!client.available()) && (timeout < 1000)) //Wait 5 seconds for data
{
delay(10); //Use this with time out
timeout++;
}

//---------------------------------------------------------------------
//If data is available before time out read it.
if(timeout < 500)
{
while(client.available()){
Serial.println(client.readString()); //Response from ThingSpeak
}
}
else
{
Serial.println("Request timeout..");
}

delay(1000); //Read Web Page every 5 seconds
}