赞
踩
本文介绍遥控器的制作
已经在阿里云上创建了该设备(Mycontroller)
根据阿里云这里的信息编写遥控器上的代码。
由于我只有一片esp32,因此遥控器用esp8266制作。
遥控器由一片esp8266,五个轻触按键开关和5V电源组成。
遥控器代码:
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <ArduinoJson.h>
- //#include "aliyun_mqtt.h"
-
- #define K1 4
- #define K2 12
- #define K3 13
- #define K4 14
- #define K5 5
- //#define K6 3 //D9
- #define WIFI_SSID "$$$$$$$$$"
- #define WIFI_PASSWD "$$$$$$$"
-
- #define PRODUCT_KEY "a1cU******"
- #define DEVICE_NAME "Mycontroller"
- #define DEVICE_SECRET "************************"
-
- #define REGION_ID "cn-shanghai"
- String Identifier = "my_command";
-
-
- /* 线上环境域名和端口号,不需要改 */
- #define MQTT_SERVER PRODUCT_KEY ".iot-as-mqtt." REGION_ID ".aliyuncs.com"
- #define MQTT_PORT 1883
- #define MQTT_USRNAME DEVICE_NAME "&" PRODUCT_KEY
-
- #define CLIENT_ID "esp8266|securemode=3,timestamp=1582210768491,signmethod=hmacsha1|"
- // 用算法工具进行加密生成MQTT_PASSWD//
- #define MQTT_PASSWD "¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥"
-
- #define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
- #define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
-
- unsigned long lastMs = 0;
- WiFiClient espClient;
- PubSubClient client(espClient);
-
- void callback(char *topic, byte *payload, unsigned int length)
- {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- payload[length] = '\0';
- Serial.println((char *)payload);
-
- }
-
- void wifiInit()
- {
- WiFi.mode(WIFI_STA);
- WiFi.begin(WIFI_SSID, WIFI_PASSWD);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(1000);
- Serial.println("WiFi not Connect");
- }
-
- Serial.println("Connected to AP");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.print("espClient [");
-
- client.setServer(MQTT_SERVER, MQTT_PORT); /* 连接WiFi之后,连接MQTT服务器 */
- client.setCallback(callback);
- }
-
- void mqttCheckConnect()
- {
- while (!client.connected())
- {
- Serial.println("Connecting to MQTT Server ...");
- if (client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD))
- {
- Serial.println("MQTT Connected!");
- }
- else
- {
- Serial.print("MQTT Connect err:");
- Serial.println(client.state());
- delay(5000);
- }
- }
- }
-
-
- void mqttIntervalPost_1()
- {
- char param[32];
- char jsonBuf[128];
-
- sprintf(param, "{\"my_command\":%d}",1);
- sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
- Serial.println(jsonBuf);
- boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
- //"/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
- // client.publish(pubTopic,("{\"id\":"CLIENT_ID+",\"params\":{\""+Identifier+"\":0},\"method\":\"thing.event.property.post\"}").c_str());
- //Serial.print("publish:0 失败;1成功");
- //Serial.println(d);
- }
-
- void mqttIntervalPost_2()
- {
- char param[32];
- char jsonBuf[128];
-
- sprintf(param, "{\"my_command\":%d}",2);
- sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
- Serial.println(jsonBuf);
- boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
- }
-
- void mqttIntervalPost_3()
- {
- char param[32];
- char jsonBuf[128];
-
- sprintf(param, "{\"my_command\":%d}",3);
- sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
- Serial.println(jsonBuf);
- boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
- }
-
- void mqttIntervalPost_4()
- {
- char param[32];
- char jsonBuf[128];
-
- sprintf(param, "{\"my_command\":%d}",4);
- sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
- Serial.println(jsonBuf);
- boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
- }
-
- void mqttIntervalPost_0()
- {
- char param[32];
- char jsonBuf[128];
-
- sprintf(param, "{\"my_command\":%d}",0);
- sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
- Serial.println(jsonBuf);
- boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
- }
-
- void mqttIntervalPost_5()
- {
- char param[32];
- char jsonBuf[128];
-
- sprintf(param, "{\"my_command\":%d}",5);
- sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
- Serial.println(jsonBuf);
- boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
- }
- void setup()
- {
- pinMode(K1, INPUT_PULLUP);
- pinMode(K2, INPUT_PULLUP);
- pinMode(K3, INPUT_PULLUP);
- pinMode(K4, INPUT_PULLUP);
- pinMode(K5, INPUT_PULLUP);
- //pinMode(K6, INPUT);
- /* initialize serial for debugging */
- Serial.begin(115200);
- Serial.println("Demo Start");
-
- wifiInit();
- }
-
- unsigned long starttime;
- unsigned long stoptime;
- unsigned long looptime;
-
- int k5_flag=0;
- // the loop function runs over and over again forever
- void loop()
- {
- int k1=digitalRead(K1);
- int k2=digitalRead(K2);
- int k3=digitalRead(K3);
- int k4=digitalRead(K4);
- int k5=digitalRead(K5);
-
- if (k1 == LOW) //turn left
- {
- k5_flag=0;
- starttime = millis();
- delay(200);
- if(k1 == LOW)
- {
- mqttCheckConnect();
- mqttIntervalPost_1();
- }
- }
-
- if (k2 == LOW) //turn right
- {
- k5_flag=0;
- starttime = millis();
- delay(200);
- if(k2 == LOW){
- mqttCheckConnect();
- mqttIntervalPost_2();}
- }
-
- if (k3 == LOW) //"backward"
- {
- k5_flag=1;
- starttime = millis();
- delay(200);
- if(k3 == LOW){
- mqttCheckConnect();
- mqttIntervalPost_3();}
- }
-
- if (k4 == LOW){ //"forward"
- k5_flag=0;
- starttime = millis();
- delay(200);
- if(k4== LOW){
- mqttCheckConnect();
- mqttIntervalPost_4();}
- }
-
- if (k5 == LOW){ //stop car
- k5_flag=1;
- starttime = millis();
- delay(200);
- if(k5 == LOW){
- mqttCheckConnect();
- mqttIntervalPost_0();}
- }
-
- stoptime = millis();
- looptime = stoptime - starttime;
- //Serial.println(looptime);
-
- if(looptime>2000 && k5_flag==0)//when stop command has sent,the command auto_drvie code(5) will not send.
- {
- mqttCheckConnect();
- mqttIntervalPost_5();
- starttime = millis();
- k5_flag=1;//don't repeat send same command code 5.
- }
- client.loop();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。