Go Down

Topic: Sending data to a local web server (Read 276 times) previous topic - next topic

Jas276

Hi there,
I'm making a project to display info while I'm camping/touring. Info such as temperature in the back of my 4x4 canopy, temperature of the fridge, and battery voltages. I currently have a Node MCU with the below code on it acting as a web server displaying Canopy temperature, voltage of 1 of my batteries, and a function to toggle the onboard LED (for testing purposes).

I'm going to build a small board (battery powered) to sit in my fridge and send that temperature value to the web server.

My question is...
What is the best way of doing that?

I have been reading up on the HTTP_GET and HTTP_POST, and although they seem like the way to go, is there a more simple way?
Most of the time I am camping in very remote places so the web server will never need to connect to the internet, just get the value(s) from any other "client" board I make up to connect to the access point the web server creates.

I'm just after some advice, and point me in the correct direction as I'm keen to work this out so I understand it.

I have made the code below from several sketch examples I have found and pieced it together to work for me (see screenshot). Now I want to move to the next step and bring in some external values.

Cheers
Jason



Code: [Select]
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "DHT.h"
#include <Wire.h>


#define DHTPIN D3     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

//DHT Info:
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Set these to your desired credentials.
const char* ssid = "Camp";
const char* password = "mynameisjason";  //Password must be at least 8 characters long


ESP8266WebServer server(80);

DHT dht(DHTPIN, DHTTYPE);

  float t;
  float h;
  float hic;
  float minTemp;
  float maxTemp;

  float batt1 = 0.00; /*
  float batt2 = 0.00;
  float batt3 = 0.00;
  */

// Just a little test message.  Go to http://192.168.4.1 in a web browser
// connected to this access point to see it.

void setup() {
delay(1000);

  float t = 0.00;
  float h = 0.00;
  float hic = 0.00;
  float minTemp = 0.00;
  float maxTemp = 0.00;

  dht.begin();

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(D3, INPUT);
  digitalWrite(LED_BUILTIN, LOW);

Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();

  Serial.print("AP Name: ");
  Serial.println(ssid);
Serial.print("AP IP address: ");
Serial.println(myIP);
 
server.on("/", HTTP_GET, handleRoot);
  server.on("/home", Home);
  server.on("/volts", volts);
  server.on("/temp", temp);
  server.on("/reset", minmax);
  server.on("/toggle", toggleLED);
  server.onNotFound([](){server.send(404, "text/plain", "404: Not found");});
   
  server.begin();
Serial.println("HTTP server started"); 
 
 
}

void loop() {
server.handleClient();
 
}

void handleRoot() {
  server.send(200, "text/html", "<form action=\"/home \" method=\"POST\"><input type=\"submit\" value=\"HOME\"></form>");

  }
 
void Home() {
 
String webpage = "<meta name = 'viewport' content = 'width = device-width'>";
  webpage+="<h1>Welcome to Camp Home Page</h1>";
  webpage+="<form action=\"/toggle\" method=\"POST\"><input type=\"submit\" value=\"TOGGLE LED\"></form>";
  webpage+= "<form action=\"/volts\" method=\"POST\"><input type=\"submit\" value=\"BATTERY VOLTAGES\"></form>";
  webpage+= "<form action=\"/temp\" method=\"POST\"><input type=\"submit\" value=\"TEMPERATURE\"></form>";

 

  server.send(200, "text/html", webpage); 

  }

void volts() {
 
      int val11=analogRead(A0);
      float val12=val11/5.90;
      float batt1=(val12/10);
/*
      int val21=analogRead(A1);
      float val22=val21/4.092;
      float batt2=(val22/10);

      int val31=analogRead(A2);
      float val32=val31/4.092;
      float batt3=(val32/10);           
*/
      Serial.print("Battery 1: ");     
      Serial.print(batt1);
      Serial.print(" Volts\t");
     
      Serial.println(val11);
/*     
      Serial.print("Battery 2: ");     
      Serial.print("batt2");
      Serial.print(" Volts\t");     
      Serial.print("Battery 3: ");     
      Serial.print("batt3");
      Serial.println(" Volts");
*/     
String webpage = "<head><meta name = 'viewport' content = 'width = device-width'><meta http-equiv='refresh' content='5'>";
  webpage+="</head><h1>Start Battery</h1><h2>";
  webpage+= batt1;
  webpage+=" V</h2>";
  webpage+="<h1>Aux Battery</h1><h2>";
  webpage+= "batt2";
  webpage+=" V</h2>";
  webpage+="<h1>Fridge Battery</h1><h2>";
  webpage+= "batt3";
  webpage+= " V</h2>";
  webpage+= "<form action=\"/home \" method=\"POST\"><input type=\"submit\" value=\"HOME\"></form>";
  webpage+= "<form action=\"/temp\" method=\"POST\"><input type=\"submit\" value=\"TEMPERATURE\"></form>";

 

  server.send(200, "text/html", webpage);
     
}

void temp() {

  h = dht.readHumidity();
  t = dht.readTemperature();
  hic = dht.computeHeatIndex(t, h, false);


  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    }

  if (minTemp > t) {
    minTemp = t;
    }

  if (maxTemp < t) {
    maxTemp = t;
    }


  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C\t");
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.println(" *C ");
  Serial.print("Min Temp: ");
  Serial.print(minTemp);
  Serial.print(" *C\t");
  Serial.print("Max Temp: ");
  Serial.print(maxTemp);
  Serial.println(" *C\t");

     
String webpage = "<head><meta name = 'viewport' content = 'width = device-width'><meta http-equiv='refresh' content='10'>";
  webpage+="</head><h1>Temperature</h1><h2>";
  webpage+= t;
  webpage+=" *C</h2>";
  webpage+="<h1>Humidity</h1><h2>";
  webpage+= h;
  webpage+=" %</h2>";
  webpage+="<h1>Feels Like</h1><h2>";
  webpage+= hic;
  webpage+= " *C</h2>";
  webpage+="<h1>Min Temp</h1><h2>";
  webpage+= minTemp;
  webpage+=" *C</h2>";
  webpage+="<h1>Max Temp</h1><h2>";
  webpage+= maxTemp;
  webpage+= " *C</h2>";
  webpage+= "<form action=\"/home \" method=\"POST\"><input type=\"submit\" value=\"HOME\"></form>";
  webpage+= "<form action=\"/volts\" method=\"POST\"><input type=\"submit\" value=\"BATTERY VOLTAGES\"></form>";
  webpage+= "<form action=\"/reset\" method=\"POST\"><input type=\"submit\" value=\"RESET MIN/MAX\"></form>";

 

  server.send(200, "text/html", webpage);

  }

void minmax() {

  minTemp = t;
  maxTemp = t;

  server.sendHeader("Location","/temp");        // Add a header to respond with a new location for the browser to go to the home page again
  server.send(303);                         // Send it back to the browser with an HTTP status 303 (See Other) to redirect
 
}

void toggleLED() {
 
  digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
 
  server.sendHeader("Location","/home");        // Add a header to respond with a new location for the browser to go to the home page again
  server.send(303);                         // Send it back to the browser with an HTTP status 303 (See Other) to redirect
 
  }






wvmarle

How do you plan to connect that board in the fridge to your NodeMCU?

Isn't it easier to just wire a temperature sensor that you can read directly?
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Jas276

How do you plan to connect that board in the fridge to your NodeMCU?

Isn't it easier to just wire a temperature sensor that you can read directly?
I'll be using another ESP8266 equipped board to connect via WiFi.

The fridge is on a slide which can move back and forth about 1 meter, so I don't want wires getting caught.

And by doing it via WiFi it'll give me the option to add other sensors in the future.

Cheers

PaulRB

#3
Sep 26, 2017, 07:35 am Last Edit: Sep 26, 2017, 07:36 am by PaulRB
A fridge is normally a metal box. That won't let RF signals out. You will need to get the antenna on the outside of the fridge.

Have a look at my temp/humidity/barometer sensors. They use a remote web server, but might give you some ideas.

Jas276

A fridge is normally a metal box. That won't let RF signals out. You will need to get the antenna on the outside of the fridge.

Have a look at my temp/humidity/barometer sensors. They use a remote web server, but might give you some ideas.
Thanks PaulRB, I'll have a read of your sketch and see if I can get my head around it. I'm sure I'll find some useful info to use in my sketch. (like the deepsleep function)

I was just looking at UDP strings and might see about implementing that as I don't need to connect to a web server on the net such as Sparkfun or similar.

Cheers

wvmarle

The fridge is on a slide which can move back and forth about 1 meter, so I don't want wires getting caught.
Attach your sensor wire to the power cable of the fridge. No wires to get caught.

Not all fridges are metal boxes. They are often made of HIPS with PU foam as insulator, much cheaper than a metal box, and should let WiFi signals go out just fine.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

PaulRB

If you need a more capable web server, you could use a Pi Zero W. You can install the entire LAMP stack (Linux, Apache web server, MySQL database and PHP script language). They are small and cheap. If you have a TV with HDMI connector in the trailer, you could connect that to the Pi to view data.

wvmarle

I was just looking at UDP strings and might see about implementing that as I don't need to connect to a web server on the net such as Sparkfun or similar.
Your main NodeMCU is web server already, you can connect your secondary NodeMCU to that one sending regular http post strings with the temperature data. No need to do anything special.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Jas276

Attach your sensor wire to the power cable of the fridge. No wires to get caught.

Not all fridges are metal boxes. They are often made of HIPS with PU foam as insulator, much cheaper than a metal box, and should let WiFi signals go out just fine.
My fridge has a plastic outer casing, so I'm pretty confident the WiFi signal will get through.
I currently use an off-the-shelf wireless temp sender which uses the 433MHz band to transmit with no problem. The problem with it is that it only has 1 function. Hence the reason I'm playing around with the Arduino stuff to customise something for me.

Cheers

Jas276

If you need a more capable web server, you could use a Pi Zero W. You can install the entire LAMP stack (Linux, Apache web server, MySQL database and PHP script language). They are small and cheap. If you have a TV with HDMI connector in the trailer, you could connect that to the Pi to view data.
Definitely no tv when I go camping, just the bush telly (aka camp fire) :P

Jas276

Your main NodeMCU is web server already, you can connect your secondary NodeMCU to that one sending regular http post strings with the temperature data. No need to do anything special.
That is what I was thinking, just wanted to make sure that using UDP strings would be the best (easiest and simplest) way to go?

Is there another form of strings I could use?

Cheers

wvmarle

No experience with UDP strings myself, using http post and get requests. Works very easily.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

PieterP

UDP does not guarantee packet delivery. TCP (on which HTTP is based) does. HTTP is the standard protocol to use with web servers.
HTTP POST is the way to go.

(HTTP GET would work as well, but it's not correct, because the HTTP specification doesn't allow GET to have side effects.)

Network protocols (and TCP vs UDP)
HTTP explained
A sketch that saves sensor data to flash and runs a web server to display it in the browser
A sketch that POSTs sensor data to a local server using HTTP, plus the server side PHP code that inserts the values it received from the ESP into a database

Pieter

wvmarle

Regarding writing to database: you can do this directly using the MySQL connector. Much easier than the http method and having to maintain a server side script as well!
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

PaulRB

Regarding writing to database: you can do this directly using the MySQL connector.
Unless the OP decides to follow my Pi Zero suggestion, he won't have a database on his NodeMCU server.

I, on the other hand, must get around to trying that MySQL connector...

Go Up