diff --git a/.gitignore b/.gitignore index e182b94..0c7325a 100644 --- a/.gitignore +++ b/.gitignore @@ -132,3 +132,4 @@ dmypy.json ## production.py +settings.h diff --git a/arduino/eeprom_prog/eeprom_prog.ino b/arduino/eeprom_prog/eeprom_prog.ino index 3892318..7010281 100644 --- a/arduino/eeprom_prog/eeprom_prog.ino +++ b/arduino/eeprom_prog/eeprom_prog.ino @@ -20,23 +20,19 @@ #include #include +#include "settings.h" #define ERASE_FIRST 0 const byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -const char serial[] = "abcd1234"; +const char serial[] = SERIAL; struct netConfig { - IPAddress address; - unsigned int port; -}; - -netConfig config = { - {192, 168, 10, 123}, - 80 -}; + IPAddress address = REMOTE_IP; + unsigned int port = REMOTE_PORT; +} config; void setup() { diff --git a/arduino/eeprom_prog/settings.h.tmpl b/arduino/eeprom_prog/settings.h.tmpl new file mode 100644 index 0000000..830e0d6 --- /dev/null +++ b/arduino/eeprom_prog/settings.h.tmpl @@ -0,0 +1,23 @@ +/* -*- coding: utf-8 -*- +* vim: tabstop=2 shiftwidth=2 softtabstop=2 syntax=c +* +* BITE - A Basic/IoT/Example +* Copyright (C) 2020-2021 Daniele ViganĂ² +* +* BITE is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* BITE is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see . +*/ + +#define SERIAL "uno_1" +#define REMOTE_IP {192, 168, 0, 1} +#define REMOTE_PORT 80 diff --git a/esp32/nvs_prog/nvs_prog.ino b/esp32/nvs_prog/nvs_prog.ino new file mode 100644 index 0000000..cb05d1f --- /dev/null +++ b/esp32/nvs_prog/nvs_prog.ino @@ -0,0 +1,75 @@ +/* -*- coding: utf-8 -*- +* vim: tabstop=2 shiftwidth=2 softtabstop=2 +* +* BITE - A Basic/IoT/Example +* Copyright (C) 2020-2021 Daniele ViganĂ² +* +* BITE is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* BITE is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see . +*/ + +#include +#include +#include "settings.h" + +#define ERASE_FIRST 1 + +Preferences preferences; +const char* serial = SERIAL; + +const char* ssid = SECRET_SSID; +const char* password = SECRET_PASSWORD; + +struct netConfig { + IPAddress address = REMOTE_IP; + unsigned int port = REMOTE_PORT; +} config; + +void setup() { + + Serial.begin(115200); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + preferences.begin("iot", false); +#if ERASE_FIRST + Serial.println("Erasing IoT data"); + preferences.clear(); +#endif + Serial.print("Writing IoT data"); + preferences.putString("serial", serial); + Serial.print("."); + Serial.println("."); + + preferences.putBytes("config", &config, sizeof(config)); + Serial.println("Committing..."); + preferences.end(); + Serial.println("IoT data written!"); + + preferences.begin("wifi", false); +#if ERASE_FIRST + Serial.println("Erasing WiFI data"); + preferences.clear(); +#endif + Serial.println("Writing WiFi data"); + preferences.putString("ssid", ssid); + preferences.putString("password", password); + Serial.println("Committing..."); + preferences.end(); + Serial.println("WiFi data written!"); +} + +void loop() { + /* Empty loop */ +} diff --git a/esp32/nvs_prog/settings.h.tmpl b/esp32/nvs_prog/settings.h.tmpl new file mode 100644 index 0000000..b260549 --- /dev/null +++ b/esp32/nvs_prog/settings.h.tmpl @@ -0,0 +1,25 @@ +/* -*- coding: utf-8 -*- +* vim: tabstop=2 shiftwidth=2 softtabstop=2 syntax=c +* +* BITE - A Basic/IoT/Example +* Copyright (C) 2020-2021 Daniele ViganĂ² +* +* BITE is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* BITE is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see . +*/ + +#define SERIAL "" +#define REMOTE_IP {192, 168, 0, 1} +#define REMOTE_PORT 80 +#define SECRET_SSID "" +#define SECRET_PASSWORD "" diff --git a/esp32/rssiHall/rssiHall.ino b/esp32/rssiHall/rssiHall.ino index 202ac51..e18413f 100644 --- a/esp32/rssiHall/rssiHall.ino +++ b/esp32/rssiHall/rssiHall.ino @@ -18,7 +18,7 @@ * along with this program. If not, see . */ -#include +#include #include #include #include @@ -33,6 +33,8 @@ // const String serverName = "sensor.server.domain"; const size_t capacity = 2 * JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(2) + 20; +Preferences preferences; + DynamicJsonDocument telemetry(capacity); JsonObject payload = telemetry.createNestedObject("payload"); @@ -45,20 +47,12 @@ bool NTPValid = false; WiFiClient ethClient; PubSubClient clientMQTT(ethClient); -const char* ssid = ""; -const char* password = ""; -const char* serial = "esp32_1"; - struct netConfig { IPAddress address; unsigned int port; -}; - -netConfig config = { - {192, 168, 10, 123}, - 80 -}; +} config; +char* serial; const String apiURL = "/api/device/subscribe/"; const String telemetryURL = "/telemetry/"; @@ -67,22 +61,32 @@ void setup(void) { StaticJsonDocument<64> api; - /* - int eeAddress = 0; + preferences.begin("iot"); + // Get the serial number from flash + serial = strdup(preferences.getString("serial").c_str()); - EEPROM.get(eeAddress, serial); - eeAddress += sizeof(serial); - EEPROM.get(eeAddress, config); - */ + // Get network configuration + size_t _len = preferences.getBytesLength("config"); + char _buffer[_len]; + preferences.getBytes("config", &_buffer, _len); + memcpy(&config, _buffer, _len); + preferences.end(); - Serial.println("Starting connecting WiFi."); + // Get WiFi parameters + preferences.begin("wifi"); + const char* _ssid = strdup(preferences.getString("ssid").c_str()); + const char* _password = strdup(preferences.getString("password").c_str()); + preferences.end(); + + Serial.print("Starting connecting WiFi to "); + Serial.print(_ssid); delay(10); - WiFi.begin(ssid, password); + WiFi.begin(_ssid, _password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } - Serial.println("WiFi connected"); + Serial.println("\nWiFi connected"); Serial.print("IoT #"); Serial.print(serial); @@ -90,7 +94,7 @@ void setup(void) { Serial.println(WiFi.localIP()); Serial.println(); Serial.print("Connecting to: "); - Serial.print(config.address); + Serial.print(config.address.toString()); Serial.print(":"); Serial.print(config.port); Serial.print(" every "); @@ -173,7 +177,7 @@ void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocu ethClient.print(URL); ethClient.println(" HTTP/1.1"); ethClient.print("Host: "); - ethClient.print(postAPI.address); + ethClient.print(postAPI.address.toString()); ethClient.print(":"); ethClient.println(postAPI.port); ethClient.println("Content-Type: application/json");