自定义串口代码
S2F芯片 串口引脚是自定义的
PDF文档
https://www.espressif.com.cn/sites/default/files/documentation/esp32-s2_datasheet_en.pdf
/* * QQ496631085 he4966.cn * ESP32-S2F 芯片 */ #include <WiFi.h> #include <WiFiMulti.h> #include <ESP32Time.h> #include <ArduinoJson.h> // 下载SomeSerial的库 #include <HardwareSerial.h> // 创建SoftwareSerial对象,将引脚39和40分别作为RX和TX // SoftwareSerial mySerial(39, 40); // 宏定义 GPIO 输出引脚 #define LED_PIN 13 #define LED_PIN2 12 WiFiMulti WiFiMulti; const int TrigPin1 = 2; const int EchoPin1 = 3; HardwareSerial Serial2(1); unsigned long Time_Echo_us1 = 0; unsigned long Len_mm1 = 0; unsigned long LastD1 = 0; unsigned long sec = 0; WiFiClient client; StaticJsonDocument<200> json; ESP32Time rtc(28800); // offset GMT8 // ESP32Time rtc1(28800); // offset GMT8 // ESP32Time rtc2(28800); // offset GMT8 void setup() { sec++; Serial1.begin(115200); Serial2.begin(115200, SERIAL_8N1, 39, 40); delay(10); Serial2.println("CLR(1);\r\n"); Serial2.println("JUMP(0);\r\n"); pinMode(EchoPin1, INPUT); // Set EchoPin as input, to receive measure result from US-025,US-026 pinMode(TrigPin1, OUTPUT); // 配置 GPIO 输出引脚 pinMode(LED_PIN, OUTPUT); huxi_deng(); // We start by connecting to a WiFi network WiFiMulti.addAP("xiaohe_mobile", "23456789"); WiFiMulti.addAP("xiaohe", "23456789"); WiFiMulti.addAP("wangke", "wxwk1898"); Serial2.println(); Serial2.println(); // Serial2.print("Waiting for WiFi... "); Serial2.println("SET_TXT(0,'Waiting for WiFi');"); while (WiFiMulti.run() != WL_CONNECTED) { Serial2.print("."); delay(2); } Serial2.println(""); Serial2.println("WiFi connected"); Serial2.println("IP address: "); Serial2.println(WiFi.localIP()); analogWrite(LED_PIN, 10); pinMode(LED_PIN2, OUTPUT); delay(500); } void loop() { cWifi(); // This will send a request to the server // uncomment this line to send an arbitrary string to the server // client.print("Send this data to the server"); // uncomment this line to send a basic document request to the server Serial2.print("{\"action\":\"door.v1.get\",\"sex\":1}"); client.println("{\"action\":\"door.v1.get\",\"sex\":1}"); int maxloops = 0; while (!client.available() && maxloops < 1000) { maxloops++; delay(1); // delay 1 msec } if (client.available() > 0) { String line = client.readStringUntil('\r'); Serial2.println(line); deserializeJson(json, line); if(json["success"] == true){ // Serial2.println("JUMP(0);\r\n"); rtc.setTime(json["time"]); Serial2.print("SET_TXT(0,'"); Serial2.print(rtc.getMonth()+1); Serial2.print("-"); Serial2.print(rtc.getDay()); Serial2.print(" "); Serial2.print(rtc.getHour(true)); // Serial2.println(rtc.getHour(true) + ":" + rtc.getMinute()+ ":" + rtc.getSecond()); Serial2.print(":"); Serial2.print(rtc.getMinute()); Serial2.print(":"); Serial2.print(rtc.getSecond()); Serial2.println("');\r\n"); Serial2.print("SET_TXT(2,'"); if(json["data"]["sex1"]["switch"]=="1"){ Serial2.print("M:open"); // rtc1.setTime(json["data"]["sex1"]["time"]); // Serial2.print(" "); // Serial2.print(rtc1.getHour(true)); // Serial2.print(":"); // Serial2.print(rtc1.getMinute()); // Serial2.print(":"); // Serial2.print(rtc1.getSecond()); }else{ Serial2.print("M:Close"); } String sex1_switch =json["data"]["sex1"]["switch"]; String sex2_switch =json["data"]["sex2"]["switch"]; if(sex2_switch=="1"){ Serial2.print(" W:open"); // rtc2.setTime(json["data"]["sex2"]["time"]); // Serial2.print(" "); // Serial2.print(rtc2.getHour(true)); // Serial2.print(":"); // Serial2.print(rtc2.getMinute()); // Serial2.print(":"); // Serial2.print(rtc2.getSecond()); }else{ Serial2.print(" W:Close"); } Serial2.println("');\r\n"); delay(20); // Serial2.println("SET_TXT(1,'男关女关');"); if(sex2_switch=="1"){ if(sex1_switch=="1"){ Serial2.println("BL(5);\r\n"); }else{ Serial2.println("BL(200);\r\n"); } }else{ if(sex1_switch=="1"){ Serial2.println("BL(100);\r\n"); }else{ Serial2.println("BL(254);\r\n"); } } delay(16); Serial2.print(sex1_switch); Serial2.println(sex2_switch); } } else { Serial2.println("client.available() timed out "); } Serial2.println("Closing connection."); // client.stop(); // Serial2.println("Waiting 1 seconds before restarting..."); delay(1000); } void huxi_deng(){ // 实现渐亮效果 for(int i=0;i<200;i++) { // 设置亮度模拟值 analogWrite(LED_PIN, i); // 延时 2ms delay(5); } // 实现渐灭效果 for(int i=200;i>=0;i--) { // 设置亮度模拟值 analogWrite(LED_PIN, i); // 延时 2ms delay(5); } } void cWifi() { const uint16_t port = 8082; const char *host = "150.158.123.141"; // ip or dns // Use WiFiClient class to create TCP connections if (!client.connect(host, port)) { Serial2.println("Connection failed."); Serial2.println("Waiting 5 seconds before retrying..."); delay(5000); //判断 wifi 是否断网,如果断网就重连接 WiFi if (WiFiMulti.run() != WL_CONNECTED) { Serial2.println("WiFi disconnected."); huxi_deng(); } // 设置亮度模拟值 analogWrite(LED_PIN, 10); return; } else { // Serial2.print("Connecting to "); // Serial2.println(host); } } long panduanJUli(long now) { analogWrite(LED_PIN2, now); // long piancha = 2; if (now != LastD1) { // if(now+piancha > LastD1 || now-piancha< LastD1){ // LastD1 = now; // return now; // } LastD1 = now; return now; } else { if(now>560) { return 0; }else{ return now; } } } long readDistance1() { digitalWrite(TrigPin1, HIGH); // begin to send a high pulse, then US-025/US-026 begin to measure the distance delayMicroseconds(20); // set this high pulse width as 20us (>10us) digitalWrite(TrigPin1, LOW); // end this high pulse Time_Echo_us1 = pulseIn(EchoPin1, HIGH); // calculate the pulse width at EchoPin, if ((Time_Echo_us1 < 60000) && (Time_Echo_us1 > 1)) // a valid pulse width should be between (1, 60000). { Len_mm1 = (Time_Echo_us1 * 34 / 1000) / 2; // calculate the distance by pulse width, Len_mm = (Time_Echo_us * 0.34mm/us) / 2 (mm) // Serial2.print("Present Distance is: "); //output result to Serial monitor Serial2.print(Len_mm1, DEC); // output result to Serial monitor Serial2.println("cm"); if (Len_mm1 >= 560) { return 0; } // output result to Serial monitor return Len_mm1; } else { return 0; } }