完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
我很高兴终于能够用 esp8266 控制我的 wemo 开关。下面是我使用的代码。这只是一个简单的示例,需要添加一个按钮并过滤来自开关的响应以检查 wemo 状态。ar
希望听到你们的一些反馈和改进。 代码:全选-------------------------------------------------------------------- /* * This sketch sends data via HTTP POST requests to a wemo device. * * */ #include const char* ssid = "Your_Wifi"; const char* password = "Your_password"; const char* host = "192.168.0.179"; const int port = 49153; int wemo_status = 0; String wemo_on="1"; String wemo_off="0"; void setup() { Serial.begin(115200); 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()); } void loop() { if (wemo_status == 0) { wemo_status = 1; Serial.println("Turning WeMo On..."); } else { wemo_status = 0; Serial.println("Turning WeMo Off..."); } Serial.println(); wemo_control(wemo_status); delay(5000); } void wemo_control(int cmd) { Serial.print("Connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; if (!client.connect(host, port)) { Serial.println("Connection failed"); return; } // This will send the request to the server client.println("POST /upnp/control/basicevent1 HTTP/1.1"); client.println("Host: " + String(host) + ":" + String(port)); client.println("User-Agent: ESP8266/1.0"); client.println("Connection: close"); client.println("Content-type: text/xml; charset=\"utf-8\""); client.print("Content-Length: "); if (cmd == 1) { client.println(wemo_on.length()); // both wemo_on and wemo_off are the same length, just in case it changes in the future } else { client.println(wemo_off.length()); } client.println("SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\""); client.println(); if (cmd == 1) { client.println(wemo_on); } else { client.println(wemo_off); } delay(10); // Read all the lines of the reply from server and print them to Serial while(client.available()){ String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("Closing connection"); } -------------------------------------------------------------------- I used the following sites to help me reach the outcome. |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
945 浏览 0 评论
1672 浏览 0 评论
请问一下我想用ESP8685直接替换ESP8266而不用写程序,可以让ESP8685直接通过之前ESP8266的外挂的flash运行程序吗
1330 浏览 1 评论
1233 浏览 1 评论
5021 浏览 2 评论
为blufi_device设置自定义名称,但是无法修改,为什么?
1253浏览 4评论
请问ESP32-S2-WROOM怎么获得ESP32-S2外接FLASH的唯一序列号?
927浏览 3评论
2338浏览 3评论
ESP-IDF的VScode插件的build按钮点击会报错的原因?
2526浏览 3评论
ESP-Jumpstart例程中第5个工程:5_cloud连接报错是哪里的问题?
1052浏览 2评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-2 05:38 , Processed in 0.679024 second(s), Total 69, Slave 52 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
3456
