Skip to content

Instantly share code, notes, and snippets.

@li2hub
Last active July 13, 2018 07:42
Show Gist options
  • Save li2hub/6eba19a29766930dd7904b8fe1235d52 to your computer and use it in GitHub Desktop.
Save li2hub/6eba19a29766930dd7904b8fe1235d52 to your computer and use it in GitHub Desktop.
/****Libraries required****/
#include <ESP8266WiFi.h> //ESP8266 main library
#include <ESP8266WebServer.h> //Library to handle Web Server commands
#include <Wire.h>
#include "MutichannelGasSensor.h"
const char *ssid = "ESP8266"; //Name of the Access Point
const char *password = "12345678"; //Password of the Access Point
/****Define a web server at port 80 for HTTP****/
ESP8266WebServer server(80); // Use port 443 for HTTPS
/****Let's include a custom (static) IP****/
IPAddress apIP(1,3,5,7); //You can use your own numbers for the IP
void setup() {
delay(1000);
Serial.begin(115200);
gas.begin(0x04);//the default I2C address of the slave is 0x04
gas.powerOn();
Serial.println();
Serial.println("Configuring access point...");
WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00
WiFi.softAP(ssid, password); // Use Wifi.softAP(ssid) if you want an unprotected Access Point
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
//The following commands tell the ESP which sub-routines to follow when a particular request is received
server.on ( "/", handleRoot );
server.onNotFound ( handleNotFound );
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handleRoot() {
char html[1000];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
float nh3,co,no2,c3h8,c4h10,ch4,h2,c2h5oh;
nh3=gas.measure_NH3();
co=gas.measure_CO();
no2=gas.measure_NO2();
c3h8=gas.measure_C3H8();
c4h10=gas.measure_C4H10();
ch4=gas.measure_CH4();
h2=gas.measure_H2();
c2h5oh=gas.measure_C2H5OH();
if(co>500){
snprintf ( html, 1000,
"<html>\
<head>\
<meta name='viewport' content='width=device-width, initial-scale=1.0'>\
<meta http-equiv='refresh' content='5'/>\
<title>ESP8266 Application</title>\
<style>\
body { background-color:#FF0000; font-family: Bookman; font-size: 1.5em; Color:#F7E543; }\
h1 { Color: #F7E543; }\
</style>\
</head>\
<body>\
<h1><center>ESP8266 Gas Sensor</center></h1>\
<p><center>Uptime: %02d:%02d:%02d</center></p>\
<p>NH3 Concentration: %f</p>\
<h2>CO Concentration: %f</p>\
<p>NO2 Concentration: %f</p>\
<p>C3H8 Concentration: %f</p>\
<p>C4H10 Concentration: %f</p>\
<p>CH4 Concentration: %f</p>\
<p>H2 Concentration: %f</p>\
<p>C2H5OH Concentration: %f</p>\
<p><center>This page refreshes every 5 seconds. Click <a href=\"javascript:window.location.reload();\">here</a> to refresh the page now.</center></p>\
</body>\
</html>",
hr, min % 60, sec % 60,
nh3,co,no2,c3h8,c4h10,ch4,h2,c2h5oh
);
server.send ( 200, "text/html", html );
}
else{
/****Build an HTML page to display on the web-server root address****/
snprintf ( html, 1000,
"<html>\
<head>\
<meta name='viewport' content='width=device-width, initial-scale=1.0'>\
<meta http-equiv='refresh' content='10'/>\
<title>ESP8266 Application</title>\
<style>\
body { background-color:#FFFFFF; font-family: Bookman; font-size: 1.5em; Color:#000000; }\
h1 { Color: #000000; }\
</style>\
</head>\
<body>\
<h1><center>ESP8266 Gas Sensor</center></h1>\
<p><center>Uptime: %02d:%02d:%02d</center></p>\
<p>NH3 Concentration: %f</p>\
<p>CO Concentration: %f</p>\
<p>NO2 Concentration: %f</p>\
<p>C3H8 Concentration: %f</p>\
<p>C4H10 Concentration: %f</p>\
<p>CH4 Concentration: %f</p>\
<p>H2 Concentration: %f</p>\
<p>C2H5OH Concentration: %f</p>\
<p><center>This page refreshes every 10 seconds. Click <a href=\"javascript:window.location.reload();\">here</a> to refresh the page now.</center></p>\
</body>\
</html>",
hr, min % 60, sec % 60,
nh3,co,no2,c3h8,c4h10,ch4,h2,c2h5oh
);
server.send ( 200, "text/html", html );
}
}
//This function handles unknown requests
void handleNotFound() {
String message = "File Not Found\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 += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment