Configuration via serial using json

This commit is contained in:
Philip Johansson 2020-06-07 21:51:33 +02:00
parent 95b059e9d3
commit 89443b76a4
6 changed files with 71 additions and 3 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "lib/Adafruit_NeoPixel"]
path = lib/Adafruit_NeoPixel
url = https://github.com/adafruit/Adafruit_NeoPixel.git
[submodule "lib/ArduinoJson"]
path = lib/ArduinoJson
url = https://github.com/bblanchon/ArduinoJson.git

View File

@ -0,0 +1,14 @@
# Bedside table
An esp8266 powered **IKEA Tvärfot** with a Neopixel ring and a proximity sensor.
![](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.ikea.com%2Fbe%2Ffr%2Fimages%2Fproducts%2Ftvaerfot-table-lamp-black-white__0772756_PE756088_S5.JPG%3Ff%3Dxxs&f=1&nofb=1)
## Writing a configuration
Open a Serial Monitor at baudrate 9600 and paste the following json configuration adapted to your needs.
```
{
"hostname": "your_hostname",
"ssid": "your_ssid",
"pass": "[48..."
}
```

View File

@ -1,3 +1,5 @@
#pragma once
#include <Arduino.h>
class Config {

1
lib/ArduinoJson Submodule

@ -0,0 +1 @@
Subproject commit 6fb52c363849557c69485f97110371d0a4454432

View File

@ -4,6 +4,18 @@
namespace {
const int addr = 0;
void printConfig(Config::Data data)
{
Serial.printf("hostname: %s\n", data.hostname);
Serial.printf("ssid: %s\n", data.ssid);
Serial.printf("pass: %s\n", data.pass);
Serial.printf("brightness: %d\n", data.brightness);
Serial.printf("color: %d, %d, %d\n",
data.color.at(0),
data.color.at(1),
data.color.at(2));
}
} // namespace
Config& Config::Instance()
@ -17,6 +29,8 @@ void Config::load()
EEPROM.begin(sizeof(data));
EEPROM.get(addr, data);
EEPROM.end();
Serial.println("Loading config");
printConfig(data);
}
void Config::write()
@ -25,6 +39,9 @@ void Config::write()
EEPROM.put(addr, data);
EEPROM.commit();
EEPROM.end();
Serial.println("Saving config");
printConfig(data);
}
void Config::write(Data data)
{

View File

@ -1,4 +1,5 @@
#include <Arduino.h>
#include <ArduinoJson.h>
// My own includes
#include "config.h"
@ -11,7 +12,7 @@ const int sensorPin = A0;
const int ledPin = D8;
const int ledCount = 4;
Config &config = Config::Instance();
Config& config = Config::Instance();
MyLed myLed(ledPin, ledCount);
@ -26,6 +27,8 @@ void setup()
Serial.begin(9600);
myLed.initialize();
config.load();
}
float filter(float input)
@ -62,6 +65,35 @@ SensorData getDist()
.validReading = validReading};
}
void checkSerial()
{
String s = Serial.readString();
if (!s.isEmpty())
{
Serial.println(s.c_str());
DynamicJsonDocument doc(200);
deserializeJson(doc, s);
JsonObject obj = doc.as<JsonObject>();
String hostname = obj["hostname"];
String ssid = obj["ssid"];
String pass = obj["pass"];
Serial.printf("Parsed hostname: %s, ssid: %s, pass: %s\n",
hostname.c_str(),
ssid.c_str(),
String(pass).c_str());
if (hostname != "null" && ssid != "null" && pass != "null")
{
hostname.toCharArray(config.data.hostname,
sizeof(config.data.hostname));
ssid.toCharArray(config.data.ssid, sizeof(config.data.ssid));
pass.toCharArray(config.data.pass, sizeof(config.data.pass));
config.write();
}
}
}
void evalDist(SensorData data)
{
/************************************************
@ -140,8 +172,7 @@ void loop()
myLed.run();
// if (data.validReading)
// Serial.printf("Value: %f\n", data.distance);
// checkSerial();
delay(50);
}