my nodelua coded with arduino code sorry beginner here. keeps crashing i think when i have multiple requests like if i request the page from web browser then from my phone but it doesnt always happen.
it just stops doing anything and doesn't show anything in serial or respond to request through the webpage.
maybe adding in a disable interrupt would work ?
Any thoughts / ideas would be appreciated
My code is as follows:
/////////////////////////////////////////////
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio/0 will set the GPIO2 low,
* http://server_ip/gpio/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 39600, 60000);
const char* ssid = "Alien";
const char* password = "ddbim48572";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
/////////////SOIL Sensor pinS/////////////////////////////
int sensorPin = A0; // moisture soil sensor connection
int sensorValue = 0; // Sensor value from input
////////////DHT TEMPERATURE + HUMIDITY SENSOR////////////
#include "DHT.h"
DHT dht;
////////////RELAY PINS//////////////////////////////
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT); // Relay Pin connection
pinMode(4, OUTPUT);
digitalWrite(2, 0);
digitalWrite(4, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
dht.setup(5); // data pin 2 FOR DHT SENSOR
timeClient.begin();
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
sensorValue = analogRead(sensorPin); // Convert Sensor pin data too value sensorvalue
timeClient.update();
static int test=0;
static int val=0;
static int val1=0;
if (req.indexOf("/gpio/0") != -1)
{
val = 0;
// digitalWrite(2, LOW);
}
else if (req.indexOf("/gpio/1") != -1)
{
val = 1;
// digitalWrite(2, HIGH);
}
else if (req.indexOf("/gpio/2") != -1)
{
val1 = 0;
//digitalWrite(4, LOW);
}
else if (req.indexOf("/gpio/3") != -1)
{
val1 = 1;
// digitalWrite(4, HIGH);
}
else if (req.indexOf("/home") != -1)
{
test = 1;
// digitalWrite(4, HIGH);
}
else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(2, val);
digitalWrite(4, val1);
client.flush();
// Prepare the response
String s = "";
s += (val)?"On":"Off";
s += "</html>\n";
String s1 = "";
s1 += (val1)?"On":"Off";
s1 += "</html>\n";
// Send the response to the client
/////////////////dht//////////////////
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(humidity, 1);
Serial.print("\t\t");
Serial.print(temperature, 1);
Serial.print("\t\t");
Serial.println(dht.toFahrenheit(temperature), 1);
//////////////////dht end/////////////
client.print("<h1 style='text-align: center;'>Symons <strong>Irrigation</strong> Controller</h1>");
//HTML TABLE
client.println("<table border=1 cellpadding=5 cellspacing=5>");
client.println("<tr>");
client.println("<th>Item Label</th>");
client.println("<th>Status/Value</th>");
client.println("<th>Control</th>");
client.println("<th>Recorded @6am</th>");
client.println("<th>Recorded @12am</th>");
client.println("<th>Recorded @6pm</th>");
client.println("<th>Recorded @12pm</th>");
client.println("</tr>");
client.println("<tr>");
client.println("<td>Plot 1 Moisture %</td>");
client.println("<td>");
client.print(sensorValue);
client.println("</td>");
client.println("</tr>");
client.println("<tr>");
client.println("<td>Plot 2 Moisture %</td>");
client.println("<td>NULL[1] </td>");
client.println("</tr>");
client.println("<td>Plot 3 Moisture %</td>");
client.println("<td>NULL[2] </td>");
client.println("</tr>");
client.println("<td>Humidity</td>");
client.println("<td>");
client.print(humidity, 1);
client.print(" %");
client.println("</td>");
client.println("</tr>");
client.println("<td>Temperature</td>");
client.println("<td>");
client.print(temperature, 1);
client.print(" C");
client.println("</td>");
client.println("</tr>");
client.println("<td>Plot 1 water</td>");
client.println("<td>");
client.print(s);
client.println("</td>");
client.println("<td>");
client.print("<a href=\'/gpio/1\'><button>ON</button></a>");
client.print("<a href=\'/gpio/0\'><button>OFF</button></a>");
client.println("</td>");
client.println("</tr>");
client.println("<tr>");
client.println("<td>Plot 2 water");
client.println("</td>");
client.println("<td>");
client.print(s1);
client.println("</td>");
client.println("<td>");
client.print("<a href=\'/gpio/3\'><button>ON</button></a>");
client.print("<a href=\'/gpio/2\'><button>OFF</button></a>");
client.println("</td>");
client.println("</tr>");
client.println("<tr>");
client.println("<td>Plot Timer 1</td>");
client.println("<td>");
client.print(s1);
client.println("</td>");
client.println("<td>");
client.print("<a href=\'/gpio/3\'><button>ON</button></a>");
client.print("<a href=\'/gpio/2\'><button>OFF</button></a>");
client.println("</td>");
client.println("</tr>");
client.println("<tr>");
client.println("<td>Plot Timer 2</td>");
client.println("<td>");
client.print(s1);
client.println("</td>");
client.println("<td>");
client.print("<a href=\'/gpio/3\'><button>ON</button></a>");
client.print("<a href=\'/gpio/2\'><button>OFF</button></a>");
client.println("</td>");
client.println("</tr>");
client.println("<tr>");
client.println("<td>Plot Timer 3</td>");
client.println("<td>");
client.print(s1);
client.println("</td>");
client.println("<td>");
client.print("<a href=\'/gpio/3\'><button>ON</button></a>");
client.print("<a href=\'/gpio/2\'><button>OFF</button></a>");
client.println("</td>");
client.println("</tr>");
client.println("</table>");
//HTML TABLE ENDS
client.print("<br>");
client.print("<br>");
client.print("<a href=\'/home\'><button>Refresh</button></a>");
client.print("<br>");
client.print("<br>");
client.print("Server time: ");
client.println(timeClient.getFormattedTime());
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
////////////////////////////////////////////////////////////////////////