赞
踩
ESP8266 01S 继电器控制 智能灯实现(使用小度音响)
最近研究esp8266,发现可以通过wifi连接物联网云平台实现远程控制,同时也可以加入小度语音DIY简单的智能家居。
本人也是刚接触esp8266纯新手有不对的希望指正。
实物连接方法 :220V接入LED灯,同时并联连220V转5V模块;5V出来给继电器供电,同时并联一个5V转3.3模块给ESP8266 01S供电;ESP8266 01S的URXD引脚也就是GPIO3接继电器控制端,继电器选择常闭端接线高电平触发,控制LED灯亮灭。
补充说明:①市面上的LED灯大多是直接用简单的整流桥芯片搭成,然后串联高亮LED灯,所以继电器选择常闭串入LED灯中,既可以用远程控制,也可以直接用家中的开关控制。
②这里ESP8266供电需要CH_PD和VCC同时加3.3V才能正常启动
代码如下:
#include <ESP8266WiFi.h> #include <aJSON.h> //贝壳物联的库,官网应该有找不到留言 #include <DNSServer.h>//web配网 #include <ESP8266WebServer.h> #include <CustomWiFiManager.h> //============= 此处必须修该============ String DEVICEID="12345"; // 你的设备编号 贝壳物联添加设备后获得 == String APIKEY = "xxxxxxx"; // 设备密码 贝壳物联添加设备后获得 == // web配网页面自定义我的图标请随便使用一个图片转base64工具转换https://tool.css-js.com/base64.html, 64*64 const char Icon[] PROGMEM = "data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gOTAK/9sAQwADAgIDAgIDAwMDBAMDBAUIBQUEBAUKBwcGCAwKDAwLCgsLDQ4SEA0OEQ4LCxAWEBETFBUVFQwPFxgWFBgSFBUU/9sAQwEDBAQFBAUJBQUJFA0LDRQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU/8AAEQgAtAEYAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBh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YsCFSevP1p4gX1/WqolI7frTvtBIxg0cwWLSwIe/60v2ZMZ3D86qfaWH8JxS/a3/ALnT1FPmJ5S2bdezUfZh/ezVU3bt/CR7Un2p/Q0cwWLP2dfXFBgXHWoPtDDtx9KPtLen6Ucw7E3kgnrTTAOu761Gblz3NNM7n1P40uYLEhiUdTz1pnlr6k0nnP1IOPc0nmsSflH50rhYQxjPemlAPX86Uux/hH50hkbuo/Oi4WGlAe1MKD0p+9sdB+dNMjjsv50hWGlPY00oD60plf0WmGds8AZpXK5RGiX3qNo1FOaZz6CozI/c4pDEYD6mmYHvTyzZ600knvmgYhFFBziigBq04etNAyKcBQNij2pwzTcU5QKCRwB7VIqEimADvzUgVeKAHhDjrxT9p9aasa0ojBxwPzoAcFIOc07GTTREKcE9DQAu32pwHHFJt6HJoxnvQA8Lmk6DihQfrSnqOaAFIz0FKq496apLd6ecgfeP507gJgnPGKMYPApCCepo+vFFwHY9qCvemHrxz70nbrii4D8CggA9800gmjbgdaYwP403GKcKZjJJqRBimnFL396TaCKAEIx3phIx1p7HFMJxntQAwnJIzTCOevPtTyTzTGODUoCNvqabge+acTTSetUA0mjv+FHXNISSKBin60UzPbpRQOwHqKWiigYueaU8c0UUEkq05aKKBEw6U4dM0UUALuOKd/DRRQJCjpSbiDRRQUOzS+lFFAhc4FKO1FFACHvSkYNFFACjpTaKKBi9hRjIzRRQIRuppo4oooARiQKZuNFFADWYkVCWJPXvRRQAwucmgnNFFADGprdKKKABaQ9aKKBjW60UUUFH/9k="; //======================================= unsigned long lastCheckInTime = 0; //记录上次报到时间 const unsigned long postingInterval = 30000; // 每隔30秒向服务器报到一次 const char* host = "www.bigiot.net";//贝壳物联网址 const int httpPort = 8181;//请求 void webconnect() { Web配网 WiFiManager wifiManager; //实例化WiFiManager wifiManager.setDebugOutput(false); //关闭Debug //wifiManager.setConnectTimeout(10); //设置超时 wifiManager.setHeadImgBase64(FPSTR(Icon)); //设置图标 wifiManager.setPageTitle("欢迎来到xx的WiFi配置页"); //设置页标题,自己改 if (!wifiManager.autoConnect("esp8266-Display")) { //AP模式wifi名字自己改 Serial.println("连接失败并超时"); ESP.restart(); //重新设置并再试一次 delay(1000); } yield(); } void setup() { //系统基本设置,我的继电器接常闭端,高电平有效,所以初始下拉 pinMode(3, OUTPUT);digitalWrite(3, LOW);//这里用的是01S的GPIO3引脚(URXD),GPIO0下拉会进入下载模式,GPIO2内部上拉,这里是个坑,其他的都不好用 webconnect(); delay(100); } WiFiClient client; void loop() { while (WiFi.status() != WL_CONNECTED) { delay(1000);//网络是否连接 } // Use WiFiClient class to create TCP connections if (!client.connected()) { if (!client.connect(host, httpPort)) { //连接贝壳物联的网址 delay(5000); return; } } if(millis()-lastCheckInTime>postingInterval||lastCheckInTime==0) checkIn(); // Read all the lines of the reply from server and print them to Serial if (client.available()) { String inputString = client.readStringUntil('\n'); inputString.trim(); int len = inputString.length()+1; if(inputString.startsWith("{") && inputString.endsWith("}")) { char jsonString[len]; inputString.toCharArray(jsonString,len); aJsonObject *msg = aJson.parse(jsonString); processMessage(msg); aJson.deleteItem(msg); } } } void processMessage(aJsonObject *msg)//解析ajson { aJsonObject* method = aJson.getObjectItem(msg, "M"); aJsonObject* content = aJson.getObjectItem(msg, "C"); aJsonObject* client_id = aJson.getObjectItem(msg, "ID"); if (!method) { return; } String M = method->valuestring; if(M == "say") { String C = content->valuestring; String F_C_ID = client_id->valuestring; if(C == "play" ) { digitalWrite(3, LOW);//打开灯 sayToClient( F_C_ID,"LED1 open!"); } else if(C == "stop") { digitalWrite(3, HIGH);//关闭灯 sayToClient( F_C_ID,"LED1 close!"); } } } void checkIn() { String msg = "{\"M\":\"checkin\",\"ID\":\"" + DEVICEID + "\",\"K\":\"" + APIKEY + "\"}\n"; client.print(msg); lastCheckInTime = millis(); } void sayToClient(String client_id, String content) { String msg = "{\"M\":\"say\",\"ID\":\"" + client_id + "\",\"C\":\"" + content + "\"}\n"; client.print(msg); lastCheckInTime = millis(); }
esp8266会将wifi记录保存到flash里 如果没清除flash就会自动连接上一次连接的wifi ,所以想下新程序不受之前程序影响就看我的另一篇教程吧。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。