Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ArduinoJson.h>
- #include <WiFi.h>
- #include <ESPmDNS.h>
- #include <AsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- #include <PubSubClient.h>
- /* You only need to format LittleFS the first time you run a
- test or else use the LITTLEFS plugin to create a partition
- https://github.com/lorol/arduino-esp32littlefs-plugin */
- WiFiClient wifiClient;
- PubSubClient client(wifiClient);
- uint8_t pin_led = 27;
- StaticJsonDocument<200> doc;
- StaticJsonDocument<300> mqtt;
- IPAddress APaddr(192,168,4,1);
- IPAddress gateway(192,168,4,1);
- IPAddress netmask(255,255,255,0);
- String hostname = "ESP32RF433";
- const char* APssid = "Balatech_AP";
- const char* APpassword = "password";
- String networksJson;
- bool connectedToServer = false;
- #include "fileFunctions.h"
- #include "wifiFunctions.h"
- #define FORMAT_LITTLEFS_IF_FAILED true
- AsyncWebServer server(80);
- void setup(){
- Serial.begin(115200);
- pinMode(pin_led,OUTPUT);
- if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
- Serial.println("LittleFS Mount Failed");
- return;
- }
- Serial.println( "Test complete" );
- autoConnectWiFi();
- if(!connectedToServer){
- if (!MQTTConnect()){
- Serial.println("MQTT Connection failed!");
- return;
- }
- }
- else
- {
- Serial.println("MQTT Connected!");
- }
- /*
- * Server should respond to these routes
- *
- * if not connected to station send the settings page to client
- */
- server.rewrite("/", "/settings.html").setFilter(ON_AP_FILTER);
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(LittleFS, "/home.html", "text/html");});
- server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(LittleFS, "/settings.html", "text/html",false,sendJson);
- });
- server.on("/getNetworkList", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(200,"text/plain", networksJson);
- });
- server.on("/saveWiFiCredentials", HTTP_POST, wifiSave);
- server.on("/saveMQTTCredentials", HTTP_POST, saveMQTT);
- // server.on("/reset", HTTP_POST, resetAll);
- server.on("/devices", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(LittleFS, "/listdevices.html", "text/html");});
- server.on("/details", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(LittleFS, "/details.html", "text/html",false, readSettings);});
- server.on("/delSW", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(LittleFS, "/delSW.html", "text/html");});
- server.on("/edit", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(LittleFS, "/addSW.html", "text/html");});
- server.serveStatic("/", LittleFS, "/"); /* this servers the css files and favicon file automatically */
- server.begin();
- }
- void loop(){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement