3810| 1
|
[项目] 远程查看家里门上的震动传感值 |
快过年了。如果出去走亲戚,难免要离家几天。怎么知道家里如没有人进去过呢。这里提供一个低成本的IOT方案。 项目用到硬件如下: NodeMCU 震动传感SW-420 NodeMCU脚位图 项目连线图 震动传感的D0连NodeMCU的A0 下面进行云平台的设置 首先进入thingspeak.com,选择它是因为它免费 注册后,建立通道 这里建立的是knock level通道 通过敲击震动传感,传感值会实时上传到云平台,并以图表方式展示 写api码是写入代码里的 读api码是在第三方手机app里调用的。 然后是撸代码 程序运行后的COM口数值 代码当中的 String apiKey = ""; const char *ssid = ""; const char *pass = ""; 必须填你的真实数据,分别是api码,wifi名,wifi密码 [mw_shl_code=applescript,true]//---------gada888撸码-------2019-12---- #include <ESP8266WiFi.h> String apiKey = ""; const char *ssid = ""; const char *pass = ""; const char* server = "api.thingspeak.com"; WiFiClient client; //--------------------SETUP--------------- void setup(){ Serial.begin(115200); delay(10); Serial.println("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); } //--------------LOOP----------------- void loop(){ float h = analogRead(A0); if (client.connect(server, 80)) { String postStr = apiKey; postStr += "&field1="; postStr += String(h/1023*100); postStr += "r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); Serial.print("Noise Level: "); Serial.println(h/1023*100); Serial.println("Data Send to Thingspeak"); } client.stop(); Serial.println("Waiting..."); // thingspeak needs minimum 15 sec delay between updates. delay(1500); } [/mw_shl_code] 查看IOT数值的话,可以从PC端登录thingspeak.com来查,也可以用app来查。时间关系,这次app还在调试中。 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed