I just started with the first project with ESP8266, also good to know that I don't have much electronic knowledge for those kind of project. What I try to do is the following. I want to connect pin D1 and the Ground pin to a Wall Switch (which is used normally to turn lights on or off). If the status of the wall switch change, I want to change the status of one philips hue light (meaning: if the light is off, then I want to turn it on if the wall switch is pressed and visa versa).
The code of the ESP8266 is:
#include <ESP8266WiFi.h>
#include <SPI.h>
// Wifi Settings
const char* ssid = "xxxxx";
const char* password = "xxxxx";
// Hue Settings
const char hueHubIP[] = "xxxxx"; // Hue Bridge IP
const int hueHubPort = 80;
const char hueUsername[]="xxxxxx";
String light="1";
// Commands
String hue_on="{\"on\":true}";
String hue_off="{\"on\":false}";
boolean state = false; // for monitoring switch state changes
boolean previousState; // ditto
boolean onOffState = false; // To store actual on/off state of light as reported by Hue bridge
int switchPin = 5; // -- control sense pin, mapped to D1 on NodeMCU
WiFiClient client;
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize LED_BUILTIN pin as output
Serial.begin(9600);
pinMode(switchPin, INPUT_PULLUP);
delay(10);
// We start by connecting to a 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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
previousState = digitalRead(switchPin);
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("State Switch:");
Serial.println(previousState);
}
void loop() {
state = digitalRead(switchPin); // for checking for state change
if (state != previousState) // state change
{
previousState == state;
getHue();
delay(500);
if (onOffState == true) // If lights are on, send "Off" command
{
Serial.println("");
Serial.print("Switch lamp off");
String command = "{\"on\": false}";
setHue(command);
digitalWrite(LED_BUILTIN, LOW);
}
else // If lights are off, send "On" command
{
Serial.println("");
Serial.print("Switch lamp on");
String command = "{\"on\": true}";
setHue(command);
digitalWrite(LED_BUILTIN, HIGH);
}
}
}
void setHue(String command)
{
if (client.connect(hueHubIP, hueHubPort))
{
client.print("PUT /api/");
client.print(hueUsername);
client.print("/lights/");
client.print(light);
client.println("/state HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(hueHubIP);
client.print("Content-Length: ");
client.println(command.length());
client.println("Content-Type: text/plain;charset=UTF-8");
client.println(); // Blank line before body
client.println(command);
client.stop();
}
}
void getHue()
{
if (client.connect(hueHubIP, hueHubPort))
{
client.print("GET /api/");
client.print(hueUsername);
client.print("/lights/");
client.print(light);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(hueHubIP);
client.println("Content-type: application/json");
client.println("keep-alive");
client.println();
while (client.connected())
{
if (client.available())
{
client.findUntil("\"on\":", "\0");
onOffState = (client.readStringUntil(',') == "true");
break;
}
}
client.stop();
}
}
When I test this setup, and I connect pin D1 with the Ground, it seems like the status of D1 is changing to high/low constantly (while the wire is still connect to ground). I don't know why this happens.
Can anywhere help me and tell me what I do wrong? Should I not link D1 to Ground, but do this in another way? Or is my coding wrong?
Thanks for the support in advance!
kind regards,
Victor