# ### ### #
# ### author: O.Artz ### #
# ### ### #
-# ### UPDATE 2022-06-21 ### #
+# ### UPDATE 2022-07-19 ### #
# ### ### #
#####################################################
ETH Disconnected/stopped -> Orange LED on
*/
-// load needed Libaries
+/*
+ * #################################### load libraries
+ */
#include <Adafruit_ADS1015.h> //for external ADC
#include <ETH.h> //for Telnet Server Connection
#include <Wire.h> //for I2C Connection
#include <Adafruit_SSD1306.h> //for OLED display driver
#include <SPI.h> //for display control
-// define the number of bytes you want to access
+/*
+ * #################################### define parameters to fixed values
+ */
/*
* Address Value
* 0x11 ADC measurment range
* 0x35, 0x36 Offset CH 2
* 0x37, 0x38 Offset CH 3
*/
+// define the number of bytes you want to access
#define EEPROM_SIZE 512
-// for screen
+// Display
#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
+#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, 0xfc, 0x0f, 0xff, 0xff, 0xff,
- 0xff, 0x80, 0xfc, 0x0f, 0xc0, 0x07, 0xf8, 0x3f, 0xff, 0xfc, 0x0f, 0xff,
- 0xff, 0xff, 0xff, 0x00, 0xfc, 0x0f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 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
+//number of clients for this telnet server
#define MAX_SRV_CLIENTS 10
#define SDA 5
WiFiServer server(2323);
WiFiClient serverClients[MAX_SRV_CLIENTS];
+/*
+ * #################################### declare all needed variables
+ */
+
// used to check the number every time, cause could changed
volatile static bool eth_connected = false;
+//reset of the EEPROM
+bool eeprom_reset = false; // =true for reset
+
bool show_connection = true;
uint8_t rxcnt = 0, txpoint = 0;
uint8_t rxbuf[11], txbuf[12];
String possible_units[] = {"mV", "mA", "mbar", "mPa" "degC"};
String unit;
+/*
+ * #################################### communication functions
+ */
void WiFiEvent(WiFiEvent_t event) {
// ensure the ethernet connection (status)
switch (event) {
digitalWrite(15, HIGH);
}
-void setup() {
- //initalize the board
- start_telnet();
- // for I2C Connection
- Wire.begin(SDA, SCL); // Initiate the Wire library
-
- // for display__________________________________________________________
- restart_screen();
- start_screen();
- connection_screen();
- // test_screen();
-
- EEPROM.begin(EEPROM_SIZE);
-//Setting for external ADC measurment range
-// val ADS1015 ADS1115
-// ------- -------
-// 0 ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV
-// 1 ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
-// 2 ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
-// 3 ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
-// 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
-
-// new_gain = 2; // chose as default for external adc setting
- new_gain = EEPROM.read(0x11);
- set_adc_gain(new_gain);
-
-// Chose right Unit
-// val Unit
-// 0 mV
-// 1 mA
-// 2 mbar
-// 3 mPa
-// 4 degC
-
-// new_unit = 0; //default
- new_unit = EEPROM.read(0x12);
- set_unit(new_unit);
-
-// Device Transformationfactor
-// new_dev_trans_fac = 1000, //default
- new_dev_trans_fac = EEPROM.read(0x13) | EEPROM.read(0x14) << 8;
- set_device_transformation_factor(new_dev_trans_fac);
-
-// Input voltage divider
-// new_vol_div_fac = 1000; //default w/o voltage devider, 1000 cause will devider by 1000 to sent float by telnet
- new_vol_div_fac = EEPROM.read(0x21) | EEPROM.read(0x22) << 8;
- set_voltage_divider_factor(new_vol_div_fac);
-
-// Channel Offset
-// new_offset_val = 0; //default
-// CH 0
- new_offset_val = EEPROM.read(0x31) | EEPROM.read(0x32) << 8;
- chan_offset_num = 0;
- set_offset(new_offset_val, chan_offset_num);
-// CH 1
- new_offset_val = EEPROM.read(0x33) | EEPROM.read(0x34) << 8;
- chan_offset_num = 1;
- set_offset(new_offset_val, chan_offset_num);
-// CH 2
- new_offset_val = EEPROM.read(0x35) | EEPROM.read(0x36) << 8;
- chan_offset_num = 2;
- set_offset(new_offset_val, chan_offset_num);
-// CH 3
- new_offset_val = EEPROM.read(0x37) | EEPROM.read(0x38) << 8;
- chan_offset_num = 3;
- set_offset(new_offset_val, chan_offset_num);
+void send_answer_buf(uint8_t *buf, size_t len) {
+ uint8_t i;
+ for (i = 0; i < MAX_SRV_CLIENTS; i++) {
+ if (serverClients[i] && serverClients[i].connected()) {
+ serverClients[i].write(buf, len);
+ delay(1);
+ }
+ }
+}
- Serial.print("\nADC Settings");
- Serial.print("\nActual Gainfactor: ");
- Serial.print(asetGAIN[new_gain]);
- Serial.print("\nConversionfactor: ");
- Serial.print(setADCconversion);
- Serial.println();
+void send_answer_hex(uint8_t *rxbuf, uint16_t v) {
+ txbuf[0] = 'A';
+ txbuf[1] = rxbuf[1];
+ txbuf[2] = rxbuf[2];
+ txbuf[3] = rxbuf[3]; // Switch deactivated
+ txbuf[4] = rxbuf[4];
+ txbuf[5] = rxbuf[5];
+ txbuf[6] = nib_to_hex(v, 3);
+ txbuf[7] = nib_to_hex(v, 2);
+ txbuf[8] = nib_to_hex(v, 1);
+ txbuf[9] = nib_to_hex(v, 0);
+ txbuf[10] = '\n';
+ txbuf[11] = 0;
+ size_t len = 12;
+ send_answer_buf(txbuf, len);
}
+/*
+ * #################################### display functions
+ */
+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, 0x3f, 0x80, 0x3f, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xe7, 0xc0,
+ 0xfc, 0x0f, 0xc0, 0x7f, 0x00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00, 0x00, 0x0f, 0xff,
+ 0xff, 0xff, 0xff, 0x80
+};
+
void restart_screen() {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, false, false)) {
delay(5000); // Pause for 5 seconds
}
-void test_screen() {
- // used as a test case
- 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(String unit) {
// show the position of the measured adc values by their label name and the acutally set unit globaly
display.clearDisplay();
display.display();
}
-void loop() {
- // for recieving and transmitting by Telnet Server Connection
- uint8_t i;
- if (eth_connected == true) {
- // check if there are any new clients
- if (server.hasClient()) {
- for (i = 0; i < MAX_SRV_CLIENTS; i++) {
- // find free/disconnected spot
- 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("\nNew client: ");
- Serial.print(i);
- Serial.print(' ');
- Serial.println(serverClients[i].remoteIP());
- break;
- }
- }
- if (i >= MAX_SRV_CLIENTS) {
- // no free/disconnected spot so reject
- server.available().stop();
- }
- }
- // 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 I2C
- while (serverClients[i].available()) {
- getstring = char(serverClients[i].read());
- for (uint8_t m = 0; m < getstring.length(); m++) {
- getdata(getstring.charAt(m));
- }
- }
- }
- } else {
- if (serverClients[i]) {
- serverClients[i].stop();
- }
- }
- }
- }
-
- get_adc_val(); // get the actual measured values from adc
-
- delay(100);
-}
-
+/*
+ * #################################### setting functions
+ */
void set_offset(int16_t new_offset_val, int chan_offset_num) {
// to calibrate the channels with an offset value
if (chan_offset_num == 0) {
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);
-
- fadc0 = adc0 * setADCconversion * vol_div_fac * dev_trans_fac + offset0;
- fadc1 = adc1 * setADCconversion * vol_div_fac * dev_trans_fac + offset1;
- fadc2 = adc2 * setADCconversion * vol_div_fac * dev_trans_fac + offset2;
- fadc3 = adc3 * setADCconversion * vol_div_fac * dev_trans_fac + offset3;
-
- String sadc0 = String(fadc0);
- String sadc1 = String(fadc1);
- String sadc2 = String(fadc2);
- String sadc3 = String(fadc3);
-
- int dadc0 = fadc0;
- int dadc1 = fadc1;
- int dadc2 = fadc2;
- int dadc3 = fadc3;
-
- //String unit = possible_units[new_unit];
- String sbuf = String("\nAIN0: " + sadc0 + ' ' + unit + "\n" + "AIN1: " + sadc1 + ' ' + unit + "\n" + "AIN2: " + sadc2 + ' ' + unit + "\n" + "AIN3: " + sadc3 + ' ' + unit + "\n ");
-
- Serial.print(sbuf); // for serial output
-
- size_t len = sbuf.length();
-
- uint8_t buf[len];
- sbuf.toCharArray((char *)buf, len); // Copies the String’s characters to the supplied buffer.
-
- counter += 1;
- if (counter == 1) {
- if (eth_connected == true) {
- if (show_connection == true) {
- connection_screen();
- show_connection = false;
- }
- }
- show_CH(unit);
- } else if (counter == 100) {
- counter = 0;
- } else {
- show_VAL(dadc0, dadc1, dadc2, dadc3, unit);
- }
-
- if (eth_connected == false) {
- show_connection = true;
- }
-
- send_answer_buf(buf, len); // for telnet continous output
-}
-
+/*
+ * #################################### converter
+ */
uint8_t nib_to_hex(uint16_t in, uint8_t nib) {
uint8_t t = (in >> (nib * 4)) & 0xF;
if (t <= 9) {
*c2 = nib_to_hex(b, 0);
}
-void send_answer_buf(uint8_t *buf, size_t len) {
- uint8_t i;
- for (i = 0; i < MAX_SRV_CLIENTS; i++) {
- if (serverClients[i] && serverClients[i].connected()) {
- serverClients[i].write(buf, len);
- delay(1);
- }
- }
-}
-
-void send_answer_hex(uint8_t *rxbuf, uint16_t v) {
- txbuf[0] = 'A';
- txbuf[1] = rxbuf[1];
- txbuf[2] = rxbuf[2];
- txbuf[3] = rxbuf[3]; // Switch deactivated
- txbuf[4] = rxbuf[4];
- txbuf[5] = rxbuf[5];
- txbuf[6] = nib_to_hex(v, 3);
- txbuf[7] = nib_to_hex(v, 2);
- txbuf[8] = nib_to_hex(v, 1);
- txbuf[9] = nib_to_hex(v, 0);
- txbuf[10] = '\n';
- txbuf[11] = 0;
- size_t len = 12;
- send_answer_buf(txbuf, len);
-}
-
+/*
+ * #################################### chaining
+ */
void forward_msg(uint8_t i) {
sub1(&rxbuf[1], &rxbuf[2]);
memcpy((uint8_t *)txbuf, (uint8_t *)rxbuf, i);
return 0;
}
}
+/*
+ * #################################### head functions
+ */
void getdata(uint8_t buf) {
// get data by register mode
if (rxcnt >= 10 || buf == '\n' || buf == '\r') {
rxcnt = 0;}
}
+
+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);
+
+ fadc0 = adc0 * setADCconversion * vol_div_fac * dev_trans_fac + offset0;
+ fadc1 = adc1 * setADCconversion * vol_div_fac * dev_trans_fac + offset1;
+ fadc2 = adc2 * setADCconversion * vol_div_fac * dev_trans_fac + offset2;
+ fadc3 = adc3 * setADCconversion * vol_div_fac * dev_trans_fac + offset3;
+
+ String sadc0 = String(fadc0);
+ String sadc1 = String(fadc1);
+ String sadc2 = String(fadc2);
+ String sadc3 = String(fadc3);
+
+ int dadc0 = fadc0;
+ int dadc1 = fadc1;
+ int dadc2 = fadc2;
+ int dadc3 = fadc3;
+
+ //String unit = possible_units[new_unit];
+ String sbuf = String("\nAIN0: " + sadc0 + ' ' + unit + "\n" + "AIN1: " + sadc1 + ' ' + unit + "\n" + "AIN2: " + sadc2 + ' ' + unit + "\n" + "AIN3: " + sadc3 + ' ' + unit + "\n ");
+
+ Serial.print(sbuf); // for serial output
+
+ size_t len = sbuf.length();
+
+ uint8_t buf[len];
+ sbuf.toCharArray((char *)buf, len); // Copies the String’s characters to the supplied buffer.
+
+ counter += 1;
+ if (counter == 1) {
+ if (eth_connected == true) {
+ if (show_connection == true) {
+ connection_screen();
+ show_connection = false;
+ }
+ }
+ show_CH(unit);
+ } else if (counter == 100) {
+ counter = 0;
+ } else {
+ show_VAL(dadc0, dadc1, dadc2, dadc3, unit);
+ }
+
+ if (eth_connected == false) {
+ show_connection = true;
+ }
+
+ send_answer_buf(buf, len); // for telnet continous output
+}
+
+void setup() {
+ //initalize the board
+ start_telnet();
+ // for I2C Connection
+ Wire.begin(SDA, SCL); // Initiate the Wire library
+
+ // for display__________________________________________________________
+ restart_screen();
+ start_screen();
+ connection_screen();
+ // test_screen();
+
+ // eeprom
+ EEPROM.begin(EEPROM_SIZE);
+
+ if (eeprom_reset == 1) {
+ for (int i = 0 ; i < EEPROM.length() ; i++) {
+ EEPROM.write(i, 0);
+ }
+ }
+
+/* Setting for external ADC measurment range
+ val ADS1015 ADS1115
+ ------- -------
+ 0 ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV
+ 1 ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
+ 2 ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
+ 3 ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
+ 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
+*/
+
+// new_gain = 2; // chose as default for external adc setting
+ new_gain = EEPROM.read(0x11);
+ if (new_gain == 0xff) {
+ new_gain = 1;
+ }
+ set_adc_gain(new_gain);
+
+/* Chose right Unit
+ val Unit
+ 0 mV
+ 1 mA
+ 2 mbar
+ 3 mPa
+ 4 degC
+*/
+ new_unit = EEPROM.read(0x12);
+ if (new_unit == 0xff) {
+ new_unit = 0; //default
+ }
+ set_unit(new_unit);
+
+// Device Transformationfactor
+ new_dev_trans_fac = EEPROM.read(0x13) | EEPROM.read(0x14) << 8;
+ if (new_dev_trans_fac == 0 || new_dev_trans_fac == 0xff) {
+ new_dev_trans_fac = 1000; //default
+ }
+ set_device_transformation_factor(new_dev_trans_fac);
+
+// Input voltage divider
+ new_vol_div_fac = EEPROM.read(0x21) | EEPROM.read(0x22) << 8;
+ if (new_vol_div_fac == 0 || new_vol_div_fac == 0xff) {
+ new_vol_div_fac = 1000; //default w/o voltage devider, 1000 cause will devider by 1000 to sent float by telnet
+ }
+ set_voltage_divider_factor(new_vol_div_fac);
+
+// Channel Offset
+// new_offset_val = 0; //default
+ // CH 0
+ new_offset_val = EEPROM.read(0x31) | EEPROM.read(0x32) << 8;
+ chan_offset_num = 0;
+ set_offset(new_offset_val, chan_offset_num);
+ // CH 1
+ new_offset_val = EEPROM.read(0x33) | EEPROM.read(0x34) << 8;
+ chan_offset_num = 1;
+ set_offset(new_offset_val, chan_offset_num);
+ // CH 2
+ new_offset_val = EEPROM.read(0x35) | EEPROM.read(0x36) << 8;
+ chan_offset_num = 2;
+ set_offset(new_offset_val, chan_offset_num);
+ // CH 3
+ new_offset_val = EEPROM.read(0x37) | EEPROM.read(0x38) << 8;
+ chan_offset_num = 3;
+ set_offset(new_offset_val, chan_offset_num);
+
+ Serial.print("\nADC Settings");
+ Serial.print("\nActual Gainfactor: ");
+ Serial.print(asetGAIN[new_gain]);
+ Serial.print("\nConversionfactor: ");
+ Serial.print(setADCconversion);
+ Serial.println();
+}
+
+void loop() {
+ // for recieving and transmitting by Telnet Server Connection
+ uint8_t i;
+ if (eth_connected == true) {
+ // check if there are any new clients
+ if (server.hasClient()) {
+ for (i = 0; i < MAX_SRV_CLIENTS; i++) {
+ // find free/disconnected spot
+ 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("\nNew client: ");
+ Serial.print(i);
+ Serial.print(' ');
+ Serial.println(serverClients[i].remoteIP());
+ break;
+ }
+ }
+ if (i >= MAX_SRV_CLIENTS) {
+ // no free/disconnected spot so reject
+ server.available().stop();
+ }
+ }
+ // 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 I2C
+ while (serverClients[i].available()) {
+ getstring = char(serverClients[i].read());
+ for (uint8_t m = 0; m < getstring.length(); m++) {
+ getdata(getstring.charAt(m));
+ }
+ }
+ }
+ } else {
+ if (serverClients[i]) {
+ serverClients[i].stop();
+ }
+ }
+ }
+ }
+
+ get_adc_val(); // get the actual measured values from adc
+
+ delay(100);
+}