Advertisement
Guest User

Untitled

a guest
Jan 27th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <DNSServer.h>
  2. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  3. #include <ESP8266WebServer.h>
  4. #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
  5. #include "PMS.h"
  6.  
  7. PMS pms(Serial);
  8. PMS::DATA data;
  9.  
  10. ESP8266WebServer server(80);
  11.  
  12. // Setup WiFi Configuration
  13. void setupWiFi()
  14. {
  15.   while (WiFi.status() != WL_CONNECTED)
  16.   {
  17.     delay(500);
  18.     Serial.print(".");
  19.   }
  20.  
  21.   Serial.print("Connected! Open http://");
  22.   Serial.print(WiFi.localIP());
  23.   Serial.println(" in your browser");
  24. }
  25.  
  26. // Setup HTTP Server
  27. void setupHttpServer()
  28. {
  29.   server.begin();
  30.   // Info Methods
  31.   server.on("/json", HTTP_GET, []() {
  32.     server.sendHeader("Access-Control-Allow-Origin", "*");
  33.     server.sendHeader("access-control-allow-credentials", "false");
  34.     server.sendHeader("access-control-allow-headers", "x-requested-with");
  35.     server.sendHeader("access-control-allow-methods", "GET,OPTIONS");
  36.     sendInfo();
  37.   });
  38.   server.on("/json", HTTP_OPTIONS, []() {
  39.     server.sendHeader("Access-Control-Allow-Origin", "*");
  40.     server.sendHeader("access-control-allow-credentials", "false");
  41.     server.sendHeader("access-control-allow-headers", "x-requested-with");
  42.     server.sendHeader("access-control-allow-methods", "GET,OPTIONS");
  43.     server.send(204);
  44.   });
  45. }
  46.  
  47. // Generates response for Info Request
  48. void sendInfo()
  49. {
  50.   String json = "{";
  51.   json += "\"PM 1.0\":" + String(data.PM_AE_UG_1_0) + ",";
  52.   json += "\"PM 2.5\":" + String(data.PM_AE_UG_2_5) + ",";
  53.   json += "\"PM 10.0\":" + String(data.PM_AE_UG_10_0);
  54.   json += "}";
  55.   server.send(200, "text/json", json);
  56.   json = String();
  57. }
  58.  
  59. void setup()
  60. {
  61.   // put your setup code here, to run once:
  62.   Serial.begin(9600);  // GPIO1, GPIO3 (TX/RX pin on ESP-12E Development Board)
  63.   Serial1.begin(9600); // GPIO2 (D4 pin on ESP-12E Development Board)
  64.  
  65.   //WiFiManager
  66.   //Local intialization. Once its business is done, there is no need to keep it around
  67.   WiFiManager wifiManager;
  68.   //reset saved settings
  69.   wifiManager.resetSettings();
  70.  
  71.   //set custom ip for portal
  72.   //wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
  73.  
  74.   //fetches ssid and pass from eeprom and tries to connect
  75.   //if it does not connect it starts an access point with the specified name
  76.   //here  "AutoConnectAP"
  77.   //and goes into a blocking loop awaiting configuration
  78.   wifiManager.autoConnect("AutoConnectAP");
  79.   //or use this for auto generated name ESP + ChipID
  80.   //wifiManager.autoConnect();
  81.  
  82.   //if you get here you have connected to the WiFi
  83.   Serial.println("connected...yeey :)");
  84.   setupWiFi();
  85.   setupHttpServer();
  86. }
  87.  
  88. void loop()
  89. {
  90.   // put your main code here, to run repeatedly:
  91.   if (pms.read(data))
  92.   {
  93.     Serial1.println("Data:");
  94.  
  95.     Serial1.print("PM 1.0 (ug/m^3): ");
  96.     Serial1.println(data.PM_AE_UG_1_0);
  97.  
  98.     Serial1.print("PM 2.5 (ug/m^3): ");
  99.     Serial1.println(data.PM_AE_UG_2_5);
  100.  
  101.     Serial1.print("PM 10.0 (ug/m^3): ");
  102.     Serial1.println(data.PM_AE_UG_10_0);
  103.  
  104.     Serial1.println();
  105.   }
  106.   server.handleClient();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement