我刚刚开始使用 ESP8266 的第一个项目,也很高兴知道我对这类项目没有太多的
电子知识。我尝试做的是以下内容。我想将引脚 D1 和接地引脚连接到墙壁开关(通常用于打开或关闭灯)。如果墙壁开关的状态发生变化,我想改变一个 philips hue 灯的状态(意思是:如果灯熄灭,那么如果按下墙壁开关我想打开它,反之亦然)。
ESP8266的代码是:
代码:
全选#include
#include
// Wifi Set
tings
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();
}
}
当我测试此设置并将引脚 D1 接地时,D1 的状态似乎不断变为高/低(而电线仍接地)。我不知道为什么会这样。
任何地方都可以帮助我并告诉我我做错了什么吗?我不应该将 D1 连接到地面,而是以其他方式连接吗?还是我的编码错了?