--- /dev/null
+ /*
+#####################################################
+# ### ### #
+# ### CODE for ESP32 of LANToI2C_Board_264 ### #
+# ### for START TELNET SERVER ### #
+# ### for LAN to I2C ### #
+# ### ### #
+# ### author: O.Artz ### #
+# ### ### #
+# ### ### #
+# ### UPDATE 2020-09-25 ### #
+# ### ### #
+#####################################################
+
+ETH MAC: 24:6F:28:1E:C5:57
+IPv4: 192.168.8.64
+Telnet: 192.168.8.64 2323
+
+Visual Status via Board LEDs:
+ETH Start -> Green LED blink for 1 sec
+Telnet work -> Green LED on
+ETH Disconnected/stopped -> Orange LED on
+*/
+
+//load needed Libaries
+#include <ETH.h> //for Telnet Server Connection
+#include <Wire.h> //for I2C Connection
+#include <Adafruit_ADS1015.h> //for external ADC
+
+//how many clients should be able to telnet to this ESP32
+#define MAX_SRV_CLIENTS 10
+
+#define SDA 32
+#define SCL 5
+
+Adafruit_ADS1115 ads(0x48); /* Use this for the 16-Bit version */
+
+WiFiServer server(2323);
+WiFiClient serverClients[MAX_SRV_CLIENTS];
+
+//used to check the number every time, cause could changed
+volatile static bool eth_connected = false;
+
+void WiFiEvent(WiFiEvent_t event)
+{
+ switch (event){
+ case SYSTEM_EVENT_ETH_START:
+ Serial.println("ETH Started");
+ //set eth hostname here
+ digitalWrite(2,LOW);
+ digitalWrite(15,HIGH);
+ delay(500);
+ digitalWrite(15, LOW);
+ ETH.setHostname("esp32-ethernet");
+ break;
+
+ case SYSTEM_EVENT_ETH_CONNECTED:
+ Serial.println("ETH Connected");
+ digitalWrite(2,LOW);
+ digitalWrite(15,HIGH);
+ break;
+
+ case SYSTEM_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 SYSTEM_EVENT_ETH_DISCONNECTED:
+ Serial.println("ETH Disconnected");
+ digitalWrite(15,LOW);
+ digitalWrite(2,HIGH);
+ eth_connected = false;
+ break;
+
+ case SYSTEM_EVENT_ETH_STOP:
+ Serial.println("ETH Stopped");
+ digitalWrite(15,LOW);
+ digitalWrite(2,HIGH);
+ eth_connected = false;
+ break;
+
+ default:
+ break;
+ }
+}
+
+void setup()
+{
+//for Telnet Server Connection
+ pinMode(2, OUTPUT); // yellow LED
+ pinMode(15, OUTPUT); // green LED
+ Serial.begin(115200);
+ Serial.println("\nConnecting");
+ WiFi.onEvent(WiFiEvent);
+ delay(1000);
+ //start UART and the server
+ ETH.begin(1,17,23,18,ETH_PHY_LAN8720,ETH_CLOCK_GPIO0_IN);
+ server.begin();
+ server.setNoDelay(true);
+
+ while (eth_connected == false);
+
+ 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
+ // ADS1015 ADS1115
+ // ------- -------
+ //ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
+ // ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
+ ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
+ // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
+ // ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
+ // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
+}
+
+void loop()
+{
+//for 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("New 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
+ 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());
+ }
+ }
+ else {
+ if (serverClients[i]) {
+ serverClients[i].stop();
+ }
+ }
+ }
+ //check UART for data
+ if(Serial.available()) {
+ size_t len = Serial.available();
+ uint8_t sbuf[len];
+ Serial.readBytes(sbuf, len);
+ //push UART data to all connected telnet clients
+ for(i = 0; i < MAX_SRV_CLIENTS; i++) {
+ if (serverClients[i] && serverClients[i].connected()) {
+ serverClients[i].write(sbuf, len);
+ delay(1);
+ }
+ }
+ }
+ }
+
+//for external ADC
+ int16_t adc0, adc1, adc2, adc3;
+
+ adc0 = ads.readADC_SingleEnded(0);
+ adc1 = ads.readADC_SingleEnded(1);
+ adc2 = ads.readADC_SingleEnded(2);
+ adc3 = ads.readADC_SingleEnded(3);
+
+ float cGAIN_TWOTHIRDS = 0.1875;
+ float cGAIN_ONE = 0.125;
+ float cGAIN_TWO = 0.0625;
+ float cGAIN_FOUR = 0.03125;
+ float cGAIN_EIGHT = 0.015625;
+ float cGAIN_SIXTEEN = 0.0078125;
+
+ float conversion, fadc0, fadc1, fadc2, fadc3;
+
+ conversion = cGAIN_TWO;
+
+ fadc0 = adc0 * conservation;
+ fadc1 = adc1 * conservation;
+ fadc2 = adc2 * conservation;
+ fadc3 = adc3 * conservation;
+
+ Serial.print("AIN0: "); Serial.print(fadc0); Serial.println(" mV");
+ Serial.print("AIN1: "); Serial.print(fadc1); Serial.println(" mV");
+ Serial.print("AIN2: "); Serial.print(fadc2); Serial.println(" mV");
+ Serial.print("AIN3: "); Serial.print(fadc3); Serial.println(" mV");
+ Serial.println(" ");
+
+delay(1000);
+}