- /*
+/*
#####################################################
# ### ### #
# ### CODE for ESP32 of LANToI2C_Board_264 ### #
# ### for START TELNET SERVER ### #
# ### for LAN to I2C ### #
+# ### connected to 4 CH ADC ### #
# ### ### #
# ### author: O.Artz ### #
# ### ### #
-# ### ### #
-# ### UPDATE 2020-09-25 ### #
+# ### UPDATE 2021-06-17 ### #
# ### ### #
#####################################################
#define SDA 32
#define SCL 5
+#define ISMYADDR() (rxbuf[1] == '0' && (rxbuf[2] == '0' )) //|| rxbuf[2] == '1' || rxbuf[2] == '2' || rxbuf[2] == '3') )
+
Adafruit_ADS1115 ads(0x48); /* Use this for the 16-Bit version */
WiFiServer server(2323);
//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
+
void WiFiEvent(WiFiEvent_t event)
{
switch (event){
//for I2C Connection
Wire.begin(SDA, SCL); // Initiate the Wire library
//for external ADC
- // ADS1015 ADS1115
+ // val 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
+ // 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
+
+ adsGain_t asetGAIN[] = {GAIN_TWOTHIRDS, GAIN_ONE, GAIN_TWO, GAIN_FOUR, GAIN_EIGHT, GAIN_SIXTEEN};
+ ads.setGain(asetGAIN[val]);
}
void loop()
if(serverClients[i]) serverClients[i].stop();
serverClients[i] = server.available();
if (!serverClients[i]) Serial.println("available broken");
- Serial.print("New client: ");
+ Serial.print("\nNew client: ");
Serial.print(i); Serial.print(' ');
Serial.println(serverClients[i].remoteIP());
break;
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());
+// 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());
}
}
else {
}
}
//check UART for data
- if(Serial.available()) {
+/* 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);
- }
- }
+*/ // Serial.readBytes(sbuf, len);
+ //check I2C for data
+ if(Wire.available()) {
+ size_t len = Wire.available();
+ uint8_t sbuf[len];
}
}
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 fadc0, fadc1, fadc2, fadc3;
+
+ float acGAIN[] = {0.1875, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125};
+ float setADCconversion = acGAIN[val];
- float conversion, fadc0, fadc1, fadc2, fadc3;
+ fadc0 = adc0 * setADCconversion;
+ fadc1 = adc1 * setADCconversion;
+ fadc2 = adc2 * setADCconversion;
+ fadc3 = adc3 * setADCconversion;
- conversion = cGAIN_TWO;
+ String sadc0 = String(fadc0);
+ String sadc1 = String(fadc1);
+ String sadc2 = String(fadc2);
+ String sadc3 = String(fadc3);
- fadc0 = adc0 * conservation;
- fadc1 = adc1 * conservation;
- fadc2 = adc2 * conservation;
- fadc3 = adc3 * conservation;
+//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");
- 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(" ");
-
+ Serial.print(sbuf);
+
+ size_t len = sbuf.length();
+
+ uint8_t buf[len];
+ sbuf.toCharArray((char*)buf, len);
+
+ send_answer_buf(buf, len);
+
delay(1000);
}
+
+uint8_t rxcnt = 0, txpoint = 0;
+uint8_t rxbuf[11];
+uint8_t txbuf[12];
+
+uint8_t nib_to_hex(uint16_t in, uint8_t nib) {
+ uint8_t t = (in >> (nib*4)) & 0xF;
+ if (t <= 9) {return t + 0x30;}
+ return t - 10 + 0x61;
+ }
+
+uint8_t hex_to_int(uint8_t h) { //assumes valid number
+ if (h < 0x40) return h-0x30;
+ if (h < 0x50) return h-0x41+10;
+ return h-0x61+10;
+ }
+
+void sub1(uint8_t* c1, uint8_t* c2) {
+ uint8_t b = hex_to_int(*c1)*16 + hex_to_int(*c2);
+ b -= 1;
+ *c1 = nib_to_hex(b,1);
+ *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);
+ }
+
+void forward_msg(uint8_t i) {
+ sub1(&rxbuf[1],&rxbuf[2]);
+ memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,i);
+ txbuf[i] = 0;
+// send_answer_buf(txbuf);
+ }
+
+uint8_t is_my_address(uint8_t s) {
+ if (ISMYADDR()) {
+ //rxbuf[2] -= '0'; // write number to buffer, instead of ascii.
+ return 1;
+ } else {
+ forward_msg(s);
+ return 0;
+ }
+}
+
+void getdata(uint8_t buf) {
+ 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
+ memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,10);
+ txbuf[11] = 0;
+// send_answer_buf(txbuf);
+ } else if (rxbuf[0] == 'S') { // Scann of chain, returns number of boards
+ txbuf[0] = 'S';
+ uint8_t length = rxcnt - 2 ; // Length of Counter
+ //read current counter value
+ uint32_t cnt = 0;
+ if (length == 0){
+ length = 1;
+ } else {
+ uint32_t base = 1;
+ for (uint8_t i = length; i > 0; i--){
+ cnt += hex_to_int(rxbuf[i])*base;
+ base *= 16;
+ }
+ }
+ // inc counter value
+ cnt++;
+ //send to next uC
+ if ((cnt % 16) == 0) length++;
+ for (uint8_t i = length; i > 0; i--){
+ txbuf[i] = nib_to_hex(cnt,length-i);
+ }
+ 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);
+ rxcnt = 0;
+ } else if (is_my_address(10)){ // message is for this uC
+ if (rxbuf[0] == 'W'){
+//write
+ //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);
+ }
+ if (rxbuf[0] == 'R'){
+//read
+ //read ADC_value
+ if (hex_to_int(rxbuf[5]) == 10) {
+ float fadc0, fadc1, fadc2, fadc3;
+ uint8_t adc_val = 0xFFFF;
+ //CH0
+ if (hex_to_int(rxbuf[4]) == 0) {adc_val=fadc0;
+ }
+ //CH1
+ if (hex_to_int(rxbuf[4]) == 1) {adc_val=fadc1;
+ }
+ //CH2
+ if (hex_to_int(rxbuf[4]) == 2) {adc_val=fadc2;
+ }
+ //CH3
+ if (hex_to_int(rxbuf[4]) == 3) {adc_val=fadc3;
+ }
+// send_answer_hex(&rxbuf[0], adc_val);
+ }
+ }
+ }
+ }
+ }
+}