From 60448f2a05fbc1998b1257eab4d9b718213d8905 Mon Sep 17 00:00:00 2001 From: Jan Michel Date: Thu, 22 Dec 2022 19:26:04 +0100 Subject: [PATCH] add display to read MAC --- .../MdcPowerDistributor.ino | 172 +++++++++++++----- 1 file changed, 123 insertions(+), 49 deletions(-) diff --git a/esp32/EthernetUART/MdcPowerDistributor/MdcPowerDistributor.ino b/esp32/EthernetUART/MdcPowerDistributor/MdcPowerDistributor.ino index ceb2a69..7a26a12 100644 --- a/esp32/EthernetUART/MdcPowerDistributor/MdcPowerDistributor.ino +++ b/esp32/EthernetUART/MdcPowerDistributor/MdcPowerDistributor.ino @@ -3,7 +3,7 @@ Telnet Port 2323 Visual Status via Board LEDs: -ETH Start -> Green LED blink for 1 sec +ETH Start -> Green LED blink once Telnet work -> Green LED on ETH Disconnected/stopped -> Orange LED on */ @@ -14,6 +14,52 @@ void getdata(uint8_t buf); #include #include +//Display +#include +#define SCREEN_ADDRESS 0x3C //< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 +Adafruit_SSD1306 display(128, 32, &Wire, -1); + +#define imageWidth 106 +#define imageHeight 32 +const unsigned char logo_bmp[] PROGMEM = { + 0xfc, 0x0f, 0xc0, 0x03, 0xf8, 0x3f, 0xff, 0xfc, 0x0f, 0xff, 0xff, 0xff, + 0xff, 0x80, 0xfc, 0x0f, 0xc0, 0x07, 0xf8, 0x3f, 0xff, 0xfc, 0x0f, 0xff, + 0xff, 0xff, 0xff, 0x00, 0xfc, 0x0f, 0xc0, 0x0f, 0xf0, 0x3f, 0xff, 0xfc, + 0x0f, 0xff, 0xff, 0xff, 0xfe, 0x40, 0xfc, 0x0f, 0xc0, 0x1f, 0xe0, 0x3f, + 0xff, 0xfc, 0x0f, 0xff, 0xff, 0xff, 0xfc, 0xc0, 0xfc, 0x0f, 0xc0, 0x3f, + 0xc0, 0x3f, 0xff, 0xfc, 0x0f, 0xff, 0xff, 0xff, 0xf1, 0xc0, 0xfc, 0x0f, + 0xc0, 0x3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, 0xc0, 0x01, 0xfc, 0x3f, 0x00, 0x00, 0x0f, 0xff, + 0xff, 0xff, 0xff, 0x80}; //number of clients for this telnet server #define MAX_SRV_CLIENTS 5 @@ -25,62 +71,50 @@ void getdata(uint8_t buf); #define CB1 33 #define CB2 14 #define CB3 32 - +#define LED_GREEN 15 +#define LED_YELLOW 2 WiFiServer server(2323); WiFiClient serverClients[MAX_SRV_CLIENTS]; volatile static bool eth_connected = false; static unsigned long lastTemperatureRead = 0; -uint16_t MEAS_temperature = 0xab12; +uint16_t MEAS_temperature = 0; uint8_t CONF_GPIO = 0; uint8_t rxcnt = 0, txpoint = 0; uint8_t rxbuf[15]; uint8_t txbuf[12]; +uint8_t firstrun = 0; void WiFiEvent(WiFiEvent_t event) { switch (event){ case ARDUINO_EVENT_ETH_START: -// Serial.println("ETH Started"); //set eth hostname here - digitalWrite(2,LOW); - digitalWrite(15,HIGH); + digitalWrite(LED_YELLOW,LOW); + digitalWrite(LED_GREEN,HIGH); delay(500); - digitalWrite(15, LOW); + digitalWrite(LED_GREEN, LOW); ETH.setHostname("esp32-ethernet"); break; case ARDUINO_EVENT_ETH_CONNECTED: -// Serial.println("ETH Connected"); - digitalWrite(2,LOW); - digitalWrite(15,HIGH); + digitalWrite(LED_YELLOW,LOW); + digitalWrite(LED_GREEN,HIGH); break; case ARDUINO_EVENT_ETH_GOT_IP: -// Serial.print("ETH MAC: "); -// Serial.print(ETH.macAddress()); -// Serial.print(", IPv4: "); -// Serial.print(ETH.localIP()); -// if (ETH.fullDuplex()) { -// Serial.print(", FULL_DUPLEX"); -// } -// Serial.print(", "); -// Serial.print(ETH.linkSpeed()); -// Serial.println("Mbps"); eth_connected = true; break; case ARDUINO_EVENT_ETH_DISCONNECTED: -// Serial.println("ETH Disconnected"); - digitalWrite(15,LOW); - digitalWrite(2,HIGH); + digitalWrite(LED_GREEN,LOW); + digitalWrite(LED_YELLOW,HIGH); eth_connected = false; break; case ARDUINO_EVENT_ETH_STOP: -// Serial.println("ETH Stopped"); - digitalWrite(15,LOW); - digitalWrite(2,HIGH); + digitalWrite(LED_GREEN,LOW); + digitalWrite(LED_YELLOW,HIGH); eth_connected = false; break; @@ -89,9 +123,51 @@ void WiFiEvent(WiFiEvent_t event) { } } +void start_screen() { + display.clearDisplay(); + display.ssd1306_command(SSD1306_SETCONTRAST); + display.ssd1306_command(0); // val=0 := 0% up to val = 255 := 100% + display.setTextSize(1); // Normal 1:1 pixel scale + display.setTextColor(SSD1306_WHITE); // Draw white text + display.cp437(true); // Use full 256 char 'Code Page 437' font + + display.drawBitmap(0, 0, logo_bmp, imageWidth, imageHeight, SSD1306_WHITE); + display.setCursor(108, 0); + display.write("ETH"); + display.setCursor(108, 8); + display.write("MDC"); + display.setCursor(108, 16); + display.write("POW"); + display.setCursor(108, 24); + display.write("JM"); + display.display(); + } + +void connection_screen() { + // show all connection relevant parameters + display.clearDisplay(); + display.setTextSize(1); // Normal 1:1 pixel scale + display.setTextColor(SSD1306_WHITE); // Draw white text + display.cp437(true); // Use full 256 char 'Code Page 437' font + + display.setCursor(0, 0); + display.write("ETH MAC / IP"); + display.setCursor(25, 8); + display.print(ETH.macAddress()); + display.setCursor(25, 16); + display.print(ETH.localIP()); + + display.setCursor(25, 24); + display.print((int8_t)(MEAS_temperature/100)); + display.print('.'); + display.print((uint8_t)(MEAS_temperature/10)%10); + display.print(" C"); + display.display(); +} + void setup() { - pinMode(2, OUTPUT); // yellow LED - pinMode(15, OUTPUT); // green LED + pinMode(LED_YELLOW, OUTPUT); // yellow LED + pinMode(LED_GREEN, OUTPUT); // green LED pinMode(CB0, OUTPUT); pinMode(CB1, OUTPUT); @@ -100,7 +176,6 @@ void setup() { //init UART Serial.begin(57600); -// Serial.println("\nConnecting"); //init temperature sensor Wire.begin(SDA,SCL); @@ -112,6 +187,10 @@ void setup() { Wire.write((uint8_t)0x00); Wire.endTransmission(); + //init display + display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, false, false); + start_screen(); + delay(1000); //start the telnet server WiFi.onEvent(WiFiEvent); @@ -119,15 +198,14 @@ void setup() { ETH.begin(1,17,23,18,ETH_PHY_LAN8720,ETH_CLOCK_GPIO0_IN); server.begin(); server.setNoDelay(true); + connection_screen(); while (eth_connected == false); + connection_screen(); -// Serial.print("ready! Use 'telnet "); -// Serial.print(ETH.localIP()); -// Serial.println(" 2323' to connect"); //visual status for telnet work - digitalWrite(2,LOW); - digitalWrite(15,HIGH); + digitalWrite(LED_YELLOW,LOW); + digitalWrite(LED_GREEN,HIGH); } void loop() { @@ -140,10 +218,6 @@ void loop() { if (!serverClients[i] || !serverClients[i].connected()) { if(serverClients[i]) serverClients[i].stop(); serverClients[i] = server.available(); -// if (!serverClients[i]) Serial.println("available broken"); -// Serial.print("New client: "); -// Serial.print(i); Serial.print(' '); -// Serial.println(serverClients[i].remoteIP()); break; } } @@ -194,14 +268,16 @@ void loop() { byte MSB = Wire.read(); byte LSB = Wire.read(); MEAS_temperature = (((int32_t) MSB << 8) | (int32_t) LSB)*100/256; + if(firstrun) connection_screen(); + firstrun = 1; } } /****************************** * Helper functions * ****************************/ +// convert integer or nibbles into hex value uint8_t nib_to_hex(uint16_t in, uint8_t nib) { - // convert integer or nibbles into hex value uint8_t t = (in >> (nib * 4)) & 0xF; if (t <= 9) { return t + 0x30; @@ -209,22 +285,19 @@ uint8_t nib_to_hex(uint16_t in, uint8_t nib) { return t - 10 + 0x61; } -uint8_t hex_to_int(uint8_t h) {//assumes valid number - // convert hex value to integer +// convert hex value to integer, assumes valid number +uint8_t hex_to_int(uint8_t h) { if (h < 0x40) return h - 0x30; if (h < 0x50) return h - 0x41 + 10; return h - 0x61 + 10; } - - - //Forward message to UART void forward_msg(uint8_t i) { Serial.write(rxbuf,i); } - +//accepts address or forward message uint8_t is_my_address(uint8_t s) { if ((rxbuf[1] == 'F' || rxbuf[1] == 'f') && (rxbuf[2] == 'F' || rxbuf[2] == 'f')) { return 1; @@ -276,19 +349,20 @@ void getdata(uint8_t buf) { forward_msg(rxcnt); } else if (rxcnt == 11 && is_my_address(11)) {// message with correct length is for this uC + uint8_t regnumber = hex_to_int(rxbuf[5]); if (rxbuf[0] == 'R') { - if (hex_to_int(rxbuf[5]) == 4) { //Register 4: Temperature in 1/100 °C + if (regnumber == 4) { //Register 4: Temperature in 1/100 °C send_answer_hex(&rxbuf[0], MEAS_temperature); } - if (hex_to_int(rxbuf[5]) == 5) { //Register 5: Information + if (regnumber == 5) { //Register 5: Information send_answer_hex(&rxbuf[0], FIRMWARE_VERSION << 4); } - if (hex_to_int(rxbuf[5]) == 0xa) { //Register A: 4 GPIO pins + if (regnumber == 0xa) { //Register A: 4 GPIO pins send_answer_hex(&rxbuf[0], CONF_GPIO); } } if (rxbuf[0] == 'W') { - if (hex_to_int(rxbuf[5]) == 0xa) { //Register A: 4 GPIO pins + if (regnumber == 0xa) { //Register A: 4 GPIO pins CONF_GPIO = hex_to_int(rxbuf[9]); send_answer_hex(&rxbuf[0], CONF_GPIO); (CONF_GPIO & 1) ? digitalWrite(CB0,HIGH):digitalWrite(CB0,LOW); -- 2.43.0