Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Azhaque
#86320 Hi All,

Here is another sketch for ESP8266 for a javascript based self-updating temperature gauge. It is a modified version of the one @ https://circuits4you.com/2018/02/03/esp8266-nodemcu-adc-analog-value-on-dial-gauge/. Thanks guys.

The hardware is to be used for controlling the hot water system in our off-grid home. The instant code would become part of a Hot Water Management System that includes:

1. electric geyser control;
2. periodic flushing/recycling the hot water feed line into the main overhead tank;

3. control of 2 pressurizing pumps to maintain system pressure at around 2.2 bars.
(required because single storey house has very little head for water for showering)

Code: Select allconst char* htmlfile = "/index.html";
int a;
//WiFi Connection configuration

const char* ssid      = "ZONG MBB-E5573-E9A6";      // WiFi SSID
const char* password  = "07237750";  // WiFi Password
ESP8266WebServer server(80);
void handleADC(){
 int a = analogRead(A0);
  a = map(a,0,1023,0,100);
  String adc = String(a);

   String adc = String(a);
  server.send(200, "text/plane",adc);
void handleRoot(){
  server.sendHeader("Location", "/index.html",true);   //Redirect to our html web page
  server.send(302, "text/plane","");
void handleWebRequests(){
  if(loadFromSpiffs(server.uri())) return;
  String message = "File Not Detected\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " NAME:"+server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
  server.send(404, "text/plain", message);
void setup() {
  //Initialize File System
  Serial.println("File System Initialized");
  //Connect to wifi Network
  WiFi.begin(ssid, password);     //Connect to your WiFi router
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
  //If connection successful show IP address in serial monitor
  Serial.print("Connected to ");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
  //Initialize Webserver
  server.on("/getADC",handleADC); //Reads ADC function is called from out index.html
  server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
void loop() {
bool loadFromSpiffs(String path){
  String dataType = "text/plain";
  if(path.endsWith("/")) path += "index.htm";
  if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
  else if(path.endsWith(".html")) dataType = "text/html";
  else if(path.endsWith(".htm")) dataType = "text/html";
  else if(path.endsWith(".css")) dataType = "text/css";
  else if(path.endsWith(".js")) dataType = "application/javascript";
  else if(path.endsWith(".png")) dataType = "image/png";
  else if(path.endsWith(".gif")) dataType = "image/gif";
  else if(path.endsWith(".jpg")) dataType = "image/jpeg";
  else if(path.endsWith(".ico")) dataType = "image/x-icon";
  else if(path.endsWith(".xml")) dataType = "text/xml";
  else if(path.endsWith(".pdf")) dataType = "application/pdf";
  else if(path.endsWith(".zip")) dataType = "application/zip";
  File dataFile = SPIFFS.open(path.c_str(), "r");
  if (server.hasArg("download")) dataType = "application/octet-stream";
  if (server.streamFile(dataFile, dataType) != dataFile.size()) {
  return true;

int ReadADC(){
//***************************Thermistor Read Function Begins***************************
//  Complete project details at https://randomnerdtutorials.com         

int samples[5];

 uint8_t i;
  float average;

  // take N samples in a row, with a slight delay
  for (i=0; i< 5; i++) {
   samples[i] = analogRead(A0);
  // average all the samples out
  average = 0;
  for (i=0; i< 5; i++) {
     average += samples[i];
  average /= 5;
// convert the value to resistance

  //average = 1023 / average - 1;
 // average = 100000 / average;                  //SERIESRESISTOR 100000
  float steinhart;
  float Tot_Current;

  average = (average/1023)*3.2;
  Tot_Current = (3.26 - average)/100;
  Tot_Current =  Tot_Current - (average/320);
  steinhart = average/Tot_Current;
  steinhart = steinhart / 100;                // (R/Ro),THERMISTORNOMINAL 100000
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= 4100;                           // 1/B * ln(R/Ro), BCOEFFICIENT 4250
  steinhart += 1.0 / (25 + 273.15);              // + (1/To), TEMPERATURENOMINAL 25   
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to degC

  a =  steinhart;       
  return a;

The hardware uses a 100K NTC resistor for sensing temperature of water in the electric geyser. The function ReadADC() manages reading and converting the 100K NTC resistor value to Temperature.

Additionally the javascript, css and jquery files are to be placed into a separate folder named <data>, and loaded into the 8266's SPIFF. A zipped version (C4u.rar) of the sketch and associated folder is attached.

Please try it out and comment if you find it useful.

You do not have the required permissions to view the files attached to this post.