完善资料让更多小伙伴认识你,还能领取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. |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
1002 浏览 1 评论
554浏览 6评论
463浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
447浏览 5评论
448浏览 4评论
422浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-30 08:49 , Processed in 0.742966 second(s), Total 45, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号