Hi everyone,
Just wandering if anybody here have worked on projects involving the ESP8266 and the ADS1115 board?
In short I have have made a small sketch that handles the analogue to digital reading from the ADS but having tremendous problems to get it to work with an asynchronous web server, haven’t quite figured out how to tight up the analogue reading to the HTML side of things!
I think I’m allowed to post my sketch here, so if there is any takers I’ll be more than happy to do so….
Thanks in advance
Kinda found what I was looking for and will do the job for the time being.
I have decided to use the ‘DS18B20 Temperature Sensor Web server’ template and with plenty of trial and error got it working.
Also have posted the sketch down below ……
#include <Arduino.h> #include <ESP8266WiFi.h> #include <Hash.h> #include <ESPAsyncTCP.h> #include <ESPAsyncWebServer.h> #include <Wire.h> #include <Adafruit_ADS1015.h> Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ //Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ float Voltage0 = 0.0; float Voltage1 = 0.0; float Voltage2 = 0.0; float Voltage3 = 0.0; // Replace with your network credentials const char* ssid = "ssid"; const char* password = "psswrd"; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); String readVoltage0() { return String(Voltage0); } String readVoltage1() { return String(Voltage1); } String readVoltage2() { return String(Voltage2); } String readVoltage3() { return String(Voltage3); } const char index_html[] PROGMEM = R"rawliteral( <!DOCTYPE HTML><html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> <style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; } h2 { font-size: 3.0rem; } p { font-size: 3.0rem; } .units { font-size: 1.2rem; } .ds-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; } </style> </head> <body> <h2>ESP Weighing Server</h2> <p> <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <span class="ds-labels">Silo1</span> <span id="Voltage0">%Voltage0%</span> <sup class="units">°Kg</sup> </p> <p> <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <span class="ds-labels">Silo2</span> <span id="Voltage1">%Voltage1%</span> <sup class="units">°Kg</sup> </p> <p> <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <span class="ds-labels">Silo3</span> <span id="Voltage2">%Voltage2%</span> <sup class="units">°Kg</sup> </p> <p> <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <span class="ds-labels">Silo4</span> <span id="Voltage3">%Voltage3%</span> <sup class="units">°Kg</sup> </p> </body> <script> setInterval(function ( ){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){ if (this.readyState == 4 && this.status == 200){ document.getElementById("Voltage0").innerHTML = this.responseText;} }; xhttp.open("GET", "/Voltage0", true); xhttp.send(); }, 5000) ; setInterval(function ( ){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){ if (this.readyState == 4 && this.status == 200){ document.getElementById("Voltage1").innerHTML = this.responseText;} }; xhttp.open("GET", "/Voltage1", true); xhttp.send(); }, 5000) ; setInterval(function ( ){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){ if (this.readyState == 4 && this.status == 200){ document.getElementById("Voltage2").innerHTML = this.responseText;} }; xhttp.open("GET", "/Voltage2", true); xhttp.send(); }, 5000) ; setInterval(function ( ){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){ if (this.readyState == 4 && this.status == 200){ document.getElementById("Voltage3").innerHTML = this.responseText;} }; xhttp.open("GET", "/Voltage3", true); xhttp.send(); }, 5000) ; </script> </html>)rawliteral"; // Replaces placeholder with DHT values String processor(const String& var) { /* Serial.println(var); if(var == "Voltage0"){ return readVoltage0(); } return String();*/ } void setup(void) { Serial.begin(115200); /*Serial.println("Hello!"); Serial.println("Getting single-ended readings from AIN0..3"); Serial.println("ADC Range: +/- 4.096V (1 bit = 2mV/ADS1115, 0.125mV/ADS1115)");*/ /*// The ADC input range (or gain) can be changed via the following // functions, but be careful never to exceed VDD +0.3V max, or to // exceed the upper and lower limits if you adjust the input range! // Setting these values incorrectly may destroy your 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*/ // Start up the ADS1115 library ads.begin(); Serial.println(); // Connect to Wi-Fi WiFi.begin(ssid, password); Serial.println("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); // Print ESP Local IP Address Serial.println(WiFi.localIP()); // Route for root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) { request->send_P(200, "text/html", index_html, processor); }); server.on("/Voltage0", HTTP_GET, [](AsyncWebServerRequest * request) { request->send_P(200, "text/plain", readVoltage0().c_str()); }); server.on("/Voltage1", HTTP_GET, [](AsyncWebServerRequest * request) { request->send_P(200, "text/plain", readVoltage1().c_str()); }); server.on("/Voltage2", HTTP_GET, [](AsyncWebServerRequest * request) { request->send_P(200, "text/plain", readVoltage2().c_str()); }); server.on("/Voltage3", HTTP_GET, [](AsyncWebServerRequest * request) { request->send_P(200, "text/plain", readVoltage3().c_str()); }); // Start server server.begin(); } void loop(void) { 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); Voltage0 = (adc0 * 0.125) / 0.06622; Voltage1 = (adc1 * 0.125) / 0.06622; Voltage2 = (adc2 * 0.125) / 0.06622; Voltage3 = (adc3 * 0.125) / 0.06622; /*Serial.print("AIN0: "); Serial.print("\tKg:"); Serial.println(Voltage0); Serial.print("AIN1: "); Serial.print("\tKg:"); Serial.println(Voltage1); Serial.print("AIN2: "); Serial.print("\tKg:"); Serial.println(Voltage2); Serial.print("AIN3: "); Serial.print("\tKg:"); Serial.println(Voltage3); Serial.println(" ");*/ delay(5000); }
And once again feedback is most welcome.
Regards:
Jorge
Hi Jorge.
I’m not familiar with the ADS1115 but I can help with the web server part. At the moment, what do you need to complete your project? Is your web server working at the moment?
Regards,
Sara
Hi Sara,
Your support is much appreciated indeed…
Somehow I have managed to get this working with the code above minus the ‘fonts-are-awesome’ part as only left it there for cosmetic purposes, In the end I decided to use Base-64 encoding for the icons.
To be honest I just realised that I haven’t thought this through as in my naivety the asynchronous server was a great option but starting to look at the amount of sensors I need o monitor this might end up falling short but never the less one step at the time…
I will have a short re-valuation in the next week or so and get back here short after.
In the mean time I shall leave you all with the best wishes and prosperous 2020.
Kindest Regards:
Jorge