Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DNSServer.h>
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- #include <ESP8266WebServer.h>
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
- #include "PMS.h"
- PMS pms(Serial);
- PMS::DATA data;
- ESP8266WebServer server(80);
- // Setup WiFi Configuration
- void setupWiFi()
- {
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.print("Connected! Open http://");
- Serial.print(WiFi.localIP());
- Serial.println(" in your browser");
- }
- // Setup HTTP Server
- void setupHttpServer()
- {
- server.begin();
- // Info Methods
- server.on("/json", HTTP_GET, []() {
- server.sendHeader("Access-Control-Allow-Origin", "*");
- server.sendHeader("access-control-allow-credentials", "false");
- server.sendHeader("access-control-allow-headers", "x-requested-with");
- server.sendHeader("access-control-allow-methods", "GET,OPTIONS");
- sendInfo();
- });
- server.on("/json", HTTP_OPTIONS, []() {
- server.sendHeader("Access-Control-Allow-Origin", "*");
- server.sendHeader("access-control-allow-credentials", "false");
- server.sendHeader("access-control-allow-headers", "x-requested-with");
- server.sendHeader("access-control-allow-methods", "GET,OPTIONS");
- server.send(204);
- });
- }
- // Generates response for Info Request
- void sendInfo()
- {
- String json = "{";
- json += "\"PM 1.0\":" + String(data.PM_AE_UG_1_0) + ",";
- json += "\"PM 2.5\":" + String(data.PM_AE_UG_2_5) + ",";
- json += "\"PM 10.0\":" + String(data.PM_AE_UG_10_0);
- json += "}";
- server.send(200, "text/json", json);
- json = String();
- }
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(9600); // GPIO1, GPIO3 (TX/RX pin on ESP-12E Development Board)
- Serial1.begin(9600); // GPIO2 (D4 pin on ESP-12E Development Board)
- //WiFiManager
- //Local intialization. Once its business is done, there is no need to keep it around
- WiFiManager wifiManager;
- //reset saved settings
- wifiManager.resetSettings();
- //set custom ip for portal
- //wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
- //fetches ssid and pass from eeprom and tries to connect
- //if it does not connect it starts an access point with the specified name
- //here "AutoConnectAP"
- //and goes into a blocking loop awaiting configuration
- wifiManager.autoConnect("AutoConnectAP");
- //or use this for auto generated name ESP + ChipID
- //wifiManager.autoConnect();
- //if you get here you have connected to the WiFi
- Serial.println("connected...yeey :)");
- setupWiFi();
- setupHttpServer();
- }
- void loop()
- {
- // put your main code here, to run repeatedly:
- if (pms.read(data))
- {
- Serial1.println("Data:");
- Serial1.print("PM 1.0 (ug/m^3): ");
- Serial1.println(data.PM_AE_UG_1_0);
- Serial1.print("PM 2.5 (ug/m^3): ");
- Serial1.println(data.PM_AE_UG_2_5);
- Serial1.print("PM 10.0 (ug/m^3): ");
- Serial1.println(data.PM_AE_UG_10_0);
- Serial1.println();
- }
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement