MQTT服务器4G远程遥控小车代码
接收机代码
#include <ESP8266WiFi.h> #include <PubSubClient.h> #include<Servo.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> // 设置wifi接入信息(根据小车要连接的WiFi信息进行修改) WiFiServer server(80); //MQTT服务器地址:broker.hivemq.com test.ranye-iot.net const char* mqttServer = "101.43.152.71"; String topicString = "lmwmm001";//与esp32代码中的该部分相同 // 如以上MQTT服务器无法正常连接,请前往以下页面寻找解决方案 // http://www.taichi-maker.com/public-mqtt-broker/ Servo servoA; Servo servoB; Servo servoC; //volatile uint16_t values[6] = {90}; int Avalue=90; int Bvalue=90; int Cvalue=90; WiFiClient wifiClient; PubSubClient mqttClient(wifiClient); void setup() { Serial.begin(115200); // 启动串口通讯(波特率为115200,波特率可以理解为串口传输数据的速度) pinMode(05,OUTPUT); pinMode(04,OUTPUT); pinMode(00,OUTPUT); // pinMode(15,OUTPUT); // pinMode(13,OUTPUT); // pinMode(12,OUTPUT); LED(); servoA.attach(05,500,2500); servoA.write(90); servoB.attach(04); servoB.write(90); servoC.attach(00,500,2500); servoC.write(0); // servo[3].attach(13,500,2500); // servo[3].write(0); // // servo[4].attach(12,500,2500); // servo[4].write(0); // // servo[5].attach(14,500,2500); // servo[5].write(0); WiFiManager wifiManager; wifiManager.autoConnect("接收机配网"); Serial.println("Connected."); server.begin(); WiFi.mode(WIFI_STA); //设置ESP8266工作模式为无线终端模式(8266像手机一样连接到wifi的模式) connectWifi(); // 连接WiFi mqttClient.setServer(mqttServer, 1883);// 设置MQTT服务器和端口号 mqttClient.setCallback(receiveCallback);// 设置MQTT订阅回调函数 connectMQTTserver();// 连接MQTT服务器 } void loop() { if (mqttClient.connected()) { // 如果开发板成功连接服务器 mqttClient.loop(); // 处理信息以及心跳 } else { // 如果开发板未能成功连接服务器 //Stop(); connectMQTTserver(); // 则尝试连接服务器 } // for (uint8_t i = 0; i < NUM; i++) // { // servo[i].write(values[i]); // } // delay(20); } // 连接MQTT服务器并订阅信息 void connectMQTTserver() { // 根据ESP8266的MAC地址生成客户端ID(避免与其它ESP8266的客户端ID重名) String clientId = "esp8266-" + WiFi.macAddress(); // 连接MQTT服务器 if (mqttClient.connect(clientId.c_str())) { Serial.println("MQTT Server Connected."); Serial.println("Server Address:"); Serial.println(mqttServer); Serial.println("ClientId: "); Serial.println(clientId); subscribeTopic(); // 订阅指定主题 } else { Serial.print("MQTT Server Connect Failed. Client State:"); Serial.println(mqttClient.state()); //Stop(); delay(5000); } } // 收到信息后的回调函数 char AA[4],BB[4],CC[4],DD[4],EE[4],FF[4];//储存接受到的油门和方向数据 int Alocation,Blocation,Clocation,Dlocation,Elocation,Flocation; int TD1,TD2,TD3,TD4,TD5,TD6; //CH1 CH2 CH3 CH4 CH5 CH6 void receiveCallback(char* topic, byte* payload, unsigned int length) { int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,I=0,j=0; analogWrite(02,180); for ( int i = 0; i <5; i++) //CH1 { if ((char)payload[i]!='A') { AA[a]=(char)payload[i]; a+=1; } else { Blocation=i; break; } } for (int i = Blocation+1; i < length; i++) //CH2 { if ((char)payload[i]!='B') { BB[b]=(char)payload[i]; b+=1; } else { Clocation=i; break; } } for (int i = Clocation+1; i < length; i++) //CH3 { if ((char)payload[i]!='C') { CC[c]=(char)payload[i]; c+=1; } else { Dlocation=i; break; } } for (int i = Dlocation+1; i < length; i++) //CH4 { if ((char)payload[i]!='D') { DD[d]=(char)payload[i]; d+=1; } else { Elocation=i; break; } } for (int i = Elocation+1; i < length; i++) //CH5 { if ((char)payload[i]!='E') { EE[e]=(char)payload[i]; e+=1; } else { Flocation=i; break; } } for (int i = Flocation+1; i < length; i++) //CH6 { if ((char)payload[i]!='F') { FF[f]=(char)payload[i]; f+=1; } else { break; } } TD1=String(AA).toInt(); // if(TD1>180){ // TD1=180; // }else if(TD1<0){ // TD1=0; // } Avalue=TD1; TD2=String(BB).toInt(); // if(TD2>180){ // TD2=180; // }else if(TD2<0){ // TD2=0; // } Bvalue=TD2; TD3=String(CC).toInt(); // if(TD3<180) // { // TD3=0; // }else{ // TD3=180; // } Cvalue=TD3; // TD4=String(DD).toInt(); // if(TD4<0||TD4<90) // { // TD4=0; // }else if(TD4>=90&&TD4<180) // { // TD4=90; // }else{ // TD4=180; // } // values[3]=TD4; // TD5=String(EE).toInt(); // if(TD5<0) // { // TD5=0; // }else if(TD5>=180){ // TD5=180; // } // values[4]=TD5; // TD6=String(FF).toInt(); // if(TD6<0) // { // TD6=0; // }else if(TD6>=180){ // TD6=180; // } // values[5]=TD6; // for(int i=0;i<NUM;i++){ // Serial.print(values[i]); // Serial.print("\t"); // } // Serial.println(); Serial.print(Avalue); Serial.print("\t"); Serial.print(Bvalue); Serial.print("\t"); Serial.print(Cvalue); Serial.println(); Go(); for (int i = 0; i < 3; i++) { AA[i]='\0'; BB[i]='\0'; CC[i]='\0'; DD[i]='\0'; EE[i]='\0'; FF[i]='\0'; } analogWrite(02,0); } // 订阅指定主题 void subscribeTopic() { char subTopic[topicString.length() + 1]; strcpy(subTopic, topicString.c_str()); if (mqttClient.subscribe(subTopic)) { Serial.println("Subscrib Topic:"); Serial.println(subTopic); analogWrite(02,180); } else { Serial.print("Subscribe Fail..."); analogWrite(02,0); // Stop(); } } void connectWifi() { //WiFi.begin(ssid, password); //等待WiFi连接,成功连接后输出成功信息 while (WiFi.status() != WL_CONNECTED) { analogWrite(02,0); delay(1000); //Stop(); Serial.print("."); analogWrite(02,180); } Serial.println(""); Serial.println("WiFi Connected!"); Serial.println(""); analogWrite(02,180); } void Go(){ servoA.write(Avalue); servoB.write(Bvalue); servoC.write(Cvalue); } //void Stop() //{ // // servoA.write(90); // servoB.write(90); // servoC.write(90); // Avalue=90; // Bvalue=90; // Cvalue=90; //} void LED() { analogWrite(02,0); for(int i=0;i<=150;i++) { analogWrite(02,i); delay(20); } for(int i=150;i>=0;i-=2) { analogWrite(02,i); delay(20); } for(int i=0;i<3;i++) { analogWrite(02,0); delay(180); analogWrite(02,180); delay(180); } analogWrite(02,0); }
发送端代码
#include <ESP8266WiFi.h> #include <PubSubClient.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> //MQTT服务器地址:broker.hivemq.com test.ranye-iot.net 43.136.64.172 101.43.152.71 const char* mqttServer = "43.136.64.172"; String topicString = "lmwmm001"; const int CH1=05; //接收机1通道 const int CH2=04; //接收机2通道 const int CH3=15; //接收机3通道 const int CH4=13; //接收机4通道 const int CH5=12; //接收机5通道 const int CH6=14; //接收机6通道 int A,B,C,D,E,F; WiFiClient wifiClient; PubSubClient mqttClient(wifiClient); void setup() { Serial.begin(115200); pinMode(CH1,INPUT); pinMode(CH2,INPUT); pinMode(CH3,INPUT); pinMode(CH4,INPUT); pinMode(CH5,INPUT); pinMode(CH6,INPUT); LED(); analogWrite(16,180);// 电源指示灯 WiFiManager wifiManager; wifiManager.autoConnect("遥控器配网"); Serial.println("Connected."); WiFi.mode(WIFI_STA); connectWifi(); mqttClient.setServer(mqttServer, 1883); connectMQTTServer(); } void loop() { if (mqttClient.connected()) { // 如果开发板成功连接服务器 pubMQTTmsg(); analogWrite(02,180); // 保持心跳 mqttClient.loop(); } else { analogWrite(02,0); connectMQTTServer(); } } void connectMQTTServer(){ // 根据ESP8266的MAC地址生成客户端ID(避免与其它ESP8266的客户端ID重名) String clientId = "esp8266-" + WiFi.macAddress(); // 连接MQTT服务器 if (mqttClient.connect(clientId.c_str())) { Serial.println("MQTT Server Connected."); Serial.println("Server Address: "); Serial.println(mqttServer); Serial.println("ClientId:"); Serial.println(clientId); } else { Serial.print("MQTT Server Connect Failed. Client State:"); Serial.println(mqttClient.state()); delay(3000); } } // 发布信息 void pubMQTTmsg(){ static int value; // 客户端发布信息用数字 char publishTopic[topicString.length() + 1]; strcpy(publishTopic, topicString.c_str()); A=pulseIn(CH1,HIGH); A=map(A/10,100,200,0,180); if(A<=0){ A=0; }else if(A>=180){ A=180; } B=pulseIn(CH2,HIGH); B=map(B/10,100,200,0,180); if(B<=0){ B=0; }else if(B>=180){ B=180; } C=pulseIn(CH3,HIGH); C=map(C/10,100,200,0,180); if(C<180){ C=0; }else{ C=180; } // D=pulseIn(CH4,HIGH); // D=map(D/10,100,200,0,180); D=90; if(D<90){ D=0; }else if(D>=90&&D<180){ D=90; }else{ D=180; } // E=pulseIn(CH5,HIGH); // E=map(E/10,100,200,0,180); E=90; if(E<0){ E=0; } // F=pulseIn(CH6,HIGH); // F=map(F/10,100,200,0,180); F=90; if(F<0){ F=0; } // 建立发布信息。信息内容以Hello World为起始,后面添加发布次数。 String messageString = String(A)+"A"+String(B)+"B"+String(C)+"C"+String(D)+"D"+String(E)+"E"+String(F)+"F"; char publishMsg[messageString.length() + 1]; strcpy(publishMsg, messageString.c_str()); // 实现ESP8266向主题发布信息 if(mqttClient.publish(publishTopic, publishMsg)){ Serial.println(publishMsg); analogWrite(02,180); delay(30); analogWrite(02,0); delay(30); analogWrite(02,180); delay(30); analogWrite(02,0); delay(30); } else { Serial.println("Message Publish Failed."); analogWrite(02,0); } } // ESP8266连接wifi void connectWifi(){ //WiFi.begin(ssid, password); //等待WiFi连接,成功连接后输出成功信息 while (WiFi.status() != WL_CONNECTED) { analogWrite(00,0); delay(1000); analogWrite(00,180); Serial.print("."); } Serial.println(""); Serial.println("WiFi Connected!"); analogWrite(00,180); Serial.println(""); } void LED() { analogWrite(16,0); analogWrite(00,0); analogWrite(02,0); for(int i=0;i<=150;i++) { analogWrite(16,i); analogWrite(00,i); analogWrite(02,i); delay(20); } for(int i=150;i>=0;i-=2) { analogWrite(16,i); analogWrite(00,i); analogWrite(02,i); delay(20); } for(int i=0;i<3;i++) { analogWrite(16,0); analogWrite(00,0); analogWrite(02,0); delay(180); analogWrite(16,180); analogWrite(00,180); analogWrite(02,180); delay(180); } analogWrite(16,0); analogWrite(00,0); analogWrite(02,0); }