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
#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
}