当前位置:   article > 正文

esp8266+arduino IDE+阿里云IOT+萤石云=远程视频遥控车(三)_esp8266遥控车代码

esp8266遥控车代码

本文介绍遥控器的制作

已经在阿里云上创建了该设备(Mycontroller)

根据阿里云这里的信息编写遥控器上的代码。

由于我只有一片esp32,因此遥控器用esp8266制作。

遥控器由一片esp8266,五个轻触按键开关和5V电源组成。

遥控器代码:

  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <ArduinoJson.h>
  4. //#include "aliyun_mqtt.h"
  5. #define K1 4
  6. #define K2 12
  7. #define K3 13
  8. #define K4 14
  9. #define K5 5
  10. //#define K6 3 //D9
  11. #define WIFI_SSID "$$$$$$$$$"
  12. #define WIFI_PASSWD "$$$$$$$"
  13. #define PRODUCT_KEY "a1cU******"
  14. #define DEVICE_NAME "Mycontroller"
  15. #define DEVICE_SECRET "************************"
  16. #define REGION_ID "cn-shanghai"
  17. String Identifier = "my_command";
  18. /* 线上环境域名和端口号,不需要改 */
  19. #define MQTT_SERVER PRODUCT_KEY ".iot-as-mqtt." REGION_ID ".aliyuncs.com"
  20. #define MQTT_PORT 1883
  21. #define MQTT_USRNAME DEVICE_NAME "&" PRODUCT_KEY
  22. #define CLIENT_ID "esp8266|securemode=3,timestamp=1582210768491,signmethod=hmacsha1|"
  23. // 用算法工具进行加密生成MQTT_PASSWD//
  24. #define MQTT_PASSWD "¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥"
  25. #define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
  26. #define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
  27. unsigned long lastMs = 0;
  28. WiFiClient espClient;
  29. PubSubClient client(espClient);
  30. void callback(char *topic, byte *payload, unsigned int length)
  31. {
  32. Serial.print("Message arrived [");
  33. Serial.print(topic);
  34. Serial.print("] ");
  35. payload[length] = '\0';
  36. Serial.println((char *)payload);
  37. }
  38. void wifiInit()
  39. {
  40. WiFi.mode(WIFI_STA);
  41. WiFi.begin(WIFI_SSID, WIFI_PASSWD);
  42. while (WiFi.status() != WL_CONNECTED)
  43. {
  44. delay(1000);
  45. Serial.println("WiFi not Connect");
  46. }
  47. Serial.println("Connected to AP");
  48. Serial.println("IP address: ");
  49. Serial.println(WiFi.localIP());
  50. Serial.print("espClient [");
  51. client.setServer(MQTT_SERVER, MQTT_PORT); /* 连接WiFi之后,连接MQTT服务器 */
  52. client.setCallback(callback);
  53. }
  54. void mqttCheckConnect()
  55. {
  56. while (!client.connected())
  57. {
  58. Serial.println("Connecting to MQTT Server ...");
  59. if (client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD))
  60. {
  61. Serial.println("MQTT Connected!");
  62. }
  63. else
  64. {
  65. Serial.print("MQTT Connect err:");
  66. Serial.println(client.state());
  67. delay(5000);
  68. }
  69. }
  70. }
  71. void mqttIntervalPost_1()
  72. {
  73. char param[32];
  74. char jsonBuf[128];
  75. sprintf(param, "{\"my_command\":%d}",1);
  76. sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
  77. Serial.println(jsonBuf);
  78. boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
  79. //"/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
  80. // client.publish(pubTopic,("{\"id\":"CLIENT_ID+",\"params\":{\""+Identifier+"\":0},\"method\":\"thing.event.property.post\"}").c_str());
  81. //Serial.print("publish:0 失败;1成功");
  82. //Serial.println(d);
  83. }
  84. void mqttIntervalPost_2()
  85. {
  86. char param[32];
  87. char jsonBuf[128];
  88. sprintf(param, "{\"my_command\":%d}",2);
  89. sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
  90. Serial.println(jsonBuf);
  91. boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
  92. }
  93. void mqttIntervalPost_3()
  94. {
  95. char param[32];
  96. char jsonBuf[128];
  97. sprintf(param, "{\"my_command\":%d}",3);
  98. sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
  99. Serial.println(jsonBuf);
  100. boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
  101. }
  102. void mqttIntervalPost_4()
  103. {
  104. char param[32];
  105. char jsonBuf[128];
  106. sprintf(param, "{\"my_command\":%d}",4);
  107. sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
  108. Serial.println(jsonBuf);
  109. boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
  110. }
  111. void mqttIntervalPost_0()
  112. {
  113. char param[32];
  114. char jsonBuf[128];
  115. sprintf(param, "{\"my_command\":%d}",0);
  116. sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
  117. Serial.println(jsonBuf);
  118. boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
  119. }
  120. void mqttIntervalPost_5()
  121. {
  122. char param[32];
  123. char jsonBuf[128];
  124. sprintf(param, "{\"my_command\":%d}",5);
  125. sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
  126. Serial.println(jsonBuf);
  127. boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
  128. }
  129. void setup()
  130. {
  131. pinMode(K1, INPUT_PULLUP);
  132. pinMode(K2, INPUT_PULLUP);
  133. pinMode(K3, INPUT_PULLUP);
  134. pinMode(K4, INPUT_PULLUP);
  135. pinMode(K5, INPUT_PULLUP);
  136. //pinMode(K6, INPUT);
  137. /* initialize serial for debugging */
  138. Serial.begin(115200);
  139. Serial.println("Demo Start");
  140. wifiInit();
  141. }
  142. unsigned long starttime;
  143. unsigned long stoptime;
  144. unsigned long looptime;
  145. int k5_flag=0;
  146. // the loop function runs over and over again forever
  147. void loop()
  148. {
  149. int k1=digitalRead(K1);
  150. int k2=digitalRead(K2);
  151. int k3=digitalRead(K3);
  152. int k4=digitalRead(K4);
  153. int k5=digitalRead(K5);
  154. if (k1 == LOW) //turn left
  155. {
  156. k5_flag=0;
  157. starttime = millis();
  158. delay(200);
  159. if(k1 == LOW)
  160. {
  161. mqttCheckConnect();
  162. mqttIntervalPost_1();
  163. }
  164. }
  165. if (k2 == LOW) //turn right
  166. {
  167. k5_flag=0;
  168. starttime = millis();
  169. delay(200);
  170. if(k2 == LOW){
  171. mqttCheckConnect();
  172. mqttIntervalPost_2();}
  173. }
  174. if (k3 == LOW) //"backward"
  175. {
  176. k5_flag=1;
  177. starttime = millis();
  178. delay(200);
  179. if(k3 == LOW){
  180. mqttCheckConnect();
  181. mqttIntervalPost_3();}
  182. }
  183. if (k4 == LOW){ //"forward"
  184. k5_flag=0;
  185. starttime = millis();
  186. delay(200);
  187. if(k4== LOW){
  188. mqttCheckConnect();
  189. mqttIntervalPost_4();}
  190. }
  191. if (k5 == LOW){ //stop car
  192. k5_flag=1;
  193. starttime = millis();
  194. delay(200);
  195. if(k5 == LOW){
  196. mqttCheckConnect();
  197. mqttIntervalPost_0();}
  198. }
  199. stoptime = millis();
  200. looptime = stoptime - starttime;
  201. //Serial.println(looptime);
  202. if(looptime>2000 && k5_flag==0)//when stop command has sent,the command auto_drvie code(5) will not send.
  203. {
  204. mqttCheckConnect();
  205. mqttIntervalPost_5();
  206. starttime = millis();
  207. k5_flag=1;//don't repeat send same command code 5.
  208. }
  209. client.loop();
  210. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/417044
推荐阅读
相关标签
  

闽ICP备14008679号