From 87056143590b609930dec9da9acbbc910d089534 Mon Sep 17 00:00:00 2001 From: Ole Artz Date: Thu, 19 Aug 2021 09:55:23 +0200 Subject: [PATCH] LANTelnetTOI2C_Board: add a 0.91 OLED Display via I2C --- .../LANTelnetToI2C_Board.ino | 300 ++++++++++++++---- 1 file changed, 241 insertions(+), 59 deletions(-) diff --git a/esp32/EthernetUART/LANTelnetToI2C_Board/LANTelnetToI2C_Board.ino b/esp32/EthernetUART/LANTelnetToI2C_Board/LANTelnetToI2C_Board.ino index 1123eef..6088267 100644 --- a/esp32/EthernetUART/LANTelnetToI2C_Board/LANTelnetToI2C_Board.ino +++ b/esp32/EthernetUART/LANTelnetToI2C_Board/LANTelnetToI2C_Board.ino @@ -8,7 +8,7 @@ # ### ### # # ### author: O.Artz ### # # ### ### # -# ### UPDATE 2021-06-17 ### # +# ### UPDATE 2021-07-01 ### # # ### ### # ##################################################### @@ -26,12 +26,66 @@ ETH Disconnected/stopped -> Orange LED on #include //for Telnet Server Connection #include //for I2C Connection #include //for external ADC +//__________________________________________ +#include //for display control +#include //for OLED display driver + +//for screen +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 32 // OLED display height, in pixels + +// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) +// The pins for I2C are defined by the Wire-library. +// On an arduino UNO: A4(SDA), A5(SCL) +// On an arduino MEGA 2560: 20(SDA), 21(SCL) +// On an arduino LEONARDO: 2(SDA), 3(SCL), ... +#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) +#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +#define NUMFLAKES 10 // Number of snowflakes in the animation example + +#define imageWidth 106 +#define imageHeight 32 + +const unsigned char logo_bmp [] PROGMEM = +{ +0xfc, 0x0f, 0xc0, 0x03, 0xf8, 0x3f, 0xff, 0x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f, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xf3, 0xc0, 0xfc, 0x0f, 0xc0, 0x07, 0xf0, 0x3f, + 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xf9, 0xc0, 0xfc, 0x0f, 0xc0, 0x07, 0xf0, 0x3f, 0x00, 0x00, + 0x0f, 0xff, 0xff, 0xff, 0xfc, 0xc0, 0xfc, 0x0f, 0xc0, 0x03, 0xf8, 0x3f, 0x00, 0x00, 0x0f, 0xff, + 0xff, 0xff, 0xfe, 0x40, 0xfc, 0x0f, 0xc0, 0x03, 0xfc, 0x3f, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, + 0xff, 0x00, 0xfc, 0x0f, 0xc0, 0x01, 0xfc, 0x3f, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80 +}; +//__________________________________________ //how many clients should be able to telnet to this ESP32 #define MAX_SRV_CLIENTS 10 -#define SDA 32 -#define SCL 5 +#define SDA 5 +#define SCL 32 #define ISMYADDR() (rxbuf[1] == '0' && (rxbuf[2] == '0' )) //|| rxbuf[2] == '1' || rxbuf[2] == '2' || rxbuf[2] == '3') ) @@ -43,7 +97,18 @@ WiFiClient serverClients[MAX_SRV_CLIENTS]; //used to check the number every time, cause could changed volatile static bool eth_connected = false; -uint8_t val = 2; //chose as default for external adc setting +uint8_t rxcnt = 0, txpoint = 0; +uint8_t rxbuf[11]; +uint8_t txbuf[12]; +uint8_t val, new_gain; +int16_t adc0, adc1, adc2, adc3; +float fadc0, fadc1, fadc2, fadc3; +uint8_t setGAIN = 0xFFFF; +float setADCconversion; +float acGAIN[] = {0.1875, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125}; +adsGain_t asetGAIN[] = {GAIN_TWOTHIRDS, GAIN_ONE, GAIN_TWO, GAIN_FOUR, GAIN_EIGHT, GAIN_SIXTEEN}; +int counter = 0; + void WiFiEvent(WiFiEvent_t event) { @@ -97,8 +162,7 @@ void WiFiEvent(WiFiEvent_t event) } } -void setup() -{ +void setup(){ //for Telnet Server Connection pinMode(2, OUTPUT); // yellow LED pinMode(15, OUTPUT); // green LED @@ -116,13 +180,15 @@ void setup() 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); //for I2C Connection Wire.begin(SDA, SCL); // Initiate the Wire library -//for external ADC + + //Setting for external ADC measurment range // val ADS1015 ADS1115 // ------- ------- // 0 ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV @@ -132,12 +198,110 @@ void setup() // 4 ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV // 5 ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV - adsGain_t asetGAIN[] = {GAIN_TWOTHIRDS, GAIN_ONE, GAIN_TWO, GAIN_FOUR, GAIN_EIGHT, GAIN_SIXTEEN}; - ads.setGain(asetGAIN[val]); + val = 2; //chose as default for external adc setting + set_adc_gain(val); + + Serial.print("\nADC Settings"); + Serial.print("\nActual Gainfactor: "); Serial.print(asetGAIN[val]); + Serial.print("\nConversionfactor: "); Serial.print(setADCconversion); Serial.println(); + +//for display__________________________________________________________ + restart_screen(); + start_screen(); + //test_screen(); } -void loop() -{ +void restart_screen() { + // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally + if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, false, false)) { + Serial.println(F("SSD1306 allocation failed")); + for(;;); // Don't proceed, loop forever + } + + // Show initial display buffer contents on the screen -- + // the library initializes this with an Adafruit splash screen. + //display.display(); + + delay(1000); // Pause for 2 seconds + display.clearDisplay(); +} + +void start_screen() { + 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.drawBitmap(0, 0, logo_bmp, imageWidth, imageHeight, SSD1306_WHITE); + display.setCursor(108, 0); + display.write("ETH"); + display.setCursor(108, 8); + display.write("ADC"); + display.setCursor(108, 16); + display.write("J.M."); + display.setCursor(108, 24); + display.write("O.A."); + + display.display(); + delay(3000); // Pause for 2 seconds + } + +void test_screen() { + display.clearDisplay(); + display.setTextSize(1); // Normal 1:1 pixel scale + display.setTextColor(SSD1306_WHITE); // Draw white text + display.setCursor(0, 0); + display.cp437(true); // Use full 256 char 'Code Page 437' font + display.write("CHECKER"); + display.display(); + display.startscrollright(0x00, 0x07); + delay(4100); + display.stopscroll(); +} + +void show_CH() { + display.clearDisplay(); + + display.setTextSize(2); // 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("CH1"); + display.setCursor(SCREEN_WIDTH*5/8, 0); + display.write("CH3"); + display.setCursor(0, SCREEN_HEIGHT/2); + display.write("CH2"); + display.setCursor(SCREEN_WIDTH*5/8, SCREEN_HEIGHT/2); + display.write("CH4"); + display.setCursor(SCREEN_WIDTH*10/28, SCREEN_HEIGHT*1/4); + display.write("mV"); + delay(500); + display.display(); + +} + +void show_VAL(int dadc0, int dadc1, int dadc2, int dadc3) { + display.clearDisplay(); + + display.setTextSize(2); // 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.print(dadc0); + display.setCursor(SCREEN_WIDTH*5/8, 0); + display.print(dadc2); + display.setCursor(0, SCREEN_HEIGHT/2 ); + display.print(dadc1); + display.setCursor(SCREEN_WIDTH*5/8, SCREEN_HEIGHT/2 ); + display.print(dadc3); + display.setCursor(SCREEN_WIDTH*10/28, SCREEN_HEIGHT*1/4); + display.write("mV"); + delay(1000); + display.display(); + +} + +void loop(){ //for Telnet Server Connection uint8_t i; if (eth_connected == true) { @@ -161,13 +325,20 @@ void loop() } } //check clients for data + String getstring; + for(i = 0; i < MAX_SRV_CLIENTS; i++) { if (serverClients[i] && serverClients[i].connected()) { if(serverClients[i].available()) { //get data from the telnet client and push it to the UART // while(serverClients[i].available()) Serial.write(serverClients[i].read()); //get data from the telnet client and push it to I2C - while(serverClients[i].available()) Wire.write(serverClients[i].read()); + while(serverClients[i].available()) {getstring = serverClients[i].read(); //<------ Hier ist der Fehler mit getstring + Serial.print("\nGETDATASTRING: "); Serial.print(getstring); + for(uint8_t m=0; m < getstring.length(); m++){ + getdata(getstring.charAt(m)); + } + } } } else { @@ -176,31 +347,27 @@ void loop() } } } - //check UART for data -/* if(Serial.available()) { - size_t len = Serial.available(); - uint8_t sbuf[len]; -*/ // Serial.readBytes(sbuf, len); - //check I2C for data - if(Wire.available()) { - size_t len = Wire.available(); - uint8_t sbuf[len]; - } } - -//for external ADC - int16_t adc0, adc1, adc2, adc3; - + + get_adc_val(); //get the actual measured values from adc + + delay(1000); + } + +void set_adc_gain(uint8_t new_gain) { + //Set the ADC Gain parameter and choose the right conversion factor + ads.setGain(asetGAIN[new_gain]); + + setADCconversion = acGAIN[new_gain]; + } + +void get_adc_val() { + //Get the actual ADC measurment values in dimension of mV adc0 = ads.readADC_SingleEnded(0); adc1 = ads.readADC_SingleEnded(1); adc2 = ads.readADC_SingleEnded(2); adc3 = ads.readADC_SingleEnded(3); - float fadc0, fadc1, fadc2, fadc3; - - float acGAIN[] = {0.1875, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125}; - float setADCconversion = acGAIN[val]; - fadc0 = adc0 * setADCconversion; fadc1 = adc1 * setADCconversion; fadc2 = adc2 * setADCconversion; @@ -211,24 +378,33 @@ void loop() String sadc2 = String(fadc2); String sadc3 = String(fadc3); -//sbuf als textstring definieren mit den werten wie serial.print - String sbuf = String("\nAIN0: " + sadc0 + "mV\n" + "AIN1: " + sadc1 + "mV\n" + "AIN2: " + sadc2 + "mV\n" + "AIN3: " + sadc3 + "mV\n"); + int dadc0 = fadc0; + int dadc1 = fadc1; + int dadc2 = fadc2; + int dadc3 = fadc3; + + String sbuf = String("\nAIN0: " + sadc0 + "mV\n" + "AIN1: " + sadc1 + "mV\n" + "AIN2: " + sadc2 + "mV\n" + "AIN3: " + sadc3 + "mV\n "); - Serial.print(sbuf); + Serial.print(sbuf); // for serial output size_t len = sbuf.length(); uint8_t buf[len]; sbuf.toCharArray((char*)buf, len); - send_answer_buf(buf, len); - -delay(1000); -} + counter += 1; + if (counter == 1) { + show_CH(); + } + else if (counter == 4) { + counter = 0; + } + else { + show_VAL(dadc0, dadc1, dadc2, dadc3); + } -uint8_t rxcnt = 0, txpoint = 0; -uint8_t rxbuf[11]; -uint8_t txbuf[12]; + send_answer_buf(buf, len); //for telnet output +} uint8_t nib_to_hex(uint16_t in, uint8_t nib) { uint8_t t = (in >> (nib*4)) & 0xF; @@ -273,7 +449,7 @@ void send_answer_hex(uint8_t* rxbuf, uint16_t v) { txbuf[10]='\n'; txbuf[11] = 0; size_t len = 12; -// send_answer_buf(txbuf, len); + send_answer_buf(txbuf, len); } void forward_msg(uint8_t i) { @@ -294,15 +470,19 @@ uint8_t is_my_address(uint8_t s) { } void getdata(uint8_t buf) { + // get data by register mode if (rxcnt != 0 || (buf == 'A' || buf == 'W' || buf == 'R')) { rxbuf[rxcnt++] = buf; } if (buf == '\n' || buf == '\r') { //End of Command - if (rxbuf[0] == 'A') { + //answer + if (rxbuf[0] == 'A') { + memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,10); txbuf[11] = 0; -// send_answer_buf(txbuf); + size_t len = 12; + send_answer_buf(txbuf, len); } else if (rxbuf[0] == 'S') { // Scann of chain, returns number of boards txbuf[0] = 'S'; uint8_t length = rxcnt - 2 ; // Length of Counter @@ -327,27 +507,29 @@ void getdata(uint8_t buf) { if (length > 8) length = 8; // skip to keep a clean ending of message txbuf[length+1] = 10; txbuf[length+2] = 0; -// send_answer_buf(txbuf); + send_answer_buf(txbuf, length); rxcnt = 0; } else if (is_my_address(10)){ // message is for this uC + +//write if (rxbuf[0] == 'W'){ -//write - //ADCsettings + Serial.print("WRITECHECK"); + //ADCsettings if (hex_to_int(rxbuf[5]) == 9) { - uint8_t val = (hex_to_int(rxbuf[7])*16+hex_to_int(rxbuf[8]))&0xFF; - uint8_t setGAIN = 0xFFFF; - adsGain_t asetGAIN[] = {GAIN_TWOTHIRDS, GAIN_ONE, GAIN_TWO, GAIN_FOUR, GAIN_EIGHT, GAIN_SIXTEEN}; - setGAIN = asetGAIN[val]; - float setADCconversion; - float acGAIN[] = {0.1875, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125}; - setADCconversion = acGAIN[val]; -// send_answer_hex(&rxbuf[0], setGAIN); +// uint8_t val = (hex_to_int(rxbuf[7])*16+hex_to_int(rxbuf[8]))&0xFF; + new_gain = (hex_to_int(rxbuf[7])*16+hex_to_int(rxbuf[8]))&0xFF; + + set_adc_gain(new_gain); + + send_answer_hex(&rxbuf[0], new_gain); } - if (rxbuf[0] == 'R'){ + //read - //read ADC_value + if (rxbuf[0] == 'R'){ + Serial.print("READCHECK"); + if (hex_to_int(rxbuf[5]) == 10) { - float fadc0, fadc1, fadc2, fadc3; + //read single ADC Channel uint8_t adc_val = 0xFFFF; //CH0 if (hex_to_int(rxbuf[4]) == 0) {adc_val=fadc0; @@ -361,7 +543,7 @@ void getdata(uint8_t buf) { //CH3 if (hex_to_int(rxbuf[4]) == 3) {adc_val=fadc3; } -// send_answer_hex(&rxbuf[0], adc_val); + send_answer_hex(&rxbuf[0], adc_val); } } } -- 2.43.0