惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - cndavy

如何在WINDOW环境下搭建ActivateMQ和zookeeper集群环境 树莓派 安装 docker CSV 文件处理成 String[] ,网银下载的文本中使用逗号分割, 使用双引号标记字段. 使用String split 会出现把引号中的逗号识别的情况 例如 " ,,,, " , "aaa" 切换网卡的脚本 apache xampp 目录防止解析php FTPS 客户端 demo, WordPress搬家教程:换空间与换域名 sympy 的 符号计算的例子 Conda 简单使用 spoolsv.exe 无法启动 太阳高度角和方位角的计算 树莓派 3 alsa 声卡驱动 PHP 7 Xdebug 深深的坑 java 线性规划 和lingo 比较 Cannot find or open the PDB file问题的解决 Python Microsoft Visual C++ Compiler Package for Python 2.7 Node debug angular 调试 js (分 karms protractor / test e2e unit ) hbase scan 的例子
Esp8266 例子
cndavy · 2018-03-26 · via 博客园 - cndavy
141 #include <ESP8266WiFi.h>          //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
142 #include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
143 #include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
144 #include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
145 #include <PubSubClient.h>         //MQTT
146 #include <ESP8266mDNS.h>
147 #include <ESP8266HTTPUpdateServer.h>
148  
149 ////**********START CUSTOM PARAMS******************//
150  
151 //Define parameters for the http firmware update
152 const char* host = "GarageESP";
153 const char* update_path = "/WebFirmwareUpgrade";
154 const char* update_username = "admin";
155 const char* update_password = "YourPassWordHere";
156  
157 //Define the pins
158 #define RELAY_PIN 5
159 #define DOOR_PIN 4
160  
161 //Define MQTT Params. If you don't need to 
162 #define mqtt_server "MQTT Broker IP Address"
163 #define door_topic "garage/door"
164 #define button_topic "garage/button"
165 const char* mqtt_user = "mqtt_user"; 
166 const char* mqtt_pass = "mqtt_pass";
167  
168 //************END CUSTOM PARAMS********************//
169 //This can be used to output the date the code was compiled
170 const char compile_date[] = __DATE__ " " __TIME__;
171  
172 //Setup the web server for http OTA updates. 
173 ESP8266WebServer httpServer(80);
174 ESP8266HTTPUpdateServer httpUpdater;
175  
176 WiFiClient espClient;
177  
178 //Initialize MQTT
179 PubSubClient client(espClient);
180  
181 //Setup Variables
182 String switch1;
183 String strTopic;
184 String strPayload;
185 char* door_state = "UNDEFINED";
186 char* last_state = "";
187  
188 //Wifi Manager will try to connect to the saved AP. If that fails, it will start up as an AP
189 //which you can connect to and setup the wifi
190 WiFiManager wifiManager;
191 long lastMsg = 0;
192  
193 void setup() {
194   //Set Relay(output) and Door(input) pins
195   pinMode(RELAY_PIN, OUTPUT);
196   pinMode(RELAY_PIN, LOW);
197   pinMode(DOOR_PIN, INPUT);
198  
199   Serial.begin(115200);
200  
201   //Set the wifi config portal to only show for 3 minutes, then continue.
202   wifiManager.setConfigPortalTimeout(180);
203   wifiManager.autoConnect(host);
204  
205   //sets up the mqtt server, and sets callback() as the function that gets called
206   //when a subscribed topic has data
207   client.setServer(mqtt_server, 1883);
208   client.setCallback(callback); //callback is the function that gets called for a topic sub
209  
210   //setup http firmware update page.
211   MDNS.begin(host);
212   httpUpdater.setup(&httpServer, update_path, update_username, update_password);
213   httpServer.begin();
214   MDNS.addService("http", "tcp", 80);
215   Serial.printf("HTTPUpdateServer ready! Open http://%s.local%s in your browser and login with username '%s' and your password\n", host, update_path, update_username);
216 }
217  
218 void loop() {
219   //If MQTT client can't connect to broker, then reconnect
220   if (!client.connected()) {
221     reconnect();
222   }
223   checkDoorState();
224   client.loop(); //the mqtt function that processes MQTT messages
225   httpServer.handleClient(); //handles requests for the firmware update page
226 }
227  
228 void callback(char* topic, byte* payload, unsigned int length) {
229   //if the 'garage/button' topic has a payload "OPEN", then 'click' the relay
230   payload[length] = '\0';
231   strTopic = String((char*)topic);
232   if (strTopic == button_topic)
233   {
234     switch1 = String((char*)payload);
235     if (switch1 == "OPEN")
236     {
237       //'click' the relay
238       Serial.println("ON");
239       pinMode(RELAY_PIN, HIGH);
240       delay(600);
241       pinMode(RELAY_PIN, LOW);
242     }
243   }
244 }
245  
246 void checkDoorState() {
247   //Checks if the door state has changed, and MQTT pub the change
248   last_state = door_state; //get previous state of door
249   if (digitalRead(DOOR_PIN) == 0) // get new state of door
250     door_state = "OPENED";
251   else if (digitalRead(DOOR_PIN) == 1)
252     door_state = "CLOSED"; 
253  
254   if (last_state != door_state) { // if the state has changed then publish the change
255     client.publish(door_topic, door_state);
256     Serial.println(door_state);
257   }
258   //pub every minute, regardless of a change.
259   long now = millis();
260   if (now - lastMsg > 60000) {
261     lastMsg = now;
262     client.publish(door_topic, door_state);
263   }
264 }
265  
266 void reconnect() {
267   //Reconnect to Wifi and to MQTT. If Wifi is already connected, then autoconnect doesn't do anything.
268   wifiManager.autoConnect(host);
269   Serial.print("Attempting MQTT connection...");
270   if (client.connect(host, mqtt_user, mqtt_pass)) {
271     Serial.println("connected");
272     client.subscribe("garage/#");
273   } else {
274     Serial.print("failed, rc=");
275     Serial.print(client.state());
276     Serial.println(" try again in 5 seconds");
277     // Wait 5 seconds before retrying
278     delay(5000);
279   }
280 }