voidloop() { if((!client.connected())) { while (!client.connected())//几个非连接的异常处理 { if (!client.connect(host, tcpPort)) { Serial.println("connection...."); //client.stop(); delay(500);
} } client.write("esp8266"); } while (client.available())//改动的就这里啦,无线读取到的数据转发到到串口 { uint8_t c = client.read(); Serial.write(c); if (c == 'a') { //pc端发送a和b来控制 digitalWrite(2, HIGH); client.write("已关灯"); } if (c == 'b') { digitalWrite(2, LOW); client.write("已开灯"); } if (c == 'c') { digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); client.write("已脉冲1"); } if (c == 'd') { digitalWrite(2, LOW); delay(200); digitalWrite(2, HIGH); client.write("已脉冲2"); } break; }
if (Serial.available())//串口读取到的转发到wifi,因为串口是一位一位的发送所以在这里缓存完再发送 { delay(500); size_t counti = Serial.available(); Serial.println(counti); char sbuf[100]; int i=0; while(Serial.available()) sbuf[i++]=Serial.read(); sbuf[i]='\n'; client.write(sbuf,counti);
} } /*int flasher_pin = 2; // GPIO2 for ESP-01S
void setup() { Serial.begin(115200); // can use Serial and flash Led at the same time on ESP-01S pinMode(2, OUTPUT); for (int i = 10; i > 0; i--) { Serial.print(i); Serial.print(' '); delay(500); } Serial.println(); delay(1000); }