Can you post your sketch may be it'll be helpful ?
Can you post your sketch may be it'll be helpful ?
Explore... Chat... Share...
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const int PIN_INPUT = 2;
const int PIN_OUTPUT = 0;
const char* ssid = "my_ssid";
const char* password = "my_pass";
int input_prev;
int input_curr;
int output;
ESP8266WebServer server(80);
void do_on() {
Serial.println("OUTPUT: on");
digitalWrite(PIN_OUTPUT, HIGH);
output = HIGH;
}
void do_off() {
Serial.println("OUTPUT: off");
digitalWrite(PIN_OUTPUT, LOW);
output = LOW;
}
void do_get() {
if (output)
server.send(200, "text/plain", "ON");
else
server.send(200, "text/plain", "OFF");
}
void handle_root() {
Serial.println("HTTP : root");
server.send(200, "text/plain", "Hello from the ESP8266!");
}
void handle_on() {
Serial.println("HTTP : on");
do_on();
do_get();
}
void handle_off() {
Serial.println("HTTP : off");
do_off();
do_get();
}
void handle_get() {
Serial.println("HTTP : get");
do_get();
}
void setup(void)
{
Serial.begin(115200);
Serial.println("\n\rStarting...");
pinMode(PIN_OUTPUT, OUTPUT);
pinMode(PIN_INPUT, INPUT);
input_prev = digitalRead(PIN_INPUT);
Serial.print("Current status: ");
Serial.println(input_prev);
if (input_prev) {
do_on();
} else {
do_off();
}
server.on("/", handle_root);
server.on("/on", handle_on);
server.on("/off", handle_off);
server.on("/get", handle_get);
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
if(WiFi.status() != WL_CONNECTED) {
Serial.print("Trying to connect");
WiFi.begin(ssid, password);
for (int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++) {
delay(500);
Serial.print(".");
}
Serial.print("\n\r");
if(WiFi.status() == WL_CONNECTED) {
Serial.print("Connected to : ");
Serial.println(ssid);
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
}
}
if(WiFi.status() == WL_CONNECTED) {
server.handleClient();
}
input_curr = digitalRead(PIN_INPUT);
if (input_curr != input_prev) {
if (input_curr) {
Serial.println("INPUT : on");
do_on();
} else {
Serial.println("INPUT : off");
do_off();
}
input_prev = input_curr;
}
delay(250);
}
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]