Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- Rui Santos
- Complete instructions at https://RandomNerdTutorials.com/esp8266-web-server-gauges/
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- *********/
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESPAsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- #include "LittleFS.h"
- #include <Arduino_JSON.h>
- #include <Adafruit_BMP280.h>
- #include <Adafruit_Sensor.h>
- // define device I2C address: 0x76 or 0x77 (0x77 is library default address)
- #define BMP280_I2C_ADDRESS 0x76
- Adafruit_BMP280 bmp280;
- // Replace with your network credentials
- const char* ssid = "YOUR WIFI SSID";
- const char* password = "*YOUR WIFI PASSWORD";
- // Create AsyncWebServer object on port 80
- AsyncWebServer server(80);
- // Create an Event Source on /events
- AsyncEventSource events("/events");
- // Json Variable to Hold Sensor Readings
- JSONVar readings;
- // Timer variables
- unsigned long lastTime = 0;
- unsigned long timerDelay = 30000;
- // Create a sensor object
- // Get Sensor Readings and return JSON object
- String getSensorReadings(){
- readings["temperature"] = String(bmp280.readTemperature());
- readings["humidity"] = String(bmp280.readPressure());
- String jsonString = JSON.stringify(readings);
- return jsonString;
- }
- // Initialize LittleFS
- void initFS() {
- if (!LittleFS.begin()) {
- Serial.println("An error has occurred while mounting LittleFS");
- }
- Serial.println("LittleFS mounted successfully");
- }
- // Initialize WiFi
- void initWiFi() {
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.print("Connecting to WiFi ..");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(1000);
- }
- Serial.println(WiFi.localIP());
- }
- void setup() {
- Serial.begin(115200);
- if (!bmp280.begin(BMP280_I2C_ADDRESS))
- {
- Serial.println("Could not find a valid BMP280 sensor, check wiring!");
- while (1);
- }
- Serial.println("BMP sensor init succesfully !");
- initWiFi();
- initFS();
- // Web Server Root URL
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(LittleFS, "/index.html", "text/html");
- });
- server.serveStatic("/", LittleFS, "/");
- // Request for the latest sensor readings
- server.on("/readings", HTTP_GET, [](AsyncWebServerRequest *request){
- String json = getSensorReadings();
- request->send(200, "application/json", json);
- json = String();
- });
- events.onConnect([](AsyncEventSourceClient *client){
- if(client->lastId()){
- Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
- }
- // send event with message "hello!", id current millis
- // and set reconnect delay to 1 second
- client->send("hello!", NULL, millis(), 10000);
- });
- server.addHandler(&events);
- // Start server
- server.begin();
- }
- void loop() {
- if ((millis() - lastTime) > timerDelay) {
- // Send Events to the client with the Sensor Readings Every 30 seconds
- events.send("ping",NULL,millis());
- events.send(getSensorReadings().c_str(),"new_readings" ,millis());
- lastTime = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement