Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <WiFiClient.h>
- #include <WiFiClientSecure.h>
- #include <ESP8266WebServer.h>
- #include <LittleFS.h>
- #include <PubSubClient.h>
- #include <ESP8266WiFi.h>
- #include <SPI.h>
- #include <MFRC522.h>
- #define SS_PIN D8
- #define RST_PIN D3
- MFRC522 mfrc522(SS_PIN, RST_PIN);
- ESP8266WebServer server(80);
- WiFiClient wifiClient;
- /* MQTT */
- const char* mqttServer = "192.168.1.55";
- const int mqttPort = 1883;
- const char* mqttUser = "";
- const char* mqttPassword = "";
- const char* mqttTopic = "timestamp_topic";
- const char* ssidAP = "Chevalet";
- const char* passwordAP = "Password";
- const char* configFile = "/config.txt";
- const char* adminUsername = "Admin";
- const char* adminPassword = "flagstuff";
- int connectionAttempts = 0;
- char* last_card_id = "";
- String ssid;
- String password;
- //String apiKey;
- String ID;
- String owner;
- String hostnameAP = "Chevalet Libre";
- //String Server;
- PubSubClient mqttClient(wifiClient);
- void mqttCallback(char* topic, byte* payload, unsigned int length)
- {
- // Handle incoming MQTT messages if needed
- // You can add your own logic here
- }
- void mqttConnect()
- {
- Serial.print("Connecting to MQTT...");
- mqttClient.setServer(mqttServer, mqttPort);
- mqttClient.setCallback(mqttCallback);
- while (!mqttClient.connected())
- {
- if (mqttClient.connect("WemosD1MiniPro", mqttUser, mqttPassword))
- {
- Serial.println("Connected to MQTT!");
- mqttClient.subscribe(mqttTopic);
- }
- else
- {
- Serial.print("Failed, rc=");
- Serial.print(mqttClient.state());
- Serial.println(" Retrying in 5 seconds...");
- delay(5000);
- }
- }
- }
- /* end MQTT */
- void handleRoot()
- {
- if (!server.authenticate(adminUsername, adminPassword)) { return server.requestAuthentication(); }
- String _ssid(ssid);
- String _password(password);
- String _owner(owner);
- String _ID(ID);
- String _mqttServer(mqttServer);
- String _mqttPort(mqttPort);
- String _mqttUser(mqttUser);
- String _mqttPassword(mqttPassword);
- String _mqttTopic(mqttTopic);
- String title = "<title>" + owner + ID + "</title>";
- String html = "";
- html += "<html>";
- html += " <head>" + title + "</head>";
- html += " <body>";
- html += " <h1>Chevalet Setup</h1>";
- html += " <form method='post' action='/save'>";
- html += " <label>SSID: </label><br><input type='text' name='ssid' value='" + _ssid + "' ><br>";
- html += " <label>Password: </label><br><input type='password' name='password' value='" + _password + "' ><br>";
- html += " <label>Owner: </label><br><input type='text' name='owner' value='" + _owner + "' ><br>";
- html += " <label>ID: </label><br><input type='text' name='ID' value='" + _ID + "' ><br>";
- html += " <label>MQTT Server </label><br><input type='text' name='mqttServer' value='" + _mqttServer + "' ><br>";
- html += " <label>MQTT Port </label><br><input type='text' name='mqttPort' value='" + _mqttPort + "' ><br>";
- html += " <label>MQTT Username</label><br><input type='text' name='mqttUser' value='" + _mqttUser + "' ><br>";
- html += " <label>MQTT Password</label><br><input type='text' name='mqttPassword' value='" + _mqttPassword + "'><br>";
- html += " <label>MQTT Topic </label><br><input type='text' name='mqttTopic' value='" + _mqttTopic + "' ><br>";
- html += " <inputtype='submit' value='Save and restart'>";
- html += " </form>";
- html += " </body>";
- html += "</html>";
- server.send(200, "text/html", html);
- }
- void handleBravo()
- {
- if (!server.authenticate(adminUsername, adminPassword)) { return server.requestAuthentication(); }
- String html = "<html><title>"+owner + ID+"</title>";
- html += "<body><h1>CONGRATULATION!</h1>";
- html += "<script>function check_card(){}</script>";
- html += "<p>Your order "+ ID +" is ready</p>";
- html += "<script>settimout(check_card; 3000);</script>";
- html += "</body></html>";
- server.send(200, "text/html", html);
- }
- ////
- void handleSave()
- {
- if (!server.authenticate(adminUsername, adminPassword)) { return server.requestAuthentication(); }
- ssid = server.arg("ssid");
- password = server.arg("password");
- owner = server.arg("owner");
- ID = server.arg("ID");
- mqttServer = server.arg("mqttServer");
- mqttPort = server.arg("mqttPort");
- mqttUser = server.arg("mqttUser");
- mqttPassword = server.arg("mqttPassword");
- mqttTopic = server.arg("mqttTopic");
- File configFile = LittleFS.open("/config.txt", "w");
- if (!configFile) { Serial.println("Error opening configuration file"); return; }
- configFile.println(ssid);
- configFile.println(password);
- configFile.println(owner);
- configFile.println(ID);
- configFile.println(mqttServer);
- configFile.println(mqttPort);
- configFile.println(mqttUser);
- configFile.println(mqttPassword);
- configFile.println(mqttTopic);
- configFile.close();
- server.send(200, "text/plain", "Configuration saved. Restart in progress...");
- delay(2000);
- ESP.restart();
- }
- bool connectToWifi()
- {
- connectionAttempts = 0;
- delay(1000);
- Serial.print(".");
- connectionAttempts++;
- if (connectionAttempts > 20)
- {
- Serial.println("");
- Serial.println("WiFi network failed to connect. Startup in AP mode");
- WiFi.disconnect();
- WiFi.hostname(hostnameAP);
- WiFi.softAP(ssidAP, passwordAP);
- WiFi.hostname(hostnameAP);
- Serial.print("Access point IP address: ");
- Serial.println(WiFi.softAPIP());
- Serial.print("Mot de passe du point d'accès: ");
- Serial.println(passwordAP);
- server.on("/", handleRoot);
- server.on("/save", HTTP_POST, handleSave);
- //server.on("/led", handleLed); // Ajout de la gestion de l'allumage des LEDs
- server.begin();
- return true;
- }
- else
- {
- return false;
- }
- }
- void handleSuccess()
- {
- if (!server.authenticate(adminUsername, adminPassword)) { return server.requestAuthentication(); }
- String html = "<html><title>" + owner + ID + "</title>";
- html += "<body><h1>Successful connection</h1>";
- html += "<p>SSID: " + ssid + "</p>";
- html += "<p>Owner: " + owner + "</p>";
- html += "<p>Server: " + mqttServer + "</p>";
- html += "<label for='ID'>Number:</label><br>";
- html += "<input type='number' name='ID' id='ID' value='" + ID + "'><br>";
- //html += "<a href='/" + apiKey + "'>Go To Animation Page</a>"; // Ajout du lien
- html += "<form method='post' action='/save'>";
- html += "<input type='submit' value='Save'>";
- html += "</form>";
- html += "<form method='post' action='/cancel'>";
- html += "<input type='submit' value='Cancel'>";
- html += "</form>";
- html += "<form method='post' action='/reset'>";
- html += "<input type='submit' value='Erase configuration and restart in AP mode'>";
- html += "</form>";
- html += "<form method='post' action='/led'>";
- html += "<input type='submit' value='Test LEDs'>";
- html += "</form>";
- html += "</body></html>";
- server.send(200, "text/html", html);
- }
- void handleReset()
- {
- if (!server.authenticate(adminUsername, adminPassword)) { return server.requestAuthentication(); }
- LittleFS.remove(configFile);
- server.send(200, "text/plain", "Configuration erased. Restart in progress...");
- delay(2000);
- ESP.restart();
- }
- void handleCancel()
- {
- if (!server.authenticate(adminUsername, adminPassword)) { return server.requestAuthentication(); }
- server.send(200, "text/plain", "Configuration cancelled. Restart in progress...");
- delay(2000);
- ESP.restart();
- }
- void handleNotFound()
- {
- String html = "<html><body>";
- html += "<h1>Page not found</h1>";
- html += "<p>The requested page does not exist.</p>";
- html += "</body></html>";
- server.send(404, "text/html", html);
- }
- void onStationModeGotIP(const WiFiEventStationModeGotIP& event)
- {
- server.sendHeader("Location", String("http://") + WiFi.localIP().toString(), true);
- server.send(302, "text/plain", "");
- }
- void ConnectedAnimation()
- {
- }
- /////////////////////////////////////////////////////
- char* checkCard()
- {
- String cardData = "";
- if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
- {
- String content = "Card Detected!<br>Card ID: ";
- for (byte i = 0; i < mfrc522.uid.size; i++)
- {
- content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
- content.concat(String(mfrc522.uid.uidByte[i], HEX));
- }
- cardData = "data: " + content;
- }
- char * val;
- strcpy(val, cardData.c_str());
- return val;
- }
- void setup()
- {
- Serial.begin(115200);
- delay(100);
- if (!LittleFS.begin()) { Serial.println("Error initializing LittleFS"); return; }
- if (!LittleFS.exists(configFile))
- {
- Serial.println("No configuration file found. Startup in AP mode");
- WiFi.softAP(ssidAP, passwordAP);
- WiFi.hostname(hostnameAP);
- Serial.print("Access point IP address: ");
- Serial.println(WiFi.softAPIP());
- Serial.print("Access point password: ");
- Serial.println(passwordAP);
- server.on("/", handleRoot);
- server.on("/save", HTTP_POST, handleSave);
- //server.on("/" + apiKey, handleAnimation); //
- server.begin();
- return;
- }
- File configFile = LittleFS.open("/config.txt", "r");
- if (!configFile) { Serial.println("Error opening configuration file"); return; }
- ssid = configFile.readStringUntil('\n'); ssid.trim();
- password = configFile.readStringUntil('\n'); password.trim();
- owner = configFile.readStringUntil('\n'); owner.trim();
- ID = configFile.readStringUntil('\n'); ID.trim();
- mqttServer = configFile.readStringUntil('\n'); mqttServer.trim();
- mqttPort = configFile.readStringUntil('\n'); mqttPort.trim();
- mqttUser = configFile.readStringUntil('\n'); mqttUser.trim();
- mqttPassword = configFile.readStringUntil('\n'); mqttPassword.trim();
- mqttTopic = configFile.readStringUntil('\n'); mqttTopic.trim();
- configFile.close();
- WiFi.hostname(owner + ID);
- WiFi.begin(ssid.c_str(), password.c_str());
- int connectionAttempts = 0;
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(1000);
- Serial.print(".");
- connectionAttempts++;
- if (connectionAttempts > 20)
- {
- Serial.println("");
- Serial.println("WiFi network failed to connect. Startup in AP mode");
- WiFi.disconnect();
- WiFi.hostname(hostnameAP);
- WiFi.softAP(ssidAP, passwordAP);
- WiFi.hostname(hostnameAP);
- //APAnimation();
- Serial.print("Access point IP address: ");
- Serial.println(WiFi.softAPIP());
- Serial.print("Mot de passe du point d'accès: ");
- Serial.println(passwordAP);
- server.on("/", handleRoot);
- server.on("/save", HTTP_POST, handleSave);
- //server.on("/led", handleLed); // Ajout de la gestion de l'allumage des LEDs
- server.begin();
- return;
- }
- }
- Serial.println("");
- Serial.println("WiFi network connection established");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- WiFi.hostname(owner + ID);
- ConnectedAnimation();
- server.on("/", handleSuccess);
- server.on("/save", HTTP_POST, handleSave);
- server.on("/cancel", HTTP_POST, handleCancel);
- server.on("/reset", HTTP_POST, handleReset);
- server.onNotFound(handleNotFound);
- server.begin();
- mqttConnect();
- }
- void loop()
- {
- server.handleClient();
- /**/
- if (wifiClient.status() == WL_CONNECTED)
- {
- if (mqttClient.connected())
- {
- const char* cardid = checkCard();
- if (strcmp(cardid, "")==0)
- {
- last_card_id = cardid;
- mqttClient.publish(mqttTopic, cardid);
- //Serial.println("Published: " + cardid);
- }
- delay(1000);
- }
- else
- {
- mqttConnect();
- }
- }
- else
- {
- if (!connectToWifi())
- {
- // run config
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment