Skip to content

Commit

Permalink
Some setup stuff for ESP Web tools
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcrisci committed Sep 1, 2024
1 parent 087e344 commit bfc663f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 31 deletions.
3 changes: 1 addition & 2 deletions include/GeoGlowTileController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <UUID.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include "MQTTClient.h"
#include "NanoleafApiWrapper.h"
#include "ColorPaletteAdapter.h"
Expand All @@ -20,7 +20,6 @@ void loadConfigFromFile();
void saveConfigToFile();
void generateMDNSNanoleafURL();
void attemptNanoleafConnection();
void setupWiFiManager();
void setupMQTTClient();
void publishStatus();
void saveConfigCallback();
Expand Down
21 changes: 21 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>ESP Web Tools</title>
<script
type="module"
src="https://unpkg.com/esp-web-tools@8.0.0/dist/esp-web-tools.js"
></script>
<script
type="module"
src="https://unpkg.com/esp-web-tools@8.0.0/dist/components/espweb-terminal.js"
></script>
</head>
<body>
<h1>Upload Firmware and Configure Your ESP</h1>
<esp-web-install-button manifest="./manifest.json"></esp-web-install-button>
<h2>Terminal for Configuration</h2>
<esp-web-terminal></esp-web-terminal>
</body>
</html>
20 changes: 20 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"manifest": {
"application": {
"name": "GeoGlow ESP Firmware",
"email": "ncrisci@th-koeln.de"
}
},
"builds": [
{
"chipFamily": "ESP8266",
"improv": true,
"parts": [
{
"path": "firmware/firmware.bin",
"offset": 0
}
]
}
]
}
90 changes: 61 additions & 29 deletions src/GeoGlow-TileController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ const size_t CONFIG_JSON_SIZE = 1024;
const int MDNS_RETRIES = 5; // Number of times to retry mDNS query
const int MDNS_RETRY_DELAY = 2000; // Delay between retries in milliseconds

// Wifi Creds
char wifiSSID[40] = "";
char wifiPassword[40] = "";

// Global Variables
WiFiManager wifiManager;
WiFiClient wifiClient;
MQTTClient mqttClient(wifiClient);
NanoleafApiWrapper nanoleaf(wifiClient);
Expand Down Expand Up @@ -37,7 +40,20 @@ void setup() {

initializeUUID();
loadConfigFromFile();
setupWiFiManager();

// Prompt for WiFi credentials
if (strlen(wifiSSID) == 0 || strlen(wifiPassword) == 0) {
Serial.println("Please enter WiFi credentials.");
Serial.println("SSID: ");
} else {
WiFi.begin(wifiSSID, wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}

generateMDNSNanoleafURL(); // Always generate MDNS Nanoleaf URL
setupMQTTClient();
attemptNanoleafConnection();
Expand All @@ -48,8 +64,43 @@ void setup() {
}

void loop() {
static String inputString = "";
static int inputState = 0; // 0: Waiting for SSID, 1: Waiting for Password

mqttClient.loop();

if (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == "\n") {
if (inputState == 0) {
strcpy(wifiSSID, inputString.c_str());
Serial.println("Password: ");
inputState = 1;
} else if (inputState == 1) {
strcpy(wifiPassword, inputString.c_str());

saveConfigToFile();

WiFi.begin(wifiSSID, wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");

// Proceed to other setup steps
generateMDNSNanoleafURL();
setupMQTTClient();
attemptNanoleafConnection();

inputState = 0;
}
inputString = "";
} else {
inputString += inChar;
}
}

if (millis() - lastPublishTime >= PUBLISH_INTERVAL) {
publishStatus();
lastPublishTime = millis();
Expand Down Expand Up @@ -97,6 +148,10 @@ void loadConfigFromFile() {
strcpy(deviceId, jsonConfig["deviceId"]);
strcpy(friendId, jsonConfig["friendId"]);

// Load WiFi credentials
strcpy(wifiSSID, jsonConfig["wifiSSID"]);
strcpy(wifiPassword, jsonConfig["wifiPassword"]);

configFile.close();
Serial.println("Parsed JSON config");
}
Expand Down Expand Up @@ -149,6 +204,10 @@ void saveConfigToFile() {
jsonConfig["friendId"] = friendId;
jsonConfig["deviceId"] = deviceId;

// Save WiFi credentials
jsonConfig["wifiSSID"] = wifiSSID;
jsonConfig["wifiPassword"] = wifiPassword;

serializeJson(jsonConfig, configFile);
configFile.close();
Serial.println("Config saved successfully");
Expand All @@ -174,33 +233,6 @@ void attemptNanoleafConnection() {
Serial.println("Nanoleaf connected");
}

void setupWiFiManager() {
WiFiManagerParameter customMqttBroker("mqttBroker", "MQTT Broker", mqttBroker, 40);
WiFiManagerParameter customMqttPort("mqttPort", "MQTT Port", mqttPort, 6);
WiFiManagerParameter customFriendId("friendId", "Friend ID", friendId, 36);

wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&customMqttBroker);
wifiManager.addParameter(&customMqttPort);
wifiManager.addParameter(&customFriendId);

if (!wifiManager.autoConnect("GeoGlow")) {
Serial.println("Failed to connect and hit timeout");
delay(3000);
ESP.restart();
delay(5000);
}

Serial.println("Connected");

strcpy(mqttBroker, customMqttBroker.getValue());
strcpy(mqttPort, customMqttPort.getValue());
strcpy(friendId, customFriendId.getValue());

if (strlen(friendId) >= sizeof(friendId) - 1) friendId[sizeof(friendId) - 1] = '\0';
if (strlen(deviceId) >= sizeof(deviceId) - 1) deviceId[sizeof(deviceId) - 1] = '\0';
}

void setupMQTTClient() {
mqttClient.setup(mqttBroker, String(mqttPort).toInt(), friendId, deviceId);
mqttClient.addTopicAdapter(&colorPaletteAdapter);
Expand Down

0 comments on commit bfc663f

Please sign in to comment.