Hi
i have code for ON/OFF relay depending on humidity, temperature, and command from web.
if i run code without code which measure humidity, it works fine. if there is also code which measure humidity, the serial monitor print humidity, there is also humidity at web page, but if i send few GET commands, esp32 stop work and serial monitor print something like this
difficult to say - could be many things
upload the code (using code tags </> )
upload a schematic showing wiring
upload the complete serial monitor output of a run
#include "DHTStable.h"
#include <OneWireNg.h>
#include <DallasTemperature.h>
DHTStable DHT;
#define ONE_WIRE_BUS 25
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
const char *ssid = "wifirelay";
const char *password = "wifirelay";
WiFiServer server(80);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define DHT11_PIN 26
int hum_sens;
int rele = 27;
int tempPin;
int hum = 70;
int j=0;
int actual =3;
int Teplota;
unsigned long myTime;
String header;
unsigned long previous_time = 0;
unsigned long delaya = 20000; // 20 seconds delay
int lastState;
void setup() {
sensors.begin();
Serial.begin(115200);
pinMode(rele, OUTPUT);
delay(10);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
server.begin();
}
void loop() {
unsigned long current_time = millis(); // number of milliseconds since the upload
// checking for WIFI connection
if ((WiFi.status() != WL_CONNECTED) and ((current_time - previous_time) > delaya)) {
Serial.print(millis());
Serial.println("Reconnecting to WIFI network");
WiFi.disconnect();
WiFi.reconnect();
previous_time = current_time;
}
delay (5);
WiFiClient client = server.available();
header = "";
if ((client) or (lastState != actual)) { //or (lastState != actual)
// If a new client connects,
Serial.println("je Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html ");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /automat/on") >= 0) {
Serial.println("auto on");
} else if (header.indexOf("GET /automat/off") >= 0) {
Serial.println("auto off");
} else if (header.indexOf("GET /relay/on") >= 0) {
actual = 2;
} else if (header.indexOf("GET /relay/off") >= 0) {
actual = 3;
}
// Display the HTML web page
// Web Page Heading
client.println("<body> <div class=\"data\"><h2>");
client.println(hum_sens);
client.println("%</h2> <h2> ");
client.println(tempPin);
client.println("°C</h2></div> ");
client.println("<div class=\"but\">");
if (automatika == 6) {
client.println("<a href=\"/automat/on\" class=\"button-3red\">Manual</a>");
}
if (automatika == 5) {
client.println("<a href=\"/automat/off\" class=\"button-3\">Automat</a>");
}
client.println("<br>");
if (actual == 2) {
client.println("<a href=\"/relay/off\" class=\"button-3\">open</a>");
lastState=2;
}
if (actual == 3) {
client.println("<a href=\"/relay/on\" class=\"button-3red\">close</a>");
lastState=3;
}
client.println("</div>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
// header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
if (j==0 or j >=50){
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
// Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
// Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
// Serial.print("Time out error,\t");
break;
default:
// Serial.print("Unknown error,\t");
break;
}
Serial.println(DHT.getHumidity(), 1);
hum_sens=DHT.getHumidity();
sensors.requestTemperatures();
tempPin = sensors.getTempCByIndex(0);
if (j >=50){
j=0;
}
}
j++;
if (actual == 2) {
Serial.println("relay on");
digitalWrite(rele, HIGH);
}
else if (actual == 3){
digitalWrite(rele, LOW);
actual = 3;
Serial.println("relay off");
}
}