Skip to content

Commit

Permalink
add poesp32 example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitshaoxiang committed Mar 31, 2022
1 parent 0aacd3a commit 4b980ca
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 1 deletion.
116 changes: 116 additions & 0 deletions examples/Unit/PoESP32/HTTP_CLIENT/HTTP_CLIENT.ino
Original file line number Diff line number Diff line change
@@ -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);
}
}
110 changes: 110 additions & 0 deletions examples/Unit/PoESP32/MQTT_CLIENT/MQTT_CLIENT.ino
Original file line number Diff line number Diff line change
@@ -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);
}
119 changes: 119 additions & 0 deletions examples/Unit/PoESP32/TCP_CLIENT/TCP_CLIENT.ino
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4b980ca

Please sign in to comment.