I asked a previous question about esp8266 that made a lot of things clear for me that esp8266 is very powerful and can be independently used as the webserver now, I have used the following library(https://github.com/sandeepmistry/esp8266-Arduino) to achieve this thing as I found a tutorial(https://learn.adafruit.com/esp8266-temperature-slash-humidity-webserver/code) to help me out. Now this is the code that was in the tutorial and is working fine
/* DHTServer - ESP8266 Webserver with a DHT sensor as an input
Based on ESP8266Webserver, DHTexample, and BlinkWithoutDelay (thank you)
Version 1.0 5/3/2014 Version 1.0 Mike Barela for Adafruit Industries
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 2
const char* ssid = "--MYRouterID";
const char* password = "--MYRouterPassword";
ESP8266WebServer server(80);
// Initialize DHT sensor
// NOTE: For working with a faster than ATmega328p 16 MHz Arduino chip, like an ESP8266,
// you need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold. It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value. The default for a 16mhz AVR is a value of 6. For an
// Arduino Due that runs at 84mhz a value of 30 works.
// This is for the ESP8266 processor on ESP-01
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
float humidity, temp_f; // Values read from sensor
String webString=""; // String to display
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor
void handle_root() {
server.send(200, "text/plain", "Hello from the weather esp8266, read from /temp or /humidity");
delay(100);
}
void setup(void)
{
// You can open the Arduino IDE Serial Monitor window to see what the code is doing
Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable
dht.begin(); // initialize temperature sensor
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("DHT Weather Reading Server");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handle_root);
server.on("/temp", [](){ // if you add this subdirectory to your webserver call, you get text below :)
gettemperature(); // read sensor
webString="Temperature: "+String((int)temp_f)+" F"; // Arduino has a hard time with float to string
server.send(200, "text/plain", webString); // send to someones browser when asked
});
server.on("/humidity", [](){ // if you add this subdirectory to your webserver call, you get text below :)
gettemperature(); // read sensor
webString="Humidity: "+String((int)humidity)+"%";
server.send(200, "text/plain", webString); // send to someones browser when asked
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
server.handleClient();
}
void gettemperature() {
// Wait at least 2 seconds seconds between measurements.
// if the difference between the current time and last time you read
// the sensor is bigger than the interval you set, read the sensor
// Works better than delay for things happening elsewhere also
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// save the last time you read the sensor
previousMillis = currentMillis;
// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
humidity = dht.readHumidity(); // Read humidity (percent)
temp_f = dht.readTemperature(true); // Read temperature as Fahrenheit
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temp_f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
Now this is my modification of code(of esp8266) as I am willing to communicate it over serial with my arduino which will further send data to the esp8266 when it requests using the attached sensor to the batteries(I am making a solar related project).
Its only a basic code but the problem is that it compiles and runs ok but I am only able to access the web page once and that is also only the root. The other links do not open up or when I try to even reload the web page root it stuck and browser says error timed out
Here is code
void handle_root() {
//server.send(208, "text/plain", "<h1>Hello from the weather esp8266, read from /temp or /humidity</h1>");
String sendx = "<h1>This Page Shows the Basic Info</h1>";
// Serial.write("MP");
// delay(20);
// while(Serial.available()>0)
// {
// rec = Serial.readString();
//}
rec = "TESt";
if(rec[0] == AllMain)
{
sendx += "<h2>Running on Main Power Now</h2>";
sendx += "<p>" + rec + "</p>";
}
else if(rec[0] == HouseMain)
{
sendx += "<h2>Home on Main Power</h2>";
sendx += "<p>" + rec + "</p>";
sendx += "<a href =\"/thos\">Turn on Hospital Main</a>";
}
else if(rec[0]==HospitalMain)
{
sendx += "<h2>Hospital on Main Power</h2>";
sendx += "<p>" + rec + "</p>";
sendx += "<a href =\"/thm\">Turn on House Main</a>";
}
else
{
sendx += "<p>" + rec + "</p>";
sendx += "<h4><a href =\"/thm\">Turn on House Main</a></h4>";
sendx += "<h4><a href =\"/thos\">Turn on Hospital Main</a></h4>";
}
// sendx += "<a href =\""+".."+"/temp\">Temp</a>";
server.send(200,"text/html",sendx);
delay(100);
}
void thm()
{
// if(Serial.available())
{
// Serial.println("thm");
String sendx = "signal sent... go back to previous page to verify";
// sendx += "<a href=\"javascript:history.back()\">Go Back</a>";
server.send(200,"text/html",sendx);
delay(100);
}
}
void thos()
{
// if(Serial.available())
{
// Serial.println("thos");
String sendx = "signal sent... go back to previous page to verify";
// sendx += "<a href=\"javascript:history.back()\">Go Back</a>";
server.send(200,"text/html",sendx);
delay(100);
}
}
void setup(void)
{
// You can open the Arduino IDE Serial Monitor window to see what the code is doing
Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable
// Connect to WiFi network
WiFi.begin(ssid, password);
if(debug)
Serial.print("\n\r \n\rWorking to connect");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if(debug)
Serial.print(".");
}
if(debug)
{
Serial.println("");
Serial.println("DHT Weather Reading Server");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", handle_root);
server.on("/thm", thm);
/*server.on("/thm", [](){ ===== I have tried this inline as well no luck ====
Serial.println("thos");
String sendx = "signal sent... go back to previous page to verify";
server.sendContent(sendx);
});*/
server.on("/thos",thos);
server.begin();
if(debug)
Serial.println("HTTP server started");
}
void loop(void)
{
server.handleClient();
}
Now please I would also appreciate your help for the commented portion of my code dealing with serial communication in function handle_root
but it would be more than enough if you can point out my misconception or error or point me in right direction