Hi,
I am trying to pilot relay and get temperature with ESP8266. I power it with voltage regulator. After few minutes/hours (no rules), no response by web interface.
My code :
#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define RELAYPINA 4
#define RELAYPINB 5
// NETWORK: Static IP details...
IPAddress ip(192, 168, 1, 85);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
const char* ssid = "aaa";
const char* password = "aaa";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
dht.begin();
// prepare RELAYPINA
pinMode(RELAYPINA, OUTPUT);
digitalWrite(RELAYPINA, 0);
pinMode(RELAYPINB, OUTPUT);
digitalWrite(RELAYPINB, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip, gateway, subnet);
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());
}
int val_relay_a=0;
int val_relay_b=0;
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
// 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();
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";
// Match the request
if (req.indexOf("/gpioA/0") != -1) val_relay_a = 0;
else if (req.indexOf("/gpioA/1") != -1) val_relay_a = 1;
else if (req.indexOf("/gpioB/0") != -1) val_relay_b = 0;
else if (req.indexOf("/gpioB/1") != -1) val_relay_b = 1;
else {
//s += req;
Serial.println("invalid request");
}
// Set RELAYPINA according to the request
digitalWrite(RELAYPINA, val_relay_a);
digitalWrite(RELAYPINB, val_relay_b);
// Prepare the response
s += "<p>\r\nGPIO A is now ";
s += (val_relay_a)?"high":"low";
s += "</p><p><a href='/gpioA/0'>go low</a> - <a href='/gpioA/1'>go high</a></p>";
s += "<p>\r\nGPIO B is now ";
s += (val_relay_b)?"high":"low";
s += "</p><p><a href='/gpioB/0'>go low</a> - <a href='/gpioB/1'>go high</a></p>";
s += "<p>Temperature : ";
s += t;
s += "<p>Humidity : ";
s += h;
s += "</p></html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}