arduino输出乱码与远程控制

最近在做项目,使用arduino控制超声波,pm2.5等传感器,以及继电器等,然后连接wifi进行远程控制
【arduino输出乱码与远程控制】发现代码都正确 ,但是板子串口输出是乱码的情况
一一排查之后发现是超声波的trig口接在了wemos版的d9上,这是一个板子自带的led灯的控制口
超声波传感器总是发出‘滋滋’的声音,吓我一跳
最后改为了,d8,d10才解决,代码如下

#include #define MAX_SRV_CLIENTS 1 #define dustPin A0 #define Trig0 //引脚Tring 连接 IO D8 #define Echo 15 //引脚Echo 连接 IO D10 int flower_led=5; //d3 int led_led=4; //d4 int door_led=14; //d5 int power_led=12; //d6 int curatin_led=13; //d7float dustVal=0; int ledPower=16; //d2 int delayTime=280; int delayTime2=40; float offTime=9680; float cm; //距离变量 float temp; ////连接 AP 的账号 const char* ssid = "Tenda_3BFE70"; //连接用的密码 const char* password = "lizhendewifi"; WiFiServer server(2222); WiFiClient serverClients[MAX_SRV_CLIENTS]; void setup() { Serial.begin(115200); pinMode(ledPower,OUTPUT); pinMode(dustPin,INPUT); pinMode(Trig, OUTPUT); pinMode(Echo, INPUT); pinMode(flower_led, OUTPUT); digitalWrite(flower_led, HIGH); //设定通电后电机不转 pinMode(led_led, OUTPUT); pinMode(door_led, OUTPUT); pinMode(power_led, OUTPUT); pinMode(curatin_led, OUTPUT); WiFi.begin(ssid, password); Serial.print("\nconnecting to "); Serial.println(ssid); uint8_t i = 0; //此处省略对于连接网络失败的判断,但是连接网络还是需要一定的延时 while (WiFi.status() != WL_CONNECTED && i++ < 20)delay(500); if (i == 21) Serial.println("error internet"); server.begin(); server.setNoDelay(true); Serial.println(WiFi.localIP()); Serial.println(" and 2222"); } void loop() { uint8_t i; if (server.hasClient()) { for (i = 0; i < MAX_SRV_CLIENTS; i++) { if (!serverClients[i] || !serverClients[i].connected()) { if (serverClients[i])serverClients[i].stop(); serverClients[i] = server.available(); Serial.println("a client accept"); Serial.println(i); continue; } } WiFiClient serverClient = server.available(); serverClient.stop(); } for (i = 0; i < MAX_SRV_CLIENTS; i++) { if (serverClients[i] && serverClients[i].connected()) { //这个是pm2.5的数据感知 delay(2000); digitalWrite(ledPower,LOW); delayMicroseconds(delayTime); dustVal=analogRead(dustPin); delayMicroseconds(delayTime2); digitalWrite(ledPower,HIGH); delayMicroseconds(offTime); char dust_string[55]; itoa(dustVal, dust_string, 10); char new_dust_string[55] = "dust:"; char flag_sectaion[] = ","; strcat(dust_string, flag_sectaion); strcat(new_dust_string,dust_string); //这个是超声波的测距信息 //给Trig发送一个低高低的短时间脉冲,触发测距 digitalWrite(Trig, LOW); //给Trig发送一个低电平 delayMicroseconds(2); //等待 2微妙 digitalWrite(Trig,HIGH); //给Trig发送一个高电平 delayMicroseconds(10); //等待 10微妙 digitalWrite(Trig, LOW); //给Trig发送一个低电平temp = float(pulseIn(Echo, HIGH)); //存储回波等待时间, //pulseIn函数会等待引脚变为HIGH,开始计算时间,再等待变为LOW并停止计时 //返回脉冲的长度//声速是:340m/1s 换算成 34000cm / 1000000μs => 34 / 1000 //因为发送到接收,实际是相同距离走了2回,所以要除以2 //距离(厘米)=(回波时间 * (34 / 1000)) / 2 //简化后的计算公式为 (回波时间 * 17)/ 1000 cm = (temp * 17 )/1000; //把回波时间换算成cmint flower_status=digitalRead(flower_led); int led_status=digitalRead(led_led); int door_status=digitalRead(door_led); int power_status=digitalRead(power_led); int curatin_status=digitalRead(curatin_led); //距离信息封装 char distance_string[55]; itoa(cm, distance_string, 10); char new_distance_string[55] = "distance:"; strcat(distance_string, flag_sectaion); strcat(new_distance_string,distance_string); //浇花信息封装 char flower_string[55]; itoa(flower_status, flower_string, 10); char new_flower_string[55] = "water_flower_status:"; strcat(flower_string, flag_sectaion); strcat(new_flower_string,flower_string); //灯信息封装 char led_string[55]; itoa(led_status, led_string, 10); char new_led_string[55] = "light_status:"; strcat(led_string, flag_sectaion); strcat(new_led_string,led_string); //门信息封装 char door_string[55]; itoa(door_status, door_string, 10); char new_door_string[55] = "door_status:"; strcat(door_string, flag_sectaion); strcat(new_door_string,door_string); //电源信息封装 char power_string[55]; itoa(power_status, power_string, 10); char new_power_string[55] = "power_status:"; strcat(power_string, flag_sectaion); strcat(new_power_string,power_string); //窗帘信息封装 char curatin_string[55]; itoa(curatin_status, curatin_string, 10); char new_curatin_string[55] = "curtain_status:"; strcat(curatin_string, flag_sectaion); strcat(new_curatin_string,curatin_string); char s[255] = "infoone-"; strcat(s, new_dust_string); strcat(s, new_flower_string); strcat(s, new_led_string); strcat(s, new_door_string); strcat(s, new_power_string); strcat(s, new_curatin_string); strcat(s, new_distance_string); serverClients[i].write(s, strlen(s)); Serial.println(s); delay(1); } } for (i = 0; i < MAX_SRV_CLIENTS; i++) { if (serverClients[i] && serverClients[i].connected()) { if (serverClients[i].available()) { while (serverClients[i].available()) { Serial.println("服务器收到数据"); String line = serverClients[i].readStringUntil('\n'); Serial.println(line); //这个地方加延时是因为服务器收到的数据零碎,不足以判别,等待接收到完整数据,并串口输出,此时数据大概已经组装好了 delay(1000); if (line.equals("curtain_open")) { digitalWrite(curatin_led, HIGH); //设定是灭的 } if (line.equals("curtain_close")) { digitalWrite(curatin_led, LOW); //设定是灭的 } if (line.equals("light_open")) { digitalWrite(led_led, HIGH); //设定是灭的 } if (line.equals("light_close")) { digitalWrite(led_led, LOW); //设定是灭的 } if (line.equals("water_open")) { digitalWrite(flower_led, HIGH); //设定是灭的 } if (line.equals("water_close")) { digitalWrite(flower_led, LOW); //设定是灭的 } if (line.equals("door_open")) { digitalWrite(door_led, HIGH); //设定是灭的 } if (line.equals("door_close")) { digitalWrite(door_led, LOW); //设定是灭的 } if (line.equals("power_open")) { digitalWrite(power_led, HIGH); //设定是灭的 } if (line.equals("power_close")) { digitalWrite(power_led, LOW); //设定是灭的 }} } } } delay(2000); }


    推荐阅读