From 4b980cac152a8d7a181bede4a3a3052091ebc58f Mon Sep 17 00:00:00 2001 From: Sean Kwok <45260108+Gitshaoxiang@users.noreply.github.com> Date: Thu, 31 Mar 2022 15:41:54 +0800 Subject: [PATCH] add poesp32 example --- .../Unit/PoESP32/HTTP_CLIENT/HTTP_CLIENT.ino | 116 +++++++++++++++++ .../Unit/PoESP32/MQTT_CLIENT/MQTT_CLIENT.ino | 110 ++++++++++++++++ .../Unit/PoESP32/TCP_CLIENT/TCP_CLIENT.ino | 119 ++++++++++++++++++ library.properties | 2 +- 4 files changed, 346 insertions(+), 1 deletion(-) create mode 100644 examples/Unit/PoESP32/HTTP_CLIENT/HTTP_CLIENT.ino create mode 100644 examples/Unit/PoESP32/MQTT_CLIENT/MQTT_CLIENT.ino create mode 100644 examples/Unit/PoESP32/TCP_CLIENT/TCP_CLIENT.ino diff --git a/examples/Unit/PoESP32/HTTP_CLIENT/HTTP_CLIENT.ino b/examples/Unit/PoESP32/HTTP_CLIENT/HTTP_CLIENT.ino new file mode 100644 index 0000000..2e2405d --- /dev/null +++ b/examples/Unit/PoESP32/HTTP_CLIENT/HTTP_CLIENT.ino @@ -0,0 +1,116 @@ +/* + Description: + Use UNIT PoESP32 to create HTTP request + before compiling: + FastLED: https://github.com/FastLED/FastLED + M5Atom: https://github.com/m5stack/M5Atom + UNIT_PoESP32: https://github.com/m5stack/UNIT_PoESP32 +*/ + +#include "M5Atom.h" +#include "UNIT_PoESP32.h" + +UNIT_PoESP32 eth; + +typedef enum { kConfig = 0, kStart, kConnecting, kConnected } DTUState_t; + +DTUState_t State = kStart; + +void TaskLED(void *pvParameters) { + while (1) { + switch (State) { + case kConfig: + M5.dis.fillpix(0x0000ff); + break; + case kConnecting: + M5.dis.fillpix(0xffff00); + break; + case kConnected: + M5.dis.fillpix(0x00ff00); + break; + case kStart: + M5.dis.fillpix(0xff0000); + break; + } + for (int i = 100; i > 0; i--) { + M5.dis.setBrightness(i); + FastLED.show(); + vTaskDelay(10 / portTICK_RATE_MS); + } + for (int i = 0; i < 100; i++) { + M5.dis.setBrightness(i); + FastLED.show(); + vTaskDelay(10 / portTICK_RATE_MS); + } + } +} + +String readstr; + +void setup() { + M5.begin(true, false, true); + eth.Init(&Serial2, 9600, 32, 26); + + xTaskCreatePinnedToCore(TaskLED, "TaskLED" // A name just for humans + , + 4096 // This stack size can be checked & adjusted + // by reading the Stack Highwater + , + NULL, + 1 // Priority, with 3 (configMAX_PRIORITIES - 1) + // being the highest, and 0 being the lowest. + , + NULL, 0); + delay(10); + + State = kStart; + + Serial.println("wait device connect"); + while (!eth.checkDeviceConnect()) { + delay(10); + } + + Serial.println("device connected"); + + State = kConnecting; + + Serial.println("wait ethernet connect"); + while (!eth.checkETHConnect()) { + delay(10); + } + Serial.println("ethernet connected"); + + State = kConnected; + + readstr = eth.createHTTPClient(HEAD, APPLICATION_X_WWW_FORM_URLENCODED, + "http://httpbin.org/get"); + Serial.println(readstr); + + readstr = eth.createHTTPClient(GET, APPLICATION_X_WWW_FORM_URLENCODED, + "http://httpbin.org/get"); + Serial.println(readstr); + + readstr = eth.createHTTPClient(POST, APPLICATION_X_WWW_FORM_URLENCODED, + "http://httpbin.org/post", + "field1=value1&field2=value2"); + Serial.println(readstr); + + readstr = eth.createHTTPClient(PUT, APPLICATION_X_WWW_FORM_URLENCODED, + "http://httpbin.org/put"); + Serial.println(readstr); + + readstr = eth.createHTTPClient(DELETE, APPLICATION_X_WWW_FORM_URLENCODED, + "http://httpbin.org/delete"); + Serial.println(readstr); +} + +void loop() { + if (Serial.available()) { + char ch = Serial.read(); + Serial2.write(ch); + } + if (Serial2.available()) { + char ch = Serial2.read(); + Serial.write(ch); + } +} diff --git a/examples/Unit/PoESP32/MQTT_CLIENT/MQTT_CLIENT.ino b/examples/Unit/PoESP32/MQTT_CLIENT/MQTT_CLIENT.ino new file mode 100644 index 0000000..7093e4d --- /dev/null +++ b/examples/Unit/PoESP32/MQTT_CLIENT/MQTT_CLIENT.ino @@ -0,0 +1,110 @@ +/* + Description: + Use UNIT PoESP32 to connect to the MQTT server, and implement subscription + and publishing messages. Check the status through Serial. When the MQTT + connection is successful, Click Btn Public Topic Please install library + before compiling: + FastLED: https://github.com/FastLED/FastLED + M5Atom: https://github.com/m5stack/M5Atom + UNIT_PoESP32: https://github.com/m5stack/UNIT_PoESP32 +*/ + +#include "M5Atom.h" +#include "UNIT_PoESP32.h" + +UNIT_PoESP32 eth; + +typedef enum { kConfig = 0, kStart, kConnecting, kConnected } DTUState_t; + +DTUState_t State = kStart; + +void TaskLED(void *pvParameters) { + while (1) { + switch (State) { + case kConfig: + M5.dis.fillpix(0x0000ff); + break; + case kConnecting: + M5.dis.fillpix(0xffff00); + break; + case kConnected: + M5.dis.fillpix(0x00ff00); + break; + case kStart: + M5.dis.fillpix(0xff0000); + break; + } + for (int i = 100; i > 0; i--) { + M5.dis.setBrightness(i); + FastLED.show(); + vTaskDelay(10 / portTICK_RATE_MS); + } + for (int i = 0; i < 100; i++) { + M5.dis.setBrightness(i); + FastLED.show(); + vTaskDelay(10 / portTICK_RATE_MS); + } + } +} + +String readstr; + +void setup() { + M5.begin(true, false, true); + eth.Init(&Serial2, 9600, 32, 26); + + xTaskCreatePinnedToCore(TaskLED, "TaskLED" // A name just for humans + , + 4096 // This stack size can be checked & adjusted + // by reading the Stack Highwater + , + NULL, + 1 // Priority, with 3 (configMAX_PRIORITIES - 1) + // being the highest, and 0 being the lowest. + , + NULL, 0); + delay(10); + + State = kStart; + + Serial.println("wait device connect"); + while (!eth.checkDeviceConnect()) { + delay(10); + } + + Serial.println("device connected"); + + State = kConnecting; + + Serial.println("wait ethernet connect"); + while (!eth.checkETHConnect()) { + delay(10); + } + Serial.println("ethernet connected"); + + State = kConfig; + + Serial.println("wait mqtt connect"); + while (!eth.createMQTTClient("120.77.157.90", "1883", "client_id", + "user_name", "password")) { + delay(10); + } + Serial.println("mqtt connected"); + + while (!eth.subscribeMQTTMsg("PoESP32_MQTT_D", "2")) { + delay(10); + } +} + +void loop() { + if (Serial.available()) { + char ch = Serial.read(); + Serial2.write(ch); + } + if (Serial2.available()) { + char ch = Serial2.read(); + Serial.write(ch); + } + eth.publicMQTTMsg("PoESP32_MQTT_U", "Hello From PoESP32", "2"); + delay(1000); +} diff --git a/examples/Unit/PoESP32/TCP_CLIENT/TCP_CLIENT.ino b/examples/Unit/PoESP32/TCP_CLIENT/TCP_CLIENT.ino new file mode 100644 index 0000000..afa394c --- /dev/null +++ b/examples/Unit/PoESP32/TCP_CLIENT/TCP_CLIENT.ino @@ -0,0 +1,119 @@ +/* + Description: + Use UNIT PoESP32 to connect TCP server + before compiling: + FastLED: https://github.com/FastLED/FastLED + M5Atom: https://github.com/m5stack/M5Atom + UNIT_PoESP32: https://github.com/m5stack/UNIT_PoESP32 +*/ + +#include "M5Atom.h" +#include "UNIT_PoESP32.h" + +UNIT_PoESP32 eth; + +typedef enum { kConfig = 0, kStart, kConnecting, kConnected } DTUState_t; + +DTUState_t State = kStart; + +void TaskLED(void *pvParameters) { + while (1) { + switch (State) { + case kConfig: + M5.dis.fillpix(0x0000ff); + break; + case kConnecting: + M5.dis.fillpix(0xffff00); + break; + case kConnected: + M5.dis.fillpix(0x00ff00); + break; + case kStart: + M5.dis.fillpix(0xff0000); + break; + } + for (int i = 100; i > 0; i--) { + M5.dis.setBrightness(i); + FastLED.show(); + vTaskDelay(10 / portTICK_RATE_MS); + } + for (int i = 0; i < 100; i++) { + M5.dis.setBrightness(i); + FastLED.show(); + vTaskDelay(10 / portTICK_RATE_MS); + } + } +} + +String readstr; + +uint8_t data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, + 0x06, 0x07, 0x08, 0x09, 0x10}; + +void setup() { + M5.begin(true, false, true); + eth.Init(&Serial2, 9600, 32, 26); + + xTaskCreatePinnedToCore(TaskLED, "TaskLED" // A name just for humans + , + 4096 // This stack size can be checked & adjusted + // by reading the Stack Highwater + , + NULL, + 1 // Priority, with 3 (configMAX_PRIORITIES - 1) + // being the highest, and 0 being the lowest. + , + NULL, 0); + delay(10); + + State = kStart; + + Serial.println("wait device connect"); + while (!eth.checkDeviceConnect()) { + delay(10); + } + + Serial.println("device connected"); + + State = kConnecting; + + Serial.println("wait ethernet connect"); + while (!eth.checkETHConnect()) { + delay(10); + } + Serial.println("ethernet connected"); + + State = kConfig; + + Serial.println("Config TCP Client"); + + // AT+CIPSTART="TCP","192.168.3.102",8080 + Serial.println("wait tcp connect"); + while (!eth.createTCPClient("120.77.157.90", 1883)) { + delay(10); + } + + // while (!eth.configTCPClient("192.168.1.5", 60000)) { + // delay(10); + // } + + Serial.println("tcp connected"); + + if (eth.sendTCPData(data, sizeof(data))) { + State = kConnected; + + } else { + State = kStart; + } +} + +void loop() { + if (Serial.available()) { + char ch = Serial.read(); + Serial2.write(ch); + } + if (Serial2.available()) { + char ch = Serial2.read(); + Serial.write(ch); + } +} diff --git a/library.properties b/library.properties index 18900ee..9078ce1 100644 --- a/library.properties +++ b/library.properties @@ -8,4 +8,4 @@ category=Device Control url=https://github.com/m5stack/M5Atom architectures=esp32 includes=M5Atom.h -depends=FastLED,UNIT_ENV,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,MAX30100lib,MFRC522_I2C,M5GFX,M5_BM8563,M5_ADS1100,M5_ADS1115,M5_FPC1020A,HX711 Arduino Library,PCA9554,TinyGPSPlus-ESP32,Adafruit SGP30 Sensor,FFT,TFTTerminal,ClosedCube TCA9548A,Ethernet2,ESP8266Audio,M5_EzData,ArduinoJson,PubSubClient,UNIT_SONIC,PoE_CAM,M5_RoverC,UNIT_UHF_RFID,M5_JoyC,ATOM_DTU_CAT1 +depends=FastLED,UNIT_ENV,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,MAX30100lib,MFRC522_I2C,M5GFX,M5_BM8563,M5_ADS1100,M5_ADS1115,M5_FPC1020A,HX711 Arduino Library,PCA9554,TinyGPSPlus-ESP32,Adafruit SGP30 Sensor,FFT,TFTTerminal,ClosedCube TCA9548A,Ethernet2,ESP8266Audio,M5_EzData,ArduinoJson,PubSubClient,UNIT_SONIC,PoE_CAM,M5_RoverC,UNIT_UHF_RFID,M5_JoyC,ATOM_DTU_CAT1,UNIT_PoESP32