Skip to content

Instantly share code, notes, and snippets.

@bakueikozo
Created February 20, 2021 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakueikozo/ed3605693ae3754ad922342f31b81d8a to your computer and use it in GitHub Desktop.
Save bakueikozo/ed3605693ae3754ad922342f31b81d8a to your computer and use it in GitHub Desktop.
/*
ESP8266 mDNS responder sample
This is an example of an HTTP server that is accessible
via http://esp8266.local URL thanks to mDNS responder.
Instructions:
- Update WiFi SSID and password as necessary.
- Flash the sketch to the ESP8266 board
- Install host software:
- For Linux, install Avahi (http://avahi.org/).
- For Windows, install Bonjour (http://www.apple.com/support/bonjour/).
- For Mac OSX and iOS support is built in through Bonjour already.
- Point your browser to http://esp8266.local, you should see a response.
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-pass"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
int ppm=0;
int count=0;
// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);
void setup(void) {
Serial.begin(9600);
// Connect to WiFi network
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Set up mDNS responder:
// - first argument is the domain name, in this example
// the fully-qualified domain name is "esp8266.local"
// - second argument is the IP address to advertise
// we send our IP address on the WiFi network
if (!MDNS.begin("co2mon")) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
// Start TCP (HTTP) server
server.begin();
Serial.println("TCP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void loop(void) {
MDNS.update();
int r=-1;
do{
r=readco2();
if( r==0 ){
// buffer is empty
break;
}else if (r == 1 ){
// read packet
Serial.print("ppm=");
Serial.println(ppm);
break;
}
}while(r==-1); // skip to sync
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while (client.connected() && !client.available()) {
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
String s;
if (req == "/") {
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += String(ppm) +" ppm,"+String(count);
s += "\r\n\r\n";
Serial.println("Sending 200");
} else {
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
client.print(s);
Serial.println("Done with client");
}
// co2のパケット解析
int readco2(){
char data[16];
if( Serial.available() > 0 ){
int syn=Serial.read();
if( syn==0x16 ){
while(!Serial.available());
int len=Serial.read();
for(int n=0;n<len;n++){
while(!Serial.available());
data[n]=Serial.read();
}
if( data[0]=0x01 ){
ppm=data[1]*256+data[2];
}
while(!Serial.available());
int cs=Serial.read();
count++;
if( count ==10000 ) count=0;
return 1;
}else{
// to be flush buffer
return -1;
}
}else{
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment