Configuration via serial using json
This commit is contained in:
parent
95b059e9d3
commit
89443b76a4
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -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
|
||||
|
14
README.md
14
README.md
@ -0,0 +1,14 @@
|
||||
# Bedside table
|
||||
An esp8266 powered **IKEA Tvärfot** with a Neopixel ring and a proximity sensor.
|
||||
|
||||

|
||||
|
||||
## 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..."
|
||||
}
|
||||
```
|
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class Config {
|
||||
|
1
lib/ArduinoJson
Submodule
1
lib/ArduinoJson
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 6fb52c363849557c69485f97110371d0a4454432
|
@ -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)
|
||||
{
|
||||
|
37
src/main.cpp
37
src/main.cpp
@ -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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user