1
0
mirror of https://github.com/daniviga/bite.git synced 2024-11-22 21:16:12 +01:00

Store ESP32 settings in the nvs

This commit is contained in:
Daniele Viganò 2021-03-24 21:54:41 +01:00
parent adcaa6104f
commit 22cbc5b8a9
Signed by: dani
GPG Key ID: DB49AFC03C40EE02
6 changed files with 155 additions and 31 deletions

1
.gitignore vendored
View File

@ -132,3 +132,4 @@ dmypy.json
##
production.py
settings.h

View File

@ -20,23 +20,19 @@
#include <EEPROM.h>
#include <Ethernet.h>
#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() {

View File

@ -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ò <daniele@vigano.me>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#define SERIAL "uno_1"
#define REMOTE_IP {192, 168, 0, 1}
#define REMOTE_PORT 80

View File

@ -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ò <daniele@vigano.me>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <Preferences.h>
#include <Ethernet.h>
#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 */
}

View File

@ -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ò <daniele@vigano.me>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#define SERIAL "<fillme>"
#define REMOTE_IP {192, 168, 0, 1}
#define REMOTE_PORT 80
#define SECRET_SSID "<fillme>"
#define SECRET_PASSWORD "<fillme>"

View File

@ -18,7 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <EEPROM.h>
#include <Preferences.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
@ -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");