Skip to content

Commit

Permalink
Merge pull request #8 from lloydrichards/feature/DeviceInfo
Browse files Browse the repository at this point in the history
Feature/device info
  • Loading branch information
lloydrichards authored Apr 15, 2020
2 parents 9daaa57 + ff1aaee commit d2f67ba
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 6 deletions.
158 changes: 157 additions & 1 deletion lib/MimirTesting/MimirTesting.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "MimirTesting.h"
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include "SPIFFS.h"
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
Expand All @@ -14,7 +16,6 @@
//Sensor Libraries
#include "Adafruit_SHT31.h" //https://github.com/adafruit/Adafruit_SHT31
#include "SparkFun_VEML6030_Ambient_Light_Sensor.h" //https://github.com/sparkfun/SparkFun_Ambient_Light_Sensor_Arduino_Library
//https://github.com/ControlEverythingCommunity/BH1715/blob/master/Arduino/BH1715.ino
#include <Adafruit_BMP280.h>
#include "ccs811.h"

Expand Down Expand Up @@ -162,7 +163,16 @@ void MimirTesting::DisplaySensors()

void MimirTesting::initWIFI(bool _display)
{
WiFiManagerParameter custom_USER("User Name", "User Name", _USER, 40);
WiFiManagerParameter custom_USER_ID("User ID", "User ID", _USER_ID, 40);
WiFiManagerParameter custom_DEVICE_ID("Device ID", "Device ID", _DEVICE_ID, 40);

WiFiManager wifiManager;

wifiManager.addParameter(&custom_USER);
wifiManager.addParameter(&custom_USER_ID);
wifiManager.addParameter(&custom_DEVICE_ID);

strip.setPixelColor(2, strip.Color(255, 255, 0));
strip.show();
wifiManager.autoConnect("mimirAP");
Expand All @@ -181,8 +191,77 @@ void MimirTesting::initWIFI(bool _display)
strip.show();
}

//Update Device Info with Params
strcpy(_USER, custom_USER.getValue());
strcpy(_USER_ID, custom_USER_ID.getValue());
strcpy(_DEVICE_ID, custom_DEVICE_ID.getValue());

saveConfig();

WiFi_OFF();
}
void MimirTesting::initConfig()
{
if (SPIFFS.begin())
{

Serial.println("mounted file system");
if (SPIFFS.exists("/config.json"))
{
//file exists, reading and loading
Serial.println("reading config file");
fs::File configFile = SPIFFS.open("/config.json", "r");
if (configFile)
{
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);

configFile.readBytes(buf.get(), size);
DynamicJsonDocument configJson(1024);
DeserializationError error = deserializeJson(configJson, buf.get());
if (error)
{
Serial.println("failed to load json config");
return;
}
Serial.println("\nparsed json");
serializeJson(configJson, Serial);

strcpy(_USER, configJson["User"] | "N/A");
strcpy(_USER_ID, configJson["UserID"] | "N/A");
strcpy(_DEVICE_ID, configJson["DeviceID"] | "N/A");
}
else
{
Serial.println("failed to load json config");
}
}
}
else
{
Serial.println("failed to mount FS");
}
}

void MimirTesting::saveConfig()
{
DynamicJsonDocument newConfigJson(1024);
newConfigJson["User"] = _USER;
newConfigJson["UserID"] = _USER_ID;
newConfigJson["DeviceID"] = _DEVICE_ID;

fs::File configFile = SPIFFS.open("/config.json", "w");
if (!configFile)
{
Serial.println("failed to open config file for writing");
}

serializeJson(newConfigJson, Serial);
serializeJson(newConfigJson, configFile);
configFile.close();
}

void MimirTesting::DisplayWiFiCredentials()
{
Expand Down Expand Up @@ -221,12 +300,40 @@ void MimirTesting::initTimer()

void MimirTesting::forceStartWiFi()
{
WiFiManagerParameter custom_USER("User Name", "User Name", _USER, 40);
WiFiManagerParameter custom_USER_ID("User ID", "User ID", _USER_ID, 40);
WiFiManagerParameter custom_DEVICE_ID("Device ID", "Device ID", _DEVICE_ID, 40);

WiFiManager wifiManager;

wifiManager.addParameter(&custom_USER);
wifiManager.addParameter(&custom_USER_ID);
wifiManager.addParameter(&custom_DEVICE_ID);

display.fillScreen(GxEPD_WHITE);
display.setCursor(2, 20);
display.println("*WiFi Config*");
display.println("****Mode*****");
display.update();

if (!wifiManager.startConfigPortal("OnDemandAP"))
{
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}

delay(5000);
//Update Device Info with Params
strcpy(_USER, custom_USER.getValue());
strcpy(_USER_ID, custom_USER_ID.getValue());
strcpy(_DEVICE_ID, custom_DEVICE_ID.getValue());

saveConfig();

WiFi_OFF();
}

void MimirTesting::WiFi_ON()
Expand Down Expand Up @@ -289,6 +396,55 @@ void MimirTesting::readSensors(bool _display)
if (_display)
DisplayReadings();
}
void MimirTesting::DisplayDeviceInfo()
{
display.fillScreen(GxEPD_WHITE);
display.setCursor(0, 20);
display.print("Time: ");
display.println(TimeStr);
display.println(DateStr);
display.println("____________");
display.print("User: ");
display.println(_USER);
display.print("UserID: ");
display.println(_USER_ID);
display.print("DeviceID: ");
display.println(_DEVICE_ID);
display.print("IP: ");
display.println(_IP_ADDRESS);
display.println("____________");
!SHT31D_L_STATUS ? display.println("SHT31_L: X ")
: display.println("SHT31_L: O");

!SHT31D_H_STATUS ? display.println("SHT31_H: X")
: display.println("SHT31_H: O");

!VEML6030_STATUS ? display.println("VEML6030: X")
: display.println("VEML6030: O");

!CCS811B_STATUS ? display.println("CCS811B: X")
: display.println("CCS811B: O");

!BMP280_STATUS ? display.println("bmp280: X")
: display.println("bmp280: O");

!TEMT600_STATUS ? display.println("TEMT600: O")
: display.println("TEMT600: X");

display.println("____________");
display.print("Battery: ");
display.println(_BATTERY);
display.print("Sensors: ");
display.println(_SENSOR);
display.print("WiFi: ");
display.println(_WIFI);
display.print("Server: ");
display.println(_SERVER);
display.print("MicroSD: ");
display.println(_MICROSD);

display.update();
}

void MimirTesting::DisplayReadings()
{
Expand Down
13 changes: 10 additions & 3 deletions lib/MimirTesting/MimirTesting.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ class MimirTesting
void initWIFI(bool display = false);
void initDash();
void initTimer();
void initConfig();

void i2cScanner();
void testNeoPixels(int repeat = 3, int delay = 500);

void readSensors(bool display = false);
void readBattery(bool display = false);

void DisplayDeviceInfo();

void sendData(bool display = false);
void WiFi_ON();
void WiFi_OFF();
Expand All @@ -53,9 +56,9 @@ class MimirTesting
int _MICROSD = 0;

String _IP_ADDRESS;
String _USER = "Lloyd Richards";
String _USER_ID = "P2003_PROTOTYPE";
String _DEVICE_ID = "PROTOTYPE3_03";
char _USER[40];
char _USER_ID[40];
char _DEVICE_ID[40];

String TimeStr, DateStr, ErrorMessage; // strings to hold time and date
const char *TZ_INFO = "CET-1CEST,M3.5.0,M10.5.0/3";
Expand Down Expand Up @@ -87,15 +90,19 @@ class MimirTesting

void writeFile(fs::FS &fs, const char *path, const char *message);
void appendFile(fs::FS &fs, const char *path, const char *message);
void saveConfig();

void printValue(float value, const char *type, const char *unit, int decimel = 2);
void getIPAddress();

void DisplayWiFiIcon(int x, int y);
void DisplayBatteryIcon(int x, int y);
void DisplaySensors();
void DisplayReadings();
void DisplayWiFiSetup();
void DisplayWiFiCredentials();
void DisplaySentData(int httpResponseCode, String response);

void drawString(int x, int y, String text, alignment align);
void blinkPixel(int pixel, int R = 255, int G = 0, int B = 0, int repeat = 1);

Expand Down
8 changes: 6 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ void setup()
{
if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TIMER)
{

Serial.begin(115200);
Wire.begin();
mimirTesting.initDisplay(115200);
mimirTesting.initNeoPixels(50);
mimirTesting.initConfig();
mimirTesting.initSensors();
mimirTesting.initWIFI();
}
pinMode(39, INPUT);
mimirTesting.initTimer();
mimirTesting.initDash();
mimirTesting.readBattery();
mimirTesting.readSensors(true);
mimirTesting.WiFi_ON();
mimirTesting.sendData(true);
mimirTesting.WiFi_OFF();
mimirTesting.SLEEP();
//mimirTesting.forceStartWiFi();
}

void loop()
{
mimirTesting.DisplayDeviceInfo();
delay(5000);
}

0 comments on commit d2f67ba

Please sign in to comment.