Hi I'm trying to controll my lights via a WeMos D1 mini with a relay via a http link.
I got that to work but now I want to turn the ligts on if they're off and off if the're on via the same url.
But now i can only turn them off and no longer on.
This is the code:
#include <ESP8266WiFi.h>
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxxxx";
int RelayPin = D1;
WiFiServer server(80);
IPAddress ip(192, 168, 0, 97); // where xx is the desired IP Address
IPAddress gateway(192, 168, 0, 1); // set gateway to match your network
void setup() {
WiFi.mode(WIFI_STA);
Serial.begin(115200);
delay(10);
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, HIGH );
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
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.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
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 request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = HIGH;
if (request.indexOf("/GO") != -1) {
if (RelayPin != LOW)
{
digitalWrite(RelayPin, LOW);
}
else
{
digitalWrite(RelayPin, HIGH);
}
}
}