ESP32-Serial กับ Nano
การทดสอบการเชื่อมต่อ ESP32 กับ Nano
สำหรับฝั่ง ESP32 ให้ใช้งาน hardware serial port RX2, TX2 หรือ RX1, TX1 ได้เลย ในกรณีนี้จะใช้ RX2, TX2
และจะทำการทดสอบ FS200-SHT1 sensor วัด temp humid ไปพร้อมๆ กัน
โดยการเชื่อมต่อ
ESP32 Nano
- GND GND
- RX2 D11
- TX2 D10
กำหนดขา RXD บน ESP32
#define RXD2 16 #define TXD2 17
และใน void setup() ประกาศใช้
void setup() { delay(10000); Serial.begin(9600); Serial2.begin(4800, SERIAL_8N1, RXD2, TXD2); // original 8N1 Serial.println("Serial Txd is on pin: "+String(TX)); Serial.println("Serial Rxd is on pin: "+String(RX)); ....
สำหรับการเรียก ขอข้อมูลจาก Nano ใช้โค้ดส่วนนี้
void serial_loop() { Serial.println("Please wait Serial.."); /// #1 while (a == "") { Serial2.print("Input1"); // ส่งคำขอ Input1 ไปยัง Arduino a = Serial2.readString(); // อ่าน Serial และนำไปเก็บในตัวแปร A delay(100); Serial.print("."); } Serial.print(" Answer 1 ");Serial.println(a); data1 = String(a); delay(1000); a=""; ///#2 while (a == "") { Serial2.print("Input2"); // ส่งคำขอ Input2 ไปยัง Arduino a = Serial2.readString(); // อ่าน Serial และนำไปเก็บในตัวแปร A delay(100); Serial.print("."); } Serial.print(" Answer 2 ");Serial.println(a); data2=String(a); a=""; ///#3 while (a == "") { Serial2.print("Input3"); // ส่งคำขอ Input3 ไปยัง Arduino a = Serial2.readString(); // อ่าน Serial และนำไปเก็บในตัวแปร A delay(100); Serial.print("."); } Serial.print(" Answer 3 ");Serial.println(a); data3 = String(a); delay(1000); a=""; ///#4 while (a == "") { Serial2.print("Input4"); // ส่งคำขอ Input4 ไปยัง Arduino a = Serial2.readString(); // อ่าน Serial และนำไปเก็บในตัวแปร A delay(100); Serial.print("."); } Serial.print(" Answer 4 ");Serial.println(a); data4=String(a); a=""; }
และเป็นโค้ดรวม
///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// //Libraries #include <Arduino.h> #include <Wire.h> #include <WiFi.h> /* * There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD. * * U0UXD is used to communicate with the ESP32 for programming and during reset/boot. * U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though * U2UXD is unused and can be used for your projects. * */ #define RXD2 16 #define TXD2 17 ///// #include <math.h> //====== #include <Adafruit_Sensor.h> #include <DHT.h> // กรณีนี้ต้องใช้คู่กันกับ DHT_U.h #include <DHT_U.h> #define DHTPIN 6 // Pin which is connected to the DHT sensor. // Uncomment the type of sensor in use: //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT_Unified dht(DHTPIN, DHTTYPE); uint32_t delayMS; //// char* host = "xxxxxxx.com"; char* code = "********"; char* dID = "******"; float dustDensity = 35; String response ="0"; String response_c = "0"; String a ; float temp_0 = 0; float tempF_0 = 0; float humid_0 = 0; float vHumidity = 0; float vTemperature = 0; String data1 ; String data2 ; // standard String data3 ; String data4 ; float data5 = 0; float data6 = 0; float data7 = 0; float data8 = 0; float data9 = 0; float data10 = 0; float data11 = 0; float data12 = 0; float data13 = 0; float data14 = 0; float data15 = 0; float data16 = 0; float data17 = 0; float data18 = 0; float data19 = 0; float data20 = 0; float temperatureC = 0; float temperatureF = 0; int counter = 1; int sentcount = 0; ///////////////////////////////////////////////////////////////////////////// void master_i2c_setup() { Wire.begin(); Serial.println("I2C Master Demonstration"); } ///////////////////////////////////////////////////////////////////////////// // Arduino setup function. /////////////// const char* ssid = "Arsenal2019_2.4G"; const char* password = "kb75699212"; const char* ssid1 = "Arsenal2019_2.4G"; const char* password1 = "kb75699212"; const char* ssid2 = "Arsenal2019_2.4G"; const char* password2 = "kb75699212"; // Helper functions to print a data value to the serial monitor. void WiFisetup() { Serial.println(); Serial.print("Connecting to "); Serial.println(ssid1); WiFi.begin(ssid1, password1); delay(1000); if (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Failed to connected and WiFi setup "); WiFi.begin(ssid2, password2); delay(1000); if (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Failed to connected and WiFi setup "); } else{ ssid = ssid2; password = password2; } } else{ ssid = ssid1; password = password1; Serial.println(""); Serial.println("WiFi connected OK"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } } /// void AM2302_setup() { dht.begin(); Serial.println("DHTxx Unified Sensor Example"); // Print temperature sensor details. sensor_t sensor; dht.temperature().getSensor(&sensor); Serial.println("------------------------------------"); Serial.println("Temperature"); Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C"); Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C"); Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C"); Serial.println("------------------------------------"); // Print humidity sensor details. dht.humidity().getSensor(&sensor); Serial.println("------------------------------------"); Serial.println("Humidity"); Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%"); Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%"); Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%"); Serial.println("------------------------------------"); // Set delay between sensor readings based on sensor details. delayMS = sensor.min_delay / 1000; } /// void setup() { delay(10000); Serial.begin(9600); Serial2.begin(4800, SERIAL_8N1, RXD2, TXD2); // original 8N1 Serial.println("Serial Txd is on pin: "+String(TX)); Serial.println("Serial Rxd is on pin: "+String(RX)); /// delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFisetup(); //WiFi.begin(ssid1, password1); delay(1000); if (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Failed connected setup "); } else{ Serial.println(""); Serial.println("WiFi OK connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // We start by connecting to a WiFi network } } int value = 0; void serverloop() { delay(1000); ++value; data1 = String(data1); data2 = String(data2); //AM2302_loop(); //data3 = temp_0; //data4 = humid_0; Serial.print("data1 rain mm = ");Serial.println(data1); Serial.print("data2 soil = ");Serial.println(data2); Serial.print("data3 temp = ");Serial.println(data3); Serial.print("data4 humid = ");Serial.println(data4); Serial.print("connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } // We now create a URI for the request String url = "/api/insertData?device_id=" + String(dID)+"&code="+String(code)+"&data1=" +String(data1) +"&data2=" + String(data2)+"&data3=" +String(data3)+"&data4=" +String(data4)+"&data5=" +String(data5) +"&data6=" +String(data6)+"&data7=" +String(data7)+"&data8=" +String(data8)+"&data9=" +String(data9) +"&data10=" +String(data10)+"&data11=" +String(data11)+"&data12=" +String(data12)+"&data13=" +String(data13) +"&data14=" +String(data14)+"&data15=" +String(data15)+"&data16=" +String(data16)+"&data17=" +String(data17) +"&data18=" +String(data18)+"&data19=" +String(data19)+"&data20=" +String(data20); Serial.print("Requesting URL: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); //unsigned long timeout = millis(); delay(1000); while (client.available() == 0) { Serial.println(">>> Client Timeout !"); client.stop(); return; } // Read all the lines of the reply from server and print them to Serial while(client.available()) { String line = client.readStringUntil('\r'); //Serial.print(line); ถอดคอมเม้น? ออก เพื่อดูการตอบสนองจาก Server } Serial.println(); Serial.println("endloop server "); delay(1000); } //==== void loop() { if (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Failed to connected and WiFi setup "); WiFisetup(); } serial_loop(); serverloop(); data1 = ""; data2 = ""; } void serial_loop() { Serial.println("Please wait Serial.."); /// #1 while (a == "") { Serial2.print("Input1"); // ส่งหัวข้อคำถาม ว่า Question1 ไปยัง Arduino a = Serial2.readString(); // อ่าน Serial และนำไปเก็บในตัวแปร A delay(100); Serial.print("."); } Serial.print(" Answer 1 ");Serial.println(a); data1 = String(a); delay(1000); a=""; ///#2 while (a == "") { Serial2.print("Input2"); // ส่งหัวข้อคำถาม ว่า Question1 ไปยัง Arduino a = Serial2.readString(); // อ่าน Serial และนำไปเก็บในตัวแปร A delay(100); Serial.print("."); } Serial.print(" Answer 2 ");Serial.println(a); data2=String(a); a=""; ///#3 while (a == "") { Serial2.print("Input3"); // ส่งหัวข้อคำถาม ว่า Question1 ไปยัง Arduino a = Serial2.readString(); // อ่าน Serial และนำไปเก็บในตัวแปร A delay(100); Serial.print("."); } Serial.print(" Answer 3 ");Serial.println(a); data3 = String(a); delay(1000); a=""; ///#4 while (a == "") { Serial2.print("Input4"); // ส่งหัวข้อคำถาม ว่า Question1 ไปยัง Arduino a = Serial2.readString(); // อ่าน Serial และนำไปเก็บในตัวแปร A delay(100); Serial.print("."); } Serial.print(" Answer 4 ");Serial.println(a); data4=String(a); a=""; }
สำหรับฝั่ง Nano จะเป็นเหมือนกับตัวอย่างก่อนที่มีการต่อกับ ESP8266
โดยในฝั่ง Nano ยังคงประการใช้ SoftwareSerial ตามปกติ
#include <EEPROM.h> #include <SoftwareSerial.h> SoftwareSerial chat(10, 11); // RX, TX //////// AM2302 #include <SHT1x.h> #define dataPin A4 #define clockPin A5 SHT1x sht1x(dataPin, clockPin); int ledPin = 2; int ledPin3 = 3; int analogPin = A1; int val = 0; //// #include <math.h> #include <Adafruit_Sensor.h> #include <DHT.h> // กรณีนี้ต้องใช้คู่กันกับ DHT_U.h #include <DHT_U.h> #define DHTPIN 6 // Pin which is connected to the DHT sensor. // Uncomment the type of sensor in use: //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT_Unified dht(DHTPIN, DHTTYPE); uint32_t delayMS; //////// int i; int sensorValue; float rainmm ; float dustDensity = 35; String response ="0"; String response_c = "0"; String a ; float temp_0 = 0; float tempF_0 = 0; float humid_0 = 0; float vHumidity = 0; float vTemperature = 0; String data1 ; String data2 ; // standard float data3 = 0; float data4 = 0; float data5 = 0; float data6 = 0; float data7 = 0; float data8 = 0; float data9 = 0; float data10 = 0; float data11 = 0; float data12 = 0; float data13 = 0; float data14 = 0; float data15 = 0; float data16 = 0; float data17 = 0; float data18 = 0; float data19 = 0; float data20 = 0; float temperatureC = 0; float temperatureF = 0; int counter = 1; int sentcount = 0; ////////////////////////////// void AM2302_setup() { dht.begin(); Serial.println("DHTxx Unified Sensor Example"); // Print temperature sensor details. sensor_t sensor; dht.temperature().getSensor(&sensor); Serial.println("------------------------------------"); Serial.println("Temperature"); Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C"); Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C"); Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C"); Serial.println("------------------------------------"); // Print humidity sensor details. dht.humidity().getSensor(&sensor); Serial.println("------------------------------------"); Serial.println("Humidity"); Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%"); Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%"); Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%"); Serial.println("------------------------------------"); // Set delay between sensor readings based on sensor details. delayMS = sensor.min_delay / 1000; } void setup() { Serial.begin(9600); chat.begin(4800); pinMode(ledPin, OUTPUT); // sets the pin as output pinMode(ledPin3, OUTPUT); // sets the pin as output } void loop() { //AM2302_loop(); sht1_loop(); input1_loop(); input2_loop(); if (chat.readString()){ // chat.print(1); if(chat.readString()== "Input1"){ //มีการถามคำถาม Question1 ส่งข้อมูลตัวแปร a ออกไป chat.print(String(sensorValue)); } if(chat.readString()== "Input2"){ //มีการถามคำถาม Question2 ส่งข้อมูลตัวแปร b ออกไป chat.print(String(rainmm)); } if(chat.readString()== "Input3"){ //มีการถามคำถาม Question1 ส่งข้อมูลตัวแปร a ออกไป chat.print(String(data3)); } if(chat.readString()== "Input4"){ //มีการถามคำถาม Question2 ส่งข้อมูลตัวแปร b ออกไป chat.print(String(data4)); } Serial.print("Send = "); Serial.println(i); } i++; delay(1000); } void input1_loop() { // read the value from the sensor: sensorValue = analogRead(A0); //Serial.print(" Analog read := ");Serial.println(sensorValue); if (sensorValue > 580) { rainmm = 80.437-0.0977*sensorValue; } else { if (sensorValue > 509) { rainmm = 443.6-0.717*sensorValue; } else { if (sensorValue > 466) { rainmm = 903.74-1.6059*sensorValue; } else{ rainmm = 770.06-1.3314*sensorValue; } } } delay(1000); //answer=String(255); Serial.print("A0 signal : ");Serial.println(sensorValue); Serial.print("Rain mm : ");Serial.println(rainmm); } void input2_loop() { // read the value from the sensor: sensorValue = analogRead(A0); //Serial.print(" Analog read := ");Serial.println(sensorValue); if (sensorValue > 580) { rainmm = 80.437-0.0977*sensorValue; } else { if (sensorValue > 509) { rainmm = 443.6-0.717*sensorValue; } else { if (sensorValue > 466) { rainmm = 903.74-1.6059*sensorValue; } else{ rainmm = 770.06-1.3314*sensorValue; } } } delay(1000); //answer=String(255); } void AM2302_loop() { // Delay between measurements. delay(delayMS); // Get temperature event and print its value. sensors_event_t event; dht.temperature().getEvent(&event); if (isnan(event.temperature)) { Serial.println("Error reading temperature!"); } else { Serial.print("Temperature: "); Serial.print(event.temperature); Serial.println(" *C"); temp_0 = event.temperature; data3 = temp_0; } // Get humidity event and print its value. dht.humidity().getEvent(&event); if (isnan(event.relative_humidity)) { Serial.println("Error reading humidity!"); } else { Serial.print("Humidity: "); Serial.print(event.relative_humidity); Serial.println("%"); humid_0 = event.relative_humidity; data4 = humid_0; } } void sht1_loop() // from myarduino.net { float temp_c; // ค่าอุณหภูมิ ที่แสดงหน่วยเป็น องศาเซลเซียส float temp_f; // ค่าอุณหภูมิ ที่แสดงหน่วยเป็น องศาฟาเรนไฮต์ float humidity; // ค่าความชื้น temp_c = sht1x.readTemperatureC(); temp_f = sht1x.readTemperatureF(); humidity = sht1x.readHumidity(); data3 = temp_c; data4 = humidity; Serial.print("Temperature: "); Serial.print(temp_c, DEC); Serial.print("C / "); Serial.print(temp_f, DEC); Serial.print("F. Humidity: "); Serial.print(humidity); Serial.println("%"); if (temp_c < 25 ) { // ค่าที่กำหนดไว้เพื่อให้วงจร LED ทำงาน ในที่นี้กำหนดการทำงานเมื่อค่าอุณหภูมิ ที่แสดงหน่วยเป็น องศาเซลเซียส ต่ำกว่า 25 digitalWrite(ledPin, LOW); // สั่งให้ LED ที่ Pin2 ดับ digitalWrite(ledPin3, HIGH); // สั่งให้ LED ที่ Pin3 ติดสว่าง } else { digitalWrite(ledPin, HIGH); // สั่งให้ LED ที่ Pin2 ติดสว่าง digitalWrite(ledPin3, LOW); // สั่งให้ LED ที่ Pin3 ดับ } delay(100); }
ขอบคุณโค้ด SHT1 จาก myarduino.net