赞
踩
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
通过Diot IOT上发送指令来远程的反向控制ESPDuio开发板以及板载的各种传感器
传感器:双继电器、led灯、有源蜂鸣器
材料:ESPDuino、继电器、led灯、有源蜂鸣器、杜邦线
代码如下(示例):
代码如下(示例):
#include <ESP8266WiFi.h> #include <Ticker.h> #define Relays1 4 #define Relays2 5 #define led 16 #define beep 13 #define u8 unsigned char Ticker timer; const char* ssid = "nfc"; const char* password = "123456789"; const char* host = "iot.doit.am";//物联网平台 const int httpPort = 8810; const char* streamId = "nfc"; const char* privateKey = "ca78c78417ae3d1c63280f9aa1c84f3c"; char str[512]; WiFiClient client;// 使用WiFi客户端类创建TCP连接 //反向控制: unsigned long MS_TIMER = 0; unsigned long lastMSTimer = 0; String comdata = ""; char flag = false; void sensor_init() { pinMode(Relays1, OUTPUT); digitalWrite(Relays1, HIGH); pinMode(Relays2, OUTPUT); digitalWrite(Relays2, HIGH); pinMode(led, OUTPUT); digitalWrite(led, LOW); pinMode(beep, OUTPUT); digitalWrite(beep, LOW); } void setup() { Serial.begin(115200); MS_TIMER = millis(); sensor_init(); delay(10); WiFi.disconnect();/ // 首先,我们连接到WiFi网络 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()); delay(50); Serial.print("connecting to "); Serial.println(host); // 使用WiFi客户端类创建TCP连接 if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } } unsigned long lastTick = 0; void loop() { if (millis() - lastTick > 1000) { lastTick = millis(); static bool first_flag = true; if (first_flag) { first_flag = false; sprintf(str, "cmd=subscribe&topic=lin\r\n"); client.print(str); return; } } if (client.available()) { //读并处理 // R读取服务器的应答的所有行,并把它们打印到串口 String recDataStr = client.readStringUntil('\n'); Serial.println(recDataStr); if (recDataStr.compareTo("cmd=publish&device_name=arduino1&device_cmd=lbopen\r") == 0) { digitalWrite(led, HIGH); digitalWrite(beep, HIGH);//蜂鸣器响 Serial.println("Sound and light ON"); } else if (recDataStr.compareTo("cmd=publish&device_name=arduino1&device_cmd=lbclose\r") == 0) { digitalWrite(led, LOW); digitalWrite(beep, LOW); Serial.println("Sound and light OFF"); } else if (recDataStr.compareTo("cmd=publish&device_name=arduino2&device_cmd=j1\r") == 0) { digitalWrite(Relays1, LOW); digitalWrite(beep, HIGH);//蜂鸣器响 Serial.println("Relays1 open"); delay(30); digitalWrite(Relays1, HIGH); digitalWrite(beep, LOW); } else if (recDataStr.compareTo("cmd=publish&device_name=arduino2&device_cmd=j2\r") == 0) { digitalWrite(Relays2, LOW); digitalWrite(beep, HIGH);//蜂鸣器响 Serial.println("Relays2 open"); delay(30); digitalWrite(Relays2, HIGH); digitalWrite(beep, LOW); } } }
利用云平台来控制单片机实现了对单片机的远程控制
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。