From 2e265b29313eb2e0af652bd05d6865bac97658c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erwan=20Queff=C3=A9lec?= Date: Fri, 16 Feb 2018 01:58:46 +0100 Subject: [PATCH 1/3] Move and reformat examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Erwan Queffélec --- .clang-format | 3 + .../CoolBoardActorExample.ino | 59 ---- .../CoolBoardExample/CoolBoardExample.ino | 69 ----- .../data/coolBoardActorConfig.json | 8 - .../data/coolBoardConfig.json | 14 - .../data/coolBoardLedConfig.json | 5 - .../data/coolBoardSensorsConfig.json | 27 -- .../data/externalSensorsConfig.json | 11 - .../data/irene3000Config.json | 22 -- .../CoolBoardExample/data/jetPackConfig.json | 85 ------ .../CoolBoardExample/data/mqttConfig.json | 7 - .../CoolBoardExample/data/rtcConfig.json | 9 - .../CoolBoardExample/data/wifiConfig.json | 6 - .../CoolBoardFarmExample.ino | 67 ----- .../data/coolBoardActorConfig.json | 8 - .../data/coolBoardConfig.json | 12 - .../data/coolBoardLedConfig.json | 5 - .../data/coolBoardSensorsConfig.json | 27 -- .../data/externalSensorsConfig.json | 11 - .../data/irene3000Config.json | 22 -- .../data/jetPackConfig.json | 85 ------ .../CoolBoardFarmExample/data/mqttConfig.json | 6 - .../CoolBoardFarmExample/data/rtcConfig.json | 7 - .../CoolBoardFarmExample/data/wifiConfig.json | 6 - .../CoolBoardLedExample.ino | 74 ------ .../CoolBoardMQTTExample.ino | 79 ------ .../CoolBoardSensorsExample.ino | 80 ------ .../CoolBoardStationExample.ino | 69 ----- .../data/coolBoardActorConfig.json | 8 - .../data/coolBoardConfig.json | 13 - .../data/coolBoardLedConfig.json | 5 - .../data/coolBoardSensorsConfig.json | 27 -- .../data/externalSensorsConfig.json | 11 - .../data/irene3000Config.json | 22 -- .../data/jetPackConfig.json | 85 ------ .../data/mqttConfig.json | 6 - .../data/rtcConfig.json | 7 - .../data/wifiConfig.json | 6 - .../CoolIrene3000Example.ino | 63 ----- .../CoolJetpackExample/CoolJetpackExample.ino | 55 ---- .../CoolTimeExample/CoolTimeExample.ino | 76 ------ .../CoolWifiExample/CoolWifiExample.ino | 57 ---- .../ExternalSensorsExample.ino | 70 ----- examples/Actor/Actor.ino | 32 +++ examples/AutoSprinkle/AutoSprinkle.ino | 81 ++---- .../data/coolBoardActorConfig.json | 25 +- .../AutoSprinkle/data/coolBoardConfig.json | 19 +- .../AutoSprinkle/data/coolBoardLedConfig.json | 4 +- .../data/coolBoardSensorsConfig.json | 37 +-- .../data/externalSensorsConfig.json | 5 +- .../AutoSprinkle/data/irene3000Config.json | 31 +-- examples/AutoSprinkle/data/jetPackConfig.json | 251 ++++++++++++------ examples/AutoSprinkle/data/mqttConfig.json | 7 +- examples/AutoSprinkle/data/rtcConfig.json | 12 +- examples/AutoSprinkle/data/wifiConfig.json | 7 +- examples/ExternalSensors/ExternalSensors.ino | 40 +++ examples/Farm/Farm.ino | 34 +++ examples/Farm/data/coolBoardActorConfig.json | 21 ++ examples/Farm/data/coolBoardConfig.json | 8 + examples/Farm/data/coolBoardLedConfig.json | 3 + .../Farm/data/coolBoardSensorsConfig.json | 14 + examples/Farm/data/externalSensorsConfig.json | 7 + examples/Farm/data/irene3000Config.json | 13 + examples/Farm/data/jetPackConfig.json | 166 ++++++++++++ examples/Farm/data/mqttConfig.json | 4 + examples/Farm/data/rtcConfig.json | 4 + examples/Farm/data/wifiConfig.json | 5 + examples/Irene3000/Irene3000.ino | 37 +++ examples/Jetpack/Jetpack.ino | 24 ++ examples/Led/Led.ino | 47 ++++ examples/MQTT/MQTT.ino | 55 ++++ examples/README.md | 7 - examples/Sensors/Sensors.ino | 47 ++++ examples/Station/Station.ino | 34 +++ .../Station/data/coolBoardActorConfig.json | 21 ++ examples/Station/data/coolBoardConfig.json | 8 + examples/Station/data/coolBoardLedConfig.json | 3 + .../Station/data/coolBoardSensorsConfig.json | 14 + .../Station/data/externalSensorsConfig.json | 7 + examples/Station/data/irene3000Config.json | 13 + examples/Station/data/jetPackConfig.json | 166 ++++++++++++ examples/Station/data/mqttConfig.json | 4 + examples/Station/data/rtcConfig.json | 4 + examples/Station/data/wifiConfig.json | 5 + examples/Time/Time.ino | 47 ++++ examples/WeatherStation/WeatherStation.ino | 81 ++---- .../data/coolBoardActorConfig.json | 25 +- .../WeatherStation/data/coolBoardConfig.json | 19 +- .../data/coolBoardLedConfig.json | 4 +- .../data/coolBoardSensorsConfig.json | 37 +-- .../data/externalSensorsConfig.json | 5 +- .../WeatherStation/data/irene3000Config.json | 31 +-- .../WeatherStation/data/jetPackConfig.json | 251 ++++++++++++------ examples/WeatherStation/data/mqttConfig.json | 7 +- examples/WeatherStation/data/rtcConfig.json | 12 +- examples/WeatherStation/data/wifiConfig.json | 7 +- examples/Wifi/Wifi.ino | 30 +++ 97 files changed, 1432 insertions(+), 1844 deletions(-) create mode 100644 .clang-format delete mode 100644 examples/API Examples/CoolBoardActorExample/CoolBoardActorExample.ino delete mode 100644 examples/API Examples/CoolBoardExample/CoolBoardExample.ino delete mode 100644 examples/API Examples/CoolBoardExample/data/coolBoardActorConfig.json delete mode 100644 examples/API Examples/CoolBoardExample/data/coolBoardConfig.json delete mode 100644 examples/API Examples/CoolBoardExample/data/coolBoardLedConfig.json delete mode 100644 examples/API Examples/CoolBoardExample/data/coolBoardSensorsConfig.json delete mode 100644 examples/API Examples/CoolBoardExample/data/externalSensorsConfig.json delete mode 100644 examples/API Examples/CoolBoardExample/data/irene3000Config.json delete mode 100644 examples/API Examples/CoolBoardExample/data/jetPackConfig.json delete mode 100644 examples/API Examples/CoolBoardExample/data/mqttConfig.json delete mode 100644 examples/API Examples/CoolBoardExample/data/rtcConfig.json delete mode 100644 examples/API Examples/CoolBoardExample/data/wifiConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/CoolBoardFarmExample.ino delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/coolBoardActorConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/coolBoardConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/coolBoardLedConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/coolBoardSensorsConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/externalSensorsConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/irene3000Config.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/jetPackConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/mqttConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/rtcConfig.json delete mode 100644 examples/API Examples/CoolBoardFarmExample/data/wifiConfig.json delete mode 100644 examples/API Examples/CoolBoardLedExample/CoolBoardLedExample.ino delete mode 100644 examples/API Examples/CoolBoardMQTTExample/CoolBoardMQTTExample.ino delete mode 100644 examples/API Examples/CoolBoardSensorsExample/CoolBoardSensorsExample.ino delete mode 100644 examples/API Examples/CoolBoardStationExample/CoolBoardStationExample.ino delete mode 100644 examples/API Examples/CoolBoardStationExample/data/coolBoardActorConfig.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/coolBoardConfig.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/coolBoardLedConfig.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/coolBoardSensorsConfig.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/externalSensorsConfig.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/irene3000Config.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/jetPackConfig.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/mqttConfig.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/rtcConfig.json delete mode 100644 examples/API Examples/CoolBoardStationExample/data/wifiConfig.json delete mode 100644 examples/API Examples/CoolIrene3000Example/CoolIrene3000Example.ino delete mode 100644 examples/API Examples/CoolJetpackExample/CoolJetpackExample.ino delete mode 100644 examples/API Examples/CoolTimeExample/CoolTimeExample.ino delete mode 100644 examples/API Examples/CoolWifiExample/CoolWifiExample.ino delete mode 100644 examples/API Examples/ExternalSensorsExample/ExternalSensorsExample.ino create mode 100644 examples/Actor/Actor.ino create mode 100644 examples/ExternalSensors/ExternalSensors.ino create mode 100644 examples/Farm/Farm.ino create mode 100644 examples/Farm/data/coolBoardActorConfig.json create mode 100644 examples/Farm/data/coolBoardConfig.json create mode 100644 examples/Farm/data/coolBoardLedConfig.json create mode 100644 examples/Farm/data/coolBoardSensorsConfig.json create mode 100644 examples/Farm/data/externalSensorsConfig.json create mode 100644 examples/Farm/data/irene3000Config.json create mode 100644 examples/Farm/data/jetPackConfig.json create mode 100644 examples/Farm/data/mqttConfig.json create mode 100644 examples/Farm/data/rtcConfig.json create mode 100644 examples/Farm/data/wifiConfig.json create mode 100644 examples/Irene3000/Irene3000.ino create mode 100644 examples/Jetpack/Jetpack.ino create mode 100644 examples/Led/Led.ino create mode 100644 examples/MQTT/MQTT.ino delete mode 100644 examples/README.md create mode 100644 examples/Sensors/Sensors.ino create mode 100644 examples/Station/Station.ino create mode 100644 examples/Station/data/coolBoardActorConfig.json create mode 100644 examples/Station/data/coolBoardConfig.json create mode 100644 examples/Station/data/coolBoardLedConfig.json create mode 100644 examples/Station/data/coolBoardSensorsConfig.json create mode 100644 examples/Station/data/externalSensorsConfig.json create mode 100644 examples/Station/data/irene3000Config.json create mode 100644 examples/Station/data/jetPackConfig.json create mode 100644 examples/Station/data/mqttConfig.json create mode 100644 examples/Station/data/rtcConfig.json create mode 100644 examples/Station/data/wifiConfig.json create mode 100644 examples/Time/Time.ino create mode 100644 examples/Wifi/Wifi.ino diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..941486e2 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +--- +BasedOnStyle: LLVM +--- diff --git a/examples/API Examples/CoolBoardActorExample/CoolBoardActorExample.ino b/examples/API Examples/CoolBoardActorExample/CoolBoardActorExample.ino deleted file mode 100644 index 4ac7298e..00000000 --- a/examples/API Examples/CoolBoardActorExample/CoolBoardActorExample.ino +++ /dev/null @@ -1,59 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include"CoolBoardActor.h" - -CoolBoardActor actor; - -void setup() -{ - - Serial.begin(115200); - - actor.begin(); - -} - -void loop() -{ - actor.write(1); - - delay(500); - - actor.write(0); - - delay(500); - -} diff --git a/examples/API Examples/CoolBoardExample/CoolBoardExample.ino b/examples/API Examples/CoolBoardExample/CoolBoardExample.ino deleted file mode 100644 index db8ee042..00000000 --- a/examples/API Examples/CoolBoardExample/CoolBoardExample.ino +++ /dev/null @@ -1,69 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include - - -CoolBoard coolBoard; - - -void setup() -{ - Serial.begin(115200); - coolBoard.config(); - - coolBoard.begin(); - - coolBoard.printConf(); - - Serial.print("one log every "); - Serial.print(coolBoard.getLogInterval()); - Serial.println(" s " ); -} - -void loop() -{ - if(coolBoard.isConnected()==0 ) - { - coolBoard.onLineMode(); - } - else - { - coolBoard.offLineMode(); - } - - - -} diff --git a/examples/API Examples/CoolBoardExample/data/coolBoardActorConfig.json b/examples/API Examples/CoolBoardExample/data/coolBoardActorConfig.json deleted file mode 100644 index ac81092b..00000000 --- a/examples/API Examples/CoolBoardExample/data/coolBoardActorConfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "actif":0, - "inverted":0, - "temporal":0, - "low":[0,5000,0,0], - "high":[0,5000,0,0], - "type":["Temperature",""] -} diff --git a/examples/API Examples/CoolBoardExample/data/coolBoardConfig.json b/examples/API Examples/CoolBoardExample/data/coolBoardConfig.json deleted file mode 100644 index 426badea..00000000 --- a/examples/API Examples/CoolBoardExample/data/coolBoardConfig.json +++ /dev/null @@ -1,14 +0,0 @@ - -{ - - "logInterval":10, - "ireneActive":0, - "jetpackActive":0, - "externalSensorsActive":0, - "sleepActive":0, - "manual":0, - "saveAsJSON":0, - "saveAsCSV":1 -} - - diff --git a/examples/API Examples/CoolBoardExample/data/coolBoardLedConfig.json b/examples/API Examples/CoolBoardExample/data/coolBoardLedConfig.json deleted file mode 100644 index 14893b32..00000000 --- a/examples/API Examples/CoolBoardExample/data/coolBoardLedConfig.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "ledActive":1 -} - - diff --git a/examples/API Examples/CoolBoardExample/data/coolBoardSensorsConfig.json b/examples/API Examples/CoolBoardExample/data/coolBoardSensorsConfig.json deleted file mode 100644 index 544e956d..00000000 --- a/examples/API Examples/CoolBoardExample/data/coolBoardSensorsConfig.json +++ /dev/null @@ -1,27 +0,0 @@ - -{ - - - "BME280": - { - "temperature" : 1, - "humidity":1, - "pressure":1 - - - }, - - "SI114X": - { - "visible":1, - "ir":1, - "uv":1 - }, - - "vbat":1, - - "soilMoisture":1 -} - - - diff --git a/examples/API Examples/CoolBoardExample/data/externalSensorsConfig.json b/examples/API Examples/CoolBoardExample/data/externalSensorsConfig.json deleted file mode 100644 index 4f937ae3..00000000 --- a/examples/API Examples/CoolBoardExample/data/externalSensorsConfig.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "sensorsNumber":1, - - "sensor0": - { - "reference":"Adafruit_TCS34725", - "type":"RGBCK" - - } -} - diff --git a/examples/API Examples/CoolBoardExample/data/irene3000Config.json b/examples/API Examples/CoolBoardExample/data/irene3000Config.json deleted file mode 100644 index e9e17ca3..00000000 --- a/examples/API Examples/CoolBoardExample/data/irene3000Config.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "waterTemp": - { - "active":1 - }, - - "phProbe": - { - "active":1 - }, - - "adc2": - { - "active":1, - "gain":0.67, - "type":"waterLevel" - } -} - - - - diff --git a/examples/API Examples/CoolBoardExample/data/jetPackConfig.json b/examples/API Examples/CoolBoardExample/data/jetPackConfig.json deleted file mode 100644 index f655a420..00000000 --- a/examples/API Examples/CoolBoardExample/data/jetPackConfig.json +++ /dev/null @@ -1,85 +0,0 @@ -{ - "Act0": - { - "actif":1, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] - }, - - "Act1": - { - "actif":1, - "inverted":1, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] - }, - - "Act2": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[0,3000,0,0], - "high":[0,5000,0,0], - "type":["",""] - }, - - "Act3": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,3000,0,0], - "high":[40,5000,0,0], - "type":["Temperature",""] - }, - - "Act4": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[0,000,9,0], - "high":[0,000,7,0], - "type":["","hour"] - }, - - "Act5": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,0,9,0], - "high":[40,0,7,0], - "type":["Temperature","hour"] - }, - - "Act6": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[0,00,0,50], - "high":[0,00,0,1], - "type":["","minute"] - }, - - "Act7": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,00,0,50], - "high":[40,00,0,1], - "type":["Temperature","minute"] - } - - -} - - diff --git a/examples/API Examples/CoolBoardExample/data/mqttConfig.json b/examples/API Examples/CoolBoardExample/data/mqttConfig.json deleted file mode 100644 index 273c15c2..00000000 --- a/examples/API Examples/CoolBoardExample/data/mqttConfig.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "mqttServer":"my.mqtt-server.com", - "bufferSize":3000 - -} - - diff --git a/examples/API Examples/CoolBoardExample/data/rtcConfig.json b/examples/API Examples/CoolBoardExample/data/rtcConfig.json deleted file mode 100644 index a66d94da..00000000 --- a/examples/API Examples/CoolBoardExample/data/rtcConfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "NTP":true, - "compileTime":false, - "timeServer":"europe.pool.ntp.org", - "localPort":8888, - "timeSync":0 -} - - diff --git a/examples/API Examples/CoolBoardExample/data/wifiConfig.json b/examples/API Examples/CoolBoardExample/data/wifiConfig.json deleted file mode 100644 index c43fbd2e..00000000 --- a/examples/API Examples/CoolBoardExample/data/wifiConfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "wifiCount":0, - "timeOut":180, - "nomad":0 - -} diff --git a/examples/API Examples/CoolBoardFarmExample/CoolBoardFarmExample.ino b/examples/API Examples/CoolBoardFarmExample/CoolBoardFarmExample.ino deleted file mode 100644 index 59192432..00000000 --- a/examples/API Examples/CoolBoardFarmExample/CoolBoardFarmExample.ino +++ /dev/null @@ -1,67 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include - - -CoolBoard coolBoard; - - -void setup() -{ - Serial.begin(115200); - coolBoard.config(); - - coolBoard.begin(); - - Serial.print("one log every "); - Serial.print(coolBoard.getLogInterval()); - Serial.println(" s " ); -} - -void loop() -{ - if(coolBoard.connect()==0 ) - { - coolBoard.onLineMode(); - } - else - { - coolBoard.offLineMode(); - } - - - -} diff --git a/examples/API Examples/CoolBoardFarmExample/data/coolBoardActorConfig.json b/examples/API Examples/CoolBoardFarmExample/data/coolBoardActorConfig.json deleted file mode 100644 index d5cce062..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/coolBoardActorConfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "actif":1, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] -} diff --git a/examples/API Examples/CoolBoardFarmExample/data/coolBoardConfig.json b/examples/API Examples/CoolBoardFarmExample/data/coolBoardConfig.json deleted file mode 100644 index 2c1df414..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/coolBoardConfig.json +++ /dev/null @@ -1,12 +0,0 @@ - -{ - - "logInterval":5, - "ireneActive":0, - "jetpackActive":1, - "externalSensorsActive":0, - "sleepActive":0, - "manual":0 -} - - diff --git a/examples/API Examples/CoolBoardFarmExample/data/coolBoardLedConfig.json b/examples/API Examples/CoolBoardFarmExample/data/coolBoardLedConfig.json deleted file mode 100644 index 14893b32..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/coolBoardLedConfig.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "ledActive":1 -} - - diff --git a/examples/API Examples/CoolBoardFarmExample/data/coolBoardSensorsConfig.json b/examples/API Examples/CoolBoardFarmExample/data/coolBoardSensorsConfig.json deleted file mode 100644 index 544e956d..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/coolBoardSensorsConfig.json +++ /dev/null @@ -1,27 +0,0 @@ - -{ - - - "BME280": - { - "temperature" : 1, - "humidity":1, - "pressure":1 - - - }, - - "SI114X": - { - "visible":1, - "ir":1, - "uv":1 - }, - - "vbat":1, - - "soilMoisture":1 -} - - - diff --git a/examples/API Examples/CoolBoardFarmExample/data/externalSensorsConfig.json b/examples/API Examples/CoolBoardFarmExample/data/externalSensorsConfig.json deleted file mode 100644 index e53ceb0e..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/externalSensorsConfig.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "sensorsNumber":1, - - "sensor0": - { - "reference":"DallasTemperature", - "type":"TempOneWire" - - } -} - diff --git a/examples/API Examples/CoolBoardFarmExample/data/irene3000Config.json b/examples/API Examples/CoolBoardFarmExample/data/irene3000Config.json deleted file mode 100644 index 6142dd17..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/irene3000Config.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "waterTemp": - { - "active":1 - }, - - "phProbe": - { - "active":1 - }, - - "adc2": - { - "active":1, - "gain":1, - "type":"voltage" - } -} - - - - diff --git a/examples/API Examples/CoolBoardFarmExample/data/jetPackConfig.json b/examples/API Examples/CoolBoardFarmExample/data/jetPackConfig.json deleted file mode 100644 index 13befffb..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/jetPackConfig.json +++ /dev/null @@ -1,85 +0,0 @@ -{ - "Act0": - { - "actif":1, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] - }, - - "Act1": - { - "actif":1, - "inverted":1, - "temporal":0, - "low":[20,5000,0,0], - "high":[30,2000,0,0], - "type":["Temperature",""] - }, - - "Act2": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,3000,0,0], - "high":[30,5000,0,0], - "type":["Temperature",""] - }, - - "Act3": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,0,13,0], - "high":[30,0,12,0], - "type":["Temperature","hour"] - }, - - "Act4": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,5000,0,59], - "high":[30,2000,0,40], - "type":["temperature","minute"] - }, - - "Act5": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,5000,13,55], - "high":[30,2000,12,45], - "type":["temperature","hourMinute"] - }, - - "Act6": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,5000,18], - "high":[30,2000,17], - "type":["temperature","hour"] - }, - - "Act7": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,5000,18], - "high":[30,2000,17], - "type":["temperature","hour"] - } - - -} - - diff --git a/examples/API Examples/CoolBoardFarmExample/data/mqttConfig.json b/examples/API Examples/CoolBoardFarmExample/data/mqttConfig.json deleted file mode 100644 index 848c9aa1..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/mqttConfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "mqttServer":"my.mqtt-server.com", - "bufferSize":3000 -} - - diff --git a/examples/API Examples/CoolBoardFarmExample/data/rtcConfig.json b/examples/API Examples/CoolBoardFarmExample/data/rtcConfig.json deleted file mode 100644 index 97bc3c7e..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/rtcConfig.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "timeServer":"europe.pool.ntp.org", - "localPort":8888, - -} - - diff --git a/examples/API Examples/CoolBoardFarmExample/data/wifiConfig.json b/examples/API Examples/CoolBoardFarmExample/data/wifiConfig.json deleted file mode 100644 index a4a64488..00000000 --- a/examples/API Examples/CoolBoardFarmExample/data/wifiConfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "wifiCount":0, - "timeOut":300, - "nomad":0 - -} diff --git a/examples/API Examples/CoolBoardLedExample/CoolBoardLedExample.ino b/examples/API Examples/CoolBoardLedExample/CoolBoardLedExample.ino deleted file mode 100644 index 62a03b94..00000000 --- a/examples/API Examples/CoolBoardLedExample/CoolBoardLedExample.ino +++ /dev/null @@ -1,74 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include - -CoolBoardLed led; -void setup() -{ - led.activate(); - led.begin(); -} - -void loop() -{ - - led.fade(255, 255, 255, 1);//fade animation(R,G,B,Time in seconds) - - led.write(0,0,0);//direct write to the led(R,G,B) - delay(1000); - - led.blink(0,200 , 0, 1);//blink animation(R,G,B,Time in seconds) - - led.write(0,0,0); - delay(1000); - - led.fadeIn(100, 100, 100, 1);//fadeIn animation(R,G,B,Time in seconds) - - led.write(0,0,0); - delay(1000); - - led.fadeOut(10,64 ,20, 1);//fadeout animation(R,G,B,Time in seconds) - - led.write(0,0,0); - delay(1000); - - led.strobe(200, 0, 200, 1);//strobe animation(R,G,B,Time in seconds) - - led.write(0,0,0); - delay(1000); - - -} diff --git a/examples/API Examples/CoolBoardMQTTExample/CoolBoardMQTTExample.ino b/examples/API Examples/CoolBoardMQTTExample/CoolBoardMQTTExample.ino deleted file mode 100644 index 37b9f4a0..00000000 --- a/examples/API Examples/CoolBoardMQTTExample/CoolBoardMQTTExample.ino +++ /dev/null @@ -1,79 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include -#include - -CoolWifi wifiManager; -CoolMQTT mqtt; - -String ssid[] = {"ssid1","ssid2"};//put up to 50 WiFi ssids -String pass[]={"pass1","pass2"};//put up to 50 WiFi passwords - - -const char mqttServer[]="----------";//the mqtt server you're going to use -const char inTopic[]="--------";//the topic you're going to subscribe to (receive from) -const char outTopic[]="-------";//the topic you're going to publish to (send to ) -const char clientId[]="------";//your user name -int bufferSize = 128;//bytes -int keepAlive=15; //seconds - - -void setup() -{ - Serial.begin(115200); - - wifiManager.config(ssid,pass,2,180,0); - wifiManager.begin(); - wifiManager.printConf(); - - mqtt.config(mqttServer,inTopic, outTopic,clientId,bufferSize); - mqtt.begin(); - mqtt.printConf(); - - wifiManager.connect(); - - mqtt.connect(keepAlive); -} - -void loop() -{ -mqtt.publish("hello world by CoolBoard");//publish this message to the outTopic -delay(1000); -Serial.println(mqtt.read());//print answer if answer is received -delay(1000); -mqtt.mqttLoop(); -delay(1000); -} diff --git a/examples/API Examples/CoolBoardSensorsExample/CoolBoardSensorsExample.ino b/examples/API Examples/CoolBoardSensorsExample/CoolBoardSensorsExample.ino deleted file mode 100644 index 1fa1e7aa..00000000 --- a/examples/API Examples/CoolBoardSensorsExample/CoolBoardSensorsExample.ino +++ /dev/null @@ -1,80 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include - -CoolBoardSensors sensors; - -void setup() -{ - - - Serial.begin(115200); - - sensors.allActive(); - - sensors.begin(); - - -} - -void loop() -{ - Serial.println("sensors data is "); - Serial.println(sensors.read()); - delay(1000); - - Serial.println("individual readings"); - - Serial.print("Vbat : ");Serial.println(sensors.readVBat()); - - Serial.print("Earth Moisture : ");Serial.println(sensors.readMoisture()); - - Serial.print("Visible light : ");Serial.println(sensors.lightSensor.ReadVisible()) ; - - Serial.print("InfraRed light : ");Serial.println(sensors.lightSensor.ReadIR() ); - - Serial.print("Ultraviolet light: ");Serial.println(sensors.lightSensor.ReadUV()/100 ); - - Serial.print("Pressure : ");Serial.println(sensors.envSensor.readFloatPressure()); - - Serial.print("Air Humidity");Serial.println(sensors.envSensor.readFloatHumidity()) ; - - Serial.print("Temperature :");Serial.print(sensors.envSensor.readTempC());Serial.println("°C"); - - delay(1000); - - -} diff --git a/examples/API Examples/CoolBoardStationExample/CoolBoardStationExample.ino b/examples/API Examples/CoolBoardStationExample/CoolBoardStationExample.ino deleted file mode 100644 index 4428228e..00000000 --- a/examples/API Examples/CoolBoardStationExample/CoolBoardStationExample.ino +++ /dev/null @@ -1,69 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include - - -CoolBoard coolBoard; - - -void setup() -{ - Serial.begin(115200); - coolBoard.config(); - - coolBoard.begin(); - - Serial.print("one log every "); - Serial.print(coolBoard.getLogInterval()); - Serial.println(" s " ); - - if(coolBoard.connect()==0 ) - { - coolBoard.onLineMode(); - } - else - { - coolBoard.offLineMode(); - } - - - -} - -void loop() -{ - -} diff --git a/examples/API Examples/CoolBoardStationExample/data/coolBoardActorConfig.json b/examples/API Examples/CoolBoardStationExample/data/coolBoardActorConfig.json deleted file mode 100644 index d5cce062..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/coolBoardActorConfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "actif":1, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] -} diff --git a/examples/API Examples/CoolBoardStationExample/data/coolBoardConfig.json b/examples/API Examples/CoolBoardStationExample/data/coolBoardConfig.json deleted file mode 100644 index 9703faa9..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/coolBoardConfig.json +++ /dev/null @@ -1,13 +0,0 @@ - -{ - - "logInterval":60, - "ireneActive":0, - "jetpackActive":0, - "externalSensorsActive":0, - "sleepActive":1, - "manual":0 - -} - - diff --git a/examples/API Examples/CoolBoardStationExample/data/coolBoardLedConfig.json b/examples/API Examples/CoolBoardStationExample/data/coolBoardLedConfig.json deleted file mode 100644 index 14893b32..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/coolBoardLedConfig.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "ledActive":1 -} - - diff --git a/examples/API Examples/CoolBoardStationExample/data/coolBoardSensorsConfig.json b/examples/API Examples/CoolBoardStationExample/data/coolBoardSensorsConfig.json deleted file mode 100644 index 544e956d..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/coolBoardSensorsConfig.json +++ /dev/null @@ -1,27 +0,0 @@ - -{ - - - "BME280": - { - "temperature" : 1, - "humidity":1, - "pressure":1 - - - }, - - "SI114X": - { - "visible":1, - "ir":1, - "uv":1 - }, - - "vbat":1, - - "soilMoisture":1 -} - - - diff --git a/examples/API Examples/CoolBoardStationExample/data/externalSensorsConfig.json b/examples/API Examples/CoolBoardStationExample/data/externalSensorsConfig.json deleted file mode 100644 index e53ceb0e..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/externalSensorsConfig.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "sensorsNumber":1, - - "sensor0": - { - "reference":"DallasTemperature", - "type":"TempOneWire" - - } -} - diff --git a/examples/API Examples/CoolBoardStationExample/data/irene3000Config.json b/examples/API Examples/CoolBoardStationExample/data/irene3000Config.json deleted file mode 100644 index b09bce3a..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/irene3000Config.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "waterTemp": - { - "active":1 - }, - - "phProbe": - { - "active":1 - }, - - "adc2": - { - "active":0, - "gain":1, - "type":"voltage" - } -} - - - - diff --git a/examples/API Examples/CoolBoardStationExample/data/jetPackConfig.json b/examples/API Examples/CoolBoardStationExample/data/jetPackConfig.json deleted file mode 100644 index 13befffb..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/jetPackConfig.json +++ /dev/null @@ -1,85 +0,0 @@ -{ - "Act0": - { - "actif":1, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] - }, - - "Act1": - { - "actif":1, - "inverted":1, - "temporal":0, - "low":[20,5000,0,0], - "high":[30,2000,0,0], - "type":["Temperature",""] - }, - - "Act2": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,3000,0,0], - "high":[30,5000,0,0], - "type":["Temperature",""] - }, - - "Act3": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,0,13,0], - "high":[30,0,12,0], - "type":["Temperature","hour"] - }, - - "Act4": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,5000,0,59], - "high":[30,2000,0,40], - "type":["temperature","minute"] - }, - - "Act5": - { - "actif":1, - "inverted":0, - "temporal":1, - "low":[20,5000,13,55], - "high":[30,2000,12,45], - "type":["temperature","hourMinute"] - }, - - "Act6": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,5000,18], - "high":[30,2000,17], - "type":["temperature","hour"] - }, - - "Act7": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,5000,18], - "high":[30,2000,17], - "type":["temperature","hour"] - } - - -} - - diff --git a/examples/API Examples/CoolBoardStationExample/data/mqttConfig.json b/examples/API Examples/CoolBoardStationExample/data/mqttConfig.json deleted file mode 100644 index 848c9aa1..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/mqttConfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "mqttServer":"my.mqtt-server.com", - "bufferSize":3000 -} - - diff --git a/examples/API Examples/CoolBoardStationExample/data/rtcConfig.json b/examples/API Examples/CoolBoardStationExample/data/rtcConfig.json deleted file mode 100644 index 5dfae5e5..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/rtcConfig.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "timeServer":"europe.pool.ntp.org", - "localPort":8888 - -} - - diff --git a/examples/API Examples/CoolBoardStationExample/data/wifiConfig.json b/examples/API Examples/CoolBoardStationExample/data/wifiConfig.json deleted file mode 100644 index a4a64488..00000000 --- a/examples/API Examples/CoolBoardStationExample/data/wifiConfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "wifiCount":0, - "timeOut":300, - "nomad":0 - -} diff --git a/examples/API Examples/CoolIrene3000Example/CoolIrene3000Example.ino b/examples/API Examples/CoolIrene3000Example/CoolIrene3000Example.ino deleted file mode 100644 index b2ae6235..00000000 --- a/examples/API Examples/CoolIrene3000Example/CoolIrene3000Example.ino +++ /dev/null @@ -1,63 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include - - -Irene3000 irene; - -double temperature; - -void setup() -{ - pinMode(5,OUTPUT); - - digitalWrite(5,HIGH); - - Serial.begin(115200); - - irene.begin(); -} - -void loop() -{ - - temperature=irene.readTemp(); - Serial.println(irene.readPh(irene.gainConvert(temperature)) ); - - Serial.println( temperature ); - - delay(2000); -} diff --git a/examples/API Examples/CoolJetpackExample/CoolJetpackExample.ino b/examples/API Examples/CoolJetpackExample/CoolJetpackExample.ino deleted file mode 100644 index 56b62bde..00000000 --- a/examples/API Examples/CoolJetpackExample/CoolJetpackExample.ino +++ /dev/null @@ -1,55 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include - -Jetpack jetpack; -void setup() -{ - - jetpack.begin(); - - -} - -void loop() -{ - - jetpack.write(0xFF); //writes to the Jetpack - delay(1000); - jetpack.writeBit(3,0); //writes to a single pin of the Jetpack - delay(1000); - -} diff --git a/examples/API Examples/CoolTimeExample/CoolTimeExample.ino b/examples/API Examples/CoolTimeExample/CoolTimeExample.ino deleted file mode 100644 index f20ee66f..00000000 --- a/examples/API Examples/CoolTimeExample/CoolTimeExample.ino +++ /dev/null @@ -1,76 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ - -#include -#include -#include - -CoolWifi wifiManager; -CoolTime clock; - -String ssid[] = {"ssid0","ssid1"}; -String pass[]={"pass0","pass1"}; - -IPAddress timeServer(132,163,4,101); -unsigned int localPort = 8888; - -void setup() -{ - Wire.begin(2,14); - - Serial.begin(115200); - - wifiManager.config(ssid,pass,2,180,0); - - wifiManager.begin(); - - wifiManager.printConf(); - - wifiManager.connect(); - - - clock.config(timeServer,localPort); - - clock.begin(); - - - -} - -void loop() -{ - Serial.println(clock.getESDate()); - delay(3000); -} diff --git a/examples/API Examples/CoolWifiExample/CoolWifiExample.ino b/examples/API Examples/CoolWifiExample/CoolWifiExample.ino deleted file mode 100644 index b2dd31e4..00000000 --- a/examples/API Examples/CoolWifiExample/CoolWifiExample.ino +++ /dev/null @@ -1,57 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ -#include - -CoolWifi wifiManager; - -String ssid[] = {"ssid0","ssid1"}; -String pass[]={"pass0","pass1"}; - - -void setup() -{ - Serial.begin(115200); - //config(ssid array, pass array, number of wifis, AP timeout,nomad flag ); - wifiManager.config(ssid,pass,2,180,0); - wifiManager.begin(); - wifiManager.printConf(); - - wifiManager.connect(); -} - - -void loop() -{ -} diff --git a/examples/API Examples/ExternalSensorsExample/ExternalSensorsExample.ino b/examples/API Examples/ExternalSensorsExample/ExternalSensorsExample.ino deleted file mode 100644 index c4c9f6df..00000000 --- a/examples/API Examples/ExternalSensorsExample/ExternalSensorsExample.ino +++ /dev/null @@ -1,70 +0,0 @@ -/** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ -* ExternalSensorsExample -* -* This example shows basic usage -* of the ExternalSensors class -* -*/ -#include"ExternalSensors.h" - -ExternalSensors externalSensors; - -String reference[]={"NDIR_I2C"}; - -String type[]={"CO2"}; - -uint8_t address[]={77}; - -int sensorsNumber=1; - -void setup() -{ - pinMode(5,OUTPUT); - - digitalWrite(5,HIGH);//HIGH= I2C Enable - - Serial.begin(115200); - - externalSensors.config(reference,type,address,sensorsNumber); - externalSensors.begin(); - externalSensors.printConf(); -} - -void loop() -{ - Serial.println(externalSensors.read()); - delay(1000); -} diff --git a/examples/Actor/Actor.ino b/examples/Actor/Actor.ino new file mode 100644 index 00000000..13484468 --- /dev/null +++ b/examples/Actor/Actor.ino @@ -0,0 +1,32 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include "CoolBoardActor.h" + +CoolBoardActor actor; + +void setup() { + + Serial.begin(115200); + + actor.begin(); +} + +void loop() { + actor.write(1); + + delay(500); + + actor.write(0); + + delay(500); +} diff --git a/examples/AutoSprinkle/AutoSprinkle.ino b/examples/AutoSprinkle/AutoSprinkle.ino index db8ee042..70d7f1f9 100644 --- a/examples/AutoSprinkle/AutoSprinkle.ino +++ b/examples/AutoSprinkle/AutoSprinkle.ino @@ -1,69 +1,36 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ #include - CoolBoard coolBoard; +void setup() { + Serial.begin(115200); + coolBoard.config(); -void setup() -{ - Serial.begin(115200); - coolBoard.config(); - - coolBoard.begin(); + coolBoard.begin(); - coolBoard.printConf(); + coolBoard.printConf(); - Serial.print("one log every "); - Serial.print(coolBoard.getLogInterval()); - Serial.println(" s " ); + Serial.print("one log every "); + Serial.print(coolBoard.getLogInterval()); + Serial.println(" s "); } -void loop() -{ - if(coolBoard.isConnected()==0 ) - { - coolBoard.onLineMode(); - } - else - { - coolBoard.offLineMode(); - } - - - +void loop() { + if (coolBoard.isConnected() == 0) { + coolBoard.onLineMode(); + } else { + coolBoard.offLineMode(); + } } diff --git a/examples/AutoSprinkle/data/coolBoardActorConfig.json b/examples/AutoSprinkle/data/coolBoardActorConfig.json index df423eb1..4df393ef 100644 --- a/examples/AutoSprinkle/data/coolBoardActorConfig.json +++ b/examples/AutoSprinkle/data/coolBoardActorConfig.json @@ -1,8 +1,21 @@ { - "actif":1, - "inverted":0, - "temporal":0, - "low":[50,0,0,0], - "high":[40,0,0,0], - "type":["soilMoisture",""] + "actif": 1, + "inverted": 0, + "temporal": 0, + "low": [ + 50, + 0, + 0, + 0 + ], + "high": [ + 40, + 0, + 0, + 0 + ], + "type": [ + "soilMoisture", + "" + ] } diff --git a/examples/AutoSprinkle/data/coolBoardConfig.json b/examples/AutoSprinkle/data/coolBoardConfig.json index 371fb3a6..ff6079fa 100644 --- a/examples/AutoSprinkle/data/coolBoardConfig.json +++ b/examples/AutoSprinkle/data/coolBoardConfig.json @@ -1,13 +1,10 @@ - { - "logInterval":3600, - "ireneActive":0, - "jetpackActive":0, - "externalSensorsActive":0, - "sleepActive":0, - "manual":0, - "saveAsJSON":1, - "saveAsCSV":0 + "logInterval": 3600, + "ireneActive": 0, + "jetpackActive": 0, + "externalSensorsActive": 0, + "sleepActive": 0, + "manual": 0, + "saveAsJSON": 1, + "saveAsCSV": 0 } - - diff --git a/examples/AutoSprinkle/data/coolBoardLedConfig.json b/examples/AutoSprinkle/data/coolBoardLedConfig.json index 14893b32..ab94a406 100644 --- a/examples/AutoSprinkle/data/coolBoardLedConfig.json +++ b/examples/AutoSprinkle/data/coolBoardLedConfig.json @@ -1,5 +1,3 @@ { - "ledActive":1 + "ledActive": 1 } - - diff --git a/examples/AutoSprinkle/data/coolBoardSensorsConfig.json b/examples/AutoSprinkle/data/coolBoardSensorsConfig.json index 544e956d..e2270644 100644 --- a/examples/AutoSprinkle/data/coolBoardSensorsConfig.json +++ b/examples/AutoSprinkle/data/coolBoardSensorsConfig.json @@ -1,27 +1,14 @@ - { - - - "BME280": - { - "temperature" : 1, - "humidity":1, - "pressure":1 - - - }, - - "SI114X": - { - "visible":1, - "ir":1, - "uv":1 - }, - - "vbat":1, - - "soilMoisture":1 + "BME280": { + "temperature": 1, + "humidity": 1, + "pressure": 1 + }, + "SI114X": { + "visible": 1, + "ir": 1, + "uv": 1 + }, + "vbat": 1, + "soilMoisture": 1 } - - - diff --git a/examples/AutoSprinkle/data/externalSensorsConfig.json b/examples/AutoSprinkle/data/externalSensorsConfig.json index 6cc3ca5e..b9e32ded 100644 --- a/examples/AutoSprinkle/data/externalSensorsConfig.json +++ b/examples/AutoSprinkle/data/externalSensorsConfig.json @@ -1,4 +1,3 @@ { - "sensorsNumber":0 -} - + "sensorsNumber": 0 +} diff --git a/examples/AutoSprinkle/data/irene3000Config.json b/examples/AutoSprinkle/data/irene3000Config.json index e9e17ca3..5819c81a 100644 --- a/examples/AutoSprinkle/data/irene3000Config.json +++ b/examples/AutoSprinkle/data/irene3000Config.json @@ -1,22 +1,13 @@ { - "waterTemp": - { - "active":1 - }, - - "phProbe": - { - "active":1 - }, - - "adc2": - { - "active":1, - "gain":0.67, - "type":"waterLevel" - } + "waterTemp": { + "active": 1 + }, + "phProbe": { + "active": 1 + }, + "adc2": { + "active": 1, + "gain": 0.67, + "type": "waterLevel" + } } - - - - diff --git a/examples/AutoSprinkle/data/jetPackConfig.json b/examples/AutoSprinkle/data/jetPackConfig.json index 70190f5e..4307c4af 100644 --- a/examples/AutoSprinkle/data/jetPackConfig.json +++ b/examples/AutoSprinkle/data/jetPackConfig.json @@ -1,83 +1,170 @@ { - "Act0": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] - }, - - "Act1": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] - }, - - "Act2": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[0,3000,0,0], - "high":[0,5000,0,0], - "type":["",""] - }, - - "Act3": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,3000,0,0], - "high":[40,5000,0,0], - "type":["Temperature",""] - }, - - "Act4": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[0,000,9,0], - "high":[0,000,7,0], - "type":["","hour"] - }, - - "Act5": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,0,9,0], - "high":[40,0,7,0], - "type":["Temperature","hour"] - }, - - "Act6": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[0,00,0,50], - "high":[0,00,0,1], - "type":["","minute"] - }, - - "Act7": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,00,0,50], - "high":[40,00,0,1], - "type":["Temperature","minute"] - } - - -} \ No newline at end of file + "Act0": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 0 + ], + "high": [ + 30, + 0, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act1": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 0 + ], + "high": [ + 30, + 0, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act2": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 0, + 3000, + 0, + 0 + ], + "high": [ + 0, + 5000, + 0, + 0 + ], + "type": [ + "", + "" + ] + }, + "Act3": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 3000, + 0, + 0 + ], + "high": [ + 40, + 5000, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act4": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 0, + 0, + 9, + 0 + ], + "high": [ + 0, + 0, + 7, + 0 + ], + "type": [ + "", + "hour" + ] + }, + "Act5": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 9, + 0 + ], + "high": [ + 40, + 0, + 7, + 0 + ], + "type": [ + "Temperature", + "hour" + ] + }, + "Act6": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 0, + 0, + 0, + 50 + ], + "high": [ + 0, + 0, + 0, + 1 + ], + "type": [ + "", + "minute" + ] + }, + "Act7": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 50 + ], + "high": [ + 40, + 0, + 0, + 1 + ], + "type": [ + "Temperature", + "minute" + ] + } +} diff --git a/examples/AutoSprinkle/data/mqttConfig.json b/examples/AutoSprinkle/data/mqttConfig.json index 273c15c2..73df7203 100644 --- a/examples/AutoSprinkle/data/mqttConfig.json +++ b/examples/AutoSprinkle/data/mqttConfig.json @@ -1,7 +1,4 @@ { - "mqttServer":"my.mqtt-server.com", - "bufferSize":3000 - + "mqttServer": "my.mqtt-server.com", + "bufferSize": 3000 } - - diff --git a/examples/AutoSprinkle/data/rtcConfig.json b/examples/AutoSprinkle/data/rtcConfig.json index a66d94da..b7176056 100644 --- a/examples/AutoSprinkle/data/rtcConfig.json +++ b/examples/AutoSprinkle/data/rtcConfig.json @@ -1,9 +1,7 @@ { - "NTP":true, - "compileTime":false, - "timeServer":"europe.pool.ntp.org", - "localPort":8888, - "timeSync":0 + "NTP": true, + "compileTime": false, + "timeServer": "europe.pool.ntp.org", + "localPort": 8888, + "timeSync": 0 } - - diff --git a/examples/AutoSprinkle/data/wifiConfig.json b/examples/AutoSprinkle/data/wifiConfig.json index c43fbd2e..a7f2c134 100644 --- a/examples/AutoSprinkle/data/wifiConfig.json +++ b/examples/AutoSprinkle/data/wifiConfig.json @@ -1,6 +1,5 @@ { - "wifiCount":0, - "timeOut":180, - "nomad":0 - + "wifiCount": 0, + "timeOut": 180, + "nomad": 0 } diff --git a/examples/ExternalSensors/ExternalSensors.ino b/examples/ExternalSensors/ExternalSensors.ino new file mode 100644 index 00000000..bbfe481c --- /dev/null +++ b/examples/ExternalSensors/ExternalSensors.ino @@ -0,0 +1,40 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include "ExternalSensors.h" + +ExternalSensors externalSensors; + +String reference[] = {"NDIR_I2C"}; + +String type[] = {"CO2"}; + +uint8_t address[] = {77}; + +int sensorsNumber = 1; + +void setup() { + pinMode(5, OUTPUT); + + digitalWrite(5, HIGH); // HIGH= I2C Enable + + Serial.begin(115200); + + externalSensors.config(reference, type, address, sensorsNumber); + externalSensors.begin(); + externalSensors.printConf(); +} + +void loop() { + Serial.println(externalSensors.read()); + delay(1000); +} diff --git a/examples/Farm/Farm.ino b/examples/Farm/Farm.ino new file mode 100644 index 00000000..15280ba8 --- /dev/null +++ b/examples/Farm/Farm.ino @@ -0,0 +1,34 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include + +CoolBoard coolBoard; + +void setup() { + Serial.begin(115200); + coolBoard.config(); + + coolBoard.begin(); + + Serial.print("one log every "); + Serial.print(coolBoard.getLogInterval()); + Serial.println(" s "); +} + +void loop() { + if (coolBoard.connect() == 0) { + coolBoard.onLineMode(); + } else { + coolBoard.offLineMode(); + } +} diff --git a/examples/Farm/data/coolBoardActorConfig.json b/examples/Farm/data/coolBoardActorConfig.json new file mode 100644 index 00000000..6db38025 --- /dev/null +++ b/examples/Farm/data/coolBoardActorConfig.json @@ -0,0 +1,21 @@ +{ + "actif": 1, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 0 + ], + "high": [ + 30, + 0, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] +} diff --git a/examples/Farm/data/coolBoardConfig.json b/examples/Farm/data/coolBoardConfig.json new file mode 100644 index 00000000..c4454d32 --- /dev/null +++ b/examples/Farm/data/coolBoardConfig.json @@ -0,0 +1,8 @@ +{ + "logInterval": 5, + "ireneActive": 0, + "jetpackActive": 1, + "externalSensorsActive": 0, + "sleepActive": 0, + "manual": 0 +} diff --git a/examples/Farm/data/coolBoardLedConfig.json b/examples/Farm/data/coolBoardLedConfig.json new file mode 100644 index 00000000..ab94a406 --- /dev/null +++ b/examples/Farm/data/coolBoardLedConfig.json @@ -0,0 +1,3 @@ +{ + "ledActive": 1 +} diff --git a/examples/Farm/data/coolBoardSensorsConfig.json b/examples/Farm/data/coolBoardSensorsConfig.json new file mode 100644 index 00000000..e2270644 --- /dev/null +++ b/examples/Farm/data/coolBoardSensorsConfig.json @@ -0,0 +1,14 @@ +{ + "BME280": { + "temperature": 1, + "humidity": 1, + "pressure": 1 + }, + "SI114X": { + "visible": 1, + "ir": 1, + "uv": 1 + }, + "vbat": 1, + "soilMoisture": 1 +} diff --git a/examples/Farm/data/externalSensorsConfig.json b/examples/Farm/data/externalSensorsConfig.json new file mode 100644 index 00000000..4959c834 --- /dev/null +++ b/examples/Farm/data/externalSensorsConfig.json @@ -0,0 +1,7 @@ +{ + "sensorsNumber": 1, + "sensor0": { + "reference": "DallasTemperature", + "type": "TempOneWire" + } +} diff --git a/examples/Farm/data/irene3000Config.json b/examples/Farm/data/irene3000Config.json new file mode 100644 index 00000000..21fa3af6 --- /dev/null +++ b/examples/Farm/data/irene3000Config.json @@ -0,0 +1,13 @@ +{ + "waterTemp": { + "active": 1 + }, + "phProbe": { + "active": 1 + }, + "adc2": { + "active": 1, + "gain": 1, + "type": "voltage" + } +} diff --git a/examples/Farm/data/jetPackConfig.json b/examples/Farm/data/jetPackConfig.json new file mode 100644 index 00000000..9919fa48 --- /dev/null +++ b/examples/Farm/data/jetPackConfig.json @@ -0,0 +1,166 @@ +{ + "Act0": { + "actif": 1, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 0 + ], + "high": [ + 30, + 0, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act1": { + "actif": 1, + "inverted": 1, + "temporal": 0, + "low": [ + 20, + 5000, + 0, + 0 + ], + "high": [ + 30, + 2000, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act2": { + "actif": 1, + "inverted": 0, + "temporal": 1, + "low": [ + 20, + 3000, + 0, + 0 + ], + "high": [ + 30, + 5000, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act3": { + "actif": 1, + "inverted": 0, + "temporal": 1, + "low": [ + 20, + 0, + 13, + 0 + ], + "high": [ + 30, + 0, + 12, + 0 + ], + "type": [ + "Temperature", + "hour" + ] + }, + "Act4": { + "actif": 1, + "inverted": 0, + "temporal": 1, + "low": [ + 20, + 5000, + 0, + 59 + ], + "high": [ + 30, + 2000, + 0, + 40 + ], + "type": [ + "temperature", + "minute" + ] + }, + "Act5": { + "actif": 1, + "inverted": 0, + "temporal": 1, + "low": [ + 20, + 5000, + 13, + 55 + ], + "high": [ + 30, + 2000, + 12, + 45 + ], + "type": [ + "temperature", + "hourMinute" + ] + }, + "Act6": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 5000, + 18 + ], + "high": [ + 30, + 2000, + 17 + ], + "type": [ + "temperature", + "hour" + ] + }, + "Act7": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 5000, + 18 + ], + "high": [ + 30, + 2000, + 17 + ], + "type": [ + "temperature", + "hour" + ] + } +} diff --git a/examples/Farm/data/mqttConfig.json b/examples/Farm/data/mqttConfig.json new file mode 100644 index 00000000..73df7203 --- /dev/null +++ b/examples/Farm/data/mqttConfig.json @@ -0,0 +1,4 @@ +{ + "mqttServer": "my.mqtt-server.com", + "bufferSize": 3000 +} diff --git a/examples/Farm/data/rtcConfig.json b/examples/Farm/data/rtcConfig.json new file mode 100644 index 00000000..17bd878a --- /dev/null +++ b/examples/Farm/data/rtcConfig.json @@ -0,0 +1,4 @@ +{ + "timeServer": "europe.pool.ntp.org", + "localPort": 8888 +} diff --git a/examples/Farm/data/wifiConfig.json b/examples/Farm/data/wifiConfig.json new file mode 100644 index 00000000..69aee403 --- /dev/null +++ b/examples/Farm/data/wifiConfig.json @@ -0,0 +1,5 @@ +{ + "wifiCount": 0, + "timeOut": 300, + "nomad": 0 +} diff --git a/examples/Irene3000/Irene3000.ino b/examples/Irene3000/Irene3000.ino new file mode 100644 index 00000000..0aeeb462 --- /dev/null +++ b/examples/Irene3000/Irene3000.ino @@ -0,0 +1,37 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include + +Irene3000 irene; + +double temperature; + +void setup() { + pinMode(5, OUTPUT); + + digitalWrite(5, HIGH); + + Serial.begin(115200); + + irene.begin(); +} + +void loop() { + + temperature = irene.readTemp(); + Serial.println(irene.readPh(irene.gainConvert(temperature))); + + Serial.println(temperature); + + delay(2000); +} diff --git a/examples/Jetpack/Jetpack.ino b/examples/Jetpack/Jetpack.ino new file mode 100644 index 00000000..60a8fe5d --- /dev/null +++ b/examples/Jetpack/Jetpack.ino @@ -0,0 +1,24 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include + +Jetpack jetpack; +void setup() { jetpack.begin(); } + +void loop() { + + jetpack.write(0xFF); // writes to the Jetpack + delay(1000); + jetpack.writeBit(3, 0); // writes to a single pin of the Jetpack + delay(1000); +} diff --git a/examples/Led/Led.ino b/examples/Led/Led.ino new file mode 100644 index 00000000..2010b686 --- /dev/null +++ b/examples/Led/Led.ino @@ -0,0 +1,47 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include + +CoolBoardLed led; +void setup() { + led.activate(); + led.begin(); +} + +void loop() { + + led.fade(255, 255, 255, 1); // fade animation(R,G,B,Time in seconds) + + led.write(0, 0, 0); // direct write to the led(R,G,B) + delay(1000); + + led.blink(0, 200, 0, 1); // blink animation(R,G,B,Time in seconds) + + led.write(0, 0, 0); + delay(1000); + + led.fadeIn(100, 100, 100, 1); // fadeIn animation(R,G,B,Time in seconds) + + led.write(0, 0, 0); + delay(1000); + + led.fadeOut(10, 64, 20, 1); // fadeout animation(R,G,B,Time in seconds) + + led.write(0, 0, 0); + delay(1000); + + led.strobe(200, 0, 200, 1); // strobe animation(R,G,B,Time in seconds) + + led.write(0, 0, 0); + delay(1000); +} diff --git a/examples/MQTT/MQTT.ino b/examples/MQTT/MQTT.ino new file mode 100644 index 00000000..725ae74c --- /dev/null +++ b/examples/MQTT/MQTT.ino @@ -0,0 +1,55 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include +#include + +CoolWifi wifiManager; +CoolMQTT mqtt; + +String ssid[] = {"ssid1", "ssid2"}; // put up to 50 WiFi ssids +String pass[] = {"pass1", "pass2"}; // put up to 50 WiFi passwords + +const char mqttServer[] = "----------"; // the mqtt server you're going to use +const char inTopic[] = + "--------"; // the topic you're going to subscribe to (receive from) +const char outTopic[] = + "-------"; // the topic you're going to publish to (send to ) +const char clientId[] = "------"; // your user name +int bufferSize = 128; // bytes +int keepAlive = 15; // seconds + +void setup() { + Serial.begin(115200); + + wifiManager.config(ssid, pass, 2, 180, 0); + wifiManager.begin(); + wifiManager.printConf(); + + mqtt.config(mqttServer, inTopic, outTopic, clientId, bufferSize); + mqtt.begin(); + mqtt.printConf(); + + wifiManager.connect(); + + mqtt.connect(keepAlive); +} + +void loop() { + mqtt.publish( + "hello world by CoolBoard"); // publish this message to the outTopic + delay(1000); + Serial.println(mqtt.read()); // print answer if answer is received + delay(1000); + mqtt.mqttLoop(); + delay(1000); +} diff --git a/examples/README.md b/examples/README.md deleted file mode 100644 index ddece030..00000000 --- a/examples/README.md +++ /dev/null @@ -1,7 +0,0 @@ - - diff --git a/examples/Sensors/Sensors.ino b/examples/Sensors/Sensors.ino new file mode 100644 index 00000000..eeb4e4c4 --- /dev/null +++ b/examples/Sensors/Sensors.ino @@ -0,0 +1,47 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include + +CoolBoardSensors sensors; + +void setup() { + Serial.begin(115200); + sensors.allActive(); + sensors.begin(); +} + +void loop() { + Serial.println("Sensor data dump:"); + Serial.println(sensors.read()); + delay(1000); + + Serial.println("Individual readings:"); + Serial.print("Battery voltage: "); + Serial.println(sensors.readVBat()); + Serial.print("Earth moisture: "); + Serial.println(sensors.readMoisture()); + Serial.print("Visible light: "); + Serial.println(sensors.lightSensor.ReadVisible()); + Serial.print("InfraRed light: "); + Serial.println(sensors.lightSensor.ReadIR()); + Serial.print("Ultraviolet light: "); + Serial.println(sensors.lightSensor.ReadUV() / 100); + Serial.print("Pressure: "); + Serial.println(sensors.envSensor.readFloatPressure()); + Serial.print("Air humidity: "); + Serial.println(sensors.envSensor.readFloatHumidity()); + Serial.print("Temperature:"); + Serial.print(sensors.envSensor.readTempC()); + Serial.println("°C"); + delay(1000); +} diff --git a/examples/Station/Station.ino b/examples/Station/Station.ino new file mode 100644 index 00000000..3377593d --- /dev/null +++ b/examples/Station/Station.ino @@ -0,0 +1,34 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include + +CoolBoard coolBoard; + +void setup() { + Serial.begin(115200); + coolBoard.config(); + + coolBoard.begin(); + + Serial.print("one log every "); + Serial.print(coolBoard.getLogInterval()); + Serial.println(" s "); + + if (coolBoard.connect() == 0) { + coolBoard.onLineMode(); + } else { + coolBoard.offLineMode(); + } +} + +void loop() {} diff --git a/examples/Station/data/coolBoardActorConfig.json b/examples/Station/data/coolBoardActorConfig.json new file mode 100644 index 00000000..6db38025 --- /dev/null +++ b/examples/Station/data/coolBoardActorConfig.json @@ -0,0 +1,21 @@ +{ + "actif": 1, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 0 + ], + "high": [ + 30, + 0, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] +} diff --git a/examples/Station/data/coolBoardConfig.json b/examples/Station/data/coolBoardConfig.json new file mode 100644 index 00000000..8826edb2 --- /dev/null +++ b/examples/Station/data/coolBoardConfig.json @@ -0,0 +1,8 @@ +{ + "logInterval": 60, + "ireneActive": 0, + "jetpackActive": 0, + "externalSensorsActive": 0, + "sleepActive": 1, + "manual": 0 +} diff --git a/examples/Station/data/coolBoardLedConfig.json b/examples/Station/data/coolBoardLedConfig.json new file mode 100644 index 00000000..ab94a406 --- /dev/null +++ b/examples/Station/data/coolBoardLedConfig.json @@ -0,0 +1,3 @@ +{ + "ledActive": 1 +} diff --git a/examples/Station/data/coolBoardSensorsConfig.json b/examples/Station/data/coolBoardSensorsConfig.json new file mode 100644 index 00000000..e2270644 --- /dev/null +++ b/examples/Station/data/coolBoardSensorsConfig.json @@ -0,0 +1,14 @@ +{ + "BME280": { + "temperature": 1, + "humidity": 1, + "pressure": 1 + }, + "SI114X": { + "visible": 1, + "ir": 1, + "uv": 1 + }, + "vbat": 1, + "soilMoisture": 1 +} diff --git a/examples/Station/data/externalSensorsConfig.json b/examples/Station/data/externalSensorsConfig.json new file mode 100644 index 00000000..4959c834 --- /dev/null +++ b/examples/Station/data/externalSensorsConfig.json @@ -0,0 +1,7 @@ +{ + "sensorsNumber": 1, + "sensor0": { + "reference": "DallasTemperature", + "type": "TempOneWire" + } +} diff --git a/examples/Station/data/irene3000Config.json b/examples/Station/data/irene3000Config.json new file mode 100644 index 00000000..c91553f6 --- /dev/null +++ b/examples/Station/data/irene3000Config.json @@ -0,0 +1,13 @@ +{ + "waterTemp": { + "active": 1 + }, + "phProbe": { + "active": 1 + }, + "adc2": { + "active": 0, + "gain": 1, + "type": "voltage" + } +} diff --git a/examples/Station/data/jetPackConfig.json b/examples/Station/data/jetPackConfig.json new file mode 100644 index 00000000..9919fa48 --- /dev/null +++ b/examples/Station/data/jetPackConfig.json @@ -0,0 +1,166 @@ +{ + "Act0": { + "actif": 1, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 0 + ], + "high": [ + 30, + 0, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act1": { + "actif": 1, + "inverted": 1, + "temporal": 0, + "low": [ + 20, + 5000, + 0, + 0 + ], + "high": [ + 30, + 2000, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act2": { + "actif": 1, + "inverted": 0, + "temporal": 1, + "low": [ + 20, + 3000, + 0, + 0 + ], + "high": [ + 30, + 5000, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act3": { + "actif": 1, + "inverted": 0, + "temporal": 1, + "low": [ + 20, + 0, + 13, + 0 + ], + "high": [ + 30, + 0, + 12, + 0 + ], + "type": [ + "Temperature", + "hour" + ] + }, + "Act4": { + "actif": 1, + "inverted": 0, + "temporal": 1, + "low": [ + 20, + 5000, + 0, + 59 + ], + "high": [ + 30, + 2000, + 0, + 40 + ], + "type": [ + "temperature", + "minute" + ] + }, + "Act5": { + "actif": 1, + "inverted": 0, + "temporal": 1, + "low": [ + 20, + 5000, + 13, + 55 + ], + "high": [ + 30, + 2000, + 12, + 45 + ], + "type": [ + "temperature", + "hourMinute" + ] + }, + "Act6": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 5000, + 18 + ], + "high": [ + 30, + 2000, + 17 + ], + "type": [ + "temperature", + "hour" + ] + }, + "Act7": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 5000, + 18 + ], + "high": [ + 30, + 2000, + 17 + ], + "type": [ + "temperature", + "hour" + ] + } +} diff --git a/examples/Station/data/mqttConfig.json b/examples/Station/data/mqttConfig.json new file mode 100644 index 00000000..73df7203 --- /dev/null +++ b/examples/Station/data/mqttConfig.json @@ -0,0 +1,4 @@ +{ + "mqttServer": "my.mqtt-server.com", + "bufferSize": 3000 +} diff --git a/examples/Station/data/rtcConfig.json b/examples/Station/data/rtcConfig.json new file mode 100644 index 00000000..17bd878a --- /dev/null +++ b/examples/Station/data/rtcConfig.json @@ -0,0 +1,4 @@ +{ + "timeServer": "europe.pool.ntp.org", + "localPort": 8888 +} diff --git a/examples/Station/data/wifiConfig.json b/examples/Station/data/wifiConfig.json new file mode 100644 index 00000000..69aee403 --- /dev/null +++ b/examples/Station/data/wifiConfig.json @@ -0,0 +1,5 @@ +{ + "wifiCount": 0, + "timeOut": 300, + "nomad": 0 +} diff --git a/examples/Time/Time.ino b/examples/Time/Time.ino new file mode 100644 index 00000000..9aead404 --- /dev/null +++ b/examples/Time/Time.ino @@ -0,0 +1,47 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include +#include +#include + +CoolWifi wifiManager; +CoolTime clock; + +String ssid[] = {"ssid0", "ssid1"}; +String pass[] = {"pass0", "pass1"}; + +IPAddress timeServer(132, 163, 4, 101); +unsigned int localPort = 8888; + +void setup() { + Wire.begin(2, 14); + + Serial.begin(115200); + + wifiManager.config(ssid, pass, 2, 180, 0); + + wifiManager.begin(); + + wifiManager.printConf(); + + wifiManager.connect(); + + clock.config(timeServer, localPort); + + clock.begin(); +} + +void loop() { + Serial.println(clock.getESDate()); + delay(3000); +} diff --git a/examples/WeatherStation/WeatherStation.ino b/examples/WeatherStation/WeatherStation.ino index db8ee042..70d7f1f9 100644 --- a/examples/WeatherStation/WeatherStation.ino +++ b/examples/WeatherStation/WeatherStation.ino @@ -1,69 +1,36 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - -/** -* CoolBoardExample -* -* This example shows typical use -* of the CoolBoard. -* -* Save this example in another location -* in order to safely modify the configuration files -* in the data folder. -* -*/ + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ #include - CoolBoard coolBoard; +void setup() { + Serial.begin(115200); + coolBoard.config(); -void setup() -{ - Serial.begin(115200); - coolBoard.config(); - - coolBoard.begin(); + coolBoard.begin(); - coolBoard.printConf(); + coolBoard.printConf(); - Serial.print("one log every "); - Serial.print(coolBoard.getLogInterval()); - Serial.println(" s " ); + Serial.print("one log every "); + Serial.print(coolBoard.getLogInterval()); + Serial.println(" s "); } -void loop() -{ - if(coolBoard.isConnected()==0 ) - { - coolBoard.onLineMode(); - } - else - { - coolBoard.offLineMode(); - } - - - +void loop() { + if (coolBoard.isConnected() == 0) { + coolBoard.onLineMode(); + } else { + coolBoard.offLineMode(); + } } diff --git a/examples/WeatherStation/data/coolBoardActorConfig.json b/examples/WeatherStation/data/coolBoardActorConfig.json index ac81092b..80670069 100644 --- a/examples/WeatherStation/data/coolBoardActorConfig.json +++ b/examples/WeatherStation/data/coolBoardActorConfig.json @@ -1,8 +1,21 @@ { - "actif":0, - "inverted":0, - "temporal":0, - "low":[0,5000,0,0], - "high":[0,5000,0,0], - "type":["Temperature",""] + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 0, + 5000, + 0, + 0 + ], + "high": [ + 0, + 5000, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] } diff --git a/examples/WeatherStation/data/coolBoardConfig.json b/examples/WeatherStation/data/coolBoardConfig.json index aa9d5444..2462b68c 100644 --- a/examples/WeatherStation/data/coolBoardConfig.json +++ b/examples/WeatherStation/data/coolBoardConfig.json @@ -1,13 +1,10 @@ - { - "logInterval":3600, - "ireneActive":0, - "jetpackActive":0, - "externalSensorsActive":0, - "sleepActive":1, - "manual":0, - "saveAsJSON":1, - "saveAsCSV":0 + "logInterval": 3600, + "ireneActive": 0, + "jetpackActive": 0, + "externalSensorsActive": 0, + "sleepActive": 1, + "manual": 0, + "saveAsJSON": 1, + "saveAsCSV": 0 } - - diff --git a/examples/WeatherStation/data/coolBoardLedConfig.json b/examples/WeatherStation/data/coolBoardLedConfig.json index 14893b32..ab94a406 100644 --- a/examples/WeatherStation/data/coolBoardLedConfig.json +++ b/examples/WeatherStation/data/coolBoardLedConfig.json @@ -1,5 +1,3 @@ { - "ledActive":1 + "ledActive": 1 } - - diff --git a/examples/WeatherStation/data/coolBoardSensorsConfig.json b/examples/WeatherStation/data/coolBoardSensorsConfig.json index 544e956d..e2270644 100644 --- a/examples/WeatherStation/data/coolBoardSensorsConfig.json +++ b/examples/WeatherStation/data/coolBoardSensorsConfig.json @@ -1,27 +1,14 @@ - { - - - "BME280": - { - "temperature" : 1, - "humidity":1, - "pressure":1 - - - }, - - "SI114X": - { - "visible":1, - "ir":1, - "uv":1 - }, - - "vbat":1, - - "soilMoisture":1 + "BME280": { + "temperature": 1, + "humidity": 1, + "pressure": 1 + }, + "SI114X": { + "visible": 1, + "ir": 1, + "uv": 1 + }, + "vbat": 1, + "soilMoisture": 1 } - - - diff --git a/examples/WeatherStation/data/externalSensorsConfig.json b/examples/WeatherStation/data/externalSensorsConfig.json index 6cc3ca5e..b9e32ded 100644 --- a/examples/WeatherStation/data/externalSensorsConfig.json +++ b/examples/WeatherStation/data/externalSensorsConfig.json @@ -1,4 +1,3 @@ { - "sensorsNumber":0 -} - + "sensorsNumber": 0 +} diff --git a/examples/WeatherStation/data/irene3000Config.json b/examples/WeatherStation/data/irene3000Config.json index e9e17ca3..5819c81a 100644 --- a/examples/WeatherStation/data/irene3000Config.json +++ b/examples/WeatherStation/data/irene3000Config.json @@ -1,22 +1,13 @@ { - "waterTemp": - { - "active":1 - }, - - "phProbe": - { - "active":1 - }, - - "adc2": - { - "active":1, - "gain":0.67, - "type":"waterLevel" - } + "waterTemp": { + "active": 1 + }, + "phProbe": { + "active": 1 + }, + "adc2": { + "active": 1, + "gain": 0.67, + "type": "waterLevel" + } } - - - - diff --git a/examples/WeatherStation/data/jetPackConfig.json b/examples/WeatherStation/data/jetPackConfig.json index 70190f5e..4307c4af 100644 --- a/examples/WeatherStation/data/jetPackConfig.json +++ b/examples/WeatherStation/data/jetPackConfig.json @@ -1,83 +1,170 @@ { - "Act0": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] - }, - - "Act1": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,0,0,0], - "high":[30,0,0,0], - "type":["Temperature",""] - }, - - "Act2": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[0,3000,0,0], - "high":[0,5000,0,0], - "type":["",""] - }, - - "Act3": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,3000,0,0], - "high":[40,5000,0,0], - "type":["Temperature",""] - }, - - "Act4": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[0,000,9,0], - "high":[0,000,7,0], - "type":["","hour"] - }, - - "Act5": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,0,9,0], - "high":[40,0,7,0], - "type":["Temperature","hour"] - }, - - "Act6": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[0,00,0,50], - "high":[0,00,0,1], - "type":["","minute"] - }, - - "Act7": - { - "actif":0, - "inverted":0, - "temporal":0, - "low":[20,00,0,50], - "high":[40,00,0,1], - "type":["Temperature","minute"] - } - - -} \ No newline at end of file + "Act0": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 0 + ], + "high": [ + 30, + 0, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act1": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 0 + ], + "high": [ + 30, + 0, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act2": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 0, + 3000, + 0, + 0 + ], + "high": [ + 0, + 5000, + 0, + 0 + ], + "type": [ + "", + "" + ] + }, + "Act3": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 3000, + 0, + 0 + ], + "high": [ + 40, + 5000, + 0, + 0 + ], + "type": [ + "Temperature", + "" + ] + }, + "Act4": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 0, + 0, + 9, + 0 + ], + "high": [ + 0, + 0, + 7, + 0 + ], + "type": [ + "", + "hour" + ] + }, + "Act5": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 9, + 0 + ], + "high": [ + 40, + 0, + 7, + 0 + ], + "type": [ + "Temperature", + "hour" + ] + }, + "Act6": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 0, + 0, + 0, + 50 + ], + "high": [ + 0, + 0, + 0, + 1 + ], + "type": [ + "", + "minute" + ] + }, + "Act7": { + "actif": 0, + "inverted": 0, + "temporal": 0, + "low": [ + 20, + 0, + 0, + 50 + ], + "high": [ + 40, + 0, + 0, + 1 + ], + "type": [ + "Temperature", + "minute" + ] + } +} diff --git a/examples/WeatherStation/data/mqttConfig.json b/examples/WeatherStation/data/mqttConfig.json index 273c15c2..73df7203 100644 --- a/examples/WeatherStation/data/mqttConfig.json +++ b/examples/WeatherStation/data/mqttConfig.json @@ -1,7 +1,4 @@ { - "mqttServer":"my.mqtt-server.com", - "bufferSize":3000 - + "mqttServer": "my.mqtt-server.com", + "bufferSize": 3000 } - - diff --git a/examples/WeatherStation/data/rtcConfig.json b/examples/WeatherStation/data/rtcConfig.json index a66d94da..b7176056 100644 --- a/examples/WeatherStation/data/rtcConfig.json +++ b/examples/WeatherStation/data/rtcConfig.json @@ -1,9 +1,7 @@ { - "NTP":true, - "compileTime":false, - "timeServer":"europe.pool.ntp.org", - "localPort":8888, - "timeSync":0 + "NTP": true, + "compileTime": false, + "timeServer": "europe.pool.ntp.org", + "localPort": 8888, + "timeSync": 0 } - - diff --git a/examples/WeatherStation/data/wifiConfig.json b/examples/WeatherStation/data/wifiConfig.json index c43fbd2e..a7f2c134 100644 --- a/examples/WeatherStation/data/wifiConfig.json +++ b/examples/WeatherStation/data/wifiConfig.json @@ -1,6 +1,5 @@ { - "wifiCount":0, - "timeOut":180, - "nomad":0 - + "wifiCount": 0, + "timeOut": 180, + "nomad": 0 } diff --git a/examples/Wifi/Wifi.ino b/examples/Wifi/Wifi.ino new file mode 100644 index 00000000..d6d95f4a --- /dev/null +++ b/examples/Wifi/Wifi.ino @@ -0,0 +1,30 @@ +/** + * CoolBoardExample + * + * This example shows typical use + * of the CoolBoard. + * + * Save this example in another location + * in order to safely modify the configuration files + * in the data folder. + * + */ + +#include + +CoolWifi wifiManager; + +String ssid[] = {"ssid0", "ssid1"}; +String pass[] = {"pass0", "pass1"}; + +void setup() { + Serial.begin(115200); + // config(ssid array, pass array, number of wifis, AP timeout,nomad flag ); + wifiManager.config(ssid, pass, 2, 180, 0); + wifiManager.begin(); + wifiManager.printConf(); + + wifiManager.connect(); +} + +void loop() {} From 4c33021ee13bc053a0baccf69ca3781a02b53c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erwan=20Queff=C3=A9lec?= Date: Fri, 16 Feb 2018 04:09:57 +0100 Subject: [PATCH 2/3] Reformat source code with LLVM standards MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Erwan Queffélec --- src/CoolBoard.cpp | 2555 +++++++--------- src/CoolBoard.h | 396 ++- src/CoolBoardActor.cpp | 2437 +++++++-------- src/CoolBoardActor.h | 387 +-- src/CoolBoardLed.cpp | 671 ++-- src/CoolBoardLed.h | 107 +- src/CoolBoardSensors.cpp | 951 +++--- src/CoolBoardSensors.h | 232 +- src/CoolFileSystem.cpp | 1905 ++++++------ src/CoolFileSystem.h | 111 +- src/CoolMQTT.cpp | 1003 +++--- src/CoolMQTT.h | 174 +- src/CoolTime.cpp | 1504 +++++---- src/CoolTime.h | 220 +- src/CoolWifi.cpp | 1191 ++++---- src/CoolWifi.h | 172 +- src/ExternalSensor.h | 1562 +++++----- src/ExternalSensors.cpp | 960 +++--- src/ExternalSensors.h | 131 +- src/Irene3000.cpp | 1364 ++++----- src/Irene3000.h | 211 +- src/Jetpack.cpp | 2714 ++++++++--------- src/Jetpack.h | 382 +-- .../extras/WiFiManager.template.html | 80 - src/internals/extras/parse.js | 60 - src/internals/extras/template.h | 35 - src/internals/travis/common.sh | 51 - 27 files changed, 9827 insertions(+), 11739 deletions(-) delete mode 100644 src/internals/extras/WiFiManager.template.html delete mode 100644 src/internals/extras/parse.js delete mode 100644 src/internals/extras/template.h delete mode 100644 src/internals/travis/common.sh diff --git a/src/CoolBoard.cpp b/src/CoolBoard.cpp index 5d4cebe2..c2c5cd70 100644 --- a/src/CoolBoard.cpp +++ b/src/CoolBoard.cpp @@ -1,1667 +1,1280 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ -#include "FS.h" #include "CoolBoard.h" -#include "ArduinoJson.h" #include "Arduino.h" +#include "ArduinoJson.h" +#include "FS.h" #include #include -#define DEBUG 0 +#define DEBUG 1 /** -* CoolBoard::CoolBoard(): -* This Constructor is provided to start -* the I2C interface and Init the different -* used pins -*/ -CoolBoard::CoolBoard() -{ + * CoolBoard::CoolBoard(): + * This Constructor is provided to start + * the I2C interface and init pins + */ +CoolBoard::CoolBoard() { #if DEBUG == 1 + Serial.println(F("Entering CoolBoard Constructor")); + Serial.println(); +#endif - Serial.println( F("Entering CoolBoard Constructor") ); - Serial.println(); + Wire.begin(2, 14); // I2C init + pinMode(EnI2C, OUTPUT); // Declare I2C Enable pin +} +/** + * CoolBoard::begin(): + * This method is provided to configure and + * start the used CoolKit Parts. + * It also starts the first connection try + * If Serial is enabled,it prints the configuration + * of the used parts. + */ +void CoolBoard::begin() { + + Serial.println(F("Starting La COOL Board...")); +#if DEBUG == 1 + Serial.println(F("Entering CoolBoard.begin() ")); + Serial.println(); #endif - - Wire.begin(2, 14); //I2C init - pinMode(EnI2C, OUTPUT); //Declare I2C Enable pin + delay(100); -} + coolBoardLed.write(255, 128, 0); // orange + this->initReadI2C(); + delay(100); + // read RTC config on startup and treat off grid if the compile flag is set + rtc.config(); + rtc.offGrid(); + delay(200); -/** -* CoolBoard::begin(): -* This method is provided to configure and -* start the used CoolKit Parts. -* It also starts the first connection try -* If Serial is enabled,it prints the configuration -* of the used parts. -*/ -void CoolBoard::begin() -{ + coolBoardSensors.config(); + coolBoardSensors.begin(); + delay(200); -#if DEBUG == 1 + onBoardActor.config(); + onBoardActor.begin(); + delay(100); - Serial.println( F("Starting the CoolBoard ") ); - Serial.println( F("Entering CoolBoard.begin() ") ); - Serial.println(); -#endif + wifiManager.config(); + wifiManager.begin(); + delay(100); -#if DEBUG == 0 - Serial.println( F("Starting Coolboard...")); -#endif + mqtt.config(); + mqtt.begin(); + delay(100); +#if DEBUG == 1 + coolBoardLed.printConf(); + coolBoardSensors.printConf(); + onBoardActor.printConf(); + wifiManager.printConf(); + mqtt.printConf(); +#endif - delay(100); - - coolBoardLed.write(255,128,0);//orange + if (jetpackActive) { + jetPack.config(); + jetPack.begin(); - this->initReadI2C(); - delay(100); +#if DEBUG == 1 + jetPack.printConf(); +#endif - //read RTC config on startup and treat Off grid if the compile flag is set - rtc.config(); - rtc.offGrid(); - delay(200); + delay(100); + } + + if (ireneActive) { + irene3000.config(); + irene3000.startADC(); + coolBoardLed.write(128, 128, 128); + delay(2000); + + int bValue = irene3000.readButton(); + + if (bValue >= 65000) { + bValue = 8800; + } + Serial.print(F("Irene Button : ")); + Serial.println(bValue); + bValue = irene3000.readButton(); + if (bValue >= 65000) { + bValue = 8800; + } + if (bValue < 2000) { + Serial.println("Enter Irene config."); + coolBoardLed.write(255, 255, 255); + delay(5000); + coolBoardLed.write(0, 50, 0); + bValue = irene3000.readButton(); + if (bValue >= 65000) { + bValue = 0; + } + while (bValue > 2000) { + bValue = irene3000.readButton(); + Serial.println(irene3000.readButton()); + if (bValue >= 65000) { + bValue = 8800; + } + delay(1000); + + yield(); + Serial.println(F("while 1")); + } + + coolBoardLed.write(0, 255, 0); + Serial.println(F("calibrating the Ph probe ")); + Serial.println(F("ph7 calibration for 25 seconds")); + delay(10000); + + irene3000.calibratepH7(); + delay(15000); + + irene3000.calibratepH7(); + coolBoardLed.write(50, 0, 0); + bValue = irene3000.readButton(); + + if (bValue >= 65000) { + bValue = 8800; + } + + while (bValue > 2000) { + bValue = irene3000.readButton(); + Serial.println(irene3000.readButton()); + if (bValue >= 65000) { + bValue = 8800; + } + delay(1000); + + yield(); + Serial.println(F("while 2")); + } + + coolBoardLed.write(255, 0, 0); + Serial.println(F("calibrating the Ph probe ")); + Serial.println(F("ph4 calibration for 25 seconds")); + delay(10000); + + irene3000.calibratepH4(); + delay(15000); + + irene3000.calibratepH4(); + irene3000.saveParams(); + + coolBoardLed.write(50, 0, 50); + bValue = irene3000.readButton(); + if (bValue >= 65000) { + bValue = 8800; + } + while (bValue > 2000) { + bValue = irene3000.readButton(); + Serial.println(irene3000.readButton()); + if (bValue >= 65000) { + bValue = 8800; + } + delay(1000); + yield(); + Serial.println(F("while 3")); + } + } - coolBoardSensors.config(); - coolBoardSensors.begin(); - delay(200); +#if DEBUG == 1 + irene3000.printConf(); +#endif - onBoardActor.config(); - onBoardActor.begin(); - delay(100); - - wifiManager.config(); - wifiManager.begin(); - delay(100); + delay(100); + } - mqtt.config(); - mqtt.begin(); - delay(100); + if (externalSensorsActive) { + externalSensors.config(); + externalSensors.begin(); + delay(100); #if DEBUG == 1 + externalSensors.printConf(); +#endif - coolBoardLed.printConf(); + } + coolBoardLed.fadeOut(255, 128, 0, 0.5); // orange - coolBoardSensors.printConf(); + this->connect(); + delay(100); - onBoardActor.printConf(); + // RTC must be configured at startup to ensure RTC in off-grid configuration + rtc.begin(); - wifiManager.printConf(); + if (this->sleepActive == 0) { + coolBoardLed.strobe(255, 0, 230, 0.5); // shade of pink - mqtt.printConf(); - + // send all config over mqtt + this->sendConfig("CoolBoard", "/coolBoardConfig.json"); + this->sendConfig("CoolSensorsBoard", "/coolBoardSensorsConfig.json"); + this->sendConfig("CoolBoardActor", "/coolBoardActorConfig.json"); + this->sendConfig("rtc", "/rtcConfig.json"); + this->sendConfig("led", "/coolBoardLedConfig.json"); -#endif + if (jetpackActive) { + this->sendConfig("jetPack", "/jetPackConfig.json"); + } + if (ireneActive) { + this->sendConfig("irene3000", "/irene3000Config.json"); + } - if (jetpackActive) - { - jetPack.config(); - jetPack.begin(); - - #if DEBUG == 1 - - jetPack.printConf(); - - #endif - delay(100); - } - - if (ireneActive) - { - irene3000.config(); - irene3000.startADC(); - coolBoardLed.write(128,128,128); - delay(2000); - int bValue=irene3000.readButton(); - if (bValue >= 65000) bValue = 8800; - Serial.print(F("Irene Button : ")); - Serial.println(bValue); - bValue=irene3000.readButton(); - if (bValue >= 65000) bValue = 8800; - if (bValue < 2000) - { - Serial.println("Enter Irene config."); - coolBoardLed.write(255,255,255); - delay(5000); - coolBoardLed.write(0,50,0); - bValue=irene3000.readButton(); - if (bValue >= 65000) bValue = 0; - while(bValue > 2000) - { - bValue=irene3000.readButton(); - Serial.println(irene3000.readButton()); - if (bValue >= 65000) bValue = 8800; - delay(1000); - yield(); - Serial.println(F("while 1")); - } - - coolBoardLed.write(0,255,0); - - Serial.println(F("calibrating the Ph probe " ) ); - - Serial.println(F("ph7 calibration for 25 seconds")); - - delay(10000); - - irene3000.calibratepH7(); - - delay(15000); - - irene3000.calibratepH7(); - - coolBoardLed.write(50,0,0); - bValue=irene3000.readButton(); - if (bValue >= 65000) bValue = 8800; - while(bValue > 2000) - { - bValue=irene3000.readButton(); - Serial.println(irene3000.readButton()); - if (bValue >= 65000) bValue = 8800; - delay(1000); - yield(); - Serial.println(F("while 2")); - } - - coolBoardLed.write(255,0,0); - - Serial.println(F("calibrating the Ph probe " ) ); - - Serial.println(F("ph4 calibration for 25 seconds")); - - delay(10000); - - irene3000.calibratepH4(); - - delay(15000); - - irene3000.calibratepH4(); - - irene3000.saveParams(); - - coolBoardLed.write(50,0,50); - bValue=irene3000.readButton(); - if (bValue >= 65000) bValue = 8800; - while(bValue > 2000) - { - bValue=irene3000.readButton(); - Serial.println(irene3000.readButton()); - if (bValue >= 65000) bValue = 8800; - delay(1000); - yield(); - Serial.println(F("while 3")); - } - } - - - - - /*irene3000.config(); - irene3000.begin();*/ - - #if DEBUG == 1 - - irene3000.printConf(); - - #endif - delay(100); - } - - if (externalSensorsActive) - { - externalSensors.config(); - externalSensors.begin(); - - #if DEBUG == 1 - - externalSensors.printConf(); - - #endif - delay(100); - } - delay(100); - coolBoardLed.fadeOut(255,128,0,0.5);//orange - - this->connect(); - - delay(100); - - //RTC must be configured at startup to ensure RTC in Off Grid configuration - - rtc.begin(); - - if( this->sleepActive==0 ) - { - coolBoardLed.strobe(255,0,230,0.5);//shade of pink - - //send all config over mqtt - - this->sendConfig("CoolBoard","/coolBoardConfig.json"); - - this->sendConfig("CoolSensorsBoard","/coolBoardSensorsConfig.json"); - - this->sendConfig("CoolBoardActor","/coolBoardActorConfig.json"); - - this->sendConfig("rtc","/rtcConfig.json"); - - this->sendConfig("led","/coolBoardLedConfig.json"); + if (externalSensorsActive) { + this->sendConfig("externalSensors", "/externalSensorsConfig.json"); + } + this->sendConfig("mqtt", "/mqttConfig.json"); + } - if(jetpackActive) - { - this->sendConfig("jetPack","/jetPackConfig.json"); - } - - if(ireneActive) - { - this->sendConfig("irene3000","/irene3000Config.json"); - } +#if DEBUG == 1 + rtc.printConf(); +#endif - if(externalSensorsActive) - { - this->sendConfig("externalSensors","/externalSensorsConfig.json"); - } + delay(100); + coolBoardLed.blink(0, 255, 0, 0.5); // green +} - this->sendConfig("mqtt","/mqttConfig.json"); - } +/** + * CoolBoard::isConnected() + * + * This method is provided to check + * if the card is connected to Wifi and MQTT + * + * \return 0 : connected + * -1: Wifi Not Connected + * -2: MQTT Not Connected + * + */ +int CoolBoard::isConnected() { #if DEBUG == 1 + Serial.println(F("Entering CoolBoard.isConnected ")); + Serial.println(); +#endif - rtc.printConf(); + if (wifiManager.state() != WL_CONNECTED) { + Serial.println(F("Wifi Not Connected")); +#if DEBUG == 1 + Serial.println(F("Wifi State is ")); + Serial.println(wifiManager.state()); #endif - delay(100); - coolBoardLed.blink(0,255,0,0.5);//green + return (-1); + } -} - -/** -* CoolBoard::isConnected() -* -* This method is provided to check -* if the card is connected to Wifi and MQTT -* -* \return 0 : connected -* -1: Wifi Not Connected -* -2: MQTT Not Connected -* -*/ -int CoolBoard::isConnected() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoard.isConnected ") ); - Serial.println(); + if (mqtt.state() != 0) { + Serial.println(F("MQTT not Connected, reconnecting later...")); +#if DEBUG == 1 + Serial.println(F("mqtt state is :")); + Serial.println(mqtt.state()); #endif - if (wifiManager.state() != WL_CONNECTED) - { - - Serial.println(F("Wifi Not Connected")); - - #if DEBUG == 1 - - Serial.println(F("Wifi State is ")); - Serial.println(wifiManager.state()); - - #endif - return(-1); - } - - if(mqtt.state() != 0) - { - - Serial.println( F("MQTT not Connected, reconnecting later...")); - - #if DEBUG==1 - Serial.println( F("mqtt state is :") ); - Serial.println(mqtt.state()); - - #endif - } - - return(0); + } + return (0); } - /** -* CoolBoard::connect(): -* This method is provided to manage the network -* connection and the mqtt connection. -* -* \return mqtt client state -*/ -int CoolBoard::connect() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoard.connect ") ); - Serial.println(); - Serial.println( F("Connecting the CoolBoard ") ); - delay(100); + * CoolBoard::connect(): + * This method is provided to manage the network + * connection and the mqtt connection. + * + * \return mqtt client state + */ +int CoolBoard::connect() { +#if DEBUG == 1 + Serial.println(F("Entering CoolBoard.connect ")); + Serial.println(); + Serial.println(F("Connecting the CoolBoard ")); + delay(100); #endif - //coolBoardLed.write(0,0,255);//blue - delay(10); - coolBoardLed.write(0,0,255);//blue - - - - -#if DEBUG == 1 - Serial.println( F("Launching CoolWifi") ); - Serial.println(); + delay(10); + coolBoardLed.write(0, 0, 255); // blue +#if DEBUG == 1 + Serial.println(F("Launching CoolWifi")); + Serial.println(); #endif - wifiManager.connect(); - delay(100); - - - //only attempt MQTT connection when Wifi is Connected - if (wifiManager.state() == WL_CONNECTED) - { - - #if DEBUG == 1 - - Serial.println( F("Launching mqtt.connect()") ); - Serial.println(); - - #endif - //logInterval in seconds - mqtt.connect(); - delay(100); - if (mqtt.state() != 0 && wifiManager.nomad == 1) - { - Serial.println( F("Known WIFI in the area but no internet connection")); - Serial.println( F(" ---> Launching Configuration Portal <---")); - wifiManager.disconnect(); - delay(200); - coolBoardLed.write(255,128,255);//whiteish violet.. - wifiManager.connectAP(); - } - } - - - - + + wifiManager.connect(); + delay(100); + + // only attempt MQTT connection if Wifi is connected + if (wifiManager.state() == WL_CONNECTED) { + #if DEBUG == 1 + Serial.println(F("Launching mqtt.connect()")); + Serial.println(); +#endif - Serial.println( F("mqtt state is :") ); - Serial.println(mqtt.state()); - Serial.println(); - delay(100); + // logInterval in seconds + mqtt.connect(); + delay(100); + if (mqtt.state() != 0 && wifiManager.nomad == 1) { + Serial.println(F("Known WIFI in the area but no internet connection")); + Serial.println(F(" ---> Launching Configuration Portal <---")); + wifiManager.disconnect(); + delay(200); + coolBoardLed.write(255, 128, 255); // whiteish violet.. + wifiManager.connectAP(); + } + } +#if DEBUG == 1 + Serial.println(F("mqtt state is :")); + Serial.println(mqtt.state()); + Serial.println(); + delay(100); #endif - coolBoardLed.blink(0,0,255,0.5);//blue + coolBoardLed.blink(0, 0, 255, 0.5); // blue - return(mqtt.state()); + return (mqtt.state()); } /** -* CoolBoard::onLineMode(): -* This method is provided to manage the online -* mode: -update clock -* -read sensor -* -do actions -* -publish data -* -read answer -* -update config -*/ -void CoolBoard::onLineMode() -{ - - coolBoardLed.fadeIn(128,255,50,0.5);//shade of green + * CoolBoard::onLineMode(): + * This method is provided to manage the online + * mode: + * - update clock + * - read sensor + * - do actions + * - publish data + * - read answer + * - update config + */ +void CoolBoard::onLineMode() { + + coolBoardLed.fadeIn(128, 255, 50, 0.5); // shade of green #if DEBUG == 1 + Serial.println(F("Entering CoolBoard.onLineMode() ")); + Serial.println(); +#endif + +#if DEBUG == 0 + Serial.println(F("CoolBoard is in Online Mode")); +#endif + + // check if we hit MQTT timeout + if (mqtt.state() != 0) { + Serial.println(F("reconnecting MQTT...")); - Serial.println( F("Entering CoolBoard.onLineMode() ") ); - Serial.println(); + mqtt.connect(); + delay(200); + } + data = ""; + answer = ""; + + // send saved data if any, check once again if the MQTT connection is OK! + if (fileSystem.isDataSaved() && isConnected() == 0 && mqtt.state() == 0) { + + coolBoardLed.fadeIn(128, 128, 255, 0.5); // shade of blue + + Serial.println(F("There is data saved on the File System")); + Serial.println(F("Sending saved data over MQTT ")); + Serial.println(); + coolBoardLed.strobe(128, 128, 255, 0.5); // shade of blue + + mqtt.publish("sending saved data"); + mqtt.mqttLoop(); + + int size = 0; + std::unique_ptr savedData( + std::move(fileSystem.getSensorSavedData(size))); + + int i = 0; + // loop through the array + while (i < size) { + // format JSON + String jsonData = "{\"state\":{\"reported\":"; + jsonData += savedData[i]; + jsonData += " } }"; + +#if DEBUG == 1 + Serial.println(F("Size is : ")); + Serial.println(size); + Serial.print(F("sending line N°")); + Serial.println(i); + Serial.println(jsonData); + Serial.println(); #endif -#if DEBUG == 0 - Serial.println( F("CoolBoard is in Online Mode")); + coolBoardLed.strobe(128, 128, 255, 0.5); // shade of blue + mqtt.publish(jsonData.c_str()); + mqtt.mqttLoop(); + coolBoardLed.fadeOut(128, 128, 255, 0.5); // shade of blue + i++; + yield(); + } +#if DEBUG == 1 + Serial.println(F("Saved data sent ")); + Serial.println(); #endif + } + + coolBoardLed.blink(128, 255, 50, 0.5); // shade of green + + // update RTC + Serial.println(F("Re-checking RTC...")); + rtc.update(); + + // read board data + data = this->boardData(); + + // format JSON + data.setCharAt(data.lastIndexOf('}'), ','); - //check if we hit mqtt Timeout - if (mqtt.state() != 0) - { - Serial.println( F("reconnecting MQTT...")); - - mqtt.connect(); - delay(200); - } - - data=""; - answer=""; - - //send saved data if any, check once again if the mqtt connection is ok! - if(fileSystem.isDataSaved() && isConnected() == 0 && mqtt.state() == 0) - { - - coolBoardLed.fadeIn(128,128,255,0.5);//shade of blue - - Serial.println( F("There is data saved on the File System") ); - Serial.println( F("Sending saved data over MQTT ") ); - Serial.println(); - coolBoardLed.strobe(128,128,255,0.5);//shade of blue - - mqtt.publish("sending saved data"); - mqtt.mqttLoop(); - - int size=0; - std::unique_ptr savedData(std::move(fileSystem.getSensorSavedData(size)));//{..,..,..} - - int i=0; - //loop through the array - while(iboardData();//{"":"","":"","",""} - - //formatting json - data.setCharAt( data.lastIndexOf('}') , ',');//{"":"","":"","","", - - //read sensors data #if DEBUG == 1 + Serial.println(F("Collecting sensors data ")); + Serial.println(); +#endif + + // read sensors data + data += this->readSensors(); + + // format JSON + data.remove(data.lastIndexOf('{'), 1); + coolBoardLed.fadeOut(245, 237, 27, 0.5); // shade of yellow + + // do actions + if (this->manual == 0) { + // get time for temporal actors + tmElements_t tm; + tm = rtc.getTimeDate(); - Serial.println( F("Collecting sensors data ") ); - Serial.println(); + if (jetpackActive) { +#if DEBUG == 1 + Serial.println(F("jetpack is Active ")); + Serial.println(); #endif - data+=this->readSensors();//{"":"","":"","","",{.......} + Serial.println(F("jetpack is active")); + coolBoardLed.fade(100, 100, 150, 0.5); // dark shade of blue - //formatting json correctly - data.remove(data.lastIndexOf('{'), 1);//{"":"","":"","","",.......} - - coolBoardLed.fadeOut(245,237,27,0.5);//shade of yellow + // save last value to see if state has changed + byte lastAction = jetPack.action; + String jetpackStatus = + jetPack.doAction(data.c_str(), int(tm.Hour), int(tm.Minute)); - //do action - if(this->manual == 0 ) - { - - //get Time for temporal actors - tmElements_t tm; - tm=rtc.getTimeDate(); + // send a message if an actor has changed + if (lastAction != jetPack.action) { + data += jetpackStatus; + data.remove(data.lastIndexOf('{'), 1); + data.setCharAt(data.indexOf('}'), ','); - if (jetpackActive) - { + // send jetpackStatus over MQTT + String jsonJetpackStatus = "{\"state\":{\"reported\":"; - #if DEBUG ==1 + jsonJetpackStatus += jetpackStatus; + jsonJetpackStatus += " } }"; + mqtt.publish(jsonJetpackStatus.c_str()); + } + } - Serial.println( F("jetpack is Active ") ); - Serial.println(); - - #endif + bool lastActionB = digitalRead(onBoardActor.pin); + String onBoardActorStatus = + onBoardActor.doAction(data.c_str(), int(tm.Hour), int(tm.Minute)); - Serial.println( F("jetpack doing action ") ); - - coolBoardLed.fade(100,100,150,0.5);//dark shade of blue - byte lastAction = jetPack.action;//keeps last value to see if state changes + // send a message if actor has changed + if (lastActionB != digitalRead(onBoardActor.pin)) { + data += onBoardActorStatus; + data.remove(data.lastIndexOf('{'), 1); + data.setCharAt(data.indexOf('}'), ','); - String jetpackStatus= jetPack.doAction(data.c_str(), int(tm.Hour), int(tm.Minute));//{....} - - if(lastAction != jetPack.action)//send a message if a actor has changed - { - data+=jetpackStatus;//{..,..,..}{..,..,..} - - data.remove(data.lastIndexOf('{'), 1);//{..,..,..}..,..,..} - - data.setCharAt( data.indexOf('}') , ',');//{..,..,..,..,..,..} - - //sending jetpackStatus over MQTT - String jsonJetpackStatus="{\"state\":{\"reported\":"; - - jsonJetpackStatus+=jetpackStatus; - - jsonJetpackStatus += " } }"; // {"state":{"reported":{..,..,..,..,..,..,..,..} } } - - mqtt.publish( jsonJetpackStatus.c_str() ); - } - - } - bool lastActionB = digitalRead(onBoardActor.pin); - - String onBoardActorStatus=onBoardActor.doAction( data.c_str(), int(tm.Hour), int(tm.Minute) ); - - if(lastActionB != digitalRead(onBoardActor.pin))//send a message if actor has changed - { - data+=onBoardActorStatus;//{..,..,..}{..,..,..} - - data.remove(data.lastIndexOf('{'), 1);//{..,..,..}..,..,..} - - data.setCharAt( data.indexOf('}') , ',');//{..,..,..,..,..,..} - - //sending onBoardActorStatus over MQTT - String jsonOnBoardActorStatus="{\"state\":{\"reported\":"; - - jsonOnBoardActorStatus+=onBoardActorStatus; - - jsonOnBoardActorStatus += " } }"; // {"state":{"reported":{..,..,..,..,..,..,..,..} } } - - mqtt.publish( jsonOnBoardActorStatus.c_str() ); - } - - } - else if(this->manual == 1 ) - { - - Serial.println(F("we are in manual mode")); - mqtt.mqttLoop(); - answer = mqtt.read(); - this -> update(answer.c_str()); - - } - - delay(50); - - - coolBoardLed.fadeIn(128,255,50,0.5);//shade of green - - //formatting data: - String jsonData = "{\"state\":{\"reported\":"; - jsonData += data; // {"state":{"reported":{..,..,..,..,..,..,..,..} - jsonData += " } }"; // {"state":{"reported":{..,..,..,..,..,..,..,..} } } - - //mqtt client loop to allow data handling - mqtt.mqttLoop(); - - coolBoardLed.blink(128,255,50,0.5);//shade of green - - //read mqtt answer - answer = mqtt.read(); - -#if DEBUG == 1 - - Serial.println( F("checking if there's an MQTT message ") ); - Serial.println( F("answer is : ") ); - Serial.println(answer); - Serial.println(); - -#endif - - coolBoardLed.fadeOut(128,255,50,0.5);//shade of green - - //check if the configuration needs update - //and update it if needed - this -> update(answer.c_str()); - - coolBoardLed.fadeIn(128,255,50,0.5);//shade of green - - //check if we hit mqtt Timeout - if (mqtt.state() != 0) - { - Serial.println( F("reconnecting MQTT...")); - - mqtt.connect(); - delay(200); - } - - //log interval is passed in seconds, logInterval*1000 = logInterval in ms, if no log was sent till now (so it also works when sleep is active since previusLogTime is 0 on startup - if( ( millis() - ( this->previousLogTime) ) >= ( logInterval*1000 ) || (this->previousLogTime == 0) ) - { - //publishing data - if( this->sleepActive==0 ) - { - coolBoardLed.strobe(255,0,230,0.5);//shade of pink - - //logInterval in seconds - if (!mqtt.publish( jsonData.c_str())) - { - fileSystem.saveSensorData( data.c_str() ); - Serial.println( F("MQTT publish failed! Saved Data as JSON in Memory : OK")); - } - - mqtt.mqttLoop(); - - } - else - { - coolBoardLed.strobe(230,255,0,0.5);//shade of yellow - - if (!mqtt.publish( jsonData.c_str())) - { - fileSystem.saveSensorData( data.c_str() ); - Serial.println( F("MQTT publish failed! Saving Data as JSON in Memory : OK")); - } - - mqtt.mqttLoop(); - - answer = mqtt.read(); - - this ->update(answer.c_str()); - - //logInterval in seconds - this->sleep( this->getLogInterval() ) ; - } - this->previousLogTime=millis(); - } - - coolBoardLed.fadeOut(128,255,50,0.5);//shade of green - - mqtt.mqttLoop(); - - //read mqtt answer - answer = mqtt.read(); - this -> update(answer.c_str()); - coolBoardLed.blink(128,255,50,0.5);//shade of green + // send onBoardActorStatus over MQTT + String jsonOnBoardActorStatus = "{\"state\":{\"reported\":"; + jsonOnBoardActorStatus += onBoardActorStatus; + jsonOnBoardActorStatus += " } }"; + mqtt.publish(jsonOnBoardActorStatus.c_str()); + } + } else if (this->manual == 1) { + Serial.println(F("we are in manual mode")); + mqtt.mqttLoop(); + answer = mqtt.read(); + this->update(answer.c_str()); + } -} + delay(50); + coolBoardLed.fadeIn(128, 255, 50, 0.5); // shade of green -/** -* CoolBoard::offlineMode(): -* This method is provided to manage the offLine -* mode: -read sensors -* -do actions -* -save data in the file system -* -if there is WiFi but no Internet : make data available over AP -* -if there is no connection : retry to connect -*/ -void CoolBoard::offLineMode() -{ - coolBoardLed.fade(51,100,50,0.5);//dark shade of green -#if DEBUG == 1 - - Serial.println( F("Entering off line mode ") ); - -#endif + String jsonData = "{\"state\":{\"reported\":"; + jsonData += data; + jsonData += " } }"; -#if DEBUG == 0 + // MQTT client loop to handle incoming data + mqtt.mqttLoop(); - Serial.println( F("CoolBoard is in Offline Mode")); + coolBoardLed.blink(128, 255, 50, 0.5); // shade of green -#endif + // read mqtt answer + answer = mqtt.read(); - coolBoardLed.fadeIn(245,237,27,0.5);//shade of yellow - coolBoardLed.blink(245,237,27,0.5);//shade of yellow - - //reading user data - data=this->boardData();//{"":"","":"","",""} - - //formatting json - data.setCharAt( data.lastIndexOf('}') , ',');//{"":"","":"","","", - - - //read sensors data - - Serial.println( F("Collecting sensors data ") ); - Serial.println(); - - data+=this->readSensors();//{"":"","":"","","",{.......} - - - - //formatting json correctly - data.remove(data.lastIndexOf('{'), 1);//{"":"","":"","","",.......} - - coolBoardLed.fade(51,100,50,0.5);//dark shade of green - - //get Time for temporal actors - tmElements_t tm; - tm=rtc.getTimeDate(); - - //do action - - if (jetpackActive) - { - - - - #if DEBUG == 1 - - Serial.println( F("jetpack is Active ") ); - Serial.println( F("jetpack doing action ") ); - Serial.println(); - - #endif - coolBoardLed.fade(100,100,150,0.5);//dark shade of blue - - data+=jetPack.doAction(data.c_str(), int(tm.Hour), (tm.Minute));//{..,..,..}{..,..,..} - - data.remove(data.lastIndexOf('{'), 1);//{..,..,..}..,..,..} - - data.setCharAt( data.indexOf('}') , ',');//{..,..,..,..,..,..} - - - } - - delay(50); - - if (onBoardActor.actor.actif == true) - { - data+=onBoardActor.doAction( data.c_str(), int(tm.Hour), int(tm.Minute) );//{..,..,..}{..,..,..} - - data.remove(data.lastIndexOf('{'), 1);//{..,..,..}..,..,..} - - data.setCharAt( data.indexOf('}') , ',');//{..,..,..,..,..,..} - } - - coolBoardLed.fade(51,100,50,0.5);//dark shade of green - //log interval is passed in seconds, logInterval*1000 = logInterval in ms, if no log was sent till now (so it also works when sleep is active since previusLogTime is 0 on startup - if( ( millis() - ( this->previousLogTime) ) >= ( logInterval*1000 ) || (this->previousLogTime == 0) ) - { - //saving data in the file system as JSON - if (this->saveAsJSON == 1) - { - fileSystem.saveSensorData( data.c_str() ); - Serial.println( F("saving Data as JSON in Memory : OK")); - } - - if (this->saveAsCSV == 1) - { - fileSystem.saveSensorDataCSV( data.c_str() ); - Serial.println( F("saving Data as CSV in Memory : OK")); - } - - this->previousLogTime=millis(); - } - - coolBoardLed.fadeOut(51,100,50,0.5);//dark shade of green - if (wifiManager.nomad == 0 || this->sleepActive == 0) - { - //case we have wifi but no internet - if( (wifiManager.state() == WL_CONNECTED) && ( mqtt.state()!=0 ) ) - { - - Serial.println(F("there is Wifi but no Internet")); - Serial.println(F("lunching AP to check saved files")); - Serial.println(F("and Add new WiFi if needed")); - - wifiManager.connectAP(); - - } - - //case we have no connection at all - if( wifiManager.state() != WL_CONNECTED ) - { - - #if DEBUG == 1 - - Serial.println(F("there is No Wifi 1")); - Serial.println(F("retrying to connect")); - - #endif - - #if DEBUG == 0 - Serial.println( F("there is no WiFi...")); - #endif - - this->connect();//nomad case : just run wifiMulti - //normal case : run wifiMulti+AP - - } - } - - if( this->sleepActive==1 ) - { - this->sleep( this->getLogInterval() ) ; - } +#if DEBUG == 1 + Serial.println(F("checking if there's an MQTT message ")); + Serial.println(F("answer is : ")); + Serial.println(answer); + Serial.println(); +#endif + coolBoardLed.fadeOut(128, 255, 50, 0.5); // shade of green + + // update configuration if needed + this->update(answer.c_str()); + + coolBoardLed.fadeIn(128, 255, 50, 0.5); // shade of green + + // check if we hit MQTT timeout + if (mqtt.state() != 0) { + Serial.println(F("reconnecting MQTT...")); + mqtt.connect(); + delay(200); + } + + // log interval is in seconds, logInterval*1000 = logInterval in ms. + unsigned long logIntervalMillis = logInterval * 1000; + unsigned long millisSinceLastLog = millis() - this->previousLogTime; + + // publish if we hit logInterval. + // works in sleep mode since previousLogTime is 0 on startup. + if (millisSinceLastLog >= logIntervalMillis || this->previousLogTime == 0) { + if (this->sleepActive == 0) { + coolBoardLed.strobe(255, 0, 230, 0.5); // shade of pink + // logInterval in seconds + if (!mqtt.publish(jsonData.c_str())) { + fileSystem.saveSensorData(data.c_str()); + Serial.println( + F("MQTT publish failed! Saved Data as JSON in Memory : OK")); + } + mqtt.mqttLoop(); + + } else { + coolBoardLed.strobe(230, 255, 0, 0.5); // shade of yellow + + if (!mqtt.publish(jsonData.c_str())) { + fileSystem.saveSensorData(data.c_str()); + Serial.println( + F("MQTT publish failed! Saving Data as JSON in Memory : OK")); + } + + mqtt.mqttLoop(); + answer = mqtt.read(); + this->update(answer.c_str()); + // logInterval in seconds + this->sleep(this->getLogInterval()); + } + this->previousLogTime = millis(); + } + + coolBoardLed.fadeOut(128, 255, 50, 0.5); // shade of green + mqtt.mqttLoop(); + // read mqtt answer + answer = mqtt.read(); + this->update(answer.c_str()); + coolBoardLed.blink(128, 255, 50, 0.5); // shade of green } /** -* CoolBoard::config(): -* This method is provided to configure -* the CoolBoard : -log interval -* -irene3000 activated/deactivated -* -jetpack activated/deactivated -* -external Sensors activated/deactivated -* -mqtt server timeout -* -* \return true if configuration is done, -* false otherwise -*/ -bool CoolBoard::config() -{ - yield(); - - Serial.println(); - Serial.println("[" + WiFi.macAddress() + "]"); + * CoolBoard::offlineMode(): + * This method is provided to manage the offLine + * mode: + * - read sensors + * - do actions + * - save data in the file system + * - if there is WiFi but no Internet : make data available over AP + * - if there is no connection : retry to connect + */ +void CoolBoard::offLineMode() { + coolBoardLed.fade(51, 100, 50, 0.5); // dark shade of green + + Serial.println(F("CoolBoard is in Offline Mode")); + + coolBoardLed.fadeIn(245, 237, 27, 0.5); // shade of yellow + coolBoardLed.blink(245, 237, 27, 0.5); // shade of yellow + + // reading user data + data = this->boardData(); + + // formatting json + data.setCharAt(data.lastIndexOf('}'), ','); + + // read sensors data + Serial.println(F("Collecting sensors data ")); + Serial.println(); + data += this->readSensors(); + + // formatting json correctly + data.remove(data.lastIndexOf('{'), 1); + coolBoardLed.fade(51, 100, 50, 0.5); // dark shade of green + + // get Time for temporal actors + tmElements_t tm; + tm = rtc.getTimeDate(); + + // do action + if (jetpackActive) { + #if DEBUG == 1 + Serial.println(F("jetpack is Active ")); + Serial.println(F("jetpack doing action ")); + Serial.println(); +#endif - Serial.println( F("Entering CoolBoard.config() ") ); - Serial.println(); + coolBoardLed.fade(100, 100, 150, 0.5); // dark shade of blue + data += jetPack.doAction(data.c_str(), int(tm.Hour), + int(tm.Minute)); //{..,..,..}{..,..,..} + data.remove(data.lastIndexOf('{'), 1); //{..,..,..}..,..,..} + data.setCharAt(data.indexOf('}'), ','); //{..,..,..,..,..,..} + } + + delay(50); + + if (onBoardActor.actor.actif == true) { + data += onBoardActor.doAction(data.c_str(), int(tm.Hour), int(tm.Minute)); + data.remove(data.lastIndexOf('{'), 1); + data.setCharAt(data.indexOf('}'), ','); + } + + coolBoardLed.fade(51, 100, 50, 0.5); // dark shade of green + // log interval is passed in seconds, logInterval*1000 = logInterval in ms, if + // no log was sent till now (so it also works when sleep is active since + // previousLogTime is 0 on startup + if ((millis() - (this->previousLogTime)) >= (logInterval * 1000) || + (this->previousLogTime == 0)) { + // saving data in the file system as JSON + if (this->saveAsJSON == 1) { + fileSystem.saveSensorData(data.c_str()); + Serial.println(F("saving Data as JSON in Memory: OK")); + } + + if (this->saveAsCSV == 1) { + fileSystem.saveSensorDataCSV(data.c_str()); + Serial.println(F("saving Data as CSV in Memory: OK")); + } + this->previousLogTime = millis(); + } + + coolBoardLed.fadeOut(51, 100, 50, 0.5); // dark shade of green + if (wifiManager.nomad == 0 || this->sleepActive == 0) { + // case we have wifi but no internet + if ((wifiManager.state() == WL_CONNECTED) && (mqtt.state() != 0)) { + + Serial.println(F("there is Wifi but no Internet")); + Serial.println(F("launching AP to serve sensor dump files")); + Serial.println(F("and reconfigure new WiFi if needed")); + + wifiManager.connectAP(); + } + + // case we have no connection at all + if (wifiManager.state() != WL_CONNECTED) { + Serial.println(F("there is no WiFi...")); +#if DEBUG == 1 + Serial.println(F("retrying to connect")); #endif -#if DEBUG == 0 - Serial.println(); - Serial.println( F("Loading configuration for this CoolBoard...")); -#endif - - //open file system - fileSystem.begin(); - - //start the led - coolBoardLed.config(); - coolBoardLed.begin(); - delay(10); - coolBoardLed.blink(243,171,46,0.5);//shade of orange - - - //open configuration file - File configFile = SPIFFS.open("/coolBoardConfig.json", "r"); - - if (!configFile) - - { - - Serial.println( F("failed to read /coolBoardConfig.json ") ); - - coolBoardLed.blink(255,0,0,0.5);//shade of red - return(false); - } - - else - { - 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); - - DynamicJsonBuffer jsonBuffer; - - JsonObject & json = jsonBuffer.parseObject(buf.get()); - - if (!json.success()) - { - - Serial.println( F("failed to parse CoolBoard Config json object ") ); - - coolBoardLed.blink(255,0,0,0.5);//shade of red - return(false); - } - - else - { - - #if DEBUG == 1 - - Serial.println( F("configuration json : ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size : ")); - Serial.print(jsonBuffer.size()); - Serial.println(); - - #endif - - //parsing logInterval key - if (json["logInterval"].success()) - { - this -> logInterval = json["logInterval"].as(); - } - else - { - this -> logInterval = this -> logInterval; - } - json["logInterval"] = this -> logInterval; - - //parsing ireneActive key - if (json["ireneActive"].success()) - { - this -> ireneActive = json["ireneActive"]; - } - else - { - this -> ireneActive = this -> ireneActive; - } - json["ireneActive"] = this -> ireneActive; - - //parsing jetpackActive key - if (json["jetpackActive"].success()) - { - this -> jetpackActive = json["jetpackActive"]; - } - else - { - this -> jetpackActive = this -> jetpackActive; - } - json["jetpackActive"] = this -> jetpackActive; - - //parsing externalSensorsActive key - if (json["externalSensorsActive"].success()) - { - this -> externalSensorsActive = json["externalSensorsActive"]; - } - else - { - this -> externalSensorsActive = this -> externalSensorsActive; - } - json["externalSensorsActive"] = this -> externalSensorsActive; - - - //parsing sleepActive key - if (json["sleepActive"].success()) - { - this -> sleepActive = json["sleepActive"]; - } - else - { - this -> sleepActive = this -> sleepActive; - } - json["sleepActive"] = this -> sleepActive; - - - //parsing manual key - if (json["manual"].success()) - { - this -> manual = json["manual"].as(); - } - else - { - this -> manual = this -> manual; - } - json["manual"] = this -> manual; - - //parsing saveAsJSON key - if (json["saveAsJSON"].success()) - { - this -> saveAsJSON = json["saveAsJSON"].as(); - } - else - { - this -> saveAsJSON = this -> saveAsJSON; - } - json["saveAsJSON"] = this -> saveAsJSON; - - //parsing saveAsCSV key - if (json["saveAsCSV"].success()) - { - this -> saveAsCSV = json["saveAsCSV"].as(); - } - else - { - this -> saveAsCSV = this -> saveAsCSV; - } - json["saveAsCSV"] = this -> saveAsCSV; - - //saving the current/correct configuration - configFile.close(); - configFile = SPIFFS.open("/coolBoardConfig.json", "w"); - if (!configFile) - { - - Serial.println( F("failed to write to /coolBoardConfig.json") ); - Serial.println(); - - coolBoardLed.blink(255,0,0,0.5);//shade of red - return(false); - } - - json.printTo(configFile); - configFile.close(); - coolBoardLed.blink(0,255,0,0.5);//green blink - - #if DEBUG == 0 - - Serial.println( F("Configuration loaded : OK")); - Serial.println(); - - #endif - - return(true); - } - } - - //coolBoardLed.strobe(243,171,46,0.5);//shade of orange - - coolBoardLed.fadeOut(243,171,46,0.5);//shade of orange + + this->connect(); // nomad case: just run wifiMulti + // normal case: run wifiMulti + AP + } + } + + if (this->sleepActive == 1) { + this->sleep(this->getLogInterval()); + } } /** -* -* CoolBoard::printConf(): -* This method is provided to print -* the configuration to the Serial -* Monitor. -*/ -void CoolBoard::printConf() -{ + * CoolBoard::config(): + * This method is provided to configure + * the CoolBoard : -log interval + * -irene3000 activated/deactivated + * -jetpack activated/deactivated + * -external Sensors activated/deactivated + * -mqtt server timeout + * + * \return true if configuration is done, + * false otherwise + */ +bool CoolBoard::config() { + yield(); + + Serial.println(); + Serial.println("[" + WiFi.macAddress() + "]"); #if DEBUG == 1 - - Serial.println( F("Entering CoolBoard.printConf() ") ); - Serial.println(); + Serial.println(F("Entering CoolBoard.config() ")); + Serial.println(); +#endif +#if DEBUG == 0 + Serial.println(); + Serial.println(F("Loading configuration for this CoolBoard...")); #endif - Serial.println( F("Printing Cool Board Configuration ")); - Serial.print( F("log interval : ")); - Serial.println(this->logInterval); + // open file system + fileSystem.begin(); - Serial.print( F("irene active : ")); - Serial.println(this->ireneActive); + // start the led + coolBoardLed.config(); + coolBoardLed.begin(); + delay(10); + coolBoardLed.blink(243, 171, 46, 0.5); // shade of orange - Serial.print( F("jetpack active : ")); - Serial.println(this->jetpackActive); + // open configuration file + File configFile = SPIFFS.open("/coolBoardConfig.json", "r"); - Serial.print( F("external sensors active : ")); - Serial.println(this->externalSensorsActive); + if (!configFile) { + Serial.println(F("failed to read /coolBoardConfig.json")); + coolBoardLed.blink(255, 0, 0, 0.5); // shade of red + return (false); + } - Serial.print( F("sleep active : ")); - Serial.println(this->sleepActive); + else { + size_t size = configFile.size(); - Serial.print( F("manual active : ")); - Serial.println(this->manual); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + configFile.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); - Serial.print( F("saveAsJSON active : ")); - Serial.println(this->saveAsJSON); + if (!json.success()) { + Serial.println(F("failed to parse CoolBoard Config json object")); + coolBoardLed.blink(255, 0, 0, 0.5); // shade of red + return (false); + } - Serial.print( F("saveAsCSV active : ")); - Serial.println(this->saveAsCSV); + else { - Serial.println(); +#if DEBUG == 1 + Serial.println(F("configuration json : ")); + json.printTo(Serial); + Serial.println(); + Serial.print(F("jsonBuffer size : ")); + Serial.print(jsonBuffer.size()); + Serial.println(); +#endif + // parsing logInterval key + if (json["logInterval"].success()) { + this->logInterval = json["logInterval"].as(); + } else { + this->logInterval = this->logInterval; + } + json["logInterval"] = this->logInterval; + + // parsing ireneActive key + if (json["ireneActive"].success()) { + this->ireneActive = json["ireneActive"]; + } else { + this->ireneActive = this->ireneActive; + } + json["ireneActive"] = this->ireneActive; + + // parsing jetpackActive key + if (json["jetpackActive"].success()) { + this->jetpackActive = json["jetpackActive"]; + } else { + this->jetpackActive = this->jetpackActive; + } + json["jetpackActive"] = this->jetpackActive; + + // parsing externalSensorsActive key + if (json["externalSensorsActive"].success()) { + this->externalSensorsActive = json["externalSensorsActive"]; + } else { + this->externalSensorsActive = this->externalSensorsActive; + } + json["externalSensorsActive"] = this->externalSensorsActive; + + // parsing sleepActive key + if (json["sleepActive"].success()) { + this->sleepActive = json["sleepActive"]; + } else { + this->sleepActive = this->sleepActive; + } + json["sleepActive"] = this->sleepActive; + + // parsing manual key + if (json["manual"].success()) { + this->manual = json["manual"].as(); + } else { + this->manual = this->manual; + } + json["manual"] = this->manual; + + // parsing saveAsJSON key + if (json["saveAsJSON"].success()) { + this->saveAsJSON = json["saveAsJSON"].as(); + } else { + this->saveAsJSON = this->saveAsJSON; + } + json["saveAsJSON"] = this->saveAsJSON; + + // parsing saveAsCSV key + if (json["saveAsCSV"].success()) { + this->saveAsCSV = json["saveAsCSV"].as(); + } else { + this->saveAsCSV = this->saveAsCSV; + } + json["saveAsCSV"] = this->saveAsCSV; + + // saving the current/correct configuration + configFile.close(); + configFile = SPIFFS.open("/coolBoardConfig.json", "w"); + + if (!configFile) { + Serial.println(F("failed to write to /coolBoardConfig.json")); + Serial.println(); + coolBoardLed.blink(255, 0, 0, 0.5); // shade of red + return (false); + } + + json.printTo(configFile); + configFile.close(); + coolBoardLed.blink(0, 255, 0, 0.5); // green blink +#if DEBUG == 0 + Serial.println(F("Configuration loaded : OK")); + Serial.println(); +#endif + return (true); + } + } + coolBoardLed.fadeOut(243, 171, 46, 0.5); // shade of orange } /** -* CoolBoard::update(mqtt answer): -* This method is provided to handle the -* configuration update of the different parts -*/ -void CoolBoard::update(const char * answer) -{ - coolBoardLed.fadeIn(153,76,0,0.5);//shade of brown + * CoolBoard::printConf(): + * This method is provided to print + * the configuration to the Serial + * Monitor. + */ +void CoolBoard::printConf() { #if DEBUG == 1 + Serial.println(F("Entering CoolBoard.printConf() ")); + Serial.println(); +#endif - Serial.println( F("Entering CoolBoard.update() ") ); - Serial.println(); - Serial.println( F("message is : ") ); - Serial.println(answer); - Serial.println(); + Serial.println(F("Printing Cool Board Configuration")); + Serial.print(F("log interval :")); + Serial.println(this->logInterval); + Serial.print(F("irene active :")); + Serial.println(this->ireneActive); + Serial.print(F("jetpack active :")); + Serial.println(this->jetpackActive); + Serial.print(F("external sensors active :")); + Serial.println(this->externalSensorsActive); + Serial.print(F("sleep active :")); + Serial.println(this->sleepActive); + Serial.print(F("manual active :")); + Serial.println(this->manual); + Serial.print(F("saveAsJSON active :")); + Serial.println(this->saveAsJSON); + Serial.print(F("saveAsCSV active :")); + Serial.println(this->saveAsCSV); + Serial.println(); +} +/** + * CoolBoard::update(mqtt answer): + * This method is provided to handle the + * configuration update of the different parts + */ +void CoolBoard::update(const char *answer) { + coolBoardLed.fadeIn(153, 76, 0, 0.5); // shade of brown + +#if DEBUG == 1 + Serial.println(F("Entering CoolBoard.update() ")); + Serial.println(); + Serial.println(F("message is : ")); + Serial.println(answer); + Serial.println(); #endif - DynamicJsonBuffer jsonBuffer; - JsonObject & root = jsonBuffer.parseObject(answer); - JsonObject & stateDesired = root["state"]; + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.parseObject(answer); + JsonObject &stateDesired = root["state"]; #if DEBUG == 1 + Serial.println(F("root json : ")); + root.printTo(Serial); + Serial.println(); + Serial.println(F("stateDesired json : ")); + stateDesired.printTo(Serial); + Serial.println(); + Serial.print(F("jsonBuffer size : ")); + Serial.println(jsonBuffer.size()); +#endif - Serial.println( F("root json : ") ); - root.printTo(Serial); - Serial.println(); - - Serial.println( F("stateDesired json : ")); - stateDesired.printTo(Serial); - Serial.println(); - - Serial.print( F("jsonBuffer size : ")); - Serial.println(jsonBuffer.size()); + if (stateDesired["CoolBoard"]["manual"].success()) { + this->manual = stateDesired["CoolBoard"]["manual"].as(); +#if DEBUG == 1 + Serial.println("Manual Flag received"); + Serial.println(this->manual); #endif - - if (stateDesired["CoolBoard"]["manual"].success()) - { - this -> manual = stateDesired["CoolBoard"]["manual"].as(); - - #if DEBUG == 1 - - Serial.println("Manual Flag received"); - Serial.println(this -> manual); - - #endif - - } - - if (stateDesired.success()) - { - - #if DEBUG == 1 - - Serial.println( F("update message parsing : success") ); - Serial.println(); - - #endif - - String answerDesired; - - stateDesired.printTo(answerDesired); - - #if DEBUG == 1 - - Serial.println( F("update is ok ") ); - Serial.println( F("desired update is : ") ); - Serial.println(answerDesired); - Serial.println("json size is : "); - Serial.println(jsonBuffer.size() ) ; - Serial.println(); - Serial.print("ManualFlag : "); - Serial.println(this->manual); - - #endif - //manual mode check - if(this->manual == 1 ) - { - Serial.println(); - Serial.print("Enter Manual Actors..."); - //json parse for actors - for(auto kv : stateDesired) - { - #if DEBUG == 1 - - Serial.print(F("writing to ")); - Serial.println(kv.key); - Serial.print(F("state : ")); - Serial.println(kv.value.as()); - - #endif - - if( strcmp(kv.key,"Act0") == 0 ) - { - - jetPack.writeBit(0,kv.value.as() ); - - } - else if(strcmp(kv.key,"Act1") == 0) - { - jetPack.writeBit(1,kv.value.as() ); - - } - else if(strcmp(kv.key,"Act2") == 0) - { - jetPack.writeBit(2,kv.value.as() ); - - } - else if(strcmp(kv.key,"Act3") == 0) - { - jetPack.writeBit(3,kv.value.as() ); - - } - else if(strcmp(kv.key,"Act4") == 0) - { - jetPack.writeBit(4,kv.value.as() ); - - } - else if(strcmp(kv.key,"Act5") == 0) - { - jetPack.writeBit(5,kv.value.as() ); - - } - else if(strcmp(kv.key,"Act6") == 0) - { - jetPack.writeBit(6,kv.value.as() ); - - } - else if (strcmp(kv.key,"Act7") == 0) - { - jetPack.writeBit(7,kv.value.as() ); - - } - else if (strcmp(kv.key,"ActB") == 0) - { - onBoardActor.write(kv.value.as() ); - - } - - - } - - - } - - //Irene calibration through update message - if(stateDesired["calibration"].success()) - { - - Serial.println(F("Starting Irene Calibration From MQTT Update")); - Serial.println(); - - delay(2000); - - Serial.println(F("ph7 calibration for 25 seconds")); - - delay(10000); - - irene3000.calibratepH7(); - - delay(15000); - - irene3000.calibratepH7(); - - delay(1000); - - Serial.println(F("ph 7 calibration ok")); - - Serial.println(); - - - Serial.println(F("ph 4 calibration for 25 seconds")); - - delay(10000); - - irene3000.calibratepH4(); - - delay(15000); - - irene3000.calibratepH4(); - - delay(1000); - - Serial.println(F("ph 4 calibration ok")); - - Serial.println(); - - irene3000.saveParams(); - - - } - - //saving the new configuration - fileSystem.updateConfigFiles(answerDesired); - - //answering the update msg: - //reported = received configuration - //desired=null - - String updateAnswer; - String tempString; - - stateDesired.printTo(tempString); - updateAnswer="{\"state\":{\"reported\":"; - updateAnswer+=tempString; - updateAnswer+=",\"desired\":null}}"; - - #if DEBUG == 1 - - Serial.println( F("preparing answer message ") ); - Serial.println(); - Serial.println( F("updateAnswer : ") ); - Serial.println(updateAnswer); - - #endif - - mqtt.publish(updateAnswer.c_str()); - - mqtt.mqttLoop(); - - delay(10); - - if(manual == 0 ) - { - //restart the esp to apply the config - ESP.restart(); - } - } - else - { - - #if DEBUG == 1 - - Serial.println( F("Failed to parse update message( OR no message received )") ); - Serial.println(); - - #endif - - } - - coolBoardLed.strobe(153,76,0,0.5);//shade of brown - coolBoardLed.fadeOut(153,76,0,0.5);//shade of brown -} + } -/** -* CoolBoard::getLogInterval(): -* This method is provided to get -* the log interval -* -* \return interval value in s -*/ -unsigned long CoolBoard::getLogInterval() -{ + if (stateDesired.success()) { #if DEBUG == 1 + Serial.println(F("update message parsing : success")); + Serial.println(); +#endif + + String answerDesired; - Serial.println( F("Entering CoolBoard.getLogInterval() ") ); - Serial.println(); - Serial.println( F("log Interval is :") ); - Serial.println(logInterval); - Serial.println(); + stateDesired.printTo(answerDesired); +#if DEBUG == 1 + Serial.println(F("update is ok ")); + Serial.println(F("desired update is : ")); + Serial.println(answerDesired); + Serial.println("json size is : "); + Serial.println(jsonBuffer.size()); + Serial.println(); + Serial.print("ManualFlag : "); + Serial.println(this->manual); #endif - return(this -> logInterval); -} + // manual mode check + if (this->manual == 1) { + Serial.println(); + Serial.print("Enter Manual Actors..."); + // json parse for actors + for (auto kv : stateDesired) { -/** -* CoolBoard::readSensors(): -* This method is provided to read and -* format all the sensors data in a -* single json. -* -* \return json string of all the sensors read. -*/ -String CoolBoard::readSensors() -{ - - coolBoardLed.fadeIn(128,255,0,0.5);//light shade of green - #if DEBUG == 1 + Serial.print(F("writing to ")); + Serial.println(kv.key); + Serial.print(F("state : ")); + Serial.println(kv.value.as()); +#endif - Serial.println( F("Entering CoolBoard.readSensors()") ); - Serial.println(); + if (strcmp(kv.key, "Act0") == 0) { + jetPack.writeBit(0, kv.value.as()); + } else if (strcmp(kv.key, "Act1") == 0) { + jetPack.writeBit(1, kv.value.as()); + } else if (strcmp(kv.key, "Act2") == 0) { + jetPack.writeBit(2, kv.value.as()); + } else if (strcmp(kv.key, "Act3") == 0) { + jetPack.writeBit(3, kv.value.as()); + } else if (strcmp(kv.key, "Act4") == 0) { + jetPack.writeBit(4, kv.value.as()); + } else if (strcmp(kv.key, "Act5") == 0) { + jetPack.writeBit(5, kv.value.as()); + } else if (strcmp(kv.key, "Act6") == 0) { + jetPack.writeBit(6, kv.value.as()); + } else if (strcmp(kv.key, "Act7") == 0) { + jetPack.writeBit(7, kv.value.as()); + } else if (strcmp(kv.key, "ActB") == 0) { + onBoardActor.write(kv.value.as()); + } + } + } + + // Irene calibration through update message + if (stateDesired["calibration"].success()) { + Serial.println(F("Starting Irene Calibration From MQTT Update")); + Serial.println(); + delay(2000); + Serial.println(F("ph7 calibration for 25 seconds")); + delay(10000); + irene3000.calibratepH7(); + delay(15000); + irene3000.calibratepH7(); + delay(1000); + Serial.println(F("ph 7 calibration OK")); + Serial.println(); + Serial.println(F("ph 4 calibration for 25 seconds")); + delay(10000); + irene3000.calibratepH4(); + delay(15000); + irene3000.calibratepH4(); + delay(1000); + Serial.println(F("ph 4 calibration OK")); + Serial.println(); + irene3000.saveParams(); + } + + // saving the new configuration + fileSystem.updateConfigFiles(answerDesired); + + // answer the update msg + // reported = received configuration + // desired=null + String updateAnswer; + String tempString; + + stateDesired.printTo(tempString); + updateAnswer = "{\"state\":{\"reported\":"; + updateAnswer += tempString; + updateAnswer += ",\"desired\":null}}"; -#endif - coolBoardLed.strobe(128,255,0,0.5);//light shade of green - - String sensorsData; - - this->initReadI2C(); - - sensorsData = coolBoardSensors.read(); // {..,..,..} - - if (externalSensorsActive) - { - sensorsData += externalSensors.read(); // {..,..,..}{..,..} - sensorsData.setCharAt(sensorsData.lastIndexOf('}'), ','); // {..,..,..}{..,.., - sensorsData.setCharAt(sensorsData.lastIndexOf('{'), ','); // {..,..,..},..,.., - sensorsData.remove(sensorsData.lastIndexOf('}'), 1); // {..,..,..,..,.., - sensorsData.setCharAt(sensorsData.lastIndexOf(','), '}'); // {..,..,..,..,..} - - } - if (ireneActive) - { - sensorsData += irene3000.read(); // {..,..,..,..,..}{..,..,..} - - sensorsData.setCharAt(sensorsData.lastIndexOf('}'), ','); // {..,..,..,..,..}{..,..,.., - sensorsData.setCharAt(sensorsData.lastIndexOf('{'), ','); // {..,..,..,..,..},..,..,.., - sensorsData.remove(sensorsData.lastIndexOf('}'), 1); // {..,..,..,..,..,..,..,.., - sensorsData.setCharAt(sensorsData.lastIndexOf(','), '}'); // {..,..,..,..,..,..,..,..} - - - } - - //getting Hour: - /*tmElements_t tm; - tm=rtc.getTimeDate(); - - //adding Hour - sensorsData.remove(sensorsData.lastIndexOf('}'), 1); // {..,..,..,..,..,..,..,.., - sensorsData+=",\"hour\":"; - sensorsData+=tm.Hour; - sensorsData+=",\"minute\":"; - sensorsData+=tm.Minute; - sensorsData+="}";*/ - #if DEBUG == 1 - Serial.println(); - Serial.println( F("sensors data is ") ); - Serial.println(sensorsData); - Serial.println(); - + Serial.println(F("preparing answer message ")); + Serial.println(); + Serial.println(F("updateAnswer : ")); + Serial.println(updateAnswer); #endif - coolBoardLed.fadeOut(128,255,0,0.5);//light shade of green - return(sensorsData); + mqtt.publish(updateAnswer.c_str()); + mqtt.mqttLoop(); + delay(10); + + if (manual == 0) { + // restart La COOL Board to apply the new configuration + ESP.restart(); + } + } else { +#if DEBUG == 1 + Serial.println( + F("Failed to parse update message( OR no message received )")); + Serial.println(); +#endif + } + + coolBoardLed.strobe(153, 76, 0, 0.5); // shade of brown + coolBoardLed.fadeOut(153, 76, 0, 0.5); // shade of brown } /** -* CoolBoard::initReadI2C(): -* This method is provided to enable the I2C -* Interface. -*/ -void CoolBoard::initReadI2C() -{ + * CoolBoard::getLogInterval(): + * This method is provided to get + * the log interval + * + * \return interval value in s + */ +unsigned long CoolBoard::getLogInterval() { #if DEBUG == 1 - - Serial.println( F("Entering CoolBoard.initReadI2C()") ); - Serial.println(); - + Serial.println(F("Entering CoolBoard.getLogInterval() ")); + Serial.println(); + Serial.println(F("log Interval is :")); + Serial.println(logInterval); + Serial.println(); #endif - - digitalWrite(EnI2C,HIGH);//HIGH= I2C Enable + return (this->logInterval); } - /** -* CoolBoard::boardData(): -* This method is provided to -* return the board's data. -* -* \return json string of the data's data -*/ -String CoolBoard::boardData() -{ + * CoolBoard::readSensors(): + * This method is provided to read and + * format all the sensors data in a + * single json. + * + * \return json string of all the sensors read. + */ +String CoolBoard::readSensors() { + String sensorsData; + + coolBoardLed.fadeIn(128, 255, 0, 0.5); // light shade of green + coolBoardLed.strobe(128, 255, 0, 0.5); // light shade of green #if DEBUG == 1 + Serial.println(F("Entering CoolBoard.readSensors()")); + Serial.println(); +#endif - Serial.println( F("Entering CoolBoard.boardData() ") ); - Serial.println(); + this->initReadI2C(); + sensorsData = coolBoardSensors.read(); + + if (externalSensorsActive) { + sensorsData += externalSensors.read(); + sensorsData.setCharAt(sensorsData.lastIndexOf('}'), ','); + sensorsData.setCharAt(sensorsData.lastIndexOf('{'), ','); + sensorsData.remove(sensorsData.lastIndexOf('}'), 1); + sensorsData.setCharAt(sensorsData.lastIndexOf(','), '}'); + } + if (ireneActive) { + sensorsData += irene3000.read(); + sensorsData.setCharAt(sensorsData.lastIndexOf('}'), ','); + sensorsData.setCharAt(sensorsData.lastIndexOf('{'), ','); + sensorsData.remove(sensorsData.lastIndexOf('}'), 1); + sensorsData.setCharAt(sensorsData.lastIndexOf(','), '}'); + } +#if DEBUG == 1 + Serial.println(); + Serial.println(F("sensors data is ")); + Serial.println(sensorsData); + Serial.println(); #endif - String tempMAC = WiFi.macAddress(); + coolBoardLed.fadeOut(128, 255, 0, 0.5); // light shade of green + return (sensorsData); +} - tempMAC.replace(":", ""); +/** + * CoolBoard::initReadI2C(): + * This method is provided to enable the I2C + * Interface. + */ +void CoolBoard::initReadI2C() { - String boardJson = "{\"timestamp\":\""; +#if DEBUG == 1 + Serial.println(F("Entering CoolBoard.initReadI2C()")); + Serial.println(); +#endif - boardJson += rtc.getESDate(); // "timestamp":"20yy-mm-ddThh:mm:ssZ" + digitalWrite(EnI2C, HIGH); // HIGH = I2C enabled +} - boardJson += "\",\"mac\":\""; +/** + * CoolBoard::boardData(): + * This method is provided to + * return the board's data. + * + * \return json string of the data's data + */ +String CoolBoard::boardData() { - boardJson += tempMAC; +#if DEBUG == 1 + Serial.println(F("Entering CoolBoard.boardData() ")); + Serial.println(); +#endif + String tempMAC = WiFi.macAddress(); + tempMAC.replace(":", ""); + String boardJson = "{\"timestamp\":\""; + boardJson += rtc.getESDate(); // "timestamp":"20yy-mm-ddThh:mm:ssZ" + boardJson += "\",\"mac\":\""; + boardJson += tempMAC; boardJson += "\",\"wifiSignal\":"; - boardJson += WiFi.RSSI(); - boardJson += "}"; #if DEBUG == 1 + Serial.println(F("boardData is : ")); + Serial.println(boardJson); + Serial.println(); +#endif - Serial.println( F("boardData is : ") ); - Serial.println(boardJson); - Serial.println(); - -#endif - - return(boardJson); - + return (boardJson); } +/** + * CoolBoard::sleep(int interval): + * This method is provided to allow the + * board to enter deepSleep mode for + * a period of time equal to interval in s + */ +void CoolBoard::sleep(unsigned long interval) { + + Serial.println(F("Entering CoolBoard.sleep()")); + Serial.print(F("going to sleep for ")); + Serial.print(interval); + Serial.println(F("s")); + Serial.println(); + + // interval is in seconds, interval*1000*1000 in µS + ESP.deepSleep((interval * 1000 * 1000), WAKE_RF_DEFAULT); +} /** -* CoolBoard::sleep(int interval): -* This method is provided to allow the -* board to enter deepSleep mode for -* a period of time equal to interval in s -*/ -void CoolBoard::sleep(unsigned long interval) -{ - - Serial.println( F("Entering CoolBoard.sleep() ") ); - Serial.print( F("going to sleep for ") ); - Serial.print(interval); - Serial.println(F("s") ); - Serial.println(); - - //interval is in seconds , interval*1000*1000 in µS - ESP.deepSleep ( ( interval * 1000 * 1000 ), WAKE_RF_DEFAULT) ; + * CoolBoard::sendConfig( module Name,file path ): + * This method is provided to send + * all the configuration files + * over MQTT + * + * \return true if successful, false if not + */ +bool CoolBoard::sendConfig(const char *moduleName, const char *filePath) { -} +#if DEBUG == 1 + Serial.println(F("Entering CoolBoard.sendConfig()")); +#endif + String result; -/** -* CoolBoard::sendConfig( module Name,file path ): -* This method is provided to send -* all the configuration files -* over MQTT -* -* \return true if successful, false if not -*/ -bool CoolBoard::sendConfig(const char* moduleName, const char* filePath) -{ + // open file + File configFile = SPIFFS.open(filePath, "r"); + + if (!configFile) { + Serial.print(F("failed to read ")); + Serial.println(filePath); + return (false); + } else { + size_t size = configFile.size(); + + // allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + configFile.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); -#if DEBUG == 1 + if (!json.success()) { + Serial.println(F("failed to parse json object")); + return (false); + } - Serial.println(F("Entering CoolBoard.sendConfig()")); + else { +#if DEBUG == 1 + Serial.println(F("configuration json : ")); + json.printTo(Serial); + Serial.println(); + Serial.print(F("jsonBuffer size : ")); + Serial.print(jsonBuffer.size()); + Serial.println(); #endif - String result; - - //open file - File configFile = SPIFFS.open(filePath, "r"); - - if (!configFile) - - { - - Serial.print( F("failed to read ") ); - - Serial.println(filePath); - - return(false); - } - else - { - - //file to json - 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); - - DynamicJsonBuffer jsonBuffer; - - JsonObject & json = jsonBuffer.parseObject(buf.get()); - - if (!json.success()) - { - - Serial.println( F("failed to parse json object ") ); - - return(false); - } - - else - { - - #if DEBUG == 1 - - Serial.println( F("configuration json : ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size : ")); - Serial.print(jsonBuffer.size()); - Serial.println(); - - #endif - - //json to string - String temporary; - - json.printTo(temporary); - - //format string - result = "{\"state\":{\"reported\":{\""; - result += moduleName; //{"state":{"reported":{"moduleName - result += "\":";//{"state":{"reported":{"moduleName": - result += temporary;//{"state":{"reported":{"moduleName":{..,..,..,..,..,..,..,..} - result += "} } }"; // {"state":{"reported":{..,..,..,..,..,..,..,..} } } } - - //string over mqtt - mqtt.publish(result.c_str()); - - mqtt.mqttLoop(); - - return(true); - - } - - } -} + String temporary; + // JSON to string + json.printTo(temporary); + // format string + result = "{\"state\":{\"reported\":{\""; + result += moduleName; + result += "\":"; + result += temporary; + result += "} } }"; + + // send over MQTT + mqtt.publish(result.c_str()); + mqtt.mqttLoop(); + return (true); + } + } +} diff --git a/src/CoolBoard.h b/src/CoolBoard.h index b8a9dcb4..3382b9a3 100644 --- a/src/CoolBoard.h +++ b/src/CoolBoard.h @@ -1,229 +1,223 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef CoolBoard_H #define CoolBoard_H +#include "CoolBoardActor.h" //CoolBoard Actor Manager +#include "CoolBoardLed.h" //CoolBoard Led Manager +#include "CoolBoardSensors.h" //CoolBoard Sensor Board Manager +#include "CoolFileSystem.h" //CoolBoard File System Manager +#include "CoolMQTT.h" //CoolBoard MQTT Manager +#include "CoolTime.h" //CoolBoard Real Time Clock Manager +#include "CoolWifi.h" //CoolBoard Wifi Manager +#include "ExternalSensors.h" //CoolBoard External Sensors Manager +#include "Irene3000.h" //CoolBoard Irene3000 Manager +#include "Jetpack.h" //CoolBoard Jetpack Manager -#include"CoolFileSystem.h" //CoolBoard File System Manager -#include"CoolBoardSensors.h" //CoolBoard Sensor Board Manager -#include"CoolBoardLed.h" //CoolBoard Led Manager -#include"CoolTime.h" //CoolBoard Real Time Clock Manager -#include"CoolMQTT.h" //CoolBoard MQTT Manager -#include"Jetpack.h" //CoolBoard Jetpack Manager -#include"Irene3000.h" //CoolBoard Irene3000 Manager -#include"ExternalSensors.h" //CoolBoard External Sensors Manager -#include"CoolWifi.h" //CoolBoard Wifi Manager -#include"CoolBoardActor.h" //CoolBoard Actor Manager - -#include"Arduino.h" //Arduino Defs +#include "Arduino.h" //Arduino Defs /** -* \class CoolBoard -* -* \brief This class manages the CoolBoard and all of -* Its functions -*/ -class CoolBoard -{ + * \class CoolBoard + * + * \brief This class manages the CoolBoard and all of + * Its functions + */ +class CoolBoard { public: - //Constructor - CoolBoard(); + // Constructor + CoolBoard(); - void begin(); - - bool config(); + void begin(); - void update(const char*answer ); + bool config(); - void offLineMode(); + void update(const char *answer); - void onLineMode(); + void offLineMode(); - int connect(); - - int isConnected(); + void onLineMode(); - unsigned long getLogInterval(); + int connect(); - void printConf(); + int isConnected(); - void sleep(unsigned long interval); + unsigned long getLogInterval(); - String readSensors(); - - void initReadI2C(); - - String boardData(); + void printConf(); - bool sendConfig(const char* moduleName, const char* filePath); + void sleep(unsigned long interval); + String readSensors(); -private: + void initReadI2C(); - /** - * fileSystem handler instance - */ - CoolFileSystem fileSystem; - - /** - * Sensor Board handler instance - */ - CoolBoardSensors coolBoardSensors; - - /** - * Led handler instance - */ - CoolBoardLed coolBoardLed; - - /** - * RTC handler instance - */ - CoolTime rtc; - - /** - * Wifi handler instance - */ - CoolWifi wifiManager; - - /** - * MQTT handler instance - */ - CoolMQTT mqtt; - - /** - * Jetpack handler instance - */ - Jetpack jetPack; - - /** - * Irene3000 handler instance - */ - Irene3000 irene3000; - - /** - * External Sensors handler instance - */ - ExternalSensors externalSensors; - - /** - * On Board Actor handler instance - */ - CoolBoardActor onBoardActor; - - /** - * userActive flag, - * set to 1 to collect userData(MAC,userName,TimeStamp) - */ - bool userActive=0; - - /** - * ireneActive flag, - * set to 1 when using an Irene module - */ - bool ireneActive=0; - - /** - * jetpackActive flag, - * set to 1 when using a Jetpack module - */ - bool jetpackActive=0; - - /** - * externalSensors flag, - * set to 1 when using 1/many external Sensor(s) - */ - bool externalSensorsActive=0; - - /** - * sleepActive flag, - * set to 1 when using sleep Mode - * in Sleep mode : the CoolBoard will do - * a cycle (init, read sensors, do action,log) - * and go to sleep for a LogInterval period of time - */ - bool sleepActive=0; - - /** - * manual flag, - * set to 1 when using manual mode - * in manual Mode , user can activate/deactivate - * actors through a specific MQTT command - * - * /!\ in manual Mode, receving an update will not reset the CoolBoard - * /!\ resetting the CoolBoard in manual mode will deactivate all actors - */ - bool manual=0; - - /** - * saveAsJSON Flag, - * save data as JSON String when in offLineMode - * ATTENTION THIS CONSUMES MUCH MORE MEMORY - * THEN SAVING DATA AS CSV - */ - bool saveAsJSON=1; - - /** - * saveAsCSV Flag, - * save data in a CSV file when in offLineMode - * BEST FOR OFF GRID SOLUTIONS!!! - */ - bool saveAsCSV=1; - - /** - * log Interval value, - * the period of time between logs - - in Seconds - */ - unsigned long logInterval=1; - - /** - * last time the Client sent a Message over MQTT - * in ms - */ - unsigned long previousLogTime=0; - - /** - * data string, - * string that contains sensors data - */ - String data=""; - - /** - * answer string, - * string that contains received MQTT messages - */ - String answer=""; - - /** - * Enable I2C pin, - * double usage for I2C and shift register latch , HIGH=I2C , LOW=shift register latch - * All I2C is over pins (2,14) - */ - const int EnI2C = 5; + String boardData(); + bool sendConfig(const char *moduleName, const char *filePath); +private: + /** + * fileSystem handler instance + */ + CoolFileSystem fileSystem; + + /** + * Sensor Board handler instance + */ + CoolBoardSensors coolBoardSensors; + + /** + * Led handler instance + */ + CoolBoardLed coolBoardLed; + + /** + * RTC handler instance + */ + CoolTime rtc; + + /** + * Wifi handler instance + */ + CoolWifi wifiManager; + + /** + * MQTT handler instance + */ + CoolMQTT mqtt; + + /** + * Jetpack handler instance + */ + Jetpack jetPack; + + /** + * Irene3000 handler instance + */ + Irene3000 irene3000; + + /** + * External Sensors handler instance + */ + ExternalSensors externalSensors; + + /** + * On Board Actor handler instance + */ + CoolBoardActor onBoardActor; + + /** + * userActive flag, + * set to 1 to collect userData(MAC,userName,TimeStamp) + */ + bool userActive = 0; + + /** + * ireneActive flag, + * set to 1 when using an Irene module + */ + bool ireneActive = 0; + + /** + * jetpackActive flag, + * set to 1 when using a Jetpack module + */ + bool jetpackActive = 0; + + /** + * externalSensors flag, + * set to 1 when using 1/many external Sensor(s) + */ + bool externalSensorsActive = 0; + + /** + * sleepActive flag, + * set to 1 when using sleep Mode + * in Sleep mode : the CoolBoard will do + * a cycle (init, read sensors, do action,log) + * and go to sleep for a LogInterval period of time + */ + bool sleepActive = 0; + + /** + * manual flag, + * set to 1 when using manual mode + * in manual Mode , user can activate/deactivate + * actors through a specific MQTT command + * + * /!\ in manual Mode, receving an update will not reset the CoolBoard + * /!\ resetting the CoolBoard in manual mode will deactivate all actors + */ + bool manual = 0; + + /** + * saveAsJSON Flag, + * save data as JSON String when in offLineMode + * ATTENTION THIS CONSUMES MUCH MORE MEMORY + * THEN SAVING DATA AS CSV + */ + bool saveAsJSON = 1; + + /** + * saveAsCSV Flag, + * save data in a CSV file when in offLineMode + * BEST FOR OFF GRID SOLUTIONS!!! + */ + bool saveAsCSV = 1; + + /** + * log Interval value, + * the period of time between logs + - in Seconds + */ + unsigned long logInterval = 1; + + /** + * last time the Client sent a Message over MQTT + * in ms + */ + unsigned long previousLogTime = 0; + + /** + * data string, + * string that contains sensors data + */ + String data = ""; + + /** + * answer string, + * string that contains received MQTT messages + */ + String answer = ""; + + /** + * Enable I2C pin, + * double usage for I2C and shift register latch , HIGH=I2C , LOW=shift + *register latch All I2C is over pins (2,14) + */ + const int EnI2C = 5; }; #endif diff --git a/src/CoolBoardActor.cpp b/src/CoolBoardActor.cpp index be598706..d3cfea8c 100644 --- a/src/CoolBoardActor.cpp +++ b/src/CoolBoardActor.cpp @@ -1,1670 +1,1441 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ -#include "FS.h" #include "Arduino.h" #include "ArduinoJson.h" +#include "FS.h" #include "CoolBoardActor.h" - #define DEBUG 0 - /** -* CoolBoardActor::begin(): -* This method is provided to -* initialise the CoolBoardActor pin -*/ -void CoolBoardActor::begin() -{ + * CoolBoardActor::begin(): + * This method is provided to + * initialise the CoolBoardActor pin + */ +void CoolBoardActor::begin() { -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoardActor.begin() ") ); - Serial.println(); +#if DEBUG == 1 -#endif + Serial.println(F("Entering CoolBoardActor.begin() ")); + Serial.println(); - pinMode(this->pin,OUTPUT); - - +#endif + pinMode(this->pin, OUTPUT); } - /** -* CoolBoardActor::write(action): -* This method is provided to write -* the given action to the CoolBoardActor. -* -*/ -void CoolBoardActor::write(bool action) -{ + * CoolBoardActor::write(action): + * This method is provided to write + * the given action to the CoolBoardActor. + * + */ +void CoolBoardActor::write(bool action) { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardActor.write()") ); - Serial.println(); + Serial.println(F("Entering CoolBoardActor.write()")); + Serial.println(); - Serial.println( F("writing this action : ") ); - Serial.println(action,BIN); - Serial.println(); + Serial.println(F("writing this action : ")); + Serial.println(action, BIN); + Serial.println(); -#endif - - digitalWrite(this->pin,action); - +#endif + digitalWrite(this->pin, action); } - + /** -* CoolBoardActor::doAction(sensor data ): -* This method is provided to automate the CoolBoardActor. -* -* The result action is the result of -* checking the different flags of the actor -* (actif , temporal ,inverted, primaryType -* and secondaryType ) and the corresponding -* call to the appropriate helping method -* -* \return a string of the actor's state -* -*/ -String CoolBoardActor::doAction( const char* data, int hour, int minute ) -{ + * CoolBoardActor::doAction(sensor data ): + * This method is provided to automate the CoolBoardActor. + * + * The result action is the result of + * checking the different flags of the actor + * (actif , temporal ,inverted, primaryType + * and secondaryType ) and the corresponding + * call to the appropriate helping method + * + * \return a string of the actor's state + * + */ +String CoolBoardActor::doAction(const char *data, int hour, int minute) { -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoardActor.doAction()") ); - Serial.println(); - - Serial.println( F("input data is :") ); - Serial.println(data); - Serial.println(); - - Serial.print( F("Hour from Coolboard.cpp : ")); - Serial.println(hour); - Serial.print( F("Minute from Coolboard.cpp : ")); - Serial.println(minute); - -#endif - - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.parseObject(data); - - String output; - DynamicJsonBuffer jsonBufferOutput; - JsonObject& rootOutput = jsonBuffer.createObject(); - - if (!root.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json object ") ); - Serial.println(); - - #endif - - } - else if(!rootOutput.success()) - { - - #if DEBUG == 1 - - Serial.println(F("failed to create output json")); - Serial.println(); - - #endif - - } - else - { - - #if DEBUG == 1 - - Serial.println( F("created Json object :") ); - root.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - - //invert the current action state for the actor - //if the value is outside the limits - - //check if actor is actif - if(this->actor.actif==1) - { - //normal actor - if(this->actor.temporal == 0) - { - //not inverted actor - if(this->actor.inverted==0) - { - this->normalAction(root[this->actor.primaryType].as()); - } - //inverted actor - else if(this->actor.inverted==1) - { - this->invertedAction(root[this->actor.primaryType].as()); - } - } - //temporal actor - else if(this->actor.temporal == 1 ) - { - //hour actor - if(this->actor.secondaryType=="hour") - { - //mixed hour actor - if(root[this->actor.primaryType].success() ) - { - this->mixedHourAction(hour,root[this->actor.primaryType].as()); - } - //normal hour actor - else - { - this->hourAction(hour);//root[this->actor.secondaryType].as()); - } - - } - //minute actor - else if(this->actor.secondaryType=="minute") - { - //mixed minute actor - if(root[this->actor.primaryType].success() ) - { - this->mixedMinuteAction(minute,root[this->actor.primaryType].as()); - } - //normal minute actor - else - { - this->minuteAction(minute); - } - } - //hourMinute actor - else if(this->actor.secondaryType=="hourMinute") - { - //mixed hourMinute actor - if(root[this->actor.primaryType].success() ) - { - this->mixedHourMinuteAction(hour,minute,root[this->actor.primaryType].as()); - } - //normal hourMinute actor - else - { - this->hourMinuteAction(hour, minute); - } - } - //normal temporal actor - else if(this->actor.secondaryType=="") - { - //mixed temporal actor - if(root[this->actor.primaryType].success() ) - { - this->mixedTemporalActionOn(root[this->actor.primaryType].as()); - } - //normal temporal actor - else - { - this->temporalActionOn(); - } - - } - - } - } - //inactif actor - else if(this->actor.actif == 0 ) - { - //temporal actor - if(this->actor.temporal==1) - { - //mixed temporal actor - if(root[this->actor.primaryType].success() ) - { - this->mixedTemporalActionOff(root[this->actor.primaryType].as()); - } - //normal temporal actor - else - { - this->temporalActionOff(); - } - } - } - - } - - rootOutput["ActB"]=digitalRead(this->pin); - - rootOutput.printTo(output); - - return(output); -} +#if DEBUG == 1 -/** -* CoolBoardActor::config(): -* This method is provided to configure the -* CoolBoardActor with a configuration file -* -* \return true if successful,false otherwise -*/ -bool CoolBoardActor::config() -{ + Serial.println(F("Entering CoolBoardActor.doAction()")); + Serial.println(); -#if DEBUG == 1 + Serial.println(F("input data is :")); + Serial.println(data); + Serial.println(); - Serial.println( F("Entering CoolBoardActor.config() ") ); - Serial.println(); + Serial.print(F("Hour from Coolboard.cpp : ")); + Serial.println(hour); + Serial.print(F("Minute from Coolboard.cpp : ")); + Serial.println(minute); #endif - File coolBoardActorConfig = SPIFFS.open("/coolBoardActorConfig.json", "r"); - - if (!coolBoardActorConfig) - { - - #if DEBUG == 1 - - Serial.println( F("failed to read /coolBoardActorConfig.json ") ); - Serial.println(); - - #endif - - return(false); - } - else - { - size_t size = coolBoardActorConfig.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - coolBoardActorConfig.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse coolBoardActor Config json from file ") ); - Serial.println(); - - #endif - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("read configuration file : ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - - //parsing actif key - if(json["actif"].success() ) - { - this->actor.actif=json["actif"]; - } - else - { - this->actor.actif=this->actor.actif; - } - json["actif"]=this->actor.actif; - - //parsing temporal key - if(json["temporal"].success() ) - { - this->actor.temporal=json["temporal"]; - } - else - { - this->actor.temporal=this->actor.temporal; - } - json["temporal"]=this->actor.temporal; - - //parsing inverted key - if(json["inverted"].success() ) - { - this->actor.inverted=json["inverted"]; - } - else - { - this->actor.inverted=this->actor.inverted; - } - json["inverted"]=this->actor.inverted; - - //parsing inverted key - if(json["inverted"].success() ) - { - this->actor.inverted=json["inverted"]; - } - else - { - this->actor.inverted=this->actor.inverted; - } - json["inverted"]=this->actor.inverted; - - //parsing low key - if(json["low"].success() ) - { - this->actor.rangeLow=json["low"][0]; - this->actor.timeLow=json["low"][1]; - this->actor.hourLow=json["low"][2]; - this->actor.minuteLow=json["low"][3]; - } - else - { - this->actor.rangeLow=this->actor.rangeLow; - this->actor.timeLow=this->actor.timeLow; - this->actor.hourLow=this->actor.hourLow; - this->actor.minuteLow=this->actor.minuteLow; - } - json["low"][0]=this->actor.rangeLow; - json["low"][1]=this->actor.timeLow; - json["low"][2]=this->actor.hourLow; - json["low"][3]=this->actor.minuteLow; - - //parsing high key - if(json["high"].success() ) - { - this->actor.rangeHigh=json["high"][0]; - this->actor.timeHigh=json["high"][1]; - this->actor.hourHigh=json["high"][2]; - this->actor.minuteHigh=json["high"][3]; - } - else - { - this->actor.rangeHigh=this->actor.rangeHigh; - this->actor.timeHigh=this->actor.timeHigh; - this->actor.hourHigh=this->actor.hourHigh; - this->actor.minuteHigh=this->actor.minuteHigh; - } - json["high"][0]=this->actor.rangeHigh; - json["high"][1]=this->actor.timeHigh; - json["high"][2]=this->actor.hourHigh; - json["high"][3]=this->actor.minuteHigh; - - //parsing type key - if(json["type"].success() ) - { - this->actor.primaryType=json["type"][0].as(); - this->actor.secondaryType=json["type"][1].as(); - - } - else - { - this->actor.primaryType=this->actor.primaryType; - this->actor.secondaryType=this->actor.secondaryType; - } - json["type"][0]=this->actor.primaryType; - json["type"][1]=this->actor.secondaryType; - - - coolBoardActorConfig.close(); - coolBoardActorConfig = SPIFFS.open("/coolBoardActorConfig.json", "w"); - if(!coolBoardActorConfig) - { - - #if DEBUG == 1 - - Serial.println( F("failed to write to /coolBoardActorConfig.json ") ); - Serial.println(); - - #endif - - return(false); - } - - json.printTo(coolBoardActorConfig); - coolBoardActorConfig.close(); - - #if DEBUG == 1 - - Serial.println(F("saved configuration : ")); - json.printTo(Serial ); - Serial.println(); - - #endif - - return(true); - } - } - + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.parseObject(data); + + String output; + DynamicJsonBuffer jsonBufferOutput; + JsonObject &rootOutput = jsonBuffer.createObject(); + + if (!root.success()) { + +#if DEBUG == 1 + + Serial.println(F("failed to parse json object ")); + Serial.println(); + +#endif + + } else if (!rootOutput.success()) { + +#if DEBUG == 1 + Serial.println(F("failed to create output json")); + Serial.println(); + +#endif + + } else { + +#if DEBUG == 1 + + Serial.println(F("created Json object :")); + root.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + // invert the current action state for the actor + // if the value is outside the limits + + // check if actor is actif + if (this->actor.actif == 1) { + // normal actor + if (this->actor.temporal == 0) { + // not inverted actor + if (this->actor.inverted == 0) { + this->normalAction(root[this->actor.primaryType].as()); + } + // inverted actor + else if (this->actor.inverted == 1) { + this->invertedAction(root[this->actor.primaryType].as()); + } + } + // temporal actor + else if (this->actor.temporal == 1) { + // hour actor + if (this->actor.secondaryType == "hour") { + // mixed hour actor + if (root[this->actor.primaryType].success()) { + this->mixedHourAction(hour, + root[this->actor.primaryType].as()); + } + // normal hour actor + else { + this->hourAction( + hour); // root[this->actor.secondaryType].as()); + } + + } + // minute actor + else if (this->actor.secondaryType == "minute") { + // mixed minute actor + if (root[this->actor.primaryType].success()) { + this->mixedMinuteAction(minute, + root[this->actor.primaryType].as()); + } + // normal minute actor + else { + this->minuteAction(minute); + } + } + // hourMinute actor + else if (this->actor.secondaryType == "hourMinute") { + // mixed hourMinute actor + if (root[this->actor.primaryType].success()) { + this->mixedHourMinuteAction( + hour, minute, root[this->actor.primaryType].as()); + } + // normal hourMinute actor + else { + this->hourMinuteAction(hour, minute); + } + } + // normal temporal actor + else if (this->actor.secondaryType == "") { + // mixed temporal actor + if (root[this->actor.primaryType].success()) { + this->mixedTemporalActionOn( + root[this->actor.primaryType].as()); + } + // normal temporal actor + else { + this->temporalActionOn(); + } + } + } + } + // inactif actor + else if (this->actor.actif == 0) { + // temporal actor + if (this->actor.temporal == 1) { + // mixed temporal actor + if (root[this->actor.primaryType].success()) { + this->mixedTemporalActionOff( + root[this->actor.primaryType].as()); + } + // normal temporal actor + else { + this->temporalActionOff(); + } + } + } + } + + rootOutput["ActB"] = digitalRead(this->pin); + + rootOutput.printTo(output); + + return (output); } /** -* CoolBoardActor::printConf(): -* This method is provided to -* print the configuration to the -* Serial Monitor -*/ -void CoolBoardActor::printConf() -{ + * CoolBoardActor::config(): + * This method is provided to configure the + * CoolBoardActor with a configuration file + * + * \return true if successful,false otherwise + */ +bool CoolBoardActor::config() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Enter CoolBoardActor.printConf() ") ); - Serial.println(); + Serial.println(F("Entering CoolBoardActor.config() ")); + Serial.println(); -#endif - Serial.println(F( "CoolBoardActor configuration " ) ) ; - - Serial.print(F(" actif :")); - Serial.println(this->actor.actif); - +#endif - Serial.print(F(" temporal :")); - Serial.println(this->actor.temporal); + File coolBoardActorConfig = SPIFFS.open("/coolBoardActorConfig.json", "r"); + if (!coolBoardActorConfig) { - Serial.print(F(" inverted :")); - Serial.println(this->actor.inverted); +#if DEBUG == 1 + Serial.println(F("failed to read /coolBoardActorConfig.json ")); + Serial.println(); +#endif - Serial.print(F(" primary Type :")); - Serial.println(this->actor.primaryType); + return (false); + } else { + size_t size = coolBoardActorConfig.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); - Serial.print(F(" secondary Type :")); - Serial.println(this->actor.secondaryType); + coolBoardActorConfig.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { +#if DEBUG == 1 - Serial.print(F(" range Low :")); - Serial.println(this->actor.rangeLow); + Serial.println( + F("failed to parse coolBoardActor Config json from file ")); + Serial.println(); +#endif - Serial.print(F(" time Low :")); - Serial.println(this->actor.timeLow); + return (false); + } else { +#if DEBUG == 1 - Serial.print(F(" hour low:")); - Serial.println(this->actor.hourLow); + Serial.println(F("read configuration file : ")); + json.printTo(Serial); + Serial.println(); + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); - Serial.print(F(" minute low:")); - Serial.println(this->actor.minuteLow); +#endif + // parsing actif key + if (json["actif"].success()) { + this->actor.actif = json["actif"]; + } else { + this->actor.actif = this->actor.actif; + } + json["actif"] = this->actor.actif; + + // parsing temporal key + if (json["temporal"].success()) { + this->actor.temporal = json["temporal"]; + } else { + this->actor.temporal = this->actor.temporal; + } + json["temporal"] = this->actor.temporal; + + // parsing inverted key + if (json["inverted"].success()) { + this->actor.inverted = json["inverted"]; + } else { + this->actor.inverted = this->actor.inverted; + } + json["inverted"] = this->actor.inverted; + + // parsing inverted key + if (json["inverted"].success()) { + this->actor.inverted = json["inverted"]; + } else { + this->actor.inverted = this->actor.inverted; + } + json["inverted"] = this->actor.inverted; + + // parsing low key + if (json["low"].success()) { + this->actor.rangeLow = json["low"][0]; + this->actor.timeLow = json["low"][1]; + this->actor.hourLow = json["low"][2]; + this->actor.minuteLow = json["low"][3]; + } else { + this->actor.rangeLow = this->actor.rangeLow; + this->actor.timeLow = this->actor.timeLow; + this->actor.hourLow = this->actor.hourLow; + this->actor.minuteLow = this->actor.minuteLow; + } + json["low"][0] = this->actor.rangeLow; + json["low"][1] = this->actor.timeLow; + json["low"][2] = this->actor.hourLow; + json["low"][3] = this->actor.minuteLow; + + // parsing high key + if (json["high"].success()) { + this->actor.rangeHigh = json["high"][0]; + this->actor.timeHigh = json["high"][1]; + this->actor.hourHigh = json["high"][2]; + this->actor.minuteHigh = json["high"][3]; + } else { + this->actor.rangeHigh = this->actor.rangeHigh; + this->actor.timeHigh = this->actor.timeHigh; + this->actor.hourHigh = this->actor.hourHigh; + this->actor.minuteHigh = this->actor.minuteHigh; + } + json["high"][0] = this->actor.rangeHigh; + json["high"][1] = this->actor.timeHigh; + json["high"][2] = this->actor.hourHigh; + json["high"][3] = this->actor.minuteHigh; + + // parsing type key + if (json["type"].success()) { + this->actor.primaryType = json["type"][0].as(); + this->actor.secondaryType = json["type"][1].as(); + + } else { + this->actor.primaryType = this->actor.primaryType; + this->actor.secondaryType = this->actor.secondaryType; + } + json["type"][0] = this->actor.primaryType; + json["type"][1] = this->actor.secondaryType; + + coolBoardActorConfig.close(); + coolBoardActorConfig = SPIFFS.open("/coolBoardActorConfig.json", "w"); + if (!coolBoardActorConfig) { - Serial.print(F(" range High:")); - Serial.println(this->actor.rangeHigh); +#if DEBUG == 1 + Serial.println(F("failed to write to /coolBoardActorConfig.json ")); + Serial.println(); - Serial.print(F(" time High:")); - Serial.println(this->actor.timeHigh); +#endif + return (false); + } - Serial.print(F(" hour high:")); - Serial.println(this->actor.hourHigh); + json.printTo(coolBoardActorConfig); + coolBoardActorConfig.close(); +#if DEBUG == 1 - Serial.print(F(" minute high:")); - Serial.println(this->actor.minuteHigh); + Serial.println(F("saved configuration : ")); + json.printTo(Serial); + Serial.println(); - Serial.println(); +#endif + return (true); + } + } } - + /** -* CoolBoardActor::normalAction( measured value): -* This method is provided to -* handle normal actors. -* it changes the action according to wether the -* measured value is: > rangeHigh ( deactivate actor) -* or < rangeLow (activate actor ) -*/ -void CoolBoardActor::normalAction(float measurment) -{ + * CoolBoardActor::printConf(): + * This method is provided to + * print the configuration to the + * Serial Monitor + */ +void CoolBoardActor::printConf() { + +#if DEBUG == 1 + + Serial.println(F("Enter CoolBoardActor.printConf() ")); + Serial.println(); + +#endif + Serial.println(F("CoolBoardActor configuration ")); + + Serial.print(F(" actif :")); + Serial.println(this->actor.actif); + + Serial.print(F(" temporal :")); + Serial.println(this->actor.temporal); + + Serial.print(F(" inverted :")); + Serial.println(this->actor.inverted); + + Serial.print(F(" primary Type :")); + Serial.println(this->actor.primaryType); + + Serial.print(F(" secondary Type :")); + Serial.println(this->actor.secondaryType); + + Serial.print(F(" range Low :")); + Serial.println(this->actor.rangeLow); + + Serial.print(F(" time Low :")); + Serial.println(this->actor.timeLow); + + Serial.print(F(" hour low:")); + Serial.println(this->actor.hourLow); + + Serial.print(F(" minute low:")); + Serial.println(this->actor.minuteLow); + + Serial.print(F(" range High:")); + Serial.println(this->actor.rangeHigh); + + Serial.print(F(" time High:")); + Serial.println(this->actor.timeHigh); + + Serial.print(F(" hour high:")); + Serial.println(this->actor.hourHigh); + + Serial.print(F(" minute high:")); + Serial.println(this->actor.minuteHigh); + + Serial.println(); +} + +/** + * CoolBoardActor::normalAction( measured value): + * This method is provided to + * handle normal actors. + * it changes the action according to wether the + * measured value is: > rangeHigh ( deactivate actor) + * or < rangeLow (activate actor ) + */ +void CoolBoardActor::normalAction(float measurment) { #if DEBUG == 1 - - Serial.print(F("none inverted Actor")); - Serial.println(); - Serial.print(F("measured value : ")); - Serial.println(measurment); + Serial.print(F("none inverted Actor")); + Serial.println(); - Serial.print(F("high range : ")); - Serial.println(this->actor.rangeHigh); + Serial.print(F("measured value : ")); + Serial.println(measurment); - Serial.print(F("low range : ")); - Serial.println(this->actor.rangeLow); + Serial.print(F("high range : ")); + Serial.println(this->actor.rangeHigh); + + Serial.print(F("low range : ")); + Serial.println(this->actor.rangeLow); #endif - //measured value lower than minimum range : activate actor - if(measurment < this->actor.rangeLow) - { - this->write( 1) ; + // measured value lower than minimum range : activate actor + if (measurment < this->actor.rangeLow) { + this->write(1); - #if DEBUG == 1 +#if DEBUG == 1 - Serial.println(F("actor ON ")); - - #endif - - } - //measured value higher than maximum range : deactivate actor - else if(measurment > this->actor.rangeHigh) - { - this->write( 0) ; + Serial.println(F("actor ON ")); - #if DEBUG == 1 +#endif - Serial.println(F("actor OFF ")); - - #endif - - } + } + // measured value higher than maximum range : deactivate actor + else if (measurment > this->actor.rangeHigh) { + this->write(0); +#if DEBUG == 1 -} + Serial.println(F("actor OFF ")); +#endif + } +} /** -* CoolBoardActor::invertedAction( measured value): -* This method is provided to -* handle inverted actors. -* it changes the action according to wether the -* measured value is: -* > rangeHigh (activate actor) -* < rangeLow ( deactivate actor ) -*/ -void CoolBoardActor::invertedAction(float measurment) -{ + * CoolBoardActor::invertedAction( measured value): + * This method is provided to + * handle inverted actors. + * it changes the action according to wether the + * measured value is: + * > rangeHigh (activate actor) + * < rangeLow ( deactivate actor ) + */ +void CoolBoardActor::invertedAction(float measurment) { #if DEBUG == 1 - - Serial.print("inverted Actor "); - Serial.println(); - Serial.print("measured value : "); - Serial.println(measurment); + Serial.print("inverted Actor "); + Serial.println(); - Serial.print("high range : "); - Serial.println(this->actor.rangeHigh); + Serial.print("measured value : "); + Serial.println(measurment); - Serial.print("low range : "); - Serial.println(this->actor.rangeLow); + Serial.print("high range : "); + Serial.println(this->actor.rangeHigh); -#endif + Serial.print("low range : "); + Serial.println(this->actor.rangeLow); - //measured value lower than minimum range : deactivate actor - if(measurment < this->actor.rangeLow) - { - this->write( 0) ; +#endif - #if DEBUG == 1 + // measured value lower than minimum range : deactivate actor + if (measurment < this->actor.rangeLow) { + this->write(0); - Serial.println(F("actor OFF ")); - - #endif +#if DEBUG == 1 - } - //measured value higher than maximum range : activate actor - else if(measurment > this->actor.rangeHigh) - { - this->write( 1) ; + Serial.println(F("actor OFF ")); - #if DEBUG == 1 +#endif - Serial.println(F("actor ON ")); - - #endif + } + // measured value higher than maximum range : activate actor + else if (measurment > this->actor.rangeHigh) { + this->write(1); - } +#if DEBUG == 1 + Serial.println(F("actor ON ")); +#endif + } } /** -* CoolBoardActor::temporalActionOff( ): -* This method is provided to -* handle temporal actors. -* it changes the action according to: -* -* currentTime - startTime > timeHigh : deactivate actor -* -*/ -void CoolBoardActor::temporalActionOff( ) -{ + * CoolBoardActor::temporalActionOff( ): + * This method is provided to + * handle temporal actors. + * it changes the action according to: + * + * currentTime - startTime > timeHigh : deactivate actor + * + */ +void CoolBoardActor::temporalActionOff() { #if DEBUG == 1 - - Serial.print(F("temporal Actor ")); - Serial.println(); - Serial.print(F("millis : ")); - Serial.println(millis()); + Serial.print(F("temporal Actor ")); + Serial.println(); - Serial.print(F("actif Time : ")); - Serial.println(this->actor.actifTime); + Serial.print(F("millis : ")); + Serial.println(millis()); - Serial.print(F("high time : ")); - Serial.println(this->actor.timeHigh); + Serial.print(F("actif Time : ")); + Serial.println(this->actor.actifTime); + Serial.print(F("high time : ")); + Serial.println(this->actor.timeHigh); #endif - - if( ( millis()- this->actor.actifTime ) >= ( this->actor.timeHigh ) ) - { - //stop the actor - this->write( 0) ; - //make the actor inactif: - this->actor.actif=0; + if ((millis() - this->actor.actifTime) >= (this->actor.timeHigh)) { + // stop the actor + this->write(0); - //start the low timer - this->actor.inactifTime=millis(); + // make the actor inactif: + this->actor.actif = 0; - #if DEBUG == 1 + // start the low timer + this->actor.inactifTime = millis(); - Serial.println(F("actor OFF ")); - - #endif - - } -} +#if DEBUG == 1 + + Serial.println(F("actor OFF ")); +#endif + } +} /** -* CoolBoardActor::mixedTemporalActionOff( measured value ): -* This method is provided to -* handle mixed temporal actors. -* it changes the action according to: -* -* currentTime - startTime >= timeHigh : -* measured value >= rangeHigh : deactivate actor -* measured value < rangeHigh : activate actor -*/ -void CoolBoardActor::mixedTemporalActionOff( float measurment) -{ + * CoolBoardActor::mixedTemporalActionOff( measured value ): + * This method is provided to + * handle mixed temporal actors. + * it changes the action according to: + * + * currentTime - startTime >= timeHigh : + * measured value >= rangeHigh : deactivate actor + * measured value < rangeHigh : activate actor + */ +void CoolBoardActor::mixedTemporalActionOff(float measurment) { #if DEBUG == 1 - - Serial.print("mixed Temporal Actor N° : "); - Serial.println(); - Serial.print("measured value : "); - Serial.println(measurment); + Serial.print("mixed Temporal Actor N° : "); + Serial.println(); - Serial.print("high range : "); - Serial.println(this->actor.rangeHigh); + Serial.print("measured value : "); + Serial.println(measurment); - Serial.print("time high : "); - Serial.println(this->actor.timeHigh); + Serial.print("high range : "); + Serial.println(this->actor.rangeHigh); - Serial.print("actif Time : "); - Serial.println(this->actor.actifTime); + Serial.print("time high : "); + Serial.println(this->actor.timeHigh); - Serial.print(F("millis : ")); - Serial.println(millis()); + Serial.print("actif Time : "); + Serial.println(this->actor.actifTime); -#endif - if( ( millis()- this->actor.actifTime ) >= ( this->actor.timeHigh ) ) - { - if( measurment >= this->actor.rangeHigh ) - { - //stop the actor - this->write( 0) ; + Serial.print(F("millis : ")); + Serial.println(millis()); - //make the actor inactif: - this->actor.actif=0; +#endif + if ((millis() - this->actor.actifTime) >= (this->actor.timeHigh)) { + if (measurment >= this->actor.rangeHigh) { + // stop the actor + this->write(0); - //start the low timer - this->actor.inactifTime=millis(); + // make the actor inactif: + this->actor.actif = 0; - #if DEBUG == 1 + // start the low timer + this->actor.inactifTime = millis(); - Serial.print(F("actor was on for at least ")); - Serial.print(this->actor.timeHigh); - Serial.println(F(" ms ")); +#if DEBUG == 1 - Serial.print(measurment); - Serial.print(F(" > " )); - Serial.println(this->actor.rangeHigh); + Serial.print(F("actor was on for at least ")); + Serial.print(this->actor.timeHigh); + Serial.println(F(" ms ")); - - Serial.println(F("actor OFF ")); + Serial.print(measurment); + Serial.print(F(" > ")); + Serial.println(this->actor.rangeHigh); - #endif + Serial.println(F("actor OFF ")); - } - else - { - this->write( 1) ; +#endif - #if DEBUG == 1 - - Serial.print(F("actor was on for at least ")); - Serial.print(this->actor.timeHigh); - Serial.println(F(" ms ")); + } else { + this->write(1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actor.rangeHigh); +#if DEBUG == 1 - Serial.println(F("actor ON ")); + Serial.print(F("actor was on for at least ")); + Serial.print(this->actor.timeHigh); + Serial.println(F(" ms ")); - #endif + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actor.rangeHigh); - } - } + Serial.println(F("actor ON ")); +#endif + } + } } - /** -* CoolBoardActor::temporalActionOn( ): -* This method is provided to -* handle temporal actors. -* it changes the action according to : -* -* currentTime - stopTime > timeLow : activate actor -* -*/ -void CoolBoardActor::temporalActionOn( ) -{ + * CoolBoardActor::temporalActionOn( ): + * This method is provided to + * handle temporal actors. + * it changes the action according to : + * + * currentTime - stopTime > timeLow : activate actor + * + */ +void CoolBoardActor::temporalActionOn() { #if DEBUG == 1 - - Serial.print(F("temporal Actor ")); - Serial.println(); - Serial.print(F("millis : ")); - Serial.println(millis()); + Serial.print(F("temporal Actor ")); + Serial.println(); - Serial.print(F("inactif Time : ")); - Serial.println(this->actor.inactifTime); + Serial.print(F("millis : ")); + Serial.println(millis()); - Serial.print(F("low time : ")); - Serial.println(this->actor.timeLow); + Serial.print(F("inactif Time : ")); + Serial.println(this->actor.inactifTime); + Serial.print(F("low time : ")); + Serial.println(this->actor.timeLow); #endif - - if( ( millis() - this->actor.inactifTime ) >= ( this->actor.timeLow ) ) - { - //start the actor - this->write( 1) ; - //make the actor actif: - this->actor.actif=1; + if ((millis() - this->actor.inactifTime) >= (this->actor.timeLow)) { + // start the actor + this->write(1); - //start the low timer - this->actor.actifTime=millis(); + // make the actor actif: + this->actor.actif = 1; - #if DEBUG == 1 + // start the low timer + this->actor.actifTime = millis(); - Serial.println(F("actor ON ")); - - #endif +#if DEBUG == 1 - } + Serial.println(F("actor ON ")); +#endif + } } /** -* CoolBoardActor::mixedTemporalActionOn( measured value ): -* This method is provided to -* handle mixed temporal actors. -* it changes the action according to : -* -* currentTime - stopTime > timeLow : -* measured value >= rangeLow : deactivate actor -* measured value < rangeLow : activate actor -* -*/ -void CoolBoardActor::mixedTemporalActionOn( float measurment) -{ + * CoolBoardActor::mixedTemporalActionOn( measured value ): + * This method is provided to + * handle mixed temporal actors. + * it changes the action according to : + * + * currentTime - stopTime > timeLow : + * measured value >= rangeLow : deactivate actor + * measured value < rangeLow : activate actor + * + */ +void CoolBoardActor::mixedTemporalActionOn(float measurment) { #if DEBUG == 1 - - Serial.print("mixed Temporal Actor "); - Serial.println(); - Serial.print("measured value : "); - Serial.println(measurment); + Serial.print("mixed Temporal Actor "); + Serial.println(); + + Serial.print("measured value : "); + Serial.println(measurment); - Serial.print("low range : "); - Serial.println(this->actor.rangeLow); + Serial.print("low range : "); + Serial.println(this->actor.rangeLow); - Serial.print("time low : "); - Serial.println(this->actor.timeLow); + Serial.print("time low : "); + Serial.println(this->actor.timeLow); - Serial.print("inactif Time : "); - Serial.println(this->actor.inactifTime); + Serial.print("inactif Time : "); + Serial.println(this->actor.inactifTime); - Serial.print(F("millis : ")); - Serial.println(millis()); + Serial.print(F("millis : ")); + Serial.println(millis()); #endif - if( ( millis() - this->actor.inactifTime ) >= ( this->actor.timeLow ) ) - { - if( measurment < this->actor.rangeLow ) - { - //start the actor - this->write( 1) ; + if ((millis() - this->actor.inactifTime) >= (this->actor.timeLow)) { + if (measurment < this->actor.rangeLow) { + // start the actor + this->write(1); - //make the actor actif: - this->actor.actif=1; + // make the actor actif: + this->actor.actif = 1; - //start the low timer - this->actor.actifTime=millis(); + // start the low timer + this->actor.actifTime = millis(); - #if DEBUG == 1 +#if DEBUG == 1 - Serial.print(F("actor was off for at least ")); - Serial.print(this->actor.timeLow); - Serial.println(F(" ms ")); + Serial.print(F("actor was off for at least ")); + Serial.print(this->actor.timeLow); + Serial.println(F(" ms ")); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actor.rangeLow); - - Serial.println(F("actor ON ")); - - #endif + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actor.rangeLow); - } - else - { - this->write( 0) ; + Serial.println(F("actor ON ")); - #if DEBUG == 1 +#endif - Serial.print(F("actor was off for at least ")); - Serial.print(this->actor.timeLow); - Serial.println(F(" ms ")); + } else { + this->write(0); - Serial.print(measurment); - Serial.print(F(" > " )); - Serial.println(this->actor.rangeLow); +#if DEBUG == 1 - Serial.println(F("actor OFF ")); - - #endif + Serial.print(F("actor was off for at least ")); + Serial.print(this->actor.timeLow); + Serial.println(F(" ms ")); - } + Serial.print(measurment); + Serial.print(F(" > ")); + Serial.println(this->actor.rangeLow); - } + Serial.println(F("actor OFF ")); - +#endif + } + } } - /** -* CoolBoardActor::hourAction( current hour ): -* This method is provided to -* handle hour actors. -* it changes the action according to: -* -* hour >= hourLow : deactivate the actor -* hour >= hourHigh : activate the actor -* -*/ -void CoolBoardActor::hourAction( int hour) -{ + * CoolBoardActor::hourAction( current hour ): + * This method is provided to + * handle hour actors. + * it changes the action according to: + * + * hour >= hourLow : deactivate the actor + * hour >= hourHigh : activate the actor + * + */ +void CoolBoardActor::hourAction(int hour) { #if DEBUG == 1 - - Serial.print(F("hour Actor ")); - Serial.println(); - Serial.print(F(" hour : ")); - Serial.println(hour); + Serial.print(F("hour Actor ")); + Serial.println(); + + Serial.print(F(" hour : ")); + Serial.println(hour); - Serial.print(F("high hour : ")); - Serial.println(this->actor.hourHigh); + Serial.print(F("high hour : ")); + Serial.println(this->actor.hourHigh); - Serial.print(F("low hour : ")); - Serial.println(this->actor.hourLow); + Serial.print(F("low hour : ")); + Serial.println(this->actor.hourLow); - Serial.print(F("inverted Flag : ")); - Serial.println(this->actor.inverted); - Serial.println(); + Serial.print(F("inverted Flag : ")); + Serial.println(this->actor.inverted); + Serial.println(); #endif - if (this->actor.hourHigh < this->actor.hourLow) - { - //stop the actor - if(hour >= this->actor.hourLow || hour < this->actor.hourHigh) - { - if (this->actor.inverted) - { - this->write( 1) ; - } - else - { - this->write( 0) ; - } - - #if DEBUG == 1 - - Serial.println(F("Daymode")); - Serial.println(F("Onboard Actor OFF ")); - - #endif - - } - //starting the actor - else - { - if (this->actor.inverted) - { - this->write( 0) ; - } - else - { - this->write( 1) ; - } - - #if DEBUG == 1 - - Serial.println(F("DayMode")); - Serial.println(F("Onboard Actor ON ")); - - #endif - - } - } - else - { - //stop the actor in Nght mode ie a light that is on over night - if(hour >= this->actor.hourLow && hour < this->actor.hourHigh) - { - if (this->actor.inverted) - { - this->write( 1) ; - } - else - { - this->write( 0) ; - } - - #if DEBUG == 1 - - Serial.println(F("Nightmode")); - Serial.println(F("Onboard Actor OFF ")); - - #endif - - } - //starting the actor - else - { - if (this->actor.inverted) - { - this->write( 0) ; - } - else - { - this->write( 1) ; - } - - #if DEBUG == 1 - - Serial.println(F("Nightmode")); - Serial.println(F("Onboard Actor ON ")); - - #endif - - } - } -} + if (this->actor.hourHigh < this->actor.hourLow) { + // stop the actor + if (hour >= this->actor.hourLow || hour < this->actor.hourHigh) { + if (this->actor.inverted) { + this->write(1); + } else { + this->write(0); + } +#if DEBUG == 1 + + Serial.println(F("Daymode")); + Serial.println(F("Onboard Actor OFF ")); + +#endif + + } + // starting the actor + else { + if (this->actor.inverted) { + this->write(0); + } else { + this->write(1); + } + +#if DEBUG == 1 + + Serial.println(F("DayMode")); + Serial.println(F("Onboard Actor ON ")); + +#endif + } + } else { + // stop the actor in Nght mode ie a light that is on over night + if (hour >= this->actor.hourLow && hour < this->actor.hourHigh) { + if (this->actor.inverted) { + this->write(1); + } else { + this->write(0); + } + +#if DEBUG == 1 + + Serial.println(F("Nightmode")); + Serial.println(F("Onboard Actor OFF ")); + +#endif + + } + // starting the actor + else { + if (this->actor.inverted) { + this->write(0); + } else { + this->write(1); + } + +#if DEBUG == 1 + + Serial.println(F("Nightmode")); + Serial.println(F("Onboard Actor ON ")); + +#endif + } + } +} /** -* CoolBoardActor::mixedHourAction( current hour, measured value ): -* This method is provided to -* handle mixed hour actors. -* it changes the action according to : -* -* hour >= hourLow : -* -measuredValue >= rangeHigh : deactivate actor -* -measured < rangeHigh : activate actor -* -* hour >= hourHigh : -* -measuredValue < rangeLow : activate actor -* -measuredValue >=rangeLow : activate actor -*/ + * CoolBoardActor::mixedHourAction( current hour, measured value ): + * This method is provided to + * handle mixed hour actors. + * it changes the action according to : + * + * hour >= hourLow : + * -measuredValue >= rangeHigh : deactivate actor + * -measured < rangeHigh : activate actor + * + * hour >= hourHigh : + * -measuredValue < rangeLow : activate actor + * -measuredValue >=rangeLow : activate actor + */ void CoolBoardActor::mixedHourAction(int hour, float measurment) { #if DEBUG == 1 - - Serial.print(F("hour Actor ")); - Serial.println(); - Serial.print(F(" hour : ")); - Serial.println(hour); + Serial.print(F("hour Actor ")); + Serial.println(); + + Serial.print(F(" hour : ")); + Serial.println(hour); - Serial.print(F("high hour : ")); - Serial.println(this->actor.hourHigh); + Serial.print(F("high hour : ")); + Serial.println(this->actor.hourHigh); - Serial.print(F("low hour : ")); - Serial.println(this->actor.hourLow); + Serial.print(F("low hour : ")); + Serial.println(this->actor.hourLow); - Serial.print(F("inverted Flag : ")); - Serial.println(this->actor.inverted); + Serial.print(F("inverted Flag : ")); + Serial.println(this->actor.inverted); - Serial.print(F("measurment : ")); - Serial.println(measurment); - Serial.println(); + Serial.print(F("measurment : ")); + Serial.println(measurment); + Serial.println(); #endif - if (measurment <= this->actor.rangeLow && this->actor.failsave == true) - { - this->actor.failsave = false; - Serial.println("!!! Reset Failsave !!!"); - } - - else if (measurment >= this->actor.rangeHigh && this->actor.failsave == false) - { - this->actor.failsave = true; - Serial.println("!!! Engage Failsave !!!"); - } - - if (this->actor.hourHigh < this->actor.hourLow) - { - //stop the actor - if((hour >= this->actor.hourLow || hour < this->actor.hourHigh) || this->actor.failsave == true) - { - if (this->actor.inverted) - { - this->write( 1) ; - } - else - { - this->write( 0) ; - } - - #if DEBUG == 1 - - Serial.println(F("Daymode")); - Serial.println(F("Onboard Actor OFF ")); - Serial.print(F("failsave Flag : ")); - Serial.println(this->actor.failsave); - - #endif - - } - //starting the actor - else if (this->actor.failsave == false) - { - if (this->actor.inverted) - { - this->write( 0) ; - } - else - { - this->write( 1) ; - } - - #if DEBUG == 1 - - Serial.println(F("DayMode")); - Serial.println(F("Onboard Actor ON ")); - - #endif - - } - } - else - { - //stop the actor in Nght mode ie a light that is on over night - if((hour >= this->actor.hourLow && hour < this->actor.hourHigh) || this->actor.failsave == true) - { - if (this->actor.inverted) - { - this->write( 1) ; - } - else - { - this->write( 0) ; - } - - #if DEBUG == 1 - - Serial.println(F("Nightmode")); - Serial.println(F("Onboard Actor OFF ")); - - #endif - - } - //starting the actor - else if (this->actor.failsave == false) - { - if (this->actor.inverted) - { - this->write( 0) ; - } - else - { - this->write( 1) ; - } - - #if DEBUG == 1 - - Serial.println(F("Nightmode")); - Serial.println(F("Onboard Actor ON ")); - - #endif - - } - } + if (measurment <= this->actor.rangeLow && this->actor.failsave == true) { + this->actor.failsave = false; + Serial.println("!!! Reset Failsave !!!"); + } + + else if (measurment >= this->actor.rangeHigh && + this->actor.failsave == false) { + this->actor.failsave = true; + Serial.println("!!! Engage Failsave !!!"); + } + + if (this->actor.hourHigh < this->actor.hourLow) { + // stop the actor + if ((hour >= this->actor.hourLow || hour < this->actor.hourHigh) || + this->actor.failsave == true) { + if (this->actor.inverted) { + this->write(1); + } else { + this->write(0); + } -} +#if DEBUG == 1 + Serial.println(F("Daymode")); + Serial.println(F("Onboard Actor OFF ")); + Serial.print(F("failsave Flag : ")); + Serial.println(this->actor.failsave); -/** -* CoolBoardActor::minteAction( current minute ): -* This method is provided to -* handle minute actors. -* it changes the action according to: -* -* minute >= minuteLow : deactivate the actor -* minute >= minuteHigh : activate the actor -* -*/ -void CoolBoardActor::minuteAction(int minute) -{ +#endif + + } + // starting the actor + else if (this->actor.failsave == false) { + if (this->actor.inverted) { + this->write(0); + } else { + this->write(1); + } + +#if DEBUG == 1 + + Serial.println(F("DayMode")); + Serial.println(F("Onboard Actor ON ")); + +#endif + } + } else { + // stop the actor in Nght mode ie a light that is on over night + if ((hour >= this->actor.hourLow && hour < this->actor.hourHigh) || + this->actor.failsave == true) { + if (this->actor.inverted) { + this->write(1); + } else { + this->write(0); + } #if DEBUG == 1 - - Serial.print(F("minute Actor ")); - Serial.println(); - Serial.print(F(" minute : ")); - Serial.println(minute); + Serial.println(F("Nightmode")); + Serial.println(F("Onboard Actor OFF ")); - Serial.print(F("high minute : ")); - Serial.println(this->actor.minuteHigh); +#endif + + } + // starting the actor + else if (this->actor.failsave == false) { + if (this->actor.inverted) { + this->write(0); + } else { + this->write(1); + } - Serial.print(F("low minute : ")); - Serial.println(this->actor.minuteLow); +#if DEBUG == 1 + + Serial.println(F("Nightmode")); + Serial.println(F("Onboard Actor ON ")); #endif + } + } +} - //stop the actor - if(minute <= this->actor.minuteLow) - { - this->write( 0) ; +/** + * CoolBoardActor::minteAction( current minute ): + * This method is provided to + * handle minute actors. + * it changes the action according to: + * + * minute >= minuteLow : deactivate the actor + * minute >= minuteHigh : activate the actor + * + */ +void CoolBoardActor::minuteAction(int minute) { - #if DEBUG == 1 +#if DEBUG == 1 - Serial.println(F("Onboard Actor OFF ")); + Serial.print(F("minute Actor ")); + Serial.println(); - #endif + Serial.print(F(" minute : ")); + Serial.println(minute); - } - //starting the actor - else if(minute >= this->actor.minuteHigh) - { - this->write( 1) ; + Serial.print(F("high minute : ")); + Serial.println(this->actor.minuteHigh); - #if DEBUG == 1 + Serial.print(F("low minute : ")); + Serial.println(this->actor.minuteLow); - Serial.println(F("Onboard Actor ON ")); +#endif - #endif + // stop the actor + if (minute <= this->actor.minuteLow) { + this->write(0); - } +#if DEBUG == 1 -} + Serial.println(F("Onboard Actor OFF ")); + +#endif + + } + // starting the actor + else if (minute >= this->actor.minuteHigh) { + this->write(1); + +#if DEBUG == 1 + + Serial.println(F("Onboard Actor ON ")); + +#endif + } +} /** -* CoolBoardActor::mixedMinuteAction( current minute, measured value ): -* This method is provided to -* handle mixed minute actors. -* it changes the action according to : -* -* minute >= minuteLow : -* -measuredValue >= rangeHigh : deactivate actor -* -measured < rangeHigh : activate actor -* -* minute >= minuteHigh : -* -measuredValue < rangeLow : activate actor -* -measuredValue >=rangeLow : activate actor -*/ -void CoolBoardActor::mixedMinuteAction(int minute,float measurment) -{ + * CoolBoardActor::mixedMinuteAction( current minute, measured value ): + * This method is provided to + * handle mixed minute actors. + * it changes the action according to : + * + * minute >= minuteLow : + * -measuredValue >= rangeHigh : deactivate actor + * -measured < rangeHigh : activate actor + * + * minute >= minuteHigh : + * -measuredValue < rangeLow : activate actor + * -measuredValue >=rangeLow : activate actor + */ +void CoolBoardActor::mixedMinuteAction(int minute, float measurment) { #if DEBUG == 1 - - Serial.print("mixed minute Actor "); - Serial.println(); - Serial.print(" minute : "); - Serial.println(minute); + Serial.print("mixed minute Actor "); + Serial.println(); - Serial.print("high minute : "); - Serial.println(this->actor.minuteHigh); + Serial.print(" minute : "); + Serial.println(minute); - Serial.print("low minute : "); - Serial.println(this->actor.minuteLow); + Serial.print("high minute : "); + Serial.println(this->actor.minuteHigh); - Serial.print("measured value : "); - Serial.println(measurment); + Serial.print("low minute : "); + Serial.println(this->actor.minuteLow); - Serial.print("high range : "); - Serial.println(this->actor.rangeHigh); + Serial.print("measured value : "); + Serial.println(measurment); - Serial.print("low range : "); - Serial.println(this->actor.rangeLow); + Serial.print("high range : "); + Serial.println(this->actor.rangeHigh); + + Serial.print("low range : "); + Serial.println(this->actor.rangeLow); #endif - //stop the actor - if(minute <= this->actor.minuteLow) - { - if( measurment > this->actor.rangeHigh ) - { - this->write( 0) ; + // stop the actor + if (minute <= this->actor.minuteLow) { + if (measurment > this->actor.rangeHigh) { + this->write(0); - #if DEBUG == 1 +#if DEBUG == 1 - Serial.print(measurment); - Serial.print(F(" > " )); - Serial.println(this->actor.rangeHigh); + Serial.print(measurment); + Serial.print(F(" > ")); + Serial.println(this->actor.rangeHigh); - Serial.println(F("Onboard Actor OFF ")); + Serial.println(F("Onboard Actor OFF ")); - #endif - - } - else - { - this->write( 1) ; +#endif - #if DEBUG == 1 + } else { + this->write(1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actor.rangeHigh); +#if DEBUG == 1 - Serial.println(F("Onboard Actor ON ")); + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actor.rangeHigh); - #endif - - } - } - //starting the actor - else if(minute >= this->actor.minuteHigh) - { - if( measurment < this->actor.rangeLow ) - { - this->write( 1) ; + Serial.println(F("Onboard Actor ON ")); - #if DEBUG == 1 +#endif + } + } + // starting the actor + else if (minute >= this->actor.minuteHigh) { + if (measurment < this->actor.rangeLow) { + this->write(1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actor.rangeLow); +#if DEBUG == 1 - Serial.println(F("Onboard Actor ON ")); + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actor.rangeLow); - #endif + Serial.println(F("Onboard Actor ON ")); - } - else - { - this->write( 0) ; - - #if DEBUG == 1 +#endif - Serial.print(measurment); - Serial.print(F(" > " )); - Serial.println(this->actor.rangeLow); + } else { + this->write(0); - Serial.println(F("Onboard Actor OFF ")); +#if DEBUG == 1 - #endif - - } + Serial.print(measurment); + Serial.print(F(" > ")); + Serial.println(this->actor.rangeLow); - } + Serial.println(F("Onboard Actor OFF ")); +#endif + } + } } - /** -* CoolBoardActor::minteAction( current hour,current minute ): -* This method is provided to -* handle hour minute actors. -* it changes the action according to: -* -* hour == hourLow : -* minute >= minuteLow : deactivate the actor -* -* hour > hourLow : deactivate the actor -* -* hour == hourHigh : -* minute >= minteHigh : activate the actor -* -* hour > hourHigh : activate the actor -*/ -void CoolBoardActor::hourMinuteAction(int hour,int minute) -{ + * CoolBoardActor::minteAction( current hour,current minute ): + * This method is provided to + * handle hour minute actors. + * it changes the action according to: + * + * hour == hourLow : + * minute >= minuteLow : deactivate the actor + * + * hour > hourLow : deactivate the actor + * + * hour == hourHigh : + * minute >= minteHigh : activate the actor + * + * hour > hourHigh : activate the actor + */ +void CoolBoardActor::hourMinuteAction(int hour, int minute) { #if DEBUG == 1 - Serial.print(F("hourMinute Actor ")); - Serial.println(); + Serial.print(F("hourMinute Actor ")); + Serial.println(); - Serial.print(F(" hour : ")); - Serial.println(hour); - Serial.print(F(" minute : ")); - Serial.println(minute); + Serial.print(F(" hour : ")); + Serial.println(hour); + Serial.print(F(" minute : ")); + Serial.println(minute); - Serial.print(F("high hour : ")); - Serial.println(this->actor.hourHigh); + Serial.print(F("high hour : ")); + Serial.println(this->actor.hourHigh); - Serial.print(F("high minute : ")); - Serial.println(this->actor.minuteHigh); + Serial.print(F("high minute : ")); + Serial.println(this->actor.minuteHigh); - Serial.print(F("low hour : ")); - Serial.println(this->actor.hourLow); + Serial.print(F("low hour : ")); + Serial.println(this->actor.hourLow); - Serial.print(F("low minute : ")); - Serial.println(this->actor.minuteLow); + Serial.print(F("low minute : ")); + Serial.println(this->actor.minuteLow); #endif - //stop the actor - if(hour==this->actor.hourLow) - { - if(minute>= this->actor.minuteLow) - { - this->write( 0) ; - #if DEBUG == 1 - - Serial.println(F("Onboard Actor OFF ")); + // stop the actor + if (hour == this->actor.hourLow) { + if (minute >= this->actor.minuteLow) { + this->write(0); +#if DEBUG == 1 - #endif - } - } - else if(hour > this->actor.hourLow) - { + Serial.println(F("Onboard Actor OFF ")); - this->write( 0) ; - #if DEBUG == 1 +#endif + } + } else if (hour > this->actor.hourLow) { - Serial.println(F("Onboard Actor OFF ")); + this->write(0); +#if DEBUG == 1 - #endif - - } - //start the actor - else if(hour==this->actor.hourHigh) - { - if(minute>= this->actor.minuteHigh) - { - this->write( 1) ; + Serial.println(F("Onboard Actor OFF ")); - #if DEBUG == 1 +#endif - Serial.println(F("actor ON ")); + } + // start the actor + else if (hour == this->actor.hourHigh) { + if (minute >= this->actor.minuteHigh) { + this->write(1); - #endif - } - } - else if(hour > this->actor.hourHigh) - { +#if DEBUG == 1 - this->write( 1) ; + Serial.println(F("actor ON ")); - #if DEBUG == 1 +#endif + } + } else if (hour > this->actor.hourHigh) { - Serial.println(F("actor ON ")); + this->write(1); - #endif +#if DEBUG == 1 - } + Serial.println(F("actor ON ")); - +#endif + } } /** -* CoolBoardActor::minteAction( current hour,current minute , measured Value ): -* This method is provided to -* handle hour minute actors. -* it changes the action according to: -* -* hour == hourLow : -* minute >= minuteLow : -* measuredValue >= rangeHigh : deactivate actor -* measuredValue < rangeHigh : activate actor -* -* hour > hourLow : -* measuredValue >= rangeHigh : deactivate actor -* measuredValue < rangeHigh : activate actor -* -* hour == hourHigh : -* minute >= minteHigh : -* measuredValue >= rangeLow : deactivate actor -* measuredValue < rangeLow : activate actor -* -* hour > hourHigh : -* measuredValue >= rangeLow : deactivate actor -* measuredValue < rangeLow : activate actor -* -*/ -void CoolBoardActor::mixedHourMinuteAction(int hour,int minute ,float measurment) -{ + * CoolBoardActor::minteAction( current hour,current minute , measured + *Value ): This method is provided to handle hour minute actors. it changes the + *action according to: + * + * hour == hourLow : + * minute >= minuteLow : + * measuredValue >= rangeHigh : deactivate actor + * measuredValue < rangeHigh : activate actor + * + * hour > hourLow : + * measuredValue >= rangeHigh : deactivate actor + * measuredValue < rangeHigh : activate actor + * + * hour == hourHigh : + * minute >= minteHigh : + * measuredValue >= rangeLow : deactivate actor + * measuredValue < rangeLow : activate actor + * + * hour > hourHigh : + * measuredValue >= rangeLow : deactivate actor + * measuredValue < rangeLow : activate actor + * + */ +void CoolBoardActor::mixedHourMinuteAction(int hour, int minute, + float measurment) { #if DEBUG == 1 - - Serial.print("hourMinute Actor "); - Serial.println(); - - Serial.print(" hour : "); - Serial.println(hour); - Serial.print(" minute : "); - Serial.println(minute); - Serial.print("high hour : "); - Serial.println(this->actor.hourHigh); + Serial.print("hourMinute Actor "); + Serial.println(); - Serial.print("high minute : "); - Serial.println(this->actor.minuteHigh); + Serial.print(" hour : "); + Serial.println(hour); + Serial.print(" minute : "); + Serial.println(minute); - Serial.print("low hour : "); - Serial.println(this->actor.hourLow); + Serial.print("high hour : "); + Serial.println(this->actor.hourHigh); - Serial.print("low minute : "); - Serial.println(this->actor.minuteLow); + Serial.print("high minute : "); + Serial.println(this->actor.minuteHigh); - Serial.print("measured value : "); - Serial.println(measurment); + Serial.print("low hour : "); + Serial.println(this->actor.hourLow); - Serial.print("high range : "); - Serial.println(this->actor.rangeHigh); + Serial.print("low minute : "); + Serial.println(this->actor.minuteLow); - Serial.print("low range : "); - Serial.println(this->actor.rangeLow); + Serial.print("measured value : "); + Serial.println(measurment); -#endif - //stop the actor - if(hour==this->actor.hourLow) - { - if(minute>= this->actor.minuteLow) - { - if( measurment >= this->actor.rangeHigh ) - { - this->write( 0) ; + Serial.print("high range : "); + Serial.println(this->actor.rangeHigh); - #if DEBUG == 1 + Serial.print("low range : "); + Serial.println(this->actor.rangeLow); - Serial.print(measurment); - Serial.print(F(" >= " )); - Serial.println(this->actor.rangeHigh); +#endif + // stop the actor + if (hour == this->actor.hourLow) { + if (minute >= this->actor.minuteLow) { + if (measurment >= this->actor.rangeHigh) { + this->write(0); - Serial.println(F("Onboard Actor OFF ")); +#if DEBUG == 1 - #endif + Serial.print(measurment); + Serial.print(F(" >= ")); + Serial.println(this->actor.rangeHigh); - } - else - { - this->write( 1) ; + Serial.println(F("Onboard Actor OFF ")); - #if DEBUG == 1 +#endif - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actor.rangeHigh); + } else { + this->write(1); - Serial.println(F("Onboard Actor ON ")); +#if DEBUG == 1 - #endif - - } - } - } - else if(hour > this->actor.hourLow) - { + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actor.rangeHigh); - if( measurment >= this->actor.rangeHigh ) - { - this->write( 0) ; + Serial.println(F("Onboard Actor ON ")); - #if DEBUG == 1 +#endif + } + } + } else if (hour > this->actor.hourLow) { - Serial.print(measurment); - Serial.print(F(" >= " )); - Serial.println(this->actor.rangeHigh); + if (measurment >= this->actor.rangeHigh) { + this->write(0); - Serial.println(F("Onboard Actor OFF ")); +#if DEBUG == 1 - #endif + Serial.print(measurment); + Serial.print(F(" >= ")); + Serial.println(this->actor.rangeHigh); - } - else - { - this->write( 1) ; + Serial.println(F("Onboard Actor OFF ")); - #if DEBUG == 1 +#endif - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actor.rangeHigh); + } else { + this->write(1); - Serial.println(F("Onboard Actor ON ")); +#if DEBUG == 1 - #endif - - } + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actor.rangeHigh); + Serial.println(F("Onboard Actor ON ")); - } - //start the actor - else if(hour==this->actor.hourHigh) - { - if(minute>= this->actor.minuteHigh) - { - if( measurment < this->actor.rangeLow ) - { - this->write( 1) ; +#endif + } - #if DEBUG == 1 + } + // start the actor + else if (hour == this->actor.hourHigh) { + if (minute >= this->actor.minuteHigh) { + if (measurment < this->actor.rangeLow) { + this->write(1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actor.rangeLow); +#if DEBUG == 1 - Serial.println(F("Onboard Actor ON ")); + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actor.rangeLow); - #endif + Serial.println(F("Onboard Actor ON ")); - } - else - { - this->write( 0) ; +#endif - #if DEBUG == 1 + } else { + this->write(0); - Serial.print(measurment); - Serial.println(F(" > " )); - Serial.print(this->actor.rangeLow); +#if DEBUG == 1 - Serial.println(F("Onboard Actor OFF ")); + Serial.print(measurment); + Serial.println(F(" > ")); + Serial.print(this->actor.rangeLow); - #endif - - } - } - } - else if(hour > this->actor.hourHigh) - { + Serial.println(F("Onboard Actor OFF ")); - if( measurment < this->actor.rangeLow ) - { - this->write( 1) ; +#endif + } + } + } else if (hour > this->actor.hourHigh) { - #if DEBUG == 1 + if (measurment < this->actor.rangeLow) { + this->write(1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actor.rangeLow); +#if DEBUG == 1 - Serial.println(F("Onboard Actor ON ")); + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actor.rangeLow); - #endif + Serial.println(F("Onboard Actor ON ")); - } - else - { - this->write( 0) ; +#endif - #if DEBUG == 1 + } else { + this->write(0); - Serial.print(measurment); - Serial.println(F(" > " )); - Serial.print(this->actor.rangeLow); +#if DEBUG == 1 - Serial.println(F("Onboard Actor OFF ")); + Serial.print(measurment); + Serial.println(F(" > ")); + Serial.print(this->actor.rangeLow); - #endif - - } - - } + Serial.println(F("Onboard Actor OFF ")); +#endif + } + } } - - diff --git a/src/CoolBoardActor.h b/src/CoolBoardActor.h index ae5f6b98..58e677b1 100644 --- a/src/CoolBoardActor.h +++ b/src/CoolBoardActor.h @@ -1,205 +1,208 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef CoolBoardActor_H #define CoolBoardActor_H #include "Arduino.h" - /** -* \class CoolBoardActor -* \brief This class manages the CoolBoardActor -* -*/ -class CoolBoardActor -{ + * \class CoolBoardActor + * \brief This class manages the CoolBoardActor + * + */ +class CoolBoardActor { public: + void begin(); // starts the CoolBoardActor + + void write(bool action); // CoolBoardActor + + String doAction(const char *data, int hour, int minute); + + void normalAction(float measurment); + + void invertedAction(float measurment); + + void temporalActionOff(); + + void temporalActionOn(); - void begin(); //starts the CoolBoardActor - - void write(bool action); //CoolBoardActor - - String doAction(const char* data, int hour, int minute ); - - void normalAction(float measurment); - - void invertedAction(float measurment); - - void temporalActionOff(); - - void temporalActionOn(); - - void mixedTemporalActionOff(float measurment); - - void mixedTemporalActionOn(float measurment); - - void hourAction( int hour); - - void mixedHourAction(int hour, float measurment); - - void minuteAction(int minute); - - void mixedMinuteAction(int minute,float measurment); - - void hourMinuteAction(int hour,int minute); - - void mixedHourMinuteAction(int hour,int minute ,float measurment); - - bool config(); - - void printConf(); - - /** - * pin Number value - * the only available pin that can be used - * to run a small actor is pin 15 - */ - const int pin = 15; //pin for the actor - -//private: - - /** - * the Actor's state - */ - struct state{ - - /** - * actif flag - * set to 1 when using an actor - * set to 0 otherwise - */ - bool actif=0; - - /** - * temporal flag - * set to 1 to declare a temporal actor - * set to 0 otherwise - */ - bool temporal=0; - - /** - * inverted flag - * set to 1 to declare an inverted actor - * set to 0 to declare a normal actor - */ - bool inverted=0; - - /** - * "type":["temperature","hour"] - * - * the primary type is related to the sensor's type (type[0]) - * - * if both types are present and valid, the actor is a mixedActor - */ - String primaryType=""; - - /** - * "type":["temperature","hour"] - * - * the secondary type if present is hour or minute or hourMinute (type[1]) - * - * if both types are present and valid, the actor is a mixedActor - */ - String secondaryType=""; - - /** - * "low":[20,5000,18,10] - * - * rangeLow : this is the minimum at which the actor becomes actif (low[0]) - * - */ - int rangeLow=0; - - /** - * "low":[20,5000,18,10] - * - * timeLow : this is the time the actor is off in temporal mode (low[1]) (ms) - */ - unsigned long timeLow=0; - - /** - * "low":[20,5000,18,10] - * - * hour low :this is the hour when to turn off the actor in temporal/hour(hourMinute) mode (low[2] ) - */ - int hourLow=0; - - /** - * "low":[20,5000,18,10] - * - * minute low :this is the minute when to turn off the actor in temporal/minute(hourMinute) mode (low[3]) - */ - int minuteLow=0; - - - /** - * "high":[30,2000,17,1] - * - * rangeHigh : this is the maximum at which the actor becomes inactif(high[0]) - */ - int rangeHigh=0; - - /** - * "high":[30,2000,17,1] - * - * timeHigh : this is the time the actor is on in temporal mode(high[1]) (ms) - */ - unsigned long timeHigh=0; - - /** - * "high":[30,2000,17,1] - * - * hourHigh : this is the hour when to turn on the actor in temporal/hour(hourMinute) mode(high[2]) - */ - int hourHigh=0; - - /** - * "high":[30,2000,17,1] - * - * minuteHigh : this is the minute when to turn on the actor in temporal/minute(hourMinute) mode (high[3]) - */ - int minuteHigh=0; - - /** - * actifTime : period of Time spent actif , used in Temporal mode - * in ms - */ - unsigned long actifTime=0; - - /** - * inactifTime : period of Time spent inactif , used in Temporal mode - * in ms - */ - unsigned long inactifTime=0; - - /** - * failsave : Switches of the actor if a metric rises above high treshold in mixed mode - */ - bool failsave = false ; - - }actor; - + void mixedTemporalActionOff(float measurment); + + void mixedTemporalActionOn(float measurment); + + void hourAction(int hour); + + void mixedHourAction(int hour, float measurment); + + void minuteAction(int minute); + + void mixedMinuteAction(int minute, float measurment); + + void hourMinuteAction(int hour, int minute); + + void mixedHourMinuteAction(int hour, int minute, float measurment); + + bool config(); + + void printConf(); + + /** + * pin Number value + * the only available pin that can be used + * to run a small actor is pin 15 + */ + const int pin = 15; // pin for the actor + + // private: + + /** + * the Actor's state + */ + struct state { + + /** + * actif flag + * set to 1 when using an actor + * set to 0 otherwise + */ + bool actif = 0; + + /** + * temporal flag + * set to 1 to declare a temporal actor + * set to 0 otherwise + */ + bool temporal = 0; + + /** + * inverted flag + * set to 1 to declare an inverted actor + * set to 0 to declare a normal actor + */ + bool inverted = 0; + + /** + * "type":["temperature","hour"] + * + * the primary type is related to the sensor's type (type[0]) + * + * if both types are present and valid, the actor is a mixedActor + */ + String primaryType = ""; + + /** + * "type":["temperature","hour"] + * + * the secondary type if present is hour or minute or hourMinute (type[1]) + * + * if both types are present and valid, the actor is a mixedActor + */ + String secondaryType = ""; + + /** + * "low":[20,5000,18,10] + * + * rangeLow : this is the minimum at which the actor becomes actif (low[0]) + * + */ + int rangeLow = 0; + + /** + * "low":[20,5000,18,10] + * + * timeLow : this is the time the actor is off in temporal mode (low[1]) + *(ms) + */ + unsigned long timeLow = 0; + + /** + * "low":[20,5000,18,10] + * + * hour low :this is the hour when to turn off the actor in + *temporal/hour(hourMinute) mode (low[2] ) + */ + int hourLow = 0; + + /** + * "low":[20,5000,18,10] + * + * minute low :this is the minute when to turn off the actor in + *temporal/minute(hourMinute) mode (low[3]) + */ + int minuteLow = 0; + + /** + * "high":[30,2000,17,1] + * + * rangeHigh : this is the maximum at which the actor becomes + *inactif(high[0]) + */ + int rangeHigh = 0; + + /** + * "high":[30,2000,17,1] + * + * timeHigh : this is the time the actor is on in temporal mode(high[1]) + *(ms) + */ + unsigned long timeHigh = 0; + + /** + * "high":[30,2000,17,1] + * + * hourHigh : this is the hour when to turn on the actor in + *temporal/hour(hourMinute) mode(high[2]) + */ + int hourHigh = 0; + + /** + * "high":[30,2000,17,1] + * + * minuteHigh : this is the minute when to turn on the actor in + *temporal/minute(hourMinute) mode (high[3]) + */ + int minuteHigh = 0; + + /** + * actifTime : period of Time spent actif , used in Temporal mode + * in ms + */ + unsigned long actifTime = 0; + + /** + * inactifTime : period of Time spent inactif , used in Temporal mode + * in ms + */ + unsigned long inactifTime = 0; + + /** + * failsave : Switches of the actor if a metric rises above high treshold + *in mixed mode + */ + bool failsave = false; + + } actor; }; #endif diff --git a/src/CoolBoardLed.cpp b/src/CoolBoardLed.cpp index 73f4c231..a20b63d4 100644 --- a/src/CoolBoardLed.cpp +++ b/src/CoolBoardLed.cpp @@ -1,438 +1,395 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ -#include "FS.h" #include "Arduino.h" +#include "FS.h" -#include -#include "CoolBoardLed.h" #include "ArduinoJson.h" +#include "CoolBoardLed.h" +#include #define DEBUG 0 +/** + * CoolBoardLed::fade ( Red , Green , Blue, Time in seconds ): + * fade animation: Fade In over T(seconds) + * Fade Out over T(seconds) + */ +void CoolBoardLed::fade(int R, int G, int B, float T) { + +#if DEBUG == 1 + Serial.println(F("Entering CoolBoardLed.fade()")); + Serial.println(); + Serial.print(F("R : ")); + Serial.println(R); + Serial.print(F("G : ")); + Serial.println(G); + Serial.print(F("B : ")); + Serial.println(B); + Serial.print(F("Time : ")); + Serial.println(T); + Serial.println(); + +#endif + if (this->ledActive == 1) { + for (int k = 0; k < 1000; k++) { + neoPixelLed->SetPixelColor( + 0, RgbColor(k * R / 1000, k * G / 1000, k * B / 1000)); + neoPixelLed->Show(); + delay(T); + } + + for (int k = 1000; k >= 0; k--) { + neoPixelLed->SetPixelColor( + 0, RgbColor(k * R / 1000, k * G / 1000, k * B / 1000)); + neoPixelLed->Show(); + delay(T); + } + } +} /** -* CoolBoardLed::fade ( Red , Green , Blue, Time in seconds ): -* fade animation: Fade In over T(seconds) -* Fade Out over T(seconds) +* CoolBoardLed::blink( Red , Green , Blue , Time in seconds ): +* Blink animation: Led On for T seconds + Led off */ -void CoolBoardLed::fade(int R, int G, int B, float T) -{ +void CoolBoardLed::blink(int R, int G, int B, float T) { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardLed.fade()") ); - Serial.println(); - Serial.print( F("R : ") ); - Serial.println(R); - Serial.print( F("G : ") ); - Serial.println(G); - Serial.print( F("B : ") ); - Serial.println(B); - Serial.print( F("Time : ") ); - Serial.println(T); - Serial.println(); - -#endif - if(this->ledActive == 1 ) - { - for (int k = 0; k < 1000; k++) - { - neoPixelLed->SetPixelColor(0, RgbColor(k * R / 1000, k * G / 1000, k * B / 1000)); - neoPixelLed->Show(); - delay(T); - } - - for (int k = 1000; k >= 0; k--) - { - neoPixelLed->SetPixelColor(0, RgbColor(k * R / 1000, k * G / 1000, k * B / 1000)); - neoPixelLed->Show(); - delay(T); - } - } + Serial.println(F("Entering CoolBoardLed.blink()")); + Serial.println(); + Serial.print(F("R : ")); + Serial.println(R); + Serial.print(F("G : ")); + Serial.println(G); + Serial.print(F("B : ")); + Serial.println(B); + Serial.print(F("Time :")); + Serial.println(T); + Serial.println(); + +#endif + + if (this->ledActive == 1) { + neoPixelLed->SetPixelColor(0, RgbColor(R, G, B)); + neoPixelLed->Show(); + delay(T * 1000); + neoPixelLed->SetPixelColor(0, RgbColor(0, 0, 0)); + neoPixelLed->Show(); + } } /** -* CoolBoardLed::blink( Red , Green , Blue , Time in seconds ): -* Blink animation: Led On for T seconds - Led off -*/ -void CoolBoardLed::blink(int R, int G, int B, float T) -{ + * CoolBoardLed::fadeIn(Red , Green , Blue , Time in seconds) + * Fade In animation: gradual increase over T(seconds) + */ +void CoolBoardLed::fadeIn(int R, int G, int B, float T) { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardLed.blink()")); - Serial.println(); - Serial.print( F("R : ")); - Serial.println(R); - Serial.print( F("G : ") ); - Serial.println(G); - Serial.print( F("B : ") ); - Serial.println(B); - Serial.print( F("Time :") ); - Serial.println(T); - Serial.println(); - -#endif - - if(this->ledActive == 1 ) - { - neoPixelLed->SetPixelColor(0, RgbColor(R, G, B)); - neoPixelLed->Show(); - delay(T*1000); - neoPixelLed->SetPixelColor(0, RgbColor(0, 0, 0)); - neoPixelLed->Show(); - } + Serial.println(F("Entering CoolBoardLed.fadeIn()")); + Serial.println(); + Serial.print(F("R : ")); + Serial.println(R); + Serial.print(F("G : ")); + Serial.println(G); + Serial.print(F("B : ")); + Serial.println(B); + Serial.print(F("Time :")); + Serial.println(T); + Serial.println(); + +#endif + + if (this->ledActive == 1) { + for (int k = 0; k < 1000; k++) { + neoPixelLed->SetPixelColor( + 0, RgbColor(k * R / 1000, k * G / 1000, k * B / 1000)); + neoPixelLed->Show(); + delay(T); + } + } } /** -* CoolBoardLed::fadeIn(Red , Green , Blue , Time in seconds) -* Fade In animation: gradual increase over T(seconds) -*/ -void CoolBoardLed::fadeIn(int R, int G, int B, float T) -{ + * CoolBoardLed::fadeOut( Red , Green , Blue , Time in seconds) + * Fade Out animation: gradual decrease over T(seconds) + */ +void CoolBoardLed::fadeOut(int R, int G, int B, float T) { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardLed.fadeIn()") ); - Serial.println(); - Serial.print( F("R : ") ); - Serial.println(R); - Serial.print( F("G : ") ); - Serial.println(G); - Serial.print( F("B : ") ); - Serial.println(B); - Serial.print( F("Time :") ); - Serial.println(T); - Serial.println(); - -#endif - - if(this->ledActive == 1 ) - { - for (int k = 0; k < 1000; k++) - { - neoPixelLed->SetPixelColor(0, RgbColor(k * R / 1000, k * G / 1000, k * B / 1000)); - neoPixelLed->Show(); - delay(T); - } - } + Serial.println(F("Entering CoolBoardLed.fadeOut()")); + Serial.println(); + Serial.print(F("R : ")); + Serial.println(R); + Serial.print(F("G : ")); + Serial.println(G); + Serial.print(F("B : ")); + Serial.println(B); + Serial.print(F("Time :")); + Serial.println(T); + Serial.println(); +#endif + + if (this->ledActive == 1) { + for (int k = 1000; k >= 0; k--) { + neoPixelLed->SetPixelColor( + 0, RgbColor(k * R / 1000, k * G / 1000, k * B / 1000)); + neoPixelLed->Show(); + delay(T); + } + } } /** -* CoolBoardLed::fadeOut( Red , Green , Blue , Time in seconds) -* Fade Out animation: gradual decrease over T(seconds) -*/ -void CoolBoardLed::fadeOut(int R, int G, int B, float T) -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoardLed.fadeOut()" ) ); - Serial.println(); - Serial.print( F("R : ") ); - Serial.println(R); - Serial.print( F("G : ") ); - Serial.println(G); - Serial.print( F("B : ") ); - Serial.println(B); - Serial.print( F("Time :") ); - Serial.println(T); - Serial.println(); - -#endif - - if(this->ledActive == 1 ) - { - for (int k = 1000; k >= 0; k--) - { - neoPixelLed->SetPixelColor(0, RgbColor(k * R / 1000, k * G / 1000, k * B / 1000)); - neoPixelLed->Show(); - delay(T); - } - } + * CoolBoardLed::strobe(Red , Green , Blue , Time in seconds) + * Strobe animation: blinks over T(seconds) + */ +void CoolBoardLed::strobe(int R, int G, int B, float T) { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolBoardLed.strobe()")); + Serial.println(); + Serial.print(F("R : ")); + Serial.println(R); + Serial.print(F("G: ")); + Serial.println(G); + Serial.print(F("B : ")); + Serial.println(B); + Serial.print(F("Time :")); + Serial.println(T); + Serial.println(); + +#endif + + if (this->ledActive == 1) { + for (int k = 1000; k >= 0; k--) { + neoPixelLed->SetPixelColor(0, RgbColor(R, G, B)); + neoPixelLed->Show(); + delay(T); + neoPixelLed->SetPixelColor(0, RgbColor(0, 0, 0)); + neoPixelLed->Show(); + delay(T); + } + } } /** -* CoolBoardLed::strobe(Red , Green , Blue , Time in seconds) -* Strobe animation: blinks over T(seconds) -*/ -void CoolBoardLed::strobe(int R, int G, int B, float T) -{ + * CoolBoardLed::end() : + * this method is provided to delete the dynamically created neoPixelLed + */ +void CoolBoardLed::end() { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardLed.strobe()") ); - Serial.println(); - Serial.print( F("R : ") ); - Serial.println(R); - Serial.print( F("G: ") ); - Serial.println(G); - Serial.print( F("B : ") ); - Serial.println(B); - Serial.print( F("Time :") ); - Serial.println(T); - Serial.println(); - -#endif - - if(this->ledActive == 1 ) - { - for (int k = 1000; k >= 0; k--) - { - neoPixelLed->SetPixelColor(0, RgbColor(R, G, B)); - neoPixelLed->Show(); - delay(T); - neoPixelLed->SetPixelColor(0, RgbColor(0, 0, 0)); - neoPixelLed->Show(); - delay(T); - } - } + Serial.println(F("Entering CoolBoardLed.end()")); + +#endif + + delete neoPixelLed; } /** -* CoolBoardLed::end() : -* this method is provided to delete the dynamically created neoPixelLed -*/ -void CoolBoardLed::end() -{ + * CoolBoardLed::begin(): + * This method is provided to start the Led Object + * by setting the correct pin and creating a dynamic + * neoPixelBus + */ +void CoolBoardLed::begin() { + +#if DEBUG == 1 -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoardLed.end()") ); + Serial.println(F("Entering CoolBoardLed.begin() ")); #endif - - delete neoPixelLed; + yield(); + if (this->ledActive == 1) { + pinMode(5, OUTPUT); + digitalWrite(5, HIGH); + neoPixelLed = new NeoPixelBus(1, 2); + neoPixelLed->Begin(); + neoPixelLed->Show(); + } } +/** + * CoolBoardLed::write(Red,Green,Blue): + * This method is provided to set the + * Color of the Led + */ +void CoolBoardLed::write(int R, int G, int B) { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolBoardLed.write()")); + Serial.println(); + Serial.print(F("R : ")); + Serial.println(R); + Serial.print(F("G : ")); + Serial.println(G); + Serial.print(F("B : ")); + Serial.println(B); + Serial.println(); + +#endif + + if (this->ledActive == 1) { + neoPixelLed->SetPixelColor(0, RgbColor(R, G, B)); + neoPixelLed->Show(); + } +} /** -* CoolBoardLed::begin(): -* This method is provided to start the Led Object -* by setting the correct pin and creating a dynamic -* neoPixelBus -*/ -void CoolBoardLed::begin( ) -{ + * CoolBoardLed::config(): + * This method is provided to configure + * the Led Object : -ledActive=0 : deactivated + * -ledActive=1 : activated + * \return true if the configuration done, + * false otherwise + */ +bool CoolBoardLed::config() { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardLed.begin() ") ); + Serial.println(F("Entering CoolBoardLed.config()")); + Serial.println(); #endif - yield(); - if(this->ledActive == 1 ) - { - pinMode(5,OUTPUT); - digitalWrite(5,HIGH); - neoPixelLed = new NeoPixelBus(1,2); - neoPixelLed->Begin(); - neoPixelLed->Show(); - } + File coolBoardLedConfig = SPIFFS.open("/coolBoardLedConfig.json", "r"); -} + if (!coolBoardLedConfig) { -/** -* CoolBoardLed::write(Red,Green,Blue): -* This method is provided to set the -* Color of the Led -*/ -void CoolBoardLed::write(int R, int G, int B) -{ +#if DEBUG == 1 + + Serial.println(F("failed to read /coolBoardLedConfig.json")); + Serial.println(); + +#endif + + return (false); + } else { + size_t size = coolBoardLedConfig.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + coolBoardLedConfig.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardLed.write()") ); - Serial.println(); - Serial.print( F("R : ") ); - Serial.println(R); - Serial.print( F("G : ") ); - Serial.println(G); - Serial.print( F("B : ") ); - Serial.println(B); - Serial.println(); + Serial.println(F("failed to parse json")); + Serial.println(); #endif - if(this->ledActive == 1 ) - { - neoPixelLed->SetPixelColor(0, RgbColor(R, G, B)); - neoPixelLed->Show(); - } + return (false); + } else { -} +#if DEBUG == 1 -/** -* CoolBoardLed::config(): -* This method is provided to configure -* the Led Object : -ledActive=0 : deactivated -* -ledActive=1 : activated -* \return true if the configuration done, -* false otherwise -*/ -bool CoolBoardLed::config() -{ + Serial.println(F("read configuration file : ")); + json.printTo(Serial); + Serial.println(); -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoardLed.config()") ); - Serial.println(); + Serial.print(F("jsonBuffer size :")); + Serial.println(jsonBuffer.size()); + Serial.println(); #endif - - File coolBoardLedConfig = SPIFFS.open("/coolBoardLedConfig.json", "r"); - - if (!coolBoardLedConfig) - { - - #if DEBUG == 1 - - Serial.println( F("failed to read /coolBoardLedConfig.json") ); - Serial.println(); - - #endif - - return(false); - } - else - { - size_t size = coolBoardLedConfig.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - coolBoardLedConfig.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json") ); - Serial.println(); - - #endif - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("read configuration file : ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size :")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - #endif - - if(json["ledActive"].success() ) - { - this->ledActive = json["ledActive"]; - } - else - { - this->ledActive=this->ledActive; - } - - json["ledActive"]=this->ledActive; - coolBoardLedConfig.close(); - - coolBoardLedConfig = SPIFFS.open("/coolBoardLedConfig.json", "w"); - if(!coolBoardLedConfig) - { - - #if DEBUG == 1 - - Serial.println( F("failed to write to /coolBoardLedConfig.json") ); - Serial.println(); - - #endif - - return(false); - } - - json.printTo(coolBoardLedConfig); - coolBoardLedConfig.close(); - - #if DEBUG == 1 - - Serial.println( F("saved Led Config is : ") ); - json.printTo(Serial); - Serial.println(); - - #endif - - return(true); - } - } - -} -/** -* CoolBoardLed::printConf(): -* This method is provided to print the -* Led Object Configuration to the Serial -* Monitor -*/ -void CoolBoardLed::printConf() -{ + if (json["ledActive"].success()) { + this->ledActive = json["ledActive"]; + } else { + this->ledActive = this->ledActive; + } + + json["ledActive"] = this->ledActive; + coolBoardLedConfig.close(); + + coolBoardLedConfig = SPIFFS.open("/coolBoardLedConfig.json", "w"); + if (!coolBoardLedConfig) { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering CoolBoardLed.printConf()") ); - Serial.println(); + Serial.println(F("failed to write to /coolBoardLedConfig.json")); + Serial.println(); #endif - Serial.println("Led Configuration"); + return (false); + } + + json.printTo(coolBoardLedConfig); + coolBoardLedConfig.close(); - Serial.print("ledActive : "); - Serial.println(ledActive); +#if DEBUG == 1 - Serial.println(); + Serial.println(F("saved Led Config is : ")); + json.printTo(Serial); + Serial.println(); + +#endif + + return (true); + } + } } /** -* CoolBoardLed::activate(): -* This method is provided to activate the -* Led Object without the configuration -* file -*/ -void CoolBoardLed::activate() -{ + * CoolBoardLed::printConf(): + * This method is provided to print the + * Led Object Configuration to the Serial + * Monitor + */ +void CoolBoardLed::printConf() { - this->ledActive=1; +#if DEBUG == 1 + Serial.println(F("Entering CoolBoardLed.printConf()")); + Serial.println(); + +#endif + + Serial.println("Led Configuration"); + + Serial.print("ledActive : "); + Serial.println(ledActive); + + Serial.println(); } +/** + * CoolBoardLed::activate(): + * This method is provided to activate the + * Led Object without the configuration + * file + */ +void CoolBoardLed::activate() { this->ledActive = 1; } diff --git a/src/CoolBoardLed.h b/src/CoolBoardLed.h index 6b1cb4e5..6ef1c0bf 100644 --- a/src/CoolBoardLed.h +++ b/src/CoolBoardLed.h @@ -1,84 +1,75 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef CoolOnBoardLed_H #define CoolOnBoardLed_H -#include"Arduino.h" +#include "Arduino.h" #include - /** -* \class CoolBoardLed -* \brief This class handles the led in the Sensor Board -*/ -class CoolBoardLed - -{ + * \class CoolBoardLed + * \brief This class handles the led in the Sensor Board + */ +class CoolBoardLed { public: + void begin(); - void begin(); - - - void write(int R, int G, int B); - - void end(); //delete the dynamic led; + void write(int R, int G, int B); - bool config(); + void end(); // delete the dynamic led; - void activate(); + bool config(); - void printConf(); + void activate(); + void printConf(); - //Neo Pixel Led methods : - void fade(int R, int G, int B, float T); + // Neo Pixel Led methods : + void fade(int R, int G, int B, float T); - void blink(int R, int G, int B, float T); + void blink(int R, int G, int B, float T); - void fadeIn(int R, int G, int B, float T); - - void fadeOut(int R, int G, int B, float T); - - void strobe(int R, int G, int B, float T); + void fadeIn(int R, int G, int B, float T); + void fadeOut(int R, int G, int B, float T); + void strobe(int R, int G, int B, float T); private: - - /** - * NeoPixel Led Instance - */ - NeoPixelBus* neoPixelLed = NULL; //template instance must be dynamic - - /** - * ledActive flag - * set to 1 if you want LED animation - */ - bool ledActive=0; - + /** + * NeoPixel Led Instance + */ + NeoPixelBus *neoPixelLed = + NULL; // template instance must be dynamic + + /** + * ledActive flag + * set to 1 if you want LED animation + */ + bool ledActive = 0; }; #endif diff --git a/src/CoolBoardSensors.cpp b/src/CoolBoardSensors.cpp index 6a71fd06..622521cf 100644 --- a/src/CoolBoardSensors.cpp +++ b/src/CoolBoardSensors.cpp @@ -1,662 +1,573 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ - -#include "FS.h" +#include "CoolBoardSensors.h" #include "Arduino.h" -#include #include "ArduinoJson.h" -#include "CoolBoardSensors.h" - +#include "FS.h" +#include #define DEBUG 0 - /** -* CoolBoardSensors::CoolBoardSensors(): -* This Constructor is provided to -* init the different used pins -*/ -CoolBoardSensors::CoolBoardSensors() -{ + * CoolBoardSensors::CoolBoardSensors(): + * This Constructor is provided to + * init the different used pins + */ +CoolBoardSensors::CoolBoardSensors() { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardSensors Constructor") ); - Serial.println(); + Serial.println(F("Entering CoolBoardSensors Constructor")); + Serial.println(); #endif - - pinMode(AnMplex, OUTPUT); //Declare Analog Multiplexer OUTPUT - pinMode(EnMoisture, OUTPUT); //Declare Moisture enable Pin - digitalWrite(EnMoisture, HIGH); //Prevent Wearing on the soil moisture fork - + pinMode(AnMplex, OUTPUT); // Declare Analog Multiplexer OUTPUT + pinMode(EnMoisture, OUTPUT); // Declare Moisture enable Pin + digitalWrite(EnMoisture, HIGH); // Prevent Wearing on the soil moisture fork } - /** -* CoolBoardSensors::allActive(): -* This method is provided to allow -* activation of all the sensor board sensors -* without passing by the configuration file/method -*/ -void CoolBoardSensors::allActive() -{ + * CoolBoardSensors::allActive(): + * This method is provided to allow + * activation of all the sensor board sensors + * without passing by the configuration file/method + */ +void CoolBoardSensors::allActive() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering CoolBoardSensors.allActive()") ); - Serial.println(); + Serial.println(F("Entering CoolBoardSensors.allActive()")); + Serial.println(); #endif - - this->lightDataActive.visible=1; - this->lightDataActive.ir=1; - this->lightDataActive.uv=1; - - this->airDataActive.temperature=1; - this->airDataActive.humidity=1; - this->airDataActive.pressure=1; + this->lightDataActive.visible = 1; + this->lightDataActive.ir = 1; + this->lightDataActive.uv = 1; - this->vbatActive=1; - - this->soilMoistureActive=1; - + this->airDataActive.temperature = 1; + this->airDataActive.humidity = 1; + this->airDataActive.pressure = 1; + this->vbatActive = 1; + this->soilMoistureActive = 1; } - /** -* CoolBoardSensors::begin(): -* This method is provided to start the -* sensors that are on the sensor board -*/ -void CoolBoardSensors::begin() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoardSensors.begin()") ); - Serial.println(); + * CoolBoardSensors::begin(): + * This method is provided to start the + * sensors that are on the sensor board + */ +void CoolBoardSensors::begin() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolBoardSensors.begin()")); + Serial.println(); #endif - while (!lightSensor.Begin()) - { - - #if DEBUG == 1 + while (!lightSensor.Begin()) { - Serial.println( F("Si1145 is not ready! 1 second") ); +#if DEBUG == 1 - #endif + Serial.println(F("Si1145 is not ready! 1 second")); - delay(1000); - } - - this->setEnvSensorSettings(); +#endif + + delay(1000); + } + + this->setEnvSensorSettings(); + + delay(100); // Make sure sensor had enough time to turn on. BME280 requires + // 2ms to start up. - delay(100); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. + this->envSensor.begin(); - this->envSensor.begin(); + delay(1000); // Make sure sensor had enough time to turn on. BME280 requires + // 2ms to start up. - delay(1000); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. +#if DEBUG == 1 -#if DEBUG == 1 - - Serial.print( F("BME280 begin answer is :") ); - Serial.println(envSensor.begin(), HEX); - Serial.println(); + Serial.print(F("BME280 begin answer is :")); + Serial.println(envSensor.begin(), HEX); + Serial.println(); #endif #if DEBUG == 0 - Serial.println( F("Onboard Sensors : OK")); - Serial.println(); + Serial.println(F("Onboard Sensors : OK")); + Serial.println(); #endif - } /** -* CoolBoardSensors::end(): -* This method is provided to end -* the sensors on the sensor board -*/ -void CoolBoardSensors::end() -{ + * CoolBoardSensors::end(): + * This method is provided to end + * the sensors on the sensor board + */ +void CoolBoardSensors::end() { -#if DEBUG == 1 - Serial.println( F("Entering CoolBoardSensors.end()") ); - Serial.println(); +#if DEBUG == 1 + Serial.println(F("Entering CoolBoardSensors.end()")); + Serial.println(); #endif - lightSensor.DeInit(); - + lightSensor.DeInit(); } /** -* CoolBoardSensors::read(): -* This method is provided to return the -* data read by the sensor board -* -* \return a json string containing the -* sensors data -**/ -String CoolBoardSensors::read() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolBoardSensors.read()") ); - Serial.println(); + * CoolBoardSensors::read(): + * This method is provided to return the + * data read by the sensor board + * + * \return a json string containing the + * sensors data + **/ +String CoolBoardSensors::read() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolBoardSensors.read()")); + Serial.println(); #endif #if DEBUG == 0 - Serial.println( F("Reading Sensors...")); + Serial.println(F("Reading Sensors...")); #endif - String data; - DynamicJsonBuffer jsonBuffer ; - JsonObject& root = jsonBuffer.createObject(); - - delay(100); - //light data - if(lightDataActive.visible) - { - //SI1145 Response Reg Value when VIS Overflow - if(lightSensor.ReadResponseReg()== CoolSI114X_VIS_OVERFLOW ) - { - root["visibleLight"] ="overflow"; - - //send NoP Command to SI1145 to clear overflow value - lightSensor.WriteParamData(CoolSI114X_COMMAND,CoolSI114X_NOP); - } - else - { - - root["visibleLight"] =lightSensor.ReadVisible() ; - - } - } - - if(lightDataActive.ir) - { - //SI1145 Response Reg Value when IR Overflow - if(lightSensor.ReadResponseReg()==CoolSI114X_IR_OVERFLOW ) - { - root["infraRed"] ="overflow"; - - //send NoP Command to SI1145 to clear overflow value - lightSensor.WriteParamData(CoolSI114X_COMMAND,CoolSI114X_NOP); - } - else - { - root["infraRed"] = lightSensor.ReadIR(); - } - } - - if(lightDataActive.uv) - { - //SI1145 Response Reg Value when UV Overflow - if(lightSensor.ReadResponseReg()==CoolSI114X_UV_OVERFLOW) - { - root["ultraViolet"] ="overflow"; - - //send NoP Command to SI1145 to clear overflow value - lightSensor.WriteParamData(CoolSI114X_COMMAND,CoolSI114X_NOP); - } - else - { - float tempUV = (float)lightSensor.ReadUV()/100 ; - root["ultraViolet"] = tempUV; - } - } - - //BME280 data - if(airDataActive.temperature) - { - - //wait for BME280 to finish data conversion( status reg bit3 ==0) - while((envSensor.readRegister(BME280_STAT_REG) & 0x10 ) != 0 ) - { - yield(); - } - root["Temperature"]=envSensor.readTempC(); - } - if(airDataActive.pressure) - { - - //wait for BME280 to finish data conversion( status reg bit3 ==0) - while((envSensor.readRegister(BME280_STAT_REG) & 0x10 ) != 0 ) - { - yield(); - } - root["Pressure"] =envSensor.readFloatPressure(); - } - - - if(airDataActive.humidity) - { - - //wait for BME280 to finish data conversion( status reg bit3 ==0) - while((envSensor.readRegister(BME280_STAT_REG) & 0x10 ) != 0 ) - { - yield(); - } - root["Humidity"] =envSensor.readFloatHumidity() ; - } - - - - //Vbat - if(vbatActive) - { - root["Vbat"]=this->readVBat(); - } - - //earth Moisture - if(soilMoistureActive) - { - root["soilMoisture"]=this->readMoisture(); - } - - - root.printTo(data); + String data; + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.createObject(); + + delay(100); + // light data + if (lightDataActive.visible) { + // SI1145 Response Reg Value when VIS Overflow + if (lightSensor.ReadResponseReg() == CoolSI114X_VIS_OVERFLOW) { + root["visibleLight"] = "overflow"; + + // send NoP Command to SI1145 to clear overflow value + lightSensor.WriteParamData(CoolSI114X_COMMAND, CoolSI114X_NOP); + } else { + + root["visibleLight"] = lightSensor.ReadVisible(); + } + } + + if (lightDataActive.ir) { + // SI1145 Response Reg Value when IR Overflow + if (lightSensor.ReadResponseReg() == CoolSI114X_IR_OVERFLOW) { + root["infraRed"] = "overflow"; + + // send NoP Command to SI1145 to clear overflow value + lightSensor.WriteParamData(CoolSI114X_COMMAND, CoolSI114X_NOP); + } else { + root["infraRed"] = lightSensor.ReadIR(); + } + } + + if (lightDataActive.uv) { + // SI1145 Response Reg Value when UV Overflow + if (lightSensor.ReadResponseReg() == CoolSI114X_UV_OVERFLOW) { + root["ultraViolet"] = "overflow"; + + // send NoP Command to SI1145 to clear overflow value + lightSensor.WriteParamData(CoolSI114X_COMMAND, CoolSI114X_NOP); + } else { + float tempUV = (float)lightSensor.ReadUV() / 100; + root["ultraViolet"] = tempUV; + } + } + + // BME280 data + if (airDataActive.temperature) { + + // wait for BME280 to finish data conversion( status reg bit3 ==0) + while ((envSensor.readRegister(BME280_STAT_REG) & 0x10) != 0) { + yield(); + } + root["Temperature"] = envSensor.readTempC(); + } + if (airDataActive.pressure) { + + // wait for BME280 to finish data conversion( status reg bit3 ==0) + while ((envSensor.readRegister(BME280_STAT_REG) & 0x10) != 0) { + yield(); + } + root["Pressure"] = envSensor.readFloatPressure(); + } + + if (airDataActive.humidity) { + + // wait for BME280 to finish data conversion( status reg bit3 ==0) + while ((envSensor.readRegister(BME280_STAT_REG) & 0x10) != 0) { + yield(); + } + root["Humidity"] = envSensor.readFloatHumidity(); + } + + // Vbat + if (vbatActive) { + root["Vbat"] = this->readVBat(); + } + + // earth Moisture + if (soilMoistureActive) { + root["soilMoisture"] = this->readMoisture(); + } + + root.printTo(data); #if DEBUG == 1 - Serial.println( F("CoolBoardSensors data is :") ); - root.printTo(Serial); - Serial.println(); - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); + Serial.println(F("CoolBoardSensors data is :")); + root.printTo(Serial); + Serial.println(); + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); #endif - return(data); - - + return (data); } /** -* CoolBoardSensors::config(): -* This method is provided to configure the -* sensor board : -activate 1 -* -deactivate 0 -* -* \return true if configuration is successful, -* false otherwise -*/ -bool CoolBoardSensors::config() -{ + * CoolBoardSensors::config(): + * This method is provided to configure the + * sensor board : -activate 1 + * -deactivate 0 + * + * \return true if configuration is successful, + * false otherwise + */ +bool CoolBoardSensors::config() { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardSensors.config()") ); - Serial.println(); + Serial.println(F("Entering CoolBoardSensors.config()")); + Serial.println(); #endif #if DEBUG == 0 - Serial.println( F("Reading Sensor Configuration...")); + Serial.println(F("Reading Sensor Configuration...")); #endif - //read config file - //update data - File coolBoardSensorsConfig = SPIFFS.open("/coolBoardSensorsConfig.json", "r"); - - if (!coolBoardSensorsConfig) - { - - Serial.println( F("failed to read /coolBoardSensorsConfig.json") ); - Serial.println(); - - return(false); - } - else - { - size_t size = coolBoardSensorsConfig.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - coolBoardSensorsConfig.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - Serial.println( F("failed to parse coolBoardSensorsConfig json") ); - Serial.println(); - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("Configuration Json is :") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - #endif - - if(json["BME280"]["temperature"].success() ) - { - this->airDataActive.temperature=json["BME280"]["temperature"]; - } - else - { - this->airDataActive.temperature=this->airDataActive.temperature; - } - json["BME280"]["temperature"]=this->airDataActive.temperature; - - - if(json["BME280"]["humidity"].success() ) - { - - this->airDataActive.humidity=json["BME280"]["humidity"]; - } - else - { - this->airDataActive.humidity=this->airDataActive.humidity; - } - json["BME280"]["humidity"]=this->airDataActive.humidity; - - - if(json["BME280"]["pressure"].success() ) - { - this->airDataActive.pressure=json["BME280"]["pressure"]; - } - else - { - this->airDataActive.pressure=this->airDataActive.pressure; - } - json["BME280"]["pressure"]=this->airDataActive.pressure; - - - if(json["SI114X"]["visible"].success() ) - { - this->lightDataActive.visible=json["SI114X"]["visible"]; - } - else - { - this->lightDataActive.visible=this->lightDataActive.visible; - } - json["SI114X"]["visible"]=this->lightDataActive.visible; - - - if(json["SI114X"]["ir"].success() ) - { - this->lightDataActive.ir=json["SI114X"]["ir"]; - } - else - { - this->lightDataActive.ir=this->lightDataActive.ir; - } - json["SI114X"]["ir"]=this->lightDataActive.ir; - - - if(json["SI114X"]["uv"].success() ) - { - this->lightDataActive.uv=json["SI114X"]["uv"]; - } - else - { - this->lightDataActive.uv=this->lightDataActive.uv; - } - json["SI114X"]["uv"]=this->lightDataActive.uv; - - - if(json["vbat"].success() ) - { - this->vbatActive=json["vbat"]; - } - else - { - this->vbatActive=this->vbatActive; - } - json["vbat"]=this->vbatActive; - - - if(json["soilMoisture"].success() ) - { - this->soilMoistureActive= json["soilMoisture"]; - } - else - { - this->soilMoistureActive=this->soilMoistureActive; - } - json["soilMoisture"]=this->soilMoistureActive; - - coolBoardSensorsConfig.close(); - coolBoardSensorsConfig = SPIFFS.open("/coolBoardSensorsConfig.json", "w"); - if(!coolBoardSensorsConfig) - { - - Serial.println( F("failed to write to /coolBoardSensorsConfig.json") ); - Serial.println(); - - return(false); - } - - json.printTo(coolBoardSensorsConfig); - coolBoardSensorsConfig.close(); - - #if DEBUG == 1 - - Serial.println( F("Saved Configuration Json is : ") ); - json.printTo(Serial); - Serial.println(); - - #endif - - #if DEBUG == 0 - Serial.println( F("Configuration loaded : OK")); - #endif - - return(true); - } - } + // read config file + // update data + File coolBoardSensorsConfig = + SPIFFS.open("/coolBoardSensorsConfig.json", "r"); -} + if (!coolBoardSensorsConfig) { + + Serial.println(F("failed to read /coolBoardSensorsConfig.json")); + Serial.println(); + + return (false); + } else { + size_t size = coolBoardSensorsConfig.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + coolBoardSensorsConfig.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + Serial.println(F("failed to parse coolBoardSensorsConfig json")); + Serial.println(); + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("Configuration Json is :")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + if (json["BME280"]["temperature"].success()) { + this->airDataActive.temperature = json["BME280"]["temperature"]; + } else { + this->airDataActive.temperature = this->airDataActive.temperature; + } + json["BME280"]["temperature"] = this->airDataActive.temperature; + + if (json["BME280"]["humidity"].success()) { + + this->airDataActive.humidity = json["BME280"]["humidity"]; + } else { + this->airDataActive.humidity = this->airDataActive.humidity; + } + json["BME280"]["humidity"] = this->airDataActive.humidity; + + if (json["BME280"]["pressure"].success()) { + this->airDataActive.pressure = json["BME280"]["pressure"]; + } else { + this->airDataActive.pressure = this->airDataActive.pressure; + } + json["BME280"]["pressure"] = this->airDataActive.pressure; + + if (json["SI114X"]["visible"].success()) { + this->lightDataActive.visible = json["SI114X"]["visible"]; + } else { + this->lightDataActive.visible = this->lightDataActive.visible; + } + json["SI114X"]["visible"] = this->lightDataActive.visible; + + if (json["SI114X"]["ir"].success()) { + this->lightDataActive.ir = json["SI114X"]["ir"]; + } else { + this->lightDataActive.ir = this->lightDataActive.ir; + } + json["SI114X"]["ir"] = this->lightDataActive.ir; + + if (json["SI114X"]["uv"].success()) { + this->lightDataActive.uv = json["SI114X"]["uv"]; + } else { + this->lightDataActive.uv = this->lightDataActive.uv; + } + json["SI114X"]["uv"] = this->lightDataActive.uv; + + if (json["vbat"].success()) { + this->vbatActive = json["vbat"]; + } else { + this->vbatActive = this->vbatActive; + } + json["vbat"] = this->vbatActive; + + if (json["soilMoisture"].success()) { + this->soilMoistureActive = json["soilMoisture"]; + } else { + this->soilMoistureActive = this->soilMoistureActive; + } + json["soilMoisture"] = this->soilMoistureActive; + + coolBoardSensorsConfig.close(); + coolBoardSensorsConfig = SPIFFS.open("/coolBoardSensorsConfig.json", "w"); + if (!coolBoardSensorsConfig) { + + Serial.println(F("failed to write to /coolBoardSensorsConfig.json")); + Serial.println(); + + return (false); + } + + json.printTo(coolBoardSensorsConfig); + coolBoardSensorsConfig.close(); + +#if DEBUG == 1 + + Serial.println(F("Saved Configuration Json is : ")); + json.printTo(Serial); + Serial.println(); + +#endif + +#if DEBUG == 0 + Serial.println(F("Configuration loaded : OK")); +#endif + + return (true); + } + } +} /** -* CoolBoardSensors::printConf(): -* This method is provided to print the -* configuration to the Serial Monitor -*/ -void CoolBoardSensors::printConf() -{ + * CoolBoardSensors::printConf(): + * This method is provided to print the + * configuration to the Serial Monitor + */ +void CoolBoardSensors::printConf() { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardSensors.printConf()") ); - Serial.println(); + Serial.println(F("Entering CoolBoardSensors.printConf()")); + Serial.println(); #endif - Serial.println( F("Sensors Configuration : ")); - - Serial.print( F("airDataActive.temperature : ")); - Serial.println(this->airDataActive.temperature); + Serial.println(F("Sensors Configuration : ")); - Serial.print( F("airDataActive.humidity : ")); - Serial.println(airDataActive.humidity); + Serial.print(F("airDataActive.temperature : ")); + Serial.println(this->airDataActive.temperature); - Serial.print( F("airDataActive.pressure : ")); - Serial.println(airDataActive.pressure); + Serial.print(F("airDataActive.humidity : ")); + Serial.println(airDataActive.humidity); - Serial.print( F("lightDataActive.visible : ")); - Serial.println(lightDataActive.visible); + Serial.print(F("airDataActive.pressure : ")); + Serial.println(airDataActive.pressure); - Serial.print( F("lightDataActive.ir : ")); - Serial.println(lightDataActive.ir); + Serial.print(F("lightDataActive.visible : ")); + Serial.println(lightDataActive.visible); - Serial.print( F("lightDataActive.uv : ")); - Serial.println(lightDataActive.uv); - - Serial.print( F("vbatActive : ")); - Serial.println(vbatActive); + Serial.print(F("lightDataActive.ir : ")); + Serial.println(lightDataActive.ir); - Serial.print( F("soilMoitureActive : ")); - Serial.println(soilMoistureActive); + Serial.print(F("lightDataActive.uv : ")); + Serial.println(lightDataActive.uv); - Serial.println(); -} + Serial.print(F("vbatActive : ")); + Serial.println(vbatActive); + + Serial.print(F("soilMoitureActive : ")); + Serial.println(soilMoistureActive); + Serial.println(); +} /** -* CoolBoardSensors::setEnvSensorSetting(): -* This method is provided to set the enviornment -* sensor settings , if argument is ommitted , default value will be assigned -* -*/ -void CoolBoardSensors::setEnvSensorSettings( uint8_t commInterface, uint8_t I2CAddress, - - uint8_t runMode , uint8_t tStandby, uint8_t filter, - uint8_t tempOverSample, uint8_t pressOverSample, - uint8_t humidOverSample) -{ + * CoolBoardSensors::setEnvSensorSetting(): + * This method is provided to set the enviornment + * sensor settings , if argument is ommitted , default value will be + *assigned + * + */ +void CoolBoardSensors::setEnvSensorSettings( + uint8_t commInterface, uint8_t I2CAddress, + + uint8_t runMode, uint8_t tStandby, uint8_t filter, uint8_t tempOverSample, + uint8_t pressOverSample, uint8_t humidOverSample) { #if DEBUG == 1 - - Serial.println( F("Entering CoolBoardSensors.setEnvSensorSettings()") ); - Serial.println(); + + Serial.println(F("Entering CoolBoardSensors.setEnvSensorSettings()")); + Serial.println(); #endif - - this->envSensor.settings.commInterface = commInterface; - this->envSensor.settings.I2CAddress = I2CAddress; + this->envSensor.settings.commInterface = commInterface; - this->envSensor.settings.runMode = runMode; + this->envSensor.settings.I2CAddress = I2CAddress; - this->envSensor.settings.tStandby = tStandby; + this->envSensor.settings.runMode = runMode; - this->envSensor.settings.filter = filter; + this->envSensor.settings.tStandby = tStandby; - this->envSensor.settings.tempOverSample = tempOverSample; + this->envSensor.settings.filter = filter; - this->envSensor.settings.pressOverSample = pressOverSample; + this->envSensor.settings.tempOverSample = tempOverSample; - this->envSensor.settings.humidOverSample = humidOverSample; + this->envSensor.settings.pressOverSample = pressOverSample; + this->envSensor.settings.humidOverSample = humidOverSample; } /** -* CoolBoardSensors::readVBat(): -* This method is provided to read the -* Battery Voltage. -* -* \return a float representing the battery -* voltage -*/ -float CoolBoardSensors::readVBat() -{ + * CoolBoardSensors::readVBat(): + * This method is provided to read the + * Battery Voltage. + * + * \return a float representing the battery + * voltage + */ +float CoolBoardSensors::readVBat() { #if DEBUG == 1 - Serial.println( F("Entering CoolBoardSensors.readVBat()") ); - Serial.println(); + Serial.println(F("Entering CoolBoardSensors.readVBat()")); + Serial.println(); #endif - digitalWrite(this->AnMplex, LOW); //Enable Analog Switch to get the batterie tension - - delay(200); - - int raw = analogRead(A0); //read in batterie tension - - float val = 6.04 / 1024 * raw; //convert it apprimatly right tension in volts - + digitalWrite(this->AnMplex, + LOW); // Enable Analog Switch to get the batterie tension + + delay(200); + + int raw = analogRead(A0); // read in batterie tension + + float val = 6.04 / 1024 * raw; // convert it apprimatly right tension in volts + #if DEBUG == 1 - Serial.println( F("Vbat is : ") ); - Serial.println(val); - Serial.println(); + Serial.println(F("Vbat is : ")); + Serial.println(val); + Serial.println(); #endif - return (val); + return (val); } /** -* CoolBoardSensors::readMoisture(): -* This method is provided to red the -* Soil Moisture -* -* \return a float represnting the -* soil moisture -*/ -float CoolBoardSensors::readMoisture() -{ + * CoolBoardSensors::readMoisture(): + * This method is provided to red the + * Soil Moisture + * + * \return a float represnting the + * soil moisture + */ +float CoolBoardSensors::readMoisture() { #if DEBUG == 1 - - Serial.println( F("Entering CoolBoardSensors.readMoisture()") ); - Serial.println(); - + + Serial.println(F("Entering CoolBoardSensors.readMoisture()")); + Serial.println(); + #endif - digitalWrite(EnMoisture, LOW); //enable moisture sensor and waith a bit + digitalWrite(EnMoisture, LOW); // enable moisture sensor and waith a bit - digitalWrite(AnMplex, HIGH); //enable analog Switch to get the moisture + digitalWrite(AnMplex, HIGH); // enable analog Switch to get the moisture - delay(2000); + delay(2000); - int val = analogRead(A0); //read the value form the moisture sensor + int val = analogRead(A0); // read the value form the moisture sensor - if (val >= 891){ - val = 890; - } - float result = (float)map(val, 0, 890, 0, 100); + if (val >= 891) { + val = 890; + } + float result = (float)map(val, 0, 890, 0, 100); - digitalWrite(EnMoisture, HIGH); //disable moisture sensor for minimum wear - -#if DEBUG == 1 + digitalWrite(EnMoisture, HIGH); // disable moisture sensor for minimum wear - Serial.println( F("RAW Moisture is : ")); - Serial.println(val); - Serial.println( F("Soil Moisture is : ") ); - Serial.println(result); - Serial.println(); +#if DEBUG == 1 -#endif + Serial.println(F("RAW Moisture is : ")); + Serial.println(val); + Serial.println(F("Soil Moisture is : ")); + Serial.println(result); + Serial.println(); - return (result); -} +#endif + return (result); +} diff --git a/src/CoolBoardSensors.h b/src/CoolBoardSensors.h index 75e444f4..d75d24f8 100644 --- a/src/CoolBoardSensors.h +++ b/src/CoolBoardSensors.h @@ -1,144 +1,142 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef CoolBoardSensors_H #define CoolBoardSensors_H #include "Arduino.h" -#include "internals/CoolSI114X.h" // Light sensor Support -#include "internals/CoolSparkFunBME280.h"// Environmental sensor Support +#include "internals/CoolSI114X.h" // Light sensor Support +#include "internals/CoolSparkFunBME280.h" // Environmental sensor Support /** -* -* \class CoolBoardSensors -* \brief This class handles the On-Board Sensors. -* -* -*/ -class CoolBoardSensors -{ + * + * \class CoolBoardSensors + * \brief This class handles the On-Board Sensors. + * + * + */ +class CoolBoardSensors { public: + // Constructor + CoolBoardSensors(); - //Constructor - CoolBoardSensors(); + void begin(); - void begin(); + // data is in json + String read(); - //data is in json - String read(); + void allActive(); - void allActive(); + // additional method + void end(); - //additional method - void end(); + bool config(); - bool config(); + void printConf(); - void printConf(); + // environment sensor methods - //environment sensor methods + // set the enviornment sensor settings , if argument is ommitted , default + // value will be assigned + void setEnvSensorSettings(uint8_t commInterface = I2C_MODE, + uint8_t I2CAddress = 0x76, uint8_t runMode = 3, - //set the enviornment sensor settings , if argument is ommitted , default value will be assigned - void setEnvSensorSettings( uint8_t commInterface=I2C_MODE, uint8_t I2CAddress=0x76, uint8_t runMode = 3, - - uint8_t tStandby=0 , uint8_t filter=0, uint8_t tempOverSample=1, - uint8_t pressOverSample= 1, uint8_t humidOverSample= 1); + uint8_t tStandby = 0, uint8_t filter = 0, + uint8_t tempOverSample = 1, + uint8_t pressOverSample = 1, + uint8_t humidOverSample = 1); - //VBat - float readVBat(); + // VBat + float readVBat(); - //Moisture + // Moisture - float readMoisture(); + float readMoisture(); - //sensor objects : - /** - * SI114X light sensor instance - */ - CoolSI114X lightSensor; - - /** - * BME280 environment sensor instance - */ - BME280 envSensor; - -private: - /** - * lightActive structure - * - * set visible to 1 to have visibleLight Readings - * - * set ir to 1 to have infraRed Readings - * - * set uv to 1 to have ultraViolet Readings - */ - struct lightActive - { - bool visible=0; - bool ir=0; - bool uv=0; - - }lightDataActive; - - /** - * airActive structure - * - * set temperature to 1 to have temperature Readings - * - * set humidity to 1 to have humidity Readings - * - * set pressure to 1 to have pressure Readings - */ - struct airActive - { - bool temperature=0; - bool humidity=0; - bool pressure=0; - - }airDataActive; - - /** - * Moisture Enable Pin - */ - const int EnMoisture = 13; - - /** - * Analog Multiplexer LOW=Vbat , HIGH=Moisture - */ - const int AnMplex = 12; - - /** - * set vbatActive to 1 to have battery voltage Readings - */ - bool vbatActive=0; - - /** - * set soilMoistureActive to 1 to have soil Moisture Readings - */ - bool soilMoistureActive=0; + // sensor objects : + /** + * SI114X light sensor instance + */ + CoolSI114X lightSensor; + /** + * BME280 environment sensor instance + */ + BME280 envSensor; + +private: + /** + * lightActive structure + * + * set visible to 1 to have visibleLight Readings + * + * set ir to 1 to have infraRed Readings + * + * set uv to 1 to have ultraViolet Readings + */ + struct lightActive { + bool visible = 0; + bool ir = 0; + bool uv = 0; + + } lightDataActive; + + /** + * airActive structure + * + * set temperature to 1 to have temperature Readings + * + * set humidity to 1 to have humidity Readings + * + * set pressure to 1 to have pressure Readings + */ + struct airActive { + bool temperature = 0; + bool humidity = 0; + bool pressure = 0; + + } airDataActive; + + /** + * Moisture Enable Pin + */ + const int EnMoisture = 13; + + /** + * Analog Multiplexer LOW=Vbat , HIGH=Moisture + */ + const int AnMplex = 12; + + /** + * set vbatActive to 1 to have battery voltage Readings + */ + bool vbatActive = 0; + + /** + * set soilMoistureActive to 1 to have soil Moisture Readings + */ + bool soilMoistureActive = 0; }; #endif diff --git a/src/CoolFileSystem.cpp b/src/CoolFileSystem.cpp index bd2c35f0..e45d9500 100644 --- a/src/CoolFileSystem.cpp +++ b/src/CoolFileSystem.cpp @@ -1,1241 +1,1084 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ -#include #include "CoolFileSystem.h" -#include "ArduinoJson.h" // Arduino JSON File controller https://github.com/bblanchon/ArduinoJson #include "Arduino.h" - +#include "ArduinoJson.h" // Arduino JSON File controller https://github.com/bblanchon/ArduinoJson +#include #define DEBUG 0 +/** + * CoolFileSystem::begin(): + * This method is provided to start the + * SPIFFS object. + * + * \return true if SPIFFS was initialized correctly, + * false otherwise + */ +bool CoolFileSystem::begin() { + bool sResult = SPIFFS.begin(); + +#if DEBUG == 1 + + Serial.println(F("Entering CoolFileSystem.begin()")); + Serial.println(); + Serial.print(F("SPIFFS success ? ")); + Serial.println(sResult); + Serial.println(); + +#endif + // get the data from the files + this->getsavedData(); + + return (sResult); // Initialize Filesystem +} /** -* CoolFileSystem::begin(): -* This method is provided to start the -* SPIFFS object. -* -* \return true if SPIFFS was initialized correctly, -* false otherwise -*/ -bool CoolFileSystem::begin() -{ - bool sResult=SPIFFS.begin(); - + * CoolFileSystem::saveSensorData( data ): + * This method is provided to save the data on the local + * memory when there is no internet available + * + * increments the saved data flag when successful + * + * \return true if the data was saved, + * false otherwise + */ +bool CoolFileSystem::saveSensorData(const char *data) { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolFileSystem.saveSensorData()")); + Serial.println(); + +#endif + + File sensorsData = SPIFFS.open("/sensorsData.json", "a"); + + if (!sensorsData) { #if DEBUG == 1 - Serial.println( F("Entering CoolFileSystem.begin()") ); - Serial.println(); - Serial.print( F("SPIFFS success ? ") ); - Serial.println(sResult); - Serial.println( ); + Serial.println(F("failed to append to /sensorsData.json")); + Serial.println(); #endif - //get the data from the files - this->getsavedData(); + // keep the old data + this->savedData = savedData; + + return (false); + } + + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.parseObject(data); + + if (root.success()) { + root.printTo(sensorsData); + sensorsData.println(); + sensorsData.close(); + +#if DEBUG == 1 + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + + sensorsData = SPIFFS.open("/sensorsData.json", "r"); + + if (!sensorsData) { + + Serial.println(F("failed to reopen /sensorsData.json")); + return (true); + } + + Serial.println(F("saved data is : ")); + root.printTo(Serial); + Serial.println(); + + Serial.println(F("/sensorsData.json")); + while (sensorsData.available()) { + Serial.println(sensorsData.readString()); + } + + Serial.println(); + + sensorsData.close(); + +#endif + + // this->saveSensorDataCSV(data); + + // new data + this->savedData++; + + this->incrementsavedData(); - return( sResult ); //Initialize Filesystem + return (true); + } else { +#if DEBUG == 1 + + Serial.println(F("failed to parse json")); + +#endif + // old data + this->savedData = savedData; + + return (false); + } } /** -* CoolFileSystem::saveSensorData( data ): -* This method is provided to save the data on the local -* memory when there is no internet available -* -* increments the saved data flag when successful -* -* \return true if the data was saved, -* false otherwise -*/ -bool CoolFileSystem::saveSensorData(const char* data ) -{ + * CoolFileSystem::saveSensorDataCSV( data ): + * This method is provided to save the data on the local + * memory in CSV format. + * + * \return true if the data was saved, + * false otherwise + */ +bool CoolFileSystem::saveSensorDataCSV(const char *data) { +#if DEBUG == 1 + + Serial.println(F("Entering CoolFileSystem.saveSensorDataCSV()")); + Serial.println(); +#endif + // parsing json + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.parseObject(data); + String header = "", values = ""; + + // if json parse success + if (root.success()) { + for (auto kv : root) { +// print the header(json keys ) to header string #if DEBUG == 1 - Serial.println( F("Entering CoolFileSystem.saveSensorData()") ); - Serial.println(); + Serial.println(kv.key); + Serial.println(kv.value.as()); #endif - - File sensorsData=SPIFFS.open("/sensorsData.json","a"); - - if(!sensorsData) - { - - #if DEBUG == 1 - - Serial.println( F("failed to append to /sensorsData.json") ); - Serial.println(); - - #endif - //keep the old data - this->savedData=savedData; - - return (false); - } - - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.parseObject(data); - - if( root.success() ) - { - root.printTo(sensorsData); - sensorsData.println(); - sensorsData.close(); - - - #if DEBUG == 1 - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - sensorsData=SPIFFS.open("/sensorsData.json","r"); - - if(!sensorsData) - { - - Serial.println(F("failed to reopen /sensorsData.json")); - return(true); - - } - - Serial.println( F("saved data is : ") ); - root.printTo(Serial); - Serial.println(); - - Serial.println(F("/sensorsData.json") ); - while (sensorsData.available()) - { - Serial.println(sensorsData.readString()) ; - } - - Serial.println(); - - sensorsData.close(); - - #endif - - //this->saveSensorDataCSV(data); - - //new data - this->savedData++; - - this->incrementsavedData(); - - return (true); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json") ); - - #endif - //old data - this->savedData=savedData; - - return(false); - } - -} + header += kv.key; + header += ','; + // print the values to header string + values += (kv.value.as()); + values += ','; + } + + header.remove(header.lastIndexOf(','), 1); + values.remove(values.lastIndexOf(','), 1); -/** -* CoolFileSystem::saveSensorDataCSV( data ): -* This method is provided to save the data on the local -* memory in CSV format. -* -* \return true if the data was saved, -* false otherwise -*/ -bool CoolFileSystem::saveSensorDataCSV(const char* data ) -{ #if DEBUG == 1 - Serial.println( F("Entering CoolFileSystem.saveSensorDataCSV()") ); - Serial.println(); + Serial.println(F(" data is : ")); + root.printTo(Serial); + Serial.println(); + + Serial.println(F(" header is :")); + Serial.println(header); + Serial.println(F(" values are : ")); + Serial.println(values); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); #endif - //parsing json - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.parseObject(data); - String header="",values=""; - - //if json parse success - if( root.success() ) - { - for (auto kv : root) - { - //print the header(json keys ) to header string - #if DEBUG ==1 - - Serial.println(kv.key); - Serial.println(kv.value.as() ); - - #endif - - header+=kv.key; - header+=','; - - //print the values to header string - values+=( kv.value.as() ); - values+=','; - } - - header.remove(header.lastIndexOf(','), 1); - values.remove(values.lastIndexOf(','), 1); - - #if DEBUG == 1 - - Serial.println( F(" data is : ") ); - root.printTo(Serial); - Serial.println(); - - Serial.println(F(" header is :" ) ) ; - Serial.println(header); - Serial.println(F(" values are : ")); - Serial.println(values); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - - } - //failed to parse json - else - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json") ); - - #endif - - return(false); - } - - //check if file exists - File sensorsData=SPIFFS.open("/sensorsData.csv","r"); - - //file doesn't exist - if( (!sensorsData) || (sensorsData.size()==0 ) ) - { - - #if DEBUG == 1 - - Serial.println( F("/sensorsData.csv not found") ); - Serial.println( F("creating /sensorsData.csv") ); - Serial.println(); - - #endif - //create file - sensorsData=SPIFFS.open("/sensorsData.csv","w"); - - if(!sensorsData) - { - - #if DEBUG == 1 - - Serial.println( F("failed to create /sensorsData.csv") ); - Serial.println(); - - #endif - - return(false); - - } - - //print the header(json keys ) to the CSV file - sensorsData.println(header); - - //print the values to the CSV file - sensorsData.println(values); - - sensorsData.close(); - - #if DEBUG == 1 - - sensorsData=SPIFFS.open("/sensorsData.csv","r"); - - if(!sensorsData) - { - Serial.println(F("failed to reopen /sensorsData.csv ")); - return(false); - } - - Serial.println( F("/sensorsData.csv : ") ); - - while (sensorsData.available()) - { - Serial.print(sensorsData.readString()) ; - } - Serial.println(); - - //close the file - sensorsData.close(); - - #endif - - - - return(true); - - } - - //file exist - else - { - - #if DEBUG == 1 - - Serial.println( F("/sensorsData.csv found") ); - Serial.println( F("appending to /sensorsData.csv") ); - Serial.println(); - - #endif - - //append to file - sensorsData=SPIFFS.open("/sensorsData.csv","a"); - - if(!sensorsData) - { - - #if DEBUG == 1 - - Serial.println( F("failed to open /sensorsData.csv") ); - Serial.println(); - - #endif - - return(false); - - } - - //print the values to the CSV file - sensorsData.println(values); - - sensorsData.close(); - - #if DEBUG == 1 - - sensorsData=SPIFFS.open("/sensorsData.csv","r"); - - if(!sensorsData) - { - Serial.println(F("failed to reopen /sensorsData.csv ")); - return(false); - } - - - Serial.println( F("/sensorsData.csv : ") ); - - while (sensorsData.available()) - { - Serial.println(sensorsData.readString()) ; - } - - Serial.println(); - - sensorsData.close(); - - #endif - - return(true); - - } + } + // failed to parse json + else { +#if DEBUG == 1 -} + Serial.println(F("failed to parse json")); + +#endif + return (false); + } + + // check if file exists + File sensorsData = SPIFFS.open("/sensorsData.csv", "r"); + + // file doesn't exist + if ((!sensorsData) || (sensorsData.size() == 0)) { + +#if DEBUG == 1 + + Serial.println(F("/sensorsData.csv not found")); + Serial.println(F("creating /sensorsData.csv")); + Serial.println(); + +#endif + // create file + sensorsData = SPIFFS.open("/sensorsData.csv", "w"); + + if (!sensorsData) { + +#if DEBUG == 1 + + Serial.println(F("failed to create /sensorsData.csv")); + Serial.println(); + +#endif + + return (false); + } + + // print the header(json keys ) to the CSV file + sensorsData.println(header); + + // print the values to the CSV file + sensorsData.println(values); + + sensorsData.close(); + +#if DEBUG == 1 + + sensorsData = SPIFFS.open("/sensorsData.csv", "r"); + + if (!sensorsData) { + Serial.println(F("failed to reopen /sensorsData.csv ")); + return (false); + } + + Serial.println(F("/sensorsData.csv : ")); + + while (sensorsData.available()) { + Serial.print(sensorsData.readString()); + } + Serial.println(); + + // close the file + sensorsData.close(); + +#endif + + return (true); + + } + + // file exist + else { + +#if DEBUG == 1 + + Serial.println(F("/sensorsData.csv found")); + Serial.println(F("appending to /sensorsData.csv")); + Serial.println(); + +#endif + + // append to file + sensorsData = SPIFFS.open("/sensorsData.csv", "a"); + + if (!sensorsData) { + +#if DEBUG == 1 + + Serial.println(F("failed to open /sensorsData.csv")); + Serial.println(); + +#endif + + return (false); + } + + // print the values to the CSV file + sensorsData.println(values); + + sensorsData.close(); + +#if DEBUG == 1 + + sensorsData = SPIFFS.open("/sensorsData.csv", "r"); + + if (!sensorsData) { + Serial.println(F("failed to reopen /sensorsData.csv ")); + return (false); + } + + Serial.println(F("/sensorsData.csv : ")); + + while (sensorsData.available()) { + Serial.println(sensorsData.readString()); + } + + Serial.println(); + + sensorsData.close(); + +#endif + + return (true); + } +} /** -* CoolFileSystem::updateConfigFiles( mqtt answer ): -* This method is provided to update the configuration files when -* the appropriate mqtt answer is received -* -* \return true if the files are updated correctly, -* false otherwise -*/ -bool CoolFileSystem::updateConfigFiles(String answer ) -{ + * CoolFileSystem::updateConfigFiles( mqtt answer ): + * This method is provided to update the configuration files when + * the appropriate mqtt answer is received + * + * \return true if the files are updated correctly, + * false otherwise + */ +bool CoolFileSystem::updateConfigFiles(String answer) { #if DEBUG == 1 - Serial.println( F("Entering CoolFileSystem.updateConfigFiles") ); - Serial.println(); - - Serial.println( F("input answer : ") ); - Serial.println(answer); + Serial.println(F("Entering CoolFileSystem.updateConfigFiles")); + Serial.println(); + + Serial.println(F("input answer : ")); + Serial.println(answer); #endif - //total json object - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.parseObject( answer.c_str() ); + // total json object + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.parseObject(answer.c_str()); #if DEBUG == 1 - - Serial.println( F("json object : ") ); - root.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); + Serial.println(F("json object : ")); + root.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); #endif - if(! ( root.success() )) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse root ") ); - Serial.println(); - - #endif - - return(false); - } - else - { - #if DEBUG == 1 - - Serial.println( F("success to parse root ") ); - Serial.println(); - - #endif - } - + if (!(root.success())) { + #if DEBUG == 1 - Serial.println( F("input message is : ") ); - root.printTo(Serial); - Serial.println(); + Serial.println(F("failed to parse root ")); + Serial.println(); #endif - //temp string - String temp; - //CoolBoard Configuration File + return (false); + } else { +#if DEBUG == 1 + + Serial.println(F("success to parse root ")); + Serial.println(); - JsonObject& jsonCoolBoard=root["CoolBoard"]; +#endif + } #if DEBUG == 1 - Serial.println( F("before config CoolBoard json") ); - jsonCoolBoard.printTo(Serial); + Serial.println(F("input message is : ")); + root.printTo(Serial); + Serial.println(); #endif + // temp string + String temp; - if(jsonCoolBoard.success()) - { - String update; - - jsonCoolBoard.printTo(update); + // CoolBoard Configuration File - this->fileUpdate(update,"/coolBoardConfig.json"); - - } - else - { - - #if DEBUG == 1 + JsonObject &jsonCoolBoard = root["CoolBoard"]; - Serial.println( F("failed to parse CoolBoard ") ); - - #endif +#if DEBUG == 1 - } + Serial.println(F("before config CoolBoard json")); + jsonCoolBoard.printTo(Serial); - - //Cool Board Sensors Configuration File - JsonObject& jsonSensorsBoard=root["CoolSensorsBoard"]; +#endif -#if DEBUG == 1 + if (jsonCoolBoard.success()) { + String update; - Serial.println( F("before config CoolSensorsBoard json") ); - jsonSensorsBoard.printTo(Serial); + jsonCoolBoard.printTo(update); -#endif - - if(jsonSensorsBoard.success()) - { - String update; - - jsonSensorsBoard.printTo(update); + this->fileUpdate(update, "/coolBoardConfig.json"); - this->fileUpdate(update,"/coolBoardSensorsConfig.json"); + } else { - } - else - { +#if DEBUG == 1 - #if DEBUG == 1 + Serial.println(F("failed to parse CoolBoard ")); - Serial.println( F("failed to parse CoolSensorsBoard sensors ") ); - - #endif +#endif + } - } + // Cool Board Sensors Configuration File + JsonObject &jsonSensorsBoard = root["CoolSensorsBoard"]; - //Cool Board Actor Configuration File - JsonObject& jsonCoolBoardActor=root["CoolBoardActor"]; +#if DEBUG == 1 -#if DEBUG == 1 + Serial.println(F("before config CoolSensorsBoard json")); + jsonSensorsBoard.printTo(Serial); - Serial.println( F("before config CoolBoardActor json") ); - jsonCoolBoardActor.printTo(Serial); +#endif -#endif - - if(jsonCoolBoardActor.success()) - { - String update; - - jsonCoolBoardActor.printTo(update); + if (jsonSensorsBoard.success()) { + String update; - this->fileUpdate(update,"/coolBoardActorConfig.json"); + jsonSensorsBoard.printTo(update); - } - else - { + this->fileUpdate(update, "/coolBoardSensorsConfig.json"); - #if DEBUG == 1 + } else { - Serial.println( F("failed to parse CoolBoardActor json ") ); - - #endif +#if DEBUG == 1 - } + Serial.println(F("failed to parse CoolSensorsBoard sensors ")); - - //rtc configuration file - JsonObject& jsonRTC=root["rtc"]; +#endif + } -#if DEBUG == 1 - - Serial.println( F("before config rtc json") ); - jsonRTC.printTo(Serial); + // Cool Board Actor Configuration File + JsonObject &jsonCoolBoardActor = root["CoolBoardActor"]; + +#if DEBUG == 1 + + Serial.println(F("before config CoolBoardActor json")); + jsonCoolBoardActor.printTo(Serial); #endif - if(jsonRTC.success() ) - { - String update; - - jsonRTC.printTo(update); - - this->fileUpdate(update,"/rtcConfig.json"); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse rtc ") ); - - #endif - - } - - - //cool board led configuration - JsonObject& jsonLedBoard=root["led"]; - -#if DEBUG == 1 - - Serial.println( F("before config Led json") ); - jsonLedBoard.printTo(Serial); + + if (jsonCoolBoardActor.success()) { + String update; + + jsonCoolBoardActor.printTo(update); + + this->fileUpdate(update, "/coolBoardActorConfig.json"); + + } else { + +#if DEBUG == 1 + + Serial.println(F("failed to parse CoolBoardActor json ")); + +#endif + } + + // rtc configuration file + JsonObject &jsonRTC = root["rtc"]; + +#if DEBUG == 1 + + Serial.println(F("before config rtc json")); + jsonRTC.printTo(Serial); + +#endif + if (jsonRTC.success()) { + String update; + + jsonRTC.printTo(update); + + this->fileUpdate(update, "/rtcConfig.json"); + } else { + +#if DEBUG == 1 + + Serial.println(F("failed to parse rtc ")); #endif + } - if(jsonLedBoard.success()) - { - String update; - - jsonLedBoard.printTo(update); + // cool board led configuration + JsonObject &jsonLedBoard = root["led"]; - this->fileUpdate(update,"/coolBoardLedConfig.json"); +#if DEBUG == 1 - - } - else - { - - #if DEBUG == 1 + Serial.println(F("before config Led json")); + jsonLedBoard.printTo(Serial); - Serial.println( F("failed to parse led") ); - - #endif +#endif - } - + if (jsonLedBoard.success()) { + String update; - + jsonLedBoard.printTo(update); - //jetpack configuration - JsonObject& jsonJetpack=root["jetPack"]; + this->fileUpdate(update, "/coolBoardLedConfig.json"); -#if DEBUG == 1 + } else { - Serial.println( F("before config jetpack json") ); - jsonJetpack.printTo(Serial); +#if DEBUG == 1 + + Serial.println(F("failed to parse led")); #endif + } - if(jsonJetpack.success()) - { - - String update; - - jsonJetpack.printTo(update); + // jetpack configuration + JsonObject &jsonJetpack = root["jetPack"]; - this->fileUpdate(update,"/jetPackConfig.json"); +#if DEBUG == 1 - } + Serial.println(F("before config jetpack json")); + jsonJetpack.printTo(Serial); - else - { - - #if DEBUG == 1 +#endif - Serial.println( F("failed to parse jetpack") ); - - #endif + if (jsonJetpack.success()) { - } - - //irene configuration - JsonObject& jsonIrene=root["irene3000"]; - -#if DEBUG == 1 + String update; - Serial.println( F("before config irene json") ); - jsonIrene.printTo(Serial); + jsonJetpack.printTo(update); -#endif + this->fileUpdate(update, "/jetPackConfig.json"); - if(jsonIrene.success()) - { + } - String update; - - jsonIrene.printTo(update); + else { - this->fileUpdate(update,"/irene3000Config.json"); - - } - else - { - - #if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("failed to parse irene") ); - - #endif + Serial.println(F("failed to parse jetpack")); +#endif + } - } - - //external sensors - JsonObject& jsonExternalSensors=root["externalSensors"]; + // irene configuration + JsonObject &jsonIrene = root["irene3000"]; -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("before config external Sensors json") ); - jsonExternalSensors.printTo(Serial); + Serial.println(F("before config irene json")); + jsonIrene.printTo(Serial); #endif - if(jsonExternalSensors.success()) - { + if (jsonIrene.success()) { - String update; - - jsonExternalSensors.printTo(update); + String update; - this->fileUpdate(update,"/externalSensorsConfig.json"); + jsonIrene.printTo(update); - } + this->fileUpdate(update, "/irene3000Config.json"); - else - { + } else { - #if DEBUG == 1 - - Serial.println( F("failed to parse external sensors") ); +#if DEBUG == 1 - #endif + Serial.println(F("failed to parse irene")); +#endif + } - } + // external sensors + JsonObject &jsonExternalSensors = root["externalSensors"]; - - //mqtt config - JsonObject& jsonMQTT=root["mqtt"]; - -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("before config mqtt json") ); - jsonMQTT.printTo(Serial); + Serial.println(F("before config external Sensors json")); + jsonExternalSensors.printTo(Serial); #endif - if(jsonMQTT.success()) - { + if (jsonExternalSensors.success()) { - String update; - - jsonMQTT.printTo(update); + String update; - this->fileUpdate(update,"/mqttConfig.json"); + jsonExternalSensors.printTo(update); - } - else - { + this->fileUpdate(update, "/externalSensorsConfig.json"); - #if DEBUG == 1 + } - Serial.println( F("failed to parse mqtt") ); - - #endif + else { - - } +#if DEBUG == 1 - //wifi config - JsonObject& jsonWifi=root["wifi"]; - -#if DEBUG == 1 + Serial.println(F("failed to parse external sensors")); + +#endif + } - Serial.println( F("before config wifi json") ); - jsonWifi.printTo(Serial); + // mqtt config + JsonObject &jsonMQTT = root["mqtt"]; + +#if DEBUG == 1 + + Serial.println(F("before config mqtt json")); + jsonMQTT.printTo(Serial); #endif - if(jsonWifi.success()) - { + if (jsonMQTT.success()) { + + String update; - String update; - - jsonWifi.printTo(update); + jsonMQTT.printTo(update); - this->fileUpdate(update,"/wifiConfig.json"); + this->fileUpdate(update, "/mqttConfig.json"); - } - else - { + } else { - #if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("failed to parse wifi") ); - - #endif + Serial.println(F("failed to parse mqtt")); - - } - - return true; +#endif + } -} + // wifi config + JsonObject &jsonWifi = root["wifi"]; -/** -* CoolFileSystem::isDataSaved(): -* This method is provided to report -* wether there is sensor data saved in the -* File System. -* -* \return true if there is data saved, false -* otherwise -*/ -int CoolFileSystem::isDataSaved() -{ +#if DEBUG == 1 -#if DEBUG == 1 + Serial.println(F("before config wifi json")); + jsonWifi.printTo(Serial); - Serial.println( F("Entering CoolFileSystem.isDataSaved()") ); - Serial.println(); +#endif - FSInfo fs_info; + if (jsonWifi.success()) { - if(SPIFFS.info(fs_info)==true) - { - Serial.print(F("used bytes/total bytes : ")); - Serial.print(fs_info.usedBytes); - Serial.print(F("/")); - Serial.print(fs_info.totalBytes); - Serial.println(); + String update; - } + jsonWifi.printTo(update); - File sensorsData=SPIFFS.open("/sensorsData.json","r"); - File sensorsDataCSV=SPIFFS.open("/sensorsData.csv","r"); - - if( (!sensorsData)||(!sensorsDataCSV) ) - { - Serial.println( F("failed to open files") ); - } - else - { - Serial.print(F("sensors Data file size in bytes : ")); - Serial.println(sensorsData.size()); - Serial.println(); - - Serial.print(F("sensors Data CSV file size in bytes : ")); - Serial.println(sensorsDataCSV.size()); - Serial.println(); - } + this->fileUpdate(update, "/wifiConfig.json"); + } else { +#if DEBUG == 1 - Serial.print( F("savedData : ") ); - Serial.println(this->savedData); + Serial.println(F("failed to parse wifi")); #endif + } - return( this->savedData ); + return true; } +/** + * CoolFileSystem::isDataSaved(): + * This method is provided to report + * wether there is sensor data saved in the + * File System. + * + * \return true if there is data saved, false + * otherwise + */ +int CoolFileSystem::isDataSaved() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolFileSystem.isDataSaved()")); + Serial.println(); + + FSInfo fs_info; + + if (SPIFFS.info(fs_info) == true) { + Serial.print(F("used bytes/total bytes : ")); + Serial.print(fs_info.usedBytes); + Serial.print(F("/")); + Serial.print(fs_info.totalBytes); + Serial.println(); + } + + File sensorsData = SPIFFS.open("/sensorsData.json", "r"); + File sensorsDataCSV = SPIFFS.open("/sensorsData.csv", "r"); + + if ((!sensorsData) || (!sensorsDataCSV)) { + Serial.println(F("failed to open files")); + } else { + Serial.print(F("sensors Data file size in bytes : ")); + Serial.println(sensorsData.size()); + Serial.println(); + + Serial.print(F("sensors Data CSV file size in bytes : ")); + Serial.println(sensorsDataCSV.size()); + Serial.println(); + } + + Serial.print(F("savedData : ")); + Serial.println(this->savedData); + +#endif + + return (this->savedData); +} /** -* CoolFileSystem::getSensorData(int &lines): -* This method is provided to return the -* sensor data saved in the File System -* 10 lines at a time - -* \return String array containing -* 50 first lines from the file -* modifies tge lines argument to reflect the number of -* lines left +* CoolFileSystem::getSensorData(int &lines): +* This method is provided to return the +* sensor data saved in the File System +* 10 lines at a time + +* \return String array containing +* 50 first lines from the file +* modifies tge lines argument to reflect the number of +* lines left */ -String* CoolFileSystem::getSensorSavedData(int& lines) -{ - int maxString=50; - String *sensorsDataArray=new String[maxString]; - lines=0; +String *CoolFileSystem::getSensorSavedData(int &lines) { + int maxString = 50; + String *sensorsDataArray = new String[maxString]; + lines = 0; + +#if DEBUG == 1 + + Serial.println(F("Entering CoolFileSystem.getSensorSavedData()")); + Serial.println(); + +#endif + + // open sensors data file + File sensorsData = SPIFFS.open("/sensorsData.json", "r"); + + if (!sensorsData) { + +#if DEBUG == 1 + + Serial.println(F("Failed to read /sensorsData.json")); + +#endif + + sensorsDataArray[0] = "Failed to read /sensorsData.json"; + lines++; + + // result=sensorsDataArray; + return (sensorsDataArray); + + } + + else { + // read the file line by line and put it in the String array(aka String*) + + // while loop until EOF is reached + String temp; + while (sensorsData.available()) { + yield(); -#if DEBUG == 1 + temp = sensorsData.readStringUntil('\r'); - Serial.println( F("Entering CoolFileSystem.getSensorSavedData()") ); - Serial.println(); + if (linesToSkip > 0) { + + linesToSkip--; + + } else { + +#if DEBUG == 1 + + Serial.println(F("temp String : ")); + Serial.println(temp); + Serial.println(); #endif - //open sensors data file - File sensorsData=SPIFFS.open("/sensorsData.json","r"); - - if (!sensorsData) - { - - #if DEBUG == 1 - - Serial.println( F("Failed to read /sensorsData.json") ); - - #endif - - sensorsDataArray[0]="Failed to read /sensorsData.json"; - lines++; - - //result=sensorsDataArray; - return(sensorsDataArray); - - } - - else - { - //read the file line by line and put it in the String array(aka String*) - - //while loop until EOF is reached - String temp; - while(sensorsData.available()) - { - yield(); - - temp=sensorsData.readStringUntil('\r'); - - if(linesToSkip>0) - { - - linesToSkip--; - - } - else - { - - #if DEBUG == 1 - - Serial.println(F("temp String : ")); - Serial.println(temp); - Serial.println(); - - #endif - - sensorsDataArray[lines]=temp; - sensorsData.read(); - - - - #if DEBUG== 1 - - Serial.print(F("read String N°")); - Serial.print(lines); - Serial.println(F(" is : ")); - Serial.println( sensorsDataArray[lines] ); - Serial.println(); - Serial.println(F("next char is : ")); - Serial.println((char)sensorsData.peek()); - Serial.println(); - - #endif - lines++; - - //maximum size is maxString(index : 0..maxString-1) - if(lines>=maxString) - { - break; - } - - yield(); - - } - - } - - - //close the file - sensorsData.close(); - - //position the saved data flag to the number of unread lines - this->savedData=savedData-lines; - - //position the number of lines to skip to the number of read lines - this->linesToSkip=lines; - - //delete data in the file only if savedData<=0 - if(this->savedData<=0) - { - File sensorsData=SPIFFS.open("/sensorsData.json","w"); - File sensorsDataCSV=SPIFFS.open("/sensorsData.csv","w"); - if( (!sensorsData)||(!sensorsDataCSV) ) - { - #if DEBUG == 1 - - Serial.println( F("failed to delete data in the file") ); - - #endif - lines++; - sensorsDataArray[lines]="failed to delete data in the file"; - - return(sensorsDataArray); - - } - - sensorsData.close(); - sensorsDataCSV.close(); - - this->linesToSkip=0; - - } - - //save the changes to linesToSkip and savedData in the file system - this->incrementsavedData(); - - //return the string - #if DEBUG == 1 - - for(int i=0;i= maxString) { + break; + } + + yield(); + } + } + + // close the file + sensorsData.close(); + + // position the saved data flag to the number of unread lines + this->savedData = savedData - lines; + + // position the number of lines to skip to the number of read lines + this->linesToSkip = lines; + + // delete data in the file only if savedData<=0 + if (this->savedData <= 0) { + File sensorsData = SPIFFS.open("/sensorsData.json", "w"); + File sensorsDataCSV = SPIFFS.open("/sensorsData.csv", "w"); + if ((!sensorsData) || (!sensorsDataCSV)) { +#if DEBUG == 1 + + Serial.println(F("failed to delete data in the file")); + +#endif + lines++; + sensorsDataArray[lines] = "failed to delete data in the file"; + + return (sensorsDataArray); + } + + sensorsData.close(); + sensorsDataCSV.close(); + + this->linesToSkip = 0; + } + + // save the changes to linesToSkip and savedData in the file system + this->incrementsavedData(); + +// return the string +#if DEBUG == 1 + + for (int i = 0; i < lines; i++) { + Serial.print(F("String N°")); + Serial.println(i); + Serial.println(sensorsDataArray[i]); + Serial.println(); + } + +#endif + return (sensorsDataArray); + } } /** -* CoolFileSystem::fileUpdate( update msg, file path): -* This method is provided to ensure the -* correct update for each configuration file in the -* File system -* -* \return true if successful , false otherwise -* -*/ -bool CoolFileSystem::fileUpdate(String update,const char* path) -{ + * CoolFileSystem::fileUpdate( update msg, file path): + * This method is provided to ensure the + * correct update for each configuration file in the + * File system + * + * \return true if successful , false otherwise + * + */ +bool CoolFileSystem::fileUpdate(String update, const char *path) { #if DEBUG == 1 - Serial.println( F("Entering CoolFileSystem.fileUpdate()") ); - Serial.println(); - - Serial.println(F("update msg is :")); - Serial.println(update); - - Serial.println(F("file path is : ")); - Serial.println(path); + Serial.println(F("Entering CoolFileSystem.fileUpdate()")); + Serial.println(); + + Serial.println(F("update msg is :")); + Serial.println(update); + + Serial.println(F("file path is : ")); + Serial.println(path); #endif - //transfer update String to json - DynamicJsonBuffer updateBuffer; - JsonObject& updateJson = updateBuffer.parseObject(update.c_str() ); - - if(updateJson.success()) - { - - #if DEBUG ==1 - - Serial.println(F("root parsing success :")); - updateJson.printTo(Serial); - - #endif - - } - else - { - - #if DEBUG == 1 - - Serial.println(F("root parsing failure ")); - - #endif - - return(false); - - } - - //open file in read mode - File configFile = SPIFFS.open( path , "r"); - - if(!configFile) - { - #if DEBUG == 1 - - Serial.print( F("failed to read ") ); - Serial.println(path); - - #endif - return(false); - } - - //copy file to a json - 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); - - DynamicJsonBuffer fileBuffer; - - JsonObject & fileJson = fileBuffer.parseObject(buf.get()); - - if (!fileJson.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json") ); - - #endif - - return(false); - } - - //modify root to contain all the json keys: updated ones and non updated ones - for (auto kv : fileJson) - { - if( updateJson[kv.key].success() ) - { - fileJson[kv.key]=updateJson[kv.key]; - } - else - { - fileJson[kv.key]=fileJson[kv.key]; - } - - - } + // transfer update String to json + DynamicJsonBuffer updateBuffer; + JsonObject &updateJson = updateBuffer.parseObject(update.c_str()); + + if (updateJson.success()) { + +#if DEBUG == 1 + + Serial.println(F("root parsing success :")); + updateJson.printTo(Serial); + +#endif + + } else { #if DEBUG == 1 - Serial.println(F("fileJson is now : ")); - fileJson.printTo(Serial); + Serial.println(F("root parsing failure ")); #endif - //close the file - configFile.close(); - - //open file in w mode - configFile = SPIFFS.open( path , "w"); - - if(!configFile) - { - #if DEBUG == 1 - - Serial.print( F("failed to open ") ); - Serial.println(path); - - #endif - return(false); - } - //print json to file - - fileJson.printTo(configFile); - - //close file - configFile.close(); + return (false); + } + // open file in read mode + File configFile = SPIFFS.open(path, "r"); + if (!configFile) { #if DEBUG == 1 - Serial.println( F("config is") ); - fileJson.printTo(Serial); - Serial.println(); + Serial.print(F("failed to read ")); + Serial.println(path); #endif - - return(true); - -} + return (false); + } + // copy file to a json + size_t size = configFile.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); -/** -* CoolFileSystem::incrementsavedData(): -* This method is provided to increment the -* savedData flag -* -* \return true if successful , false otherwise -* -*/ -bool CoolFileSystem::incrementsavedData() -{ + configFile.readBytes(buf.get(), size); + + DynamicJsonBuffer fileBuffer; + + JsonObject &fileJson = fileBuffer.parseObject(buf.get()); + if (!fileJson.success()) { #if DEBUG == 1 - - Serial.println(F("Entering CoolFileSystem.incrementsavedData()")); - + + Serial.println(F("failed to parse json")); + #endif - //open file - File file=SPIFFS.open("/savedDataFlag.txt","w"); - //read file - if( !file) - { - Serial.println( F("failed to open savedDataFlag.txt") ); - - return(false); - } - else - { - //write to file - file.print(this->savedData); - - file.print(" "); - - file.println(this->linesToSkip); - - file.close(); - - #if DEBUG == 1 - - Serial.print(F("number of lines to read :")); - Serial.println(this->savedData); - - Serial.print(F("number of lines to skip :")); - Serial.println(this->linesToSkip); - - #endif - - return(true); - } - + + return (false); + } + + // modify root to contain all the json keys: updated ones and non updated ones + for (auto kv : fileJson) { + if (updateJson[kv.key].success()) { + fileJson[kv.key] = updateJson[kv.key]; + } else { + fileJson[kv.key] = fileJson[kv.key]; + } + } + +#if DEBUG == 1 + + Serial.println(F("fileJson is now : ")); + fileJson.printTo(Serial); + +#endif + + // close the file + configFile.close(); + + // open file in w mode + configFile = SPIFFS.open(path, "w"); + + if (!configFile) { +#if DEBUG == 1 + + Serial.print(F("failed to open ")); + Serial.println(path); + +#endif + return (false); + } + // print json to file + + fileJson.printTo(configFile); + + // close file + configFile.close(); + +#if DEBUG == 1 + + Serial.println(F("config is")); + fileJson.printTo(Serial); + Serial.println(); + +#endif + + return (true); } + /** -* CoolFileSystem::getsavedData(): -* This method is provided to get the -* savedData flag from the file system -* -* -*/ -void CoolFileSystem::getsavedData() -{ + * CoolFileSystem::incrementsavedData(): + * This method is provided to increment the + * savedData flag + * + * \return true if successful , false otherwise + * + */ +bool CoolFileSystem::incrementsavedData() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolFileSystem.incrementsavedData()")); + +#endif + // open file + File file = SPIFFS.open("/savedDataFlag.txt", "w"); + // read file + if (!file) { + Serial.println(F("failed to open savedDataFlag.txt")); + + return (false); + } else { + // write to file + file.print(this->savedData); + + file.print(" "); + file.println(this->linesToSkip); + + file.close(); + +#if DEBUG == 1 + + Serial.print(F("number of lines to read :")); + Serial.println(this->savedData); + + Serial.print(F("number of lines to skip :")); + Serial.println(this->linesToSkip); + +#endif + + return (true); + } +} +/** + * CoolFileSystem::getsavedData(): + * This method is provided to get the + * savedData flag from the file system + * + * + */ +void CoolFileSystem::getsavedData() { #if DEBUG == 1 - - Serial.println(F("Entering CoolFileSystem.getsavedData()")); - + + Serial.println(F("Entering CoolFileSystem.getsavedData()")); + #endif - //open file - File file=SPIFFS.open("/savedDataFlag.txt","r"); - //read file - if( !file) - { - Serial.println( F("failed to read savedDataFlag.txt") ); - - } - else - { - //read from file - String temp=file.readStringUntil(' '); - - this->savedData=temp.toInt(); - - - temp=file.readStringUntil('\n'); - this-> linesToSkip=temp.toInt();; - - file.close(); - } - + // open file + File file = SPIFFS.open("/savedDataFlag.txt", "r"); + // read file + if (!file) { + Serial.println(F("failed to read savedDataFlag.txt")); + + } else { + // read from file + String temp = file.readStringUntil(' '); + + this->savedData = temp.toInt(); + + temp = file.readStringUntil('\n'); + this->linesToSkip = temp.toInt(); + ; + + file.close(); + } + #if DEBUG == 1 - Serial.print(F("number of lines to read :")); - Serial.println(this->savedData); - - Serial.print(F("number of lines to skip :")); - Serial.println(this->linesToSkip); + Serial.print(F("number of lines to read :")); + Serial.println(this->savedData); + + Serial.print(F("number of lines to skip :")); + Serial.println(this->linesToSkip); #endif - } diff --git a/src/CoolFileSystem.h b/src/CoolFileSystem.h index f6ad56b3..4c598941 100644 --- a/src/CoolFileSystem.h +++ b/src/CoolFileSystem.h @@ -1,75 +1,70 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef CoolFileSystem_H #define CoolFileSystem_H - #include "Arduino.h" - /** -* \class CoolFileSystem -* -* \brief This class handles the file system -* -*/ -class CoolFileSystem -{ + * \class CoolFileSystem + * + * \brief This class handles the file system + * + */ +class CoolFileSystem { public: - bool begin(); + bool begin(); + + bool updateConfigFiles(String answer); + + bool fileUpdate(String update, const char *path); + + bool saveSensorData(const char *data); + + bool saveSensorDataCSV(const char *data); + + int isDataSaved(); + + String *getSensorSavedData(int &size); + + bool incrementsavedData(); - bool updateConfigFiles(String answer ); - - bool fileUpdate(String update,const char* path); - - bool saveSensorData(const char* data ); - - bool saveSensorDataCSV(const char* data ); - - int isDataSaved(); - - String* getSensorSavedData(int& size); - - bool incrementsavedData(); + void getsavedData(); - void getsavedData(); - private: - - /** - * Number of lines to read when - * retrieving saved Data - */ - int savedData=0; - - /** - * Number of lines to Skip - * when retrieving saved Data - */ - int linesToSkip=0; + /** + * Number of lines to read when + * retrieving saved Data + */ + int savedData = 0; + /** + * Number of lines to Skip + * when retrieving saved Data + */ + int linesToSkip = 0; }; #endif diff --git a/src/CoolMQTT.cpp b/src/CoolMQTT.cpp index a3479a91..7dd749b6 100644 --- a/src/CoolMQTT.cpp +++ b/src/CoolMQTT.cpp @@ -1,636 +1,565 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ - - -#include "FS.h" -#include "Arduino.h" -#include #include "CoolMQTT.h" +#include "Arduino.h" #include "ArduinoJson.h" #include "CoolWifi.h" - +#include "FS.h" +#include #define DEBUG 0 - - /** -* CoolMQTT::begin(): -* This method is provided to set the mqtt -* client's parameters: -client -* -server -* -callback method -* -buffer size -*/ -void CoolMQTT::begin() -{ + * CoolMQTT::begin(): + * This method is provided to set the mqtt + * client's parameters: -client + * -server + * -callback method + * -buffer size + */ +void CoolMQTT::begin() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering CoolMQTT.begin()") ); - Serial.println(); + Serial.println(F("Entering CoolMQTT.begin()")); + Serial.println(); #endif - client.setClient(espClient); - client.setServer(mqttServer, 1883); - client.setCallback([this] (char* topic, byte* payload, unsigned int length) { this->callback(topic, payload, length); }); - client.setBufferSize((unsigned short)bufferSize); - + client.setClient(espClient); + client.setServer(mqttServer, 1883); + client.setCallback([this](char *topic, byte *payload, unsigned int length) { + this->callback(topic, payload, length); + }); + client.setBufferSize((unsigned short)bufferSize); } /** -* CoolMQTT::state(): -* This method is provided to return the -* mqtt client's state. -* \return mqtt client state: -* -4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time -* -3 : MQTT_CONNECTION_LOST - the network connection was broken -* -2 : MQTT_CONNECT_FAILED - the network connection failed -* -1 : MQTT_DISCONNECTED - the client is disconnected cleanly -* 0 : MQTT_CONNECTED - the cient is connected -* 1 : MQTT_CONNECT_BAD_PROTOCOL - the server doesn't support the requested version of MQTT -* 2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier -* 3 : MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection -* 4 : MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected -* 5 : MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect -*/ -int CoolMQTT::state() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolMQTT.state()") ); - Serial.println(); - Serial.print( F("state : ") ); - Serial.println( this->client.state() ); + * CoolMQTT::state(): + * This method is provided to return the + * mqtt client's state. + * \return mqtt client state: + * -4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within + *the keepalive time -3 : MQTT_CONNECTION_LOST - the network connection was + *broken -2 : MQTT_CONNECT_FAILED - the network connection failed -1 : + *MQTT_DISCONNECTED - the client is disconnected cleanly 0 : MQTT_CONNECTED - + *the cient is connected 1 : MQTT_CONNECT_BAD_PROTOCOL - the server doesn't + *support the requested version of MQTT 2 : MQTT_CONNECT_BAD_CLIENT_ID - the + *server rejected the client identifier 3 : MQTT_CONNECT_UNAVAILABLE - the + *server was unable to accept the connection 4 : MQTT_CONNECT_BAD_CREDENTIALS + *- the username/password were rejected 5 : MQTT_CONNECT_UNAUTHORIZED - the + *client was not authorized to connect + */ +int CoolMQTT::state() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolMQTT.state()")); + Serial.println(); + Serial.print(F("state : ")); + Serial.println(this->client.state()); #endif - - return( this->client.state() ); + + return (this->client.state()); } /** -* CoolMQTT::connect(): -* This method is provided to connect the client to the server, -* publish to the out topic and subscribe to the in topic. -* -* \return mqtt client state -*/ -int CoolMQTT::connect() -{ + * CoolMQTT::connect(): + * This method is provided to connect the client to the server, + * publish to the out topic and subscribe to the in topic. + * + * \return mqtt client state + */ +int CoolMQTT::connect() { + + int i = 0; + +#if DEBUG == 1 + + Serial.println(F("Entering CoolMQTT.connect()")); + +#endif + Serial.println(F("MQTT connecting...")); + + String tempMAC = WiFi.macAddress(); - int i=0; + tempMAC.replace(":", ""); -#if DEBUG == 1 + char MAC[12]; + tempMAC.toCharArray(MAC, 12); - Serial.println( F("Entering CoolMQTT.connect()") ); + while ((!this->client.connected()) && (i < 5)) { + // Attempt to connect + if (this->client.connect( + MAC)) // use the mac as MQTT client ID to assure a unique id + { + client.subscribe(this->inTopic); + +#if DEBUG == 1 + + Serial.println(F(" subscribed , leavin ")); #endif - Serial.println( F("MQTT connecting...") ); - - String tempMAC = WiFi.macAddress(); - - tempMAC.replace(":", ""); - - char MAC[12]; - tempMAC.toCharArray(MAC, 12); - - while( ( !this->client.connected() ) && ( i<5 ) ) - { - // Attempt to connect - if( this->client.connect( MAC ) ) //use the mac as MQTT client ID to assure a unique id - { - client.subscribe( this->inTopic ); - - #if DEBUG == 1 - - Serial.println( F(" subscribed , leavin ") ) ; - - #endif - - return( this->state() ); - } - - else - { - - #if DEBUG == 1 - - Serial.println( F("not connected , retrying") ); - - #endif - - - } - - delay(5); - i++; - } - if (state() == 0) - { - Serial.println( F("MQTT connected : OK") ); - Serial.println(); - } - else Serial.println( F("MQTT not jet connected...")); - return( this->state() ); + return (this->state()); + } + + else { + +#if DEBUG == 1 + + Serial.println(F("not connected , retrying")); + +#endif + } + + delay(5); + i++; + } + if (state() == 0) { + Serial.println(F("MQTT connected : OK")); + Serial.println(); + } else + Serial.println(F("MQTT not jet connected...")); + return (this->state()); } /** -* CoolMQTT::publish(data): -* This method is provided to publish data -* to the out topic. If the publish fails -* it deconnects and reconnects the Wifi -* and MQTT. This prevents lost MQTT syndrome -* on cheaper Wifi hotspots -* -* \return true if publish successful, -* false otherwise -*/ -bool CoolMQTT::publish(const char* data) -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolMQTT.publish()") ); - Serial.println(); - //data is in JSON, publish it directly - - Serial.println( F("data to publish : ") ); - Serial.println(data); - Serial.print( F("data size : ") ); - Serial.println(strlen(data)); - - Serial.println(); + * CoolMQTT::publish(data): + * This method is provided to publish data + * to the out topic. If the publish fails + * it deconnects and reconnects the Wifi + * and MQTT. This prevents lost MQTT syndrome + * on cheaper Wifi hotspots + * + * \return true if publish successful, + * false otherwise + */ +bool CoolMQTT::publish(const char *data) { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolMQTT.publish()")); + Serial.println(); + // data is in JSON, publish it directly + + Serial.println(F("data to publish : ")); + Serial.println(data); + Serial.print(F("data size : ")); + Serial.println(strlen(data)); + + Serial.println(); #endif #if DEBUG == 0 - Serial.println( F("Publishing Message : ")); - Serial.println(data); - Serial.println(); + Serial.println(F("Publishing Message : ")); + Serial.println(data); + Serial.println(); #endif +#if DEBUG == 1 -#if DEBUG == 1 - - Serial.print( F("success : ") ); - Serial.println(pub); + Serial.print(F("success : ")); + Serial.println(pub); #endif - byte retries = 0; - //bool published = false; - bool published = client.publish( this->outTopic,(byte*) data,strlen(data),false ); - while (!published && retries < 5) - { - published = client.publish( this->outTopic,(byte*) data,strlen(data),false ); - if (!published ) - { - if (wifiManager.state() != 3) - { - Serial.println( F("No WiFi Re-connecting..")); - wifiManager.disconnect(); - delay(200); - wifiManager.begin(); - delay(200); - } - } - Serial.println( F("No MQTT, re-connecting..")); - connect(); - delay(100); //wait a little and treat network - yield(); - retries++; - } - delay(100); - yield(); - if (published) - { - Serial.println( F("Publish : OK")); - } - else - { - Serial.println( F("Published failed after 5 retries")); - } - return(published); - + byte retries = 0; + // bool published = false; + bool published = + client.publish(this->outTopic, (byte *)data, strlen(data), false); + while (!published && retries < 5) { + published = + client.publish(this->outTopic, (byte *)data, strlen(data), false); + if (!published) { + if (wifiManager.state() != 3) { + Serial.println(F("No WiFi Re-connecting..")); + wifiManager.disconnect(); + delay(200); + wifiManager.begin(); + delay(200); + } + } + Serial.println(F("No MQTT, re-connecting..")); + connect(); + delay(100); // wait a little and treat network + yield(); + retries++; + } + delay(100); + yield(); + if (published) { + Serial.println(F("Publish : OK")); + } else { + Serial.println(F("Published failed after 5 retries")); + } + return (published); } - /** -* CoolMQTT::mqttLoop(): -* This method is provided to allow the -* client to process the data -* -* \return true if successful,false -* otherwise -*/ -bool CoolMQTT::mqttLoop() -{ - - unsigned long lastTime=millis(); + * CoolMQTT::mqttLoop(): + * This method is provided to allow the + * client to process the data + * + * \return true if successful,false + * otherwise + */ +bool CoolMQTT::mqttLoop() { + + unsigned long lastTime = millis(); #if DEBUG == 1 - Serial.println( F("Entering CoolMQTT.mqttLoop()") ); - Serial.println(); + Serial.println(F("Entering CoolMQTT.mqttLoop()")); + Serial.println(); -#endif +#endif - while( ( millis() - lastTime ) < 1000) - { - this->client.loop(); - yield(); - } + while ((millis() - lastTime) < 1000) { + this->client.loop(); + yield(); + } -#if DEBUG == 1 - - Serial.print( F("loop result : ") ); - Serial.println( this->client.loop() ); - Serial.println(); +#if DEBUG == 1 + + Serial.print(F("loop result : ")); + Serial.println(this->client.loop()); + Serial.println(); #endif - return( this->client.loop() ); + return (this->client.loop()); } /** -* CoolMQTT::callback(in topic, incoming message , message length): -* This method is provided to handle incoming messages from the -* subscribed inTopic. -* -* Arguments are automatically assigned in client.setCallback() -*/ -void CoolMQTT::callback(char* topic, byte* payload, unsigned int length) -{ + * CoolMQTT::callback(in topic, incoming message , message length): + * This method is provided to handle incoming messages from the + * subscribed inTopic. + * + * Arguments are automatically assigned in client.setCallback() + */ +void CoolMQTT::callback(char *topic, byte *payload, unsigned int length) { #if DEBUG == 1 - Serial.println( F("Entering CoolMQTT.callback() ") ); - Serial.println(); - -#endif - - if(this->newMsg==false) - { - char temp[length+1]; - - #if DEBUG == 1 - - Serial.println( F("received temp msg : ") ); - - #endif - - for (unsigned int i = 0; i < length; i++) - { - temp[i]=(char)payload[i]; - - #if DEBUG == 1 - - Serial.print( (char)payload[i] ); - - #endif - - } - - #if DEBUG == 1 - - Serial.println(); - Serial.println( F("storing new message : ") ); - - Serial.print(F("length : ")); - Serial.println(length); - - Serial.print(F("size : ")); - Serial.print(sizeof(payload)); - Serial.println(); - - #endif - - this->newMsg=true; - - temp[length+1]='\0'; - - this->msg=String(temp); - this->msg.remove(length,1); - - #if DEBUG == 1 - - Serial.println( F("stored message : ") ); - Serial.println(this->msg); - - #endif - - } - else - { - - #if DEBUG == 1 - - Serial.println( F("did not read last message") ); - - #endif - - } + Serial.println(F("Entering CoolMQTT.callback() ")); + Serial.println(); -} +#endif -/** -* CoolMQTT::read(): -* This method is provided to return the last -* read message. -*/ -String CoolMQTT::read() -{ + if (this->newMsg == false) { + char temp[length + 1]; -#if DEBUG == 1 +#if DEBUG == 1 + + Serial.println(F("received temp msg : ")); - Serial.println( F("Entering CoolMQTT.read()") ); - Serial.println(); +#endif -#endif + for (unsigned int i = 0; i < length; i++) { + temp[i] = (char)payload[i]; - if(this->newMsg==true) - { - - this->newMsg=false; +#if DEBUG == 1 -#if DEBUG == 1 - Serial.println( F("received new message") ); - Serial.println( F("message : ") ); - Serial.println(this->msg); - Serial.println(); + Serial.print((char)payload[i]); #endif + } - return(this->msg); - - } - return(""); +#if DEBUG == 1 + + Serial.println(); + Serial.println(F("storing new message : ")); + + Serial.print(F("length : ")); + Serial.println(length); + + Serial.print(F("size : ")); + Serial.print(sizeof(payload)); + Serial.println(); + +#endif + + this->newMsg = true; + + temp[length + 1] = '\0'; + this->msg = String(temp); + this->msg.remove(length, 1); + +#if DEBUG == 1 + + Serial.println(F("stored message : ")); + Serial.println(this->msg); + +#endif + + } else { + +#if DEBUG == 1 + + Serial.println(F("did not read last message")); + +#endif + } } /** -* CoolMQTT::config(): -* This method is provided to configure -* the mqttClient : -server -* -inTopic -* -outTopic -* -client Id -* -buffer size -* -* \return true if successful,false otherwise -*/ -bool CoolMQTT::config() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolMQTT.config()") ); - Serial.println(); + * CoolMQTT::read(): + * This method is provided to return the last + * read message. + */ +String CoolMQTT::read() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolMQTT.read()")); + Serial.println(); #endif - //read config file - //update data - File configFile = SPIFFS.open("/mqttConfig.json", "r"); - - if (!configFile) - { - - #if DEBUG == 1 - - Serial.println( F("failed to read /mqttConfig.json") ); - Serial.println(); - - #endif - - return(false); - } - else - { - size_t size = configFile.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - configFile.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json ") ); - Serial.println(); - - #endif - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("configuration json is ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - - if(json["mqttServer"].success() ) - { - const char* tempmqttServer = json["mqttServer"]; - for(int i =0;i< 50 ;i++) - { - mqttServer[i]=tempmqttServer[i]; - } - } - else - { - for(int i =0;i< 50 ;i++) - { - this->mqttServer[i]=this->mqttServer[i]; - } - - } - json["mqttServer"]=this->mqttServer; - - - if(json["inTopic"].success() ) - { - const char* tempInTopic = json["inTopic"]; - for(int i =0;i< 50;i++) - { - inTopic[i]=tempInTopic[i]; - } - } - else - { - String tempMAC = WiFi.macAddress(); - tempMAC.replace(":",""); - snprintf(inTopic, 50, "$aws/things/%s/shadow/update/delta", tempMAC.c_str()); - - #if DEBUG == 1 - - Serial.print( F("Set Incomming MQTT Channel to : ") ); - Serial.println(inTopic); - - #endif - - } - json["inTopic"]=this->inTopic; - - - if(json["outTopic"].success() ) - { - const char* tempOutTopic = json["outTopic"]; - for(int i =0;i<50;i++) - { - outTopic[i]=tempOutTopic[i]; - } - } - else - { - String tempMAC = WiFi.macAddress(); - tempMAC.replace(":",""); - snprintf(outTopic, 50, "$aws/things/%s/shadow/update", tempMAC.c_str()); - - #if DEBUG == 1 - - Serial.print( F("Set Outgoing MQTT Channel to : ") ); - Serial.println(outTopic); - - #endif - - } - json["outTopic"]=this->outTopic; - - if(json["bufferSize"].success() ) - { - int tempBufferSize = json["bufferSize"]; - bufferSize=tempBufferSize; - } - else - { - this->bufferSize=this->bufferSize; - } - json["bufferSize"]=this->bufferSize; - - configFile.close(); - configFile = SPIFFS.open("/mqttConfig.json", "w"); - if(!configFile) - { - - #if DEBUG == 1 - - Serial.println( F("failed to write to /mqttConfig.json") ); - - #endif - - return(false); - } - - json.printTo(configFile); - configFile.close(); - - #if DEBUG == 1 - - Serial.println( F("saved configuration is :") ); - json.printTo(Serial); - Serial.println(); - - #endif - - return(true); - } - } - + if (this->newMsg == true) { + + this->newMsg = false; + +#if DEBUG == 1 + Serial.println(F("received new message")); + Serial.println(F("message : ")); + Serial.println(this->msg); + Serial.println(); +#endif + + return (this->msg); + } + return (""); } /** -* CoolMQTT::config(server,in topic, out topic, buffer size): -* This method is provided to manually configure the mqtt client -* -*/ -void CoolMQTT::config(const char mqttServer[], const char inTopic[], const char outTopic[], int bufferSize) -{ + * CoolMQTT::config(): + * This method is provided to configure + * the mqttClient : -server + * -inTopic + * -outTopic + * -client Id + * -buffer size + * + * \return true if successful,false otherwise + */ +bool CoolMQTT::config() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolMQTT.config()")); + Serial.println(); + +#endif + + // read config file + // update data + File configFile = SPIFFS.open("/mqttConfig.json", "r"); + + if (!configFile) { + +#if DEBUG == 1 + + Serial.println(F("failed to read /mqttConfig.json")); + Serial.println(); + +#endif + + return (false); + } else { + size_t size = configFile.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + configFile.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + +#if DEBUG == 1 + + Serial.println(F("failed to parse json ")); + Serial.println(); + +#endif + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("configuration json is ")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + if (json["mqttServer"].success()) { + const char *tempmqttServer = json["mqttServer"]; + for (int i = 0; i < 50; i++) { + mqttServer[i] = tempmqttServer[i]; + } + } else { + for (int i = 0; i < 50; i++) { + this->mqttServer[i] = this->mqttServer[i]; + } + } + json["mqttServer"] = this->mqttServer; + + if (json["inTopic"].success()) { + const char *tempInTopic = json["inTopic"]; + for (int i = 0; i < 50; i++) { + inTopic[i] = tempInTopic[i]; + } + } else { + String tempMAC = WiFi.macAddress(); + tempMAC.replace(":", ""); + snprintf(inTopic, 50, "$aws/things/%s/shadow/update/delta", + tempMAC.c_str()); + +#if DEBUG == 1 + + Serial.print(F("Set Incomming MQTT Channel to : ")); + Serial.println(inTopic); + +#endif + } + json["inTopic"] = this->inTopic; + + if (json["outTopic"].success()) { + const char *tempOutTopic = json["outTopic"]; + for (int i = 0; i < 50; i++) { + outTopic[i] = tempOutTopic[i]; + } + } else { + String tempMAC = WiFi.macAddress(); + tempMAC.replace(":", ""); + snprintf(outTopic, 50, "$aws/things/%s/shadow/update", tempMAC.c_str()); + +#if DEBUG == 1 + + Serial.print(F("Set Outgoing MQTT Channel to : ")); + Serial.println(outTopic); + +#endif + } + json["outTopic"] = this->outTopic; + + if (json["bufferSize"].success()) { + int tempBufferSize = json["bufferSize"]; + bufferSize = tempBufferSize; + } else { + this->bufferSize = this->bufferSize; + } + json["bufferSize"] = this->bufferSize; + + configFile.close(); + configFile = SPIFFS.open("/mqttConfig.json", "w"); + if (!configFile) { #if DEBUG == 1 - Serial.println( F("Entering CoolMQTT.config() , no SPIFFS variant") ); - Serial.println(); + Serial.println(F("failed to write to /mqttConfig.json")); #endif - for(int i =0;i< 50 ;i++) - { - this->mqttServer[i] = mqttServer[i]; - this->inTopic[i] = inTopic[i]; - this->outTopic[i] = outTopic[i]; - } - this->bufferSize = bufferSize; - + return (false); + } + json.printTo(configFile); + configFile.close(); + +#if DEBUG == 1 + + Serial.println(F("saved configuration is :")); + json.printTo(Serial); + Serial.println(); + +#endif + + return (true); + } + } } /** -* CoolMQTT::printConf(): -* This method is provided to print the -* configuration to the Serial Monitor -*/ -void CoolMQTT::printConf() -{ + * CoolMQTT::config(server,in topic, out topic, buffer size): + * This method is provided to manually configure the mqtt client + * + */ +void CoolMQTT::config(const char mqttServer[], const char inTopic[], + const char outTopic[], int bufferSize) { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering CoolMQTT.printConf()") ); - Serial.println(); + Serial.println(F("Entering CoolMQTT.config() , no SPIFFS variant")); + Serial.println(); #endif - - Serial.println("MQTT configuration "); - Serial.print("mqttServer : "); - Serial.println(this->mqttServer); + for (int i = 0; i < 50; i++) { + this->mqttServer[i] = mqttServer[i]; + this->inTopic[i] = inTopic[i]; + this->outTopic[i] = outTopic[i]; + } + this->bufferSize = bufferSize; +} + +/** + * CoolMQTT::printConf(): + * This method is provided to print the + * configuration to the Serial Monitor + */ +void CoolMQTT::printConf() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolMQTT.printConf()")); + Serial.println(); + +#endif - Serial.print("inTopic : "); - Serial.println(this->inTopic); + Serial.println("MQTT configuration "); - Serial.print("outTopic : "); - Serial.println(this->outTopic); + Serial.print("mqttServer : "); + Serial.println(this->mqttServer); - Serial.print("bufferSize : "); - Serial.println(this->bufferSize); + Serial.print("inTopic : "); + Serial.println(this->inTopic); - Serial.println(); + Serial.print("outTopic : "); + Serial.println(this->outTopic); + Serial.print("bufferSize : "); + Serial.println(this->bufferSize); -} \ No newline at end of file + Serial.println(); +} diff --git a/src/CoolMQTT.h b/src/CoolMQTT.h index dfb2d7ff..bd2d0819 100644 --- a/src/CoolMQTT.h +++ b/src/CoolMQTT.h @@ -1,115 +1,111 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef CoolMQTT_H #define CoolMQTT_H -#include"Arduino.h" -#include -#include "internals/CoolPubSubClient.h" +#include "Arduino.h" #include "CoolWifi.h" +#include "internals/CoolPubSubClient.h" +#include /** -* \class CoolMQTT -* \brief This class handles the mqtt client -*/ -class CoolMQTT -{ + * \class CoolMQTT + * \brief This class handles the mqtt client + */ +class CoolMQTT { public: + void begin(); - void begin(); - - int connect(); + int connect(); - bool publish(const char* data); + bool publish(const char *data); - bool publish(const char* data,unsigned long logInterval); + bool publish(const char *data, unsigned long logInterval); - String read(); + String read(); - void config(const char mqttServer[],const char inTopic[],const char outTopic[],int bufferSize); + void config(const char mqttServer[], const char inTopic[], + const char outTopic[], int bufferSize); - bool config(); + bool config(); - void callback(char* topic, byte* payload, unsigned int length); + void callback(char *topic, byte *payload, unsigned int length); - void printConf(); + void printConf(); - int state(); + int state(); - bool mqttLoop(); + bool mqttLoop(); - String getUser(); + String getUser(); private: - - /** - * MQTT Server name/ip - */ - char mqttServer[50]={'0'}; - - /** - * String to store incoming messages - */ - String msg=""; - - /** - * MQTT Topic to subscribe/listen to - */ - char inTopic[50]={'0'}; - - /** - * MQTT topic to publish/write to - */ - char outTopic[50]={'0'}; - - /** - * MQTT inner bufferSize - */ - int bufferSize=3000; - - /** - * WifiClient instance - */ - WiFiClient espClient; - - /** - * MQTT Client instance - */ - CoolPubSubClient client; - - /** - * WiFi Client instance - */ - CoolWifi wifiManager; - - /** - * new message flag - */ - bool newMsg=0; - + /** + * MQTT Server name/ip + */ + char mqttServer[50] = {'0'}; + + /** + * String to store incoming messages + */ + String msg = ""; + + /** + * MQTT Topic to subscribe/listen to + */ + char inTopic[50] = {'0'}; + + /** + * MQTT topic to publish/write to + */ + char outTopic[50] = {'0'}; + + /** + * MQTT inner bufferSize + */ + int bufferSize = 3000; + + /** + * WifiClient instance + */ + WiFiClient espClient; + + /** + * MQTT Client instance + */ + CoolPubSubClient client; + + /** + * WiFi Client instance + */ + CoolWifi wifiManager; + + /** + * new message flag + */ + bool newMsg = 0; }; #endif diff --git a/src/CoolTime.cpp b/src/CoolTime.cpp index 96b2d611..393e1025 100644 --- a/src/CoolTime.cpp +++ b/src/CoolTime.cpp @@ -1,26 +1,25 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #include "FS.h" @@ -32,916 +31,829 @@ #include "TimeLib.h" - #define DEBUG 0 - /** -* CoolTime::begin(): -* This method is provided to init -* the udp connection -* -*/ -void CoolTime::begin() -{ + * CoolTime::begin(): + * This method is provided to init + * the udp connection + * + */ +void CoolTime::begin() { + +#if DEBUG == 1 -#if DEBUG == 1 + Serial.println(F("Entering CoolTime.begin()")); + Serial.println(); - Serial.println( F("Entering CoolTime.begin()") ); - Serial.println(); +#endif -#endif + Udp.begin(localPort); - Udp.begin(localPort); - - this->update(); - + this->update(); } /** -* CoolTime::offGrid: -* This method is provided to init -* the udp connection -* -*/ -void CoolTime::offGrid() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolTime.offGrid()") ); - Serial.println(); - -#endif - if (compileTime == 1 && NTP == 0) - { - char posMarker = 0; - for (int i = 0; i <= sizeof(__TIMESTAMP__); i++) - { - if (__TIMESTAMP__[i] == ':') - { -#if DEBUG == 1 - Serial.print("position of ':' : "); - Serial.println(i); -#endif - posMarker = i; - break; - } - } - char monthAbbr[4] = {__TIMESTAMP__[4],__TIMESTAMP__[5],__TIMESTAMP__[6], 0}; - - char tempDay[3] = {__TIMESTAMP__[8], __TIMESTAMP__[9], 0}; - int Day = atoi(&tempDay[0]); - - char tempHour[3] = {__TIMESTAMP__[posMarker - 2], __TIMESTAMP__[posMarker - 1], '\0'}; - int Hour = atoi(&tempHour[0]); - - char tempMinute[3] = {__TIMESTAMP__[posMarker + 1], __TIMESTAMP__[posMarker + 2], '\0'}; - int Minute = atoi(&tempMinute[0]); - - char tempSecond[3] = {__TIMESTAMP__[posMarker + 4], __TIMESTAMP__[posMarker + 5], '\0'}; - int Second = atoi(&tempSecond[0]); - - char tempYear[3] = { __TIMESTAMP__[posMarker + 9],__TIMESTAMP__[posMarker + 10], '\0'}; //__TIMESTAMP__[posMarker + 7],__TIMESTAMP__[posMarker + 8], - int Year = atoi(&tempYear[0]); - int Month; - - if (strstr (monthAbbr, "Jan")) - { - //Serial.println(F("Month January")); - Month=1; - } - if (strstr(monthAbbr, "Feb")) - { - //Serial.println(F("Month February")); - Month=2; - } - if (strstr(monthAbbr, "Mar")) - { - //Serial.println(F("Month March")); - Month=3; - } - if (strstr(monthAbbr, "Apr")) - { - //Serial.println(F("Month April")); - Month=4; - } - if (strstr(monthAbbr, "May")) - { - //Serial.println(F("Month May")); - Month=5; - } - if (strstr(monthAbbr, "Jun")) - { - //Serial.println(F("Month June")); - Month=6; - } - if (strstr(monthAbbr, "Jul")) - { - //Serial.println(F("Month July")); - Month=7; - } - if (strstr(monthAbbr, "Aug")) - { - //Serial.println(F("Month August")); - Month=8; - } - if (strstr(monthAbbr, "Sep")) - { - //Serial.println(F("Month September")); - Month=9; - } - if (strstr(monthAbbr, "Oct")) - { - //Serial.println(F("Month October")); - Month=10; - } - if (strstr(monthAbbr, "Nov")) - { - //Serial.println(F("Month november")); - Month=11; - } - if (strstr(monthAbbr, "Dec")) - { - //Serial.println(F("Month december")); - Month=12; - } - //tmElements_t tm; - setDateTime(y2kYearToTm(Year), Month, Day, Hour, Minute, Second); - unsigned long instantTime = RTC.get(CLOCK_ADDRESS); - this->timeSync = instantTime; - this->compileTime = 0; - saveTimeSync(); - -#if DEBUG == 1 - Serial.print(F ("compileTime : ")); - Serial.println(__TIMESTAMP__); - Serial.print(F ("Month Abbrevation : ")); - Serial.println(monthAbbr); - Serial.print(F ("Day : ")); - Serial.println(Day); - Serial.print(F ("Month : ")); - Serial.println(Month); - Serial.print(F ("Year : ")); - Serial.println(Year); - Serial.print(F ("Hour : ")); - Serial.println(Hour); - Serial.print(F ("Minute : ")); - Serial.println(Minute); - Serial.print(F ("Seconds : ")); - Serial.println(Second); -#endif - Serial.println("RTC set from __TIMESTAMP__"); - Serial.print("Seconds since 1970 : "); - Serial.println(instantTime); - } + * CoolTime::offGrid: + * This method is provided to init + * the udp connection + * + */ +void CoolTime::offGrid() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolTime.offGrid()")); + Serial.println(); + +#endif + if (compileTime == 1 && NTP == 0) { + char posMarker = 0; + for (int i = 0; i <= sizeof(__TIMESTAMP__); i++) { + if (__TIMESTAMP__[i] == ':') { +#if DEBUG == 1 + Serial.print("position of ':' : "); + Serial.println(i); +#endif + posMarker = i; + break; + } + } + char monthAbbr[4] = {__TIMESTAMP__[4], __TIMESTAMP__[5], __TIMESTAMP__[6], + 0}; + + char tempDay[3] = {__TIMESTAMP__[8], __TIMESTAMP__[9], 0}; + int Day = atoi(&tempDay[0]); + + char tempHour[3] = {__TIMESTAMP__[posMarker - 2], + __TIMESTAMP__[posMarker - 1], '\0'}; + int Hour = atoi(&tempHour[0]); + + char tempMinute[3] = {__TIMESTAMP__[posMarker + 1], + __TIMESTAMP__[posMarker + 2], '\0'}; + int Minute = atoi(&tempMinute[0]); + + char tempSecond[3] = {__TIMESTAMP__[posMarker + 4], + __TIMESTAMP__[posMarker + 5], '\0'}; + int Second = atoi(&tempSecond[0]); + + char tempYear[3] = { + __TIMESTAMP__[posMarker + 9], __TIMESTAMP__[posMarker + 10], + '\0'}; //__TIMESTAMP__[posMarker + 7],__TIMESTAMP__[posMarker + 8], + int Year = atoi(&tempYear[0]); + int Month; + + if (strstr(monthAbbr, "Jan")) { + // Serial.println(F("Month January")); + Month = 1; + } + if (strstr(monthAbbr, "Feb")) { + // Serial.println(F("Month February")); + Month = 2; + } + if (strstr(monthAbbr, "Mar")) { + // Serial.println(F("Month March")); + Month = 3; + } + if (strstr(monthAbbr, "Apr")) { + // Serial.println(F("Month April")); + Month = 4; + } + if (strstr(monthAbbr, "May")) { + // Serial.println(F("Month May")); + Month = 5; + } + if (strstr(monthAbbr, "Jun")) { + // Serial.println(F("Month June")); + Month = 6; + } + if (strstr(monthAbbr, "Jul")) { + // Serial.println(F("Month July")); + Month = 7; + } + if (strstr(monthAbbr, "Aug")) { + // Serial.println(F("Month August")); + Month = 8; + } + if (strstr(monthAbbr, "Sep")) { + // Serial.println(F("Month September")); + Month = 9; + } + if (strstr(monthAbbr, "Oct")) { + // Serial.println(F("Month October")); + Month = 10; + } + if (strstr(monthAbbr, "Nov")) { + // Serial.println(F("Month november")); + Month = 11; + } + if (strstr(monthAbbr, "Dec")) { + // Serial.println(F("Month december")); + Month = 12; + } + // tmElements_t tm; + setDateTime(y2kYearToTm(Year), Month, Day, Hour, Minute, Second); + unsigned long instantTime = RTC.get(CLOCK_ADDRESS); + this->timeSync = instantTime; + this->compileTime = 0; + saveTimeSync(); + +#if DEBUG == 1 + Serial.print(F("compileTime : ")); + Serial.println(__TIMESTAMP__); + Serial.print(F("Month Abbrevation : ")); + Serial.println(monthAbbr); + Serial.print(F("Day : ")); + Serial.println(Day); + Serial.print(F("Month : ")); + Serial.println(Month); + Serial.print(F("Year : ")); + Serial.println(Year); + Serial.print(F("Hour : ")); + Serial.println(Hour); + Serial.print(F("Minute : ")); + Serial.println(Minute); + Serial.print(F("Seconds : ")); + Serial.println(Second); +#endif + Serial.println("RTC set from __TIMESTAMP__"); + Serial.print("Seconds since 1970 : "); + Serial.println(instantTime); + } } /** -* CoolTime::update(): -* This method is provided to correct the -* rtc Time when it drifts,once every week. -*/ -void CoolTime::update() -{ + * CoolTime::update(): + * This method is provided to correct the + * rtc Time when it drifts,once every week. + */ +void CoolTime::update() { #if DEBUG == 1 - Serial.println( F("Entering CoolTime.update()") ); - Serial.println(); + Serial.println(F("Entering CoolTime.update()")); + Serial.println(); -#endif - if (this->NTP == 1) //ensure that NTP is accessible!!! - { - if( !( this->isTimeSync() ) ) - { - - #if DEBUG == 1 +#endif + if (this->NTP == 1) // ensure that NTP is accessible!!! + { + if (!(this->isTimeSync())) { + +#if DEBUG == 1 - Serial.println( F("waiting for sync") ); - Serial.println(); + Serial.println(F("waiting for sync")); + Serial.println(); - #endif +#endif - this->timeSync=this->getNtpTime(); - breakTime(this->getNtpTime(), this->tmSet); - this->rtc.set(makeTime(this->tmSet), CLOCK_ADDRESS); // set the clock - this->saveTimeSync(); - } - } + this->timeSync = this->getNtpTime(); + breakTime(this->getNtpTime(), this->tmSet); + this->rtc.set(makeTime(this->tmSet), CLOCK_ADDRESS); // set the clock + this->saveTimeSync(); + } + } } /** -* CoolTime::setDateTime(year,month,dat,hour,minutes,seconds): -* This method is provided to manually set the RTc Time -* -*/ -void CoolTime::setDateTime(int year, int month, int day, int hour, int minutes, int seconds) -{ + * CoolTime::setDateTime(year,month,dat,hour,minutes,seconds): + * This method is provided to manually set the RTc Time + * + */ +void CoolTime::setDateTime(int year, int month, int day, int hour, int minutes, + int seconds) { #if DEBUG == 1 - Serial.println( F("Entering CoolTime.setDateTime") ); - Serial.println(); + Serial.println(F("Entering CoolTime.setDateTime")); + Serial.println(); #endif - tmElements_t tm; - tm.Second=seconds; - tm.Minute=minutes; - tm.Hour=hour; - tm.Day=day; - tm.Month=month; - tm.Year=year; - this->rtc.set(makeTime(tm),CLOCK_ADDRESS); + tmElements_t tm; + tm.Second = seconds; + tm.Minute = minutes; + tm.Hour = hour; + tm.Day = day; + tm.Month = month; + tm.Year = year; + this->rtc.set(makeTime(tm), CLOCK_ADDRESS); #if DEBUG == 1 - Serial.print( F("setting time to : ") );//"20yy-mm-ddT00:00:00Z + Serial.print(F("setting time to : ")); //"20yy-mm-ddT00:00:00Z - Serial.print(tm.Year); - Serial.print( F("-") ); - Serial.print( this->formatDigits( tm.Month ) ); - Serial.print( F("-") ); - Serial.print( this->formatDigits( tm.Day ) ); - Serial.print( F("T") ); - Serial.print( this->formatDigits( tm.Hour ) ); - Serial.print( F(":") ); - Serial.print( this->formatDigits( tm.Minute ) ); - Serial.print( F(":") ); - Serial.print( this->formatDigits( tm.Second ) ); - Serial.print( F("Z") ); + Serial.print(tm.Year); + Serial.print(F("-")); + Serial.print(this->formatDigits(tm.Month)); + Serial.print(F("-")); + Serial.print(this->formatDigits(tm.Day)); + Serial.print(F("T")); + Serial.print(this->formatDigits(tm.Hour)); + Serial.print(F(":")); + Serial.print(this->formatDigits(tm.Minute)); + Serial.print(F(":")); + Serial.print(this->formatDigits(tm.Second)); + Serial.print(F("Z")); - Serial.println(); - - Serial.print( F("time set to : ") ); - Serial.println(this->getESDate()); - Serial.println(); + Serial.println(); -#endif + Serial.print(F("time set to : ")); + Serial.println(this->getESDate()); + Serial.println(); +#endif } /** -* CoolTime::getTimeDate(): -* This method is provided to get the RTC Time -* -* \returns a tmElements_t structre that has -* the time in it -*/ -tmElements_t CoolTime::getTimeDate() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolTime.getTimeDate()") ); - Serial.println(); - -#endif - - tmElements_t tm; - rtc.get(CLOCK_ADDRESS); //experimental to prevent slow rtc data - delay(50); - time_t timeDate = this->rtc.get(CLOCK_ADDRESS); - breakTime(timeDate,tm); - -#if DEBUG == 1 - - Serial.print( F("time is : ") ); - Serial.print(tm.Year+ 1970 ); - Serial.print( F("-") ); - Serial.print( this->formatDigits( tm.Month ) ); - Serial.print( F("-") ); - Serial.print( this->formatDigits( tm.Day ) ); - Serial.print( F("T") ); - Serial.print( this->formatDigits( tm.Hour ) ); - Serial.print( F(":") ); - Serial.print( this->formatDigits( tm.Minute ) ); - Serial.print( F(":") ); - Serial.print( this->formatDigits( tm.Second ) ); - Serial.print( F("Z") ); - -#endif - - return(tm); + * CoolTime::getTimeDate(): + * This method is provided to get the RTC Time + * + * \returns a tmElements_t structre that has + * the time in it + */ +tmElements_t CoolTime::getTimeDate() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolTime.getTimeDate()")); + Serial.println(); + +#endif + + tmElements_t tm; + rtc.get(CLOCK_ADDRESS); // experimental to prevent slow rtc data + delay(50); + time_t timeDate = this->rtc.get(CLOCK_ADDRESS); + breakTime(timeDate, tm); + +#if DEBUG == 1 + + Serial.print(F("time is : ")); + Serial.print(tm.Year + 1970); + Serial.print(F("-")); + Serial.print(this->formatDigits(tm.Month)); + Serial.print(F("-")); + Serial.print(this->formatDigits(tm.Day)); + Serial.print(F("T")); + Serial.print(this->formatDigits(tm.Hour)); + Serial.print(F(":")); + Serial.print(this->formatDigits(tm.Minute)); + Serial.print(F(":")); + Serial.print(this->formatDigits(tm.Second)); + Serial.print(F("Z")); + +#endif + + return (tm); } /** -* CoolTime::getESD(): -* This method is provided to return an -* Elastic Search compatible date Format -* -* \return date String in Elastic Search -* format -*/ -String CoolTime::getESDate() -{ + * CoolTime::getESD(): + * This method is provided to return an + * Elastic Search compatible date Format + * + * \return date String in Elastic Search + * format + */ +String CoolTime::getESDate() { + +#if DEBUG == 1 -#if DEBUG == 1 + Serial.println(F("Entering CoolTime.getESDate()")); + Serial.println(); - Serial.println( F("Entering CoolTime.getESDate()") ); - Serial.println(); +#endif -#endif + tmElements_t tm = this->getTimeDate(); - tmElements_t tm=this->getTimeDate(); + //"20yy-mm-ddT00:00:00Z" + String elasticSearchString = + String(tm.Year + 1970) + "-" + this->formatDigits(tm.Month) + "-"; - //"20yy-mm-ddT00:00:00Z" - String elasticSearchString =String(tm.Year+1970)+"-"+this->formatDigits(tm.Month)+"-"; + elasticSearchString += + this->formatDigits(tm.Day) + "T" + this->formatDigits(tm.Hour) + ":"; - elasticSearchString +=this->formatDigits(tm.Day)+"T"+this->formatDigits(tm.Hour)+":"; - - elasticSearchString +=this->formatDigits(tm.Minute)+":"+this->formatDigits(tm.Second)+"Z"; + elasticSearchString += + this->formatDigits(tm.Minute) + ":" + this->formatDigits(tm.Second) + "Z"; -#if DEBUG == 1 +#if DEBUG == 1 - Serial.print( F("elastic Search date : ") ); - Serial.println(elasticSearchString); - Serial.println(); + Serial.print(F("elastic Search date : ")); + Serial.println(elasticSearchString); + Serial.println(); #endif - return (elasticSearchString); + return (elasticSearchString); } /** -* CoolTime::getLastSyncTime(): -* This method is provided to get the last time -* we syncronised the time -* -* \return unsigned long representation of -* last syncronisation time in seconds -*/ -unsigned long CoolTime::getLastSyncTime() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolTime.getLastSyncTime()") ); - Serial.println(); - - Serial.print( F("last sync time : ") ); - Serial.println(this->timeSync); - -#endif - - return(this->timeSync); -} + * CoolTime::getLastSyncTime(): + * This method is provided to get the last time + * we syncronised the time + * + * \return unsigned long representation of + * last syncronisation time in seconds + */ +unsigned long CoolTime::getLastSyncTime() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolTime.getLastSyncTime()")); + Serial.println(); + + Serial.print(F("last sync time : ")); + Serial.println(this->timeSync); + +#endif + return (this->timeSync); +} /** -* CoolTime::isTimeSync( time in seconds): -* This method is provided to test if the -* time is syncronised or not. -* By default we test once per week. -* -* \return true if time is syncronised,false -* otherwise -*/ -bool CoolTime::isTimeSync(unsigned long seconds) -{ + * CoolTime::isTimeSync( time in seconds): + * This method is provided to test if the + * time is syncronised or not. + * By default we test once per week. + * + * \return true if time is syncronised,false + * otherwise + */ +bool CoolTime::isTimeSync(unsigned long seconds) { #if DEBUG == 1 - Serial.println( F("Entering CoolTime.isTimeSync() ") ); - Serial.println(); + Serial.println(F("Entering CoolTime.isTimeSync() ")); + Serial.println(); + +#endif -#endif + // expermental to prevent slow rtc data + RTC.get(CLOCK_ADDRESS); + unsigned long instantTime = RTC.get(CLOCK_ADDRESS); - // expermental to prevent slow rtc data - RTC.get(CLOCK_ADDRESS); - unsigned long instantTime = RTC.get(CLOCK_ADDRESS); - #if DEBUG == 1 - Serial.print(F("Instant Time : ")); - Serial.println(instantTime); - Serial.print(F("Last Sync : ")); - Serial.println(this->getLastSyncTime()); - unsigned long testSync = instantTime - this->timeSync; - Serial.print(F("Time since sy : ")); - Serial.println(testSync); + Serial.print(F("Instant Time : ")); + Serial.println(instantTime); + Serial.print(F("Last Sync : ")); + Serial.println(this->getLastSyncTime()); + unsigned long testSync = instantTime - this->timeSync; + Serial.print(F("Time since sy : ")); + Serial.println(testSync); #endif - //default is once per week we try to get a time update + // default is once per week we try to get a time update - if(( instantTime - this->timeSync) > ( seconds ) ) - { + if ((instantTime - this->timeSync) > (seconds)) { - Serial.println( F("time is not syncronised ") ); - - return(false); - } - - Serial.println( F("time is syncronised : OK") ); - Serial.println(); + Serial.println(F("time is not syncronised ")); - return(true); -} + return (false); + } + + Serial.println(F("time is syncronised : OK")); + Serial.println(); + return (true); +} /** -* CoolTime::getNtopTime(): -* This method is provided to get the -* Time through an NTP request to -* a Time Server -* -* \return a time_t (unsigned long ) timestamp in seconds -*/ -time_t CoolTime::getNtpTime() -{ + * CoolTime::getNtopTime(): + * This method is provided to get the + * Time through an NTP request to + * a Time Server + * + * \return a time_t (unsigned long ) timestamp in seconds + */ +time_t CoolTime::getNtpTime() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering CoolTime.getNtpTime()") ); - Serial.println(); + Serial.println(F("Entering CoolTime.getNtpTime()")); + Serial.println(); -#endif +#endif - while (Udp.parsePacket() > 0) ; // discard any previously received packets + while (Udp.parsePacket() > 0) + ; // discard any previously received packets - WiFi.hostByName(timeServer, timeServerIP); + WiFi.hostByName(timeServer, timeServerIP); #if DEBUG == 1 - Serial.println(timeServer); - Serial.println(timeServerIP); + Serial.println(timeServer); + Serial.println(timeServerIP); #endif - Serial.println( F("Transmit NTP Request") ); + Serial.println(F("Transmit NTP Request")); - sendNTPpacket(timeServerIP); + sendNTPpacket(timeServerIP); - uint32_t beginWait = millis(); + uint32_t beginWait = millis(); - while (millis() - beginWait < 2000) - { - int size = Udp.parsePacket(); - if (size >= NTP_PACKET_SIZE) - { - - #if DEBUG == 1 + while (millis() - beginWait < 2000) { + int size = Udp.parsePacket(); + if (size >= NTP_PACKET_SIZE) { - Serial.println( F("Receive NTP Response") ); - - #endif +#if DEBUG == 1 - Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer - unsigned long secsSince1900; - // convert four bytes starting at location 40 to a long integer - secsSince1900 = (unsigned long)packetBuffer[40] << 24; - secsSince1900 |= (unsigned long)packetBuffer[41] << 16; - secsSince1900 |= (unsigned long)packetBuffer[42] << 8; - secsSince1900 |= (unsigned long)packetBuffer[43]; - - #if DEBUG == 1 - - Serial.print( F("received unix time : ") ); - Serial.println(secsSince1900 - 2208988800UL); - Serial.println(); + Serial.println(F("Receive NTP Response")); - #endif +#endif - return secsSince1900 - 2208988800UL ; - } - } - - Serial.println( F("No NTP Response :-(") ); + Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer + unsigned long secsSince1900; + // convert four bytes starting at location 40 to a long integer + secsSince1900 = (unsigned long)packetBuffer[40] << 24; + secsSince1900 |= (unsigned long)packetBuffer[41] << 16; + secsSince1900 |= (unsigned long)packetBuffer[42] << 8; + secsSince1900 |= (unsigned long)packetBuffer[43]; - return 0; // return 0 if unable to get the time +#if DEBUG == 1 + + Serial.print(F("received unix time : ")); + Serial.println(secsSince1900 - 2208988800UL); + Serial.println(); + +#endif + + return secsSince1900 - 2208988800UL; + } + } + + Serial.println(F("No NTP Response :-(")); + + return 0; // return 0 if unable to get the time } /** -* CoolTime::sendNTPpacket( Time Server IP address): -* This method is provided to send an NTP request to -* the time server at the given address -*/ -void CoolTime::sendNTPpacket(IPAddress &address) -{ - -#if DEBUG == 1 - - Serial.println( F("Enter CoolTime.sendNTPpacket()") ); - Serial.println(); - -#endif - - memset(packetBuffer, 0, NTP_PACKET_SIZE); - // Initialize values needed to form NTP request - // (see URL above for details on the packets) - packetBuffer[0] = 0b11100011; // LI, Version, Mode - packetBuffer[1] = 0; // Stratum, or type of clock - packetBuffer[2] = 6; // Polling Interval - packetBuffer[3] = 0xEC; // Peer Clock Precision - // 8 bytes of zero for Root Delay & Root Dispersion - packetBuffer[12] = 49; - packetBuffer[13] = 0x4E; - packetBuffer[14] = 49; - packetBuffer[15] = 52; - // all NTP fields have been given values, now - // you can send a packet requesting a timestamp: - Udp.beginPacket(address, 123); //NTP requests are to port 123 - Udp.write(packetBuffer, NTP_PACKET_SIZE); - Udp.endPacket(); + * CoolTime::sendNTPpacket( Time Server IP address): + * This method is provided to send an NTP request to + * the time server at the given address + */ +void CoolTime::sendNTPpacket(IPAddress &address) { + +#if DEBUG == 1 + + Serial.println(F("Enter CoolTime.sendNTPpacket()")); + Serial.println(); + +#endif + + memset(packetBuffer, 0, NTP_PACKET_SIZE); + // Initialize values needed to form NTP request + // (see URL above for details on the packets) + packetBuffer[0] = 0b11100011; // LI, Version, Mode + packetBuffer[1] = 0; // Stratum, or type of clock + packetBuffer[2] = 6; // Polling Interval + packetBuffer[3] = 0xEC; // Peer Clock Precision + // 8 bytes of zero for Root Delay & Root Dispersion + packetBuffer[12] = 49; + packetBuffer[13] = 0x4E; + packetBuffer[14] = 49; + packetBuffer[15] = 52; + // all NTP fields have been given values, now + // you can send a packet requesting a timestamp: + Udp.beginPacket(address, 123); // NTP requests are to port 123 + Udp.write(packetBuffer, NTP_PACKET_SIZE); + Udp.endPacket(); } /** -* CoolTime::config(Time server IP , udp Port): -* This method is provided to do manual configuration. -* -*/ -void CoolTime::config(IPAddress timeServer,unsigned int localPort) -{ + * CoolTime::config(Time server IP , udp Port): + * This method is provided to do manual configuration. + * + */ +void CoolTime::config(IPAddress timeServer, unsigned int localPort) { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Enter CoomTime.config() , no SPIFFS variant ") ); - Serial.println(); + Serial.println(F("Enter CoomTime.config() , no SPIFFS variant ")); + Serial.println(); -#endif +#endif - this->timeServerIP=timeServerIP; - this->localPort=localPort; - -} + this->timeServerIP = timeServerIP; + this->localPort = localPort; +} /** -* CoolTime::config(): -* This method is provided to configure -* the CoolTime object through a configuration -* file. -* -* \return true if successful,false otherwise -*/ -bool CoolTime::config() -{ - -#if DEBUG == 1 - - Serial.println( F("Enter CoolTime.config()") ); - Serial.println(); - -#endif - - File rtcConfig = SPIFFS.open("/rtcConfig.json", "r"); - - if (!rtcConfig) - { - - Serial.println( F("failed to read /rtcConfig.json") ); - Serial.println(); - - return(false); - } - else - { - size_t size = rtcConfig.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - rtcConfig.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - Serial.println( F("failed to parse rtcConfig json") ); - Serial.println(); - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("configuration json is :") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - - if(json["timeServer"].success() ) - { - const char* tempServer = json["timeServer"]; - for(int i =0;i<50;i++) - { - timeServer[i]=tempServer[i]; - } - } - else - { - for(int i=0;i<50;i++) - { - this->timeServer[i]=this->timeServer[i]; - } - } - json["timeServer"]=this->timeServer; - - if(json["localPort"].success() ) - { - this->localPort=json["localPort"]; - } - else - { - this->localPort=this->localPort; - } - json["localPort"]=this->localPort; - - - if( json["timeSync"].success() ) - { - - this->timeSync=json["timeSync"]; - } - else - { - this->timeSync=this->timeSync; - } - json["timeSync"]=this->timeSync; - - if( json["NTP"].success() ) - { - - this->NTP=json["NTP"].as(); - } - else - { - this->NTP=this->NTP; - } - json["NTP"]=this->NTP; - - if( json["compileTime"].success() ) //Get the compile Flag and immediatly reset it to prevent re-reading on a later startup - { - - this->compileTime=json["compileTime"].as(); - } - else - { - this->compileTime=this->compileTime; - } - json["compileTime"]=this->compileTime; - - rtcConfig.close(); - rtcConfig= SPIFFS.open("/rtcConfig.json", "w"); - - if(!rtcConfig) - { - - #if DEBUG == 1 - - Serial.println( F("failed to write to /rtcConfig.json") ); - Serial.println(); - - #endif - - return(false); - } - - json.printTo(rtcConfig); - rtcConfig.close(); - - #if DEBUG == 1 - - Serial.println( F("configuration is :") ); - json.printTo(Serial); - Serial.println(); - - #endif - - return(true); - } - } + * CoolTime::config(): + * This method is provided to configure + * the CoolTime object through a configuration + * file. + * + * \return true if successful,false otherwise + */ +bool CoolTime::config() { + +#if DEBUG == 1 + + Serial.println(F("Enter CoolTime.config()")); + Serial.println(); + +#endif + + File rtcConfig = SPIFFS.open("/rtcConfig.json", "r"); + + if (!rtcConfig) { + + Serial.println(F("failed to read /rtcConfig.json")); + Serial.println(); + + return (false); + } else { + size_t size = rtcConfig.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + rtcConfig.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + + Serial.println(F("failed to parse rtcConfig json")); + Serial.println(); + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("configuration json is :")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + if (json["timeServer"].success()) { + const char *tempServer = json["timeServer"]; + for (int i = 0; i < 50; i++) { + timeServer[i] = tempServer[i]; + } + } else { + for (int i = 0; i < 50; i++) { + this->timeServer[i] = this->timeServer[i]; + } + } + json["timeServer"] = this->timeServer; + + if (json["localPort"].success()) { + this->localPort = json["localPort"]; + } else { + this->localPort = this->localPort; + } + json["localPort"] = this->localPort; + + if (json["timeSync"].success()) { + + this->timeSync = json["timeSync"]; + } else { + this->timeSync = this->timeSync; + } + json["timeSync"] = this->timeSync; + + if (json["NTP"].success()) { + + this->NTP = json["NTP"].as(); + } else { + this->NTP = this->NTP; + } + json["NTP"] = this->NTP; + + if (json["compileTime"].success()) // Get the compile Flag and immediatly + // reset it to prevent re-reading on a + // later startup + { + + this->compileTime = json["compileTime"].as(); + } else { + this->compileTime = this->compileTime; + } + json["compileTime"] = this->compileTime; + + rtcConfig.close(); + rtcConfig = SPIFFS.open("/rtcConfig.json", "w"); + + if (!rtcConfig) { + +#if DEBUG == 1 + + Serial.println(F("failed to write to /rtcConfig.json")); + Serial.println(); + +#endif + return (false); + } + json.printTo(rtcConfig); + rtcConfig.close(); +#if DEBUG == 1 + + Serial.println(F("configuration is :")); + json.printTo(Serial); + Serial.println(); + +#endif + + return (true); + } + } } /** -* CoolTime::saveTimeSync() -* This method is provided to save -* the last sync time in the -* SPIFFS. -* -* \return true if successful,false -* otherwise -*/ -bool CoolTime::saveTimeSync() -{ - Serial.println( F("Enter CoolTime.saveTimeSync()") ); - Serial.println(); - - File rtcConfig = SPIFFS.open("/rtcConfig.json", "r"); - - if (!rtcConfig) - { - Serial.println( F("failed to read /rtcConfig.json") ); - Serial.println(); - - return(false); - } - else - { - size_t size = rtcConfig.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - rtcConfig.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - Serial.println( F("failed to parse json") ); - Serial.println(); - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("configuration json is :") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - #endif - - //String server; - - if(json["timeServer"].success() ) - { - const char* tempServer = json["timeServer"]; - for(int i =0;i<50;i++) - { - timeServer[i]=tempServer[i]; - } - } - else - { - for(int i=0;i<50;i++) - { - this->timeServer[i]=this->timeServer[i]; - } - } - json["timeServer"]=this->timeServer; - - if(json["localPort"].success() ) - { - this->localPort=json["localPort"]; - } - else - { - this->localPort=this->localPort; - } - json["localPort"]=this->localPort; - - - if( json["timeSync"].success() ) - { - json["timeSync"]=this->timeSync; - } - else - { - this->timeSync=this->timeSync; - } - json["timeSync"]=this->timeSync; - - if( json["NTP"].success() ) - { - - json["NTP"]=this->NTP; - } - else - { - this->NTP=this->NTP; - } - json["NTP"]=this->NTP; - - if( json["compileTime"].success() ) - { - - json["compileTime"]=this->compileTime; - } - else - { - this->compileTime=this->compileTime; - } - json["compileTime"]=this->compileTime; - - rtcConfig.close(); - rtcConfig= SPIFFS.open("/rtcConfig.json", "w"); - - if(!rtcConfig) - { - #if DEBUG == 1 - - Serial.println( F("failed to write timeSync to /rtcConfig.json") ); - Serial.println(); - - #endif - - return(false); - } - - json.printTo(rtcConfig); - rtcConfig.close(); - - #if DEBUG == 1 - - Serial.println( F("configuration is :") ); - json.printTo(Serial); - Serial.println(); - - #endif - return(true); - } - } + * CoolTime::saveTimeSync() + * This method is provided to save + * the last sync time in the + * SPIFFS. + * + * \return true if successful,false + * otherwise + */ +bool CoolTime::saveTimeSync() { + Serial.println(F("Enter CoolTime.saveTimeSync()")); + Serial.println(); + + File rtcConfig = SPIFFS.open("/rtcConfig.json", "r"); + + if (!rtcConfig) { + Serial.println(F("failed to read /rtcConfig.json")); + Serial.println(); + + return (false); + } else { + size_t size = rtcConfig.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + rtcConfig.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + + Serial.println(F("failed to parse json")); + Serial.println(); + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("configuration json is :")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + // String server; + + if (json["timeServer"].success()) { + const char *tempServer = json["timeServer"]; + for (int i = 0; i < 50; i++) { + timeServer[i] = tempServer[i]; + } + } else { + for (int i = 0; i < 50; i++) { + this->timeServer[i] = this->timeServer[i]; + } + } + json["timeServer"] = this->timeServer; + + if (json["localPort"].success()) { + this->localPort = json["localPort"]; + } else { + this->localPort = this->localPort; + } + json["localPort"] = this->localPort; + + if (json["timeSync"].success()) { + json["timeSync"] = this->timeSync; + } else { + this->timeSync = this->timeSync; + } + json["timeSync"] = this->timeSync; + + if (json["NTP"].success()) { + + json["NTP"] = this->NTP; + } else { + this->NTP = this->NTP; + } + json["NTP"] = this->NTP; + + if (json["compileTime"].success()) { + + json["compileTime"] = this->compileTime; + } else { + this->compileTime = this->compileTime; + } + json["compileTime"] = this->compileTime; + + rtcConfig.close(); + rtcConfig = SPIFFS.open("/rtcConfig.json", "w"); + + if (!rtcConfig) { +#if DEBUG == 1 + + Serial.println(F("failed to write timeSync to /rtcConfig.json")); + Serial.println(); + +#endif + return (false); + } + json.printTo(rtcConfig); + rtcConfig.close(); +#if DEBUG == 1 + + Serial.println(F("configuration is :")); + json.printTo(Serial); + Serial.println(); + +#endif + return (true); + } + } } /** -* CoolTime::printConf(): -* This method is provided to print -* the CoolTime configuration to the -* Serial Monitor -*/ -void CoolTime::printConf() -{ + * CoolTime::printConf(): + * This method is provided to print + * the CoolTime configuration to the + * Serial Monitor + */ +void CoolTime::printConf() { #if DEBUG == 1 - Serial.println(F("Entering CoolTime.printConf()")); - Serial.println(); + Serial.println(F("Entering CoolTime.printConf()")); + Serial.println(); -#endif +#endif + + Serial.println(F("RTC Configuration")); - Serial.println(F("RTC Configuration")); + Serial.print(F("timeServer : ")); + Serial.println(timeServer); - Serial.print(F("timeServer : ")); - Serial.println(timeServer); - - Serial.print(F("localPort : :")); - Serial.println(localPort); + Serial.print(F("localPort : :")); + Serial.println(localPort); - Serial.print(F("NTP Flag :")); - Serial.println(NTP); + Serial.print(F("NTP Flag :")); + Serial.println(NTP); - Serial.print(F("compileTime Flag :")); - Serial.println(compileTime); + Serial.print(F("compileTime Flag :")); + Serial.println(compileTime); } /** -* CoolTime::printDigits(digit) -* -* utility method for digital clock display -* adds leading 0 -* -* \return formatted string of the input digit -*/ -String CoolTime::formatDigits(int digits) -{ + * CoolTime::printDigits(digit) + * + * utility method for digital clock display + * adds leading 0 + * + * \return formatted string of the input digit + */ +String CoolTime::formatDigits(int digits) { -#if DEBUG == 1 +#if DEBUG == 1 - //Serial.println( F("Entering CoolTime.formatDigits()") ); - //Serial.println(); +// Serial.println( F("Entering CoolTime.formatDigits()") ); +// Serial.println(); -#endif +#endif - if(digits < 10) - { - - #if DEBUG == 1 + if (digits < 10) { - //Serial.println( F("output digit : ") ); - //Serial.println( String("0") + String(digits) ); +#if DEBUG == 1 - #endif + // Serial.println( F("output digit : ") ); + // Serial.println( String("0") + String(digits) ); - return( String("0") + String(digits) ); - } - -#if DEBUG == 1 +#endif - //Serial.println( F("output digit : ") ); - //Serial.println(digits); + return (String("0") + String(digits)); + } -#endif +#if DEBUG == 1 - return( String(digits) ); -} + // Serial.println( F("output digit : ") ); + // Serial.println(digits); +#endif + return (String(digits)); +} diff --git a/src/CoolTime.h b/src/CoolTime.h index 20b00e3a..241743b2 100644 --- a/src/CoolTime.h +++ b/src/CoolTime.h @@ -1,25 +1,25 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef CoolTime_H #define CoolTime_H @@ -36,109 +36,107 @@ #include -#define NTP_PACKET_SIZE 48 // NTP time is in the first 48 bytes of message +#define NTP_PACKET_SIZE 48 // NTP time is in the first 48 bytes of message /** -* \class CoolTime -* -* \brief This class manages the DS1337 RTC . -* -*/ + * \class CoolTime + * + * \brief This class manages the DS1337 RTC . + * + */ -class CoolTime -{ +class CoolTime { public: - void begin(); + void begin(); - void offGrid(); - - void update(); - - bool config(); + void offGrid(); - void config(IPAddress timeServer,unsigned int localPort); - - void printConf(); + void update(); - void setDateTime(int year, int month, int day, int hour, int minutes, int seconds); - - tmElements_t getTimeDate(); + bool config(); - String getESDate(); - - unsigned long getLastSyncTime(); - - bool isTimeSync(unsigned long seconds=63113904);//604800); + void config(IPAddress timeServer, unsigned int localPort); - time_t getNtpTime(); + void printConf(); - void sendNTPpacket(IPAddress &address); - - String formatDigits(int digits); - - bool saveTimeSync(); + void setDateTime(int year, int month, int day, int hour, int minutes, + int seconds); -private: - - /** - * last Time the RTC syncronised with the NTP server - * unix Time - */ - unsigned long timeSync=0; - - /** - * NTP Server IP Address - */ - IPAddress timeServerIP; - - /** - * NTP Server DNS Address - */ - char timeServer[50]={'0'}; - - /** - * NTP flag, - * only set to zero if your coolBoard is in a place - * where NTP synchronisation is impossible (Off Grid) - */ - bool NTP=1; - - /** - * compileTime flag, - * only set to One if your coolBoard is in a place - * where NTP synchronisation is impossible (Off Grid). - * this flag set's the RTC and timeSync from __TIMESTAMP__ - * then reset's the flag - */ - bool compileTime=1; - - /** - * UDP Client instance - */ - WiFiUDP Udp; - - /** - * port number for UDP packets - */ - unsigned int localPort=0; - - /** - * UDP buffer - * to hold incoming & outgoing packets - */ - byte packetBuffer[NTP_PACKET_SIZE]; - - /** - * Time Elements Instance to hold various Time Values - */ - tmElements_t tmSet; - - /** - * RTC instance - */ - DS1337RTC rtc; + tmElements_t getTimeDate(); + + String getESDate(); + + unsigned long getLastSyncTime(); + + bool isTimeSync(unsigned long seconds = 63113904); // 604800); + + time_t getNtpTime(); + void sendNTPpacket(IPAddress &address); + + String formatDigits(int digits); + + bool saveTimeSync(); + +private: + /** + * last Time the RTC syncronised with the NTP server + * unix Time + */ + unsigned long timeSync = 0; + + /** + * NTP Server IP Address + */ + IPAddress timeServerIP; + + /** + * NTP Server DNS Address + */ + char timeServer[50] = {'0'}; + + /** + * NTP flag, + * only set to zero if your coolBoard is in a place + * where NTP synchronisation is impossible (Off Grid) + */ + bool NTP = 1; + + /** + * compileTime flag, + * only set to One if your coolBoard is in a place + * where NTP synchronisation is impossible (Off Grid). + * this flag set's the RTC and timeSync from __TIMESTAMP__ + * then reset's the flag + */ + bool compileTime = 1; + + /** + * UDP Client instance + */ + WiFiUDP Udp; + + /** + * port number for UDP packets + */ + unsigned int localPort = 0; + + /** + * UDP buffer + * to hold incoming & outgoing packets + */ + byte packetBuffer[NTP_PACKET_SIZE]; + + /** + * Time Elements Instance to hold various Time Values + */ + tmElements_t tmSet; + + /** + * RTC instance + */ + DS1337RTC rtc; }; #endif diff --git a/src/CoolWifi.cpp b/src/CoolWifi.cpp index 15d7bea7..39b74b62 100644 --- a/src/CoolWifi.cpp +++ b/src/CoolWifi.cpp @@ -1,733 +1,630 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +#include "Arduino.h" +#include "ArduinoJson.h" #include "FS.h" -#include "Arduino.h" -#include -#include #include "internals/WiFiManagerReadFileButton.h" #include -#include "ArduinoJson.h" - +#include +#include #define DEBUG 0 - /** -* CoolWifi::begin(): -* This method is provided to set the -* wifiMulti Access points and the -* wifiManager time out -*/ -void CoolWifi::begin() -{ + * CoolWifi::begin(): + * This method is provided to set the + * wifiMulti Access points and the + * wifiManager time out + */ +void CoolWifi::begin() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering CoolWifi.begin()") ); - Serial.println(); + Serial.println(F("Entering CoolWifi.begin()")); + Serial.println(); #endif - for(int i =0;iwifiCount;i++) - { - this->wifiMulti.addAP(this->ssid[i].c_str() , this->pass[i].c_str() ); - } - + for (int i = 0; i < this->wifiCount; i++) { + this->wifiMulti.addAP(this->ssid[i].c_str(), this->pass[i].c_str()); + } } /** -* CoolWifi::state(): -* This method is provided to return the -* Wifi client's state. -* \return wifi client state: -* WL_NO_SHIELD = 255, -* WL_IDLE_STATUS = 0, -* WL_NO_SSID_AVAIL = 1, -* WL_SCAN_COMPLETED = 2, -* WL_CONNECTED = 3, -* WL_CONNECT_FAILED = 4, -* WL_CONNECTION_LOST = 5, -* WL_DISCONNECTED = 6 -*/ -wl_status_t CoolWifi::state() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolWifi.state()") ); - Serial.println(); - Serial.print( F("state : ") ); - Serial.println( WiFi.status() ); + * CoolWifi::state(): + * This method is provided to return the + * Wifi client's state. + * \return wifi client state: + * WL_NO_SHIELD = 255, + * WL_IDLE_STATUS = 0, + * WL_NO_SSID_AVAIL = 1, + * WL_SCAN_COMPLETED = 2, + * WL_CONNECTED = 3, + * WL_CONNECT_FAILED = 4, + * WL_CONNECTION_LOST = 5, + * WL_DISCONNECTED = 6 + */ +wl_status_t CoolWifi::state() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolWifi.state()")); + Serial.println(); + Serial.print(F("state : ")); + Serial.println(WiFi.status()); #endif - - return( WiFi.status() ) ; + + return (WiFi.status()); } /** -* CoolWifi::disconnect(): -* This method is provided to disconnect -* from current WiFi network and returns -* the Wifi client's state. -* \return wifi client state: -* WL_NO_SHIELD = 255, -* WL_IDLE_STATUS = 0, -* WL_NO_SSID_AVAIL = 1, -* WL_SCAN_COMPLETED = 2, -* WL_CONNECTED = 3, -* WL_CONNECT_FAILED = 4, -* WL_CONNECTION_LOST = 5, -* WL_DISCONNECTED = 6 -*/ -wl_status_t CoolWifi::disconnect() -{ - - WiFi.disconnect(); - -#if DEBUG == 1 - - Serial.println( F("Entering CoolWifi.disconnect()") ); - Serial.println(); - Serial.print( F("state : ") ); - Serial.println( WiFi.status() ); + * CoolWifi::disconnect(): + * This method is provided to disconnect + * from current WiFi network and returns + * the Wifi client's state. + * \return wifi client state: + * WL_NO_SHIELD = 255, + * WL_IDLE_STATUS = 0, + * WL_NO_SSID_AVAIL = 1, + * WL_SCAN_COMPLETED = 2, + * WL_CONNECTED = 3, + * WL_CONNECT_FAILED = 4, + * WL_CONNECTION_LOST = 5, + * WL_DISCONNECTED = 6 + */ +wl_status_t CoolWifi::disconnect() { + + WiFi.disconnect(); + +#if DEBUG == 1 + + Serial.println(F("Entering CoolWifi.disconnect()")); + Serial.println(); + Serial.print(F("state : ")); + Serial.println(WiFi.status()); #endif - - return( WiFi.status() ) ; + + return (WiFi.status()); } /** -* CoolWifi::connect( ): -* This method is provided to connect to the strongest WiFi -* in the provided list of wiFis. -* If none are found , it starts the AP mode. -* -* \return wifi state -*/ -wl_status_t CoolWifi::connect() -{ + * CoolWifi::connect( ): + * This method is provided to connect to the strongest WiFi + * in the provided list of wiFis. + * If none are found , it starts the AP mode. + * + * \return wifi state + */ +wl_status_t CoolWifi::connect() { +#if DEBUG == 1 + Serial.println(F("Entering CoolWifi.connect()")); -#if DEBUG == 1 +#endif + + Serial.println(F("Wifi connecting...")); + + // if WifiCount > 0 , lunch wifiMulti + // else no need to , skip this part + // to wifiManager + if (this->wifiCount != 0) { + + this->connectWifiMulti(); + + // if nomad is true, only check wifi list + if (this->nomad == true) { + +#if DEBUG == 1 + + Serial.print(F("nomad mode :")); + Serial.println(this->nomad); + + Serial.print(F("Wifi status: ")); + Serial.println(WiFi.status()); - Serial.println( F("Entering CoolWifi.connect()") ); +#endif + return (WiFi.status()); + } + + } else { + WiFiManager wifiManager; + wifiManager.resetSettings(); + } + + // Wifi Manager + if (WiFi.status() != WL_CONNECTED) { + +#if DEBUG == 1 + + Serial.println(F("No matching wifi Found ")); + Serial.println(F("Starting Access Point ")); + Serial.println(); #endif - Serial.println( F("Wifi connecting...") ); - - - //if WifiCount > 0 , lunch wifiMulti - //else no need to , skip this part - // to wifiManager - if(this->wifiCount !=0) - { - - this->connectWifiMulti(); - - //if nomad is true, only check wifi list - if(this->nomad == true) - { - - #if DEBUG == 1 - - Serial.print(F("nomad mode :")); - Serial.println(this->nomad); - - Serial.print(F("Wifi status: ")); - Serial.println(WiFi.status()); - - #endif - return(WiFi.status()); - } - - } - else { - WiFiManager wifiManager; - wifiManager.resetSettings(); - } - - - //Wifi Manager - if( WiFi.status() != WL_CONNECTED ) - { - - #if DEBUG == 1 - - Serial.println(F("No matching wifi Found ") ); - Serial.println( F("Starting Access Point ") ); - Serial.println(); - - #endif - - this->connectAP(); - - } - else - { - - Serial.println(F("connected to ")); - Serial.println( WiFi.SSID() ); - Serial.println(); - - } - - return( WiFi.status() ) ; + this->connectAP(); + + } else { + Serial.println(F("connected to ")); + Serial.println(WiFi.SSID()); + Serial.println(); + } + + return (WiFi.status()); } /** -* CoolWifi::connectWifiMulti() -* This function is provided to -* run the WifiMulti part of the -* Wifi connection process -* -* \return wifi state -*/ -wl_status_t CoolWifi::connectWifiMulti() -{ - int i=0; - -#if DEBUG == 1 - - Serial.println(F("Entering CoolWifi.connectWifiMulti()")); - Serial.println(); - - Serial.println( F("entry time to multi : ") ); - Serial.println(millis() ) ; + * CoolWifi::connectWifiMulti() + * This function is provided to + * run the WifiMulti part of the + * Wifi connection process + * + * \return wifi state + */ +wl_status_t CoolWifi::connectWifiMulti() { + int i = 0; + +#if DEBUG == 1 + + Serial.println(F("Entering CoolWifi.connectWifiMulti()")); + Serial.println(); + + Serial.println(F("entry time to multi : ")); + Serial.println(millis()); #endif - - //Wifi MULTI - while( (this->wifiMulti.run() != WL_CONNECTED) && (i<500) ) - { - - #if DEBUG == 1 - - Serial.print(F(".")); - - #endif - i++; - delay(5); - } - -#if DEBUG == 1 - - Serial.println(); - Serial.println(F("exit point from multi : ")); - Serial.println(millis() ); - - Serial.print(F("Wifi Status :")); - Serial.println(WiFi.status()); + + // Wifi MULTI + while ((this->wifiMulti.run() != WL_CONNECTED) && (i < 500)) { + +#if DEBUG == 1 + + Serial.print(F(".")); + #endif + i++; + delay(5); + } - return(WiFi.status()); +#if DEBUG == 1 -} + Serial.println(); + Serial.println(F("exit point from multi : ")); + Serial.println(millis()); + Serial.print(F("Wifi Status :")); + Serial.println(WiFi.status()); +#endif + + return (WiFi.status()); +} /** -* CoolWifi::connectAP() -* This function is provided to -* run the WifiManager part of the -* Wifi connection process -* -* \return wifi state -*/ -wl_status_t CoolWifi::connectAP() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolWifi.connectAP()") ); - Serial.println(); + * CoolWifi::connectAP() + * This function is provided to + * run the WifiManager part of the + * Wifi connection process + * + * \return wifi state + */ +wl_status_t CoolWifi::connectAP() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolWifi.connectAP()")); + Serial.println(); #endif - WiFiManager wifiManager; - - wifiManager.setRemoveDuplicateAPs(true); + WiFiManager wifiManager; - wifiManager.setTimeout(this->timeOut); - - String tempMAC = WiFi.macAddress(); - tempMAC.replace(":",""); + wifiManager.setRemoveDuplicateAPs(true); - String name="CoolBoard-"+tempMAC; + wifiManager.setTimeout(this->timeOut); - if(!wifiManager.autoConnect(name.c_str())) - { + String tempMAC = WiFi.macAddress(); + tempMAC.replace(":", ""); - Serial.println( F("failed to connect and hit timeout") ); + String name = "CoolBoard-" + tempMAC; - delay(30); + if (!wifiManager.autoConnect(name.c_str())) { - } + Serial.println(F("failed to connect and hit timeout")); - //if you get here you have connected to the WiFi + delay(30); + } - if(WiFi.status()==WL_CONNECTED) - { + // if you get here you have connected to the WiFi - #if DEBUG == 1 + if (WiFi.status() == WL_CONNECTED) { - Serial.println( F("connected...yeey :)" )); - Serial.println("connected to "); - Serial.println( WiFi.SSID() ); - //Serial.println( WiFi.psk() ) ; - - #endif +#if DEBUG == 1 - this->addWifi( WiFi.SSID() , WiFi.psk() ); - - } - else - { - Serial.println( F("Not connected...:(" )); - } - - return(WiFi.status()); + Serial.println(F("connected...yeey :)")); + Serial.println("connected to "); + Serial.println(WiFi.SSID()); + // Serial.println( WiFi.psk() ) ; -} +#endif + this->addWifi(WiFi.SSID(), WiFi.psk()); + + } else { + Serial.println(F("Not connected...:(")); + } + + return (WiFi.status()); +} /** -* CoolWifi::config(): -* This method is provided to set -* the wifi parameters : -ssid -* -pass -* -AP timeOut -* -wifiCount -* -* \return true if successful,false otherwise -*/ -bool CoolWifi::config() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering CoolWifi.config()") ); - Serial.println(); + * CoolWifi::config(): + * This method is provided to set + * the wifi parameters : -ssid + * -pass + * -AP timeOut + * -wifiCount + * + * \return true if successful,false otherwise + */ +bool CoolWifi::config() { + +#if DEBUG == 1 + + Serial.println(F("Entering CoolWifi.config()")); + Serial.println(); #endif #if DEBUG == 0 - Serial.println( "Reading Wifi Configuration.."); - delay(100); -#endif - - //read config file - //update data - File configFile = SPIFFS.open("/wifiConfig.json", "r"); - - if (!configFile) - { - - Serial.println( F("failed to read /wifiConfig.json") ); - Serial.println(); - - return(false); - } - else - { - size_t size = configFile.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - configFile.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - Serial.println( F("failed to parse json ") ); - Serial.println(); - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("configuration json is ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - //wifiCount - if(json["wifiCount"].success() ) - { - this->wifiCount=json["wifiCount"]; - } - else - { - this->wifiCount=this->wifiCount; - } - json["wifiCount"]=this->wifiCount; - - - //AP timeOut - if(json["timeOut"].success() ) - { - this->timeOut=json["timeOut"]; - } - else - { - this->timeOut=this->timeOut; - - } - json["timeOut"]=this->timeOut; - - //nomad - if(json["nomad"].success() ) - { - this->nomad=json["nomad"]; - } - else - { - this->nomad=this->nomad; - - } - json["nomad"]=this->nomad; - - - - //Wifis SSID and PASS - for(int i =0; iwifiCount ;i++) - { - if ( json["Wifi"+String(i)].success() ) - { - - if( json["Wifi"+String(i)]["ssid"].success() ) - { - const char* tempSsid=json["Wifi"+String(i)]["ssid"]; - this->ssid[i]=tempSsid; - } - else - { - this->ssid[i]=this->ssid[i]; - } - json["Wifi"+String(i)]["ssid"]=this->ssid[i].c_str(); - - - if( json["Wifi"+String(i)]["pass"].success() ) - { - const char* tempPass =json["Wifi"+String(i)]["pass"]; - this->pass[i]=tempPass ; - } - else - { - this->pass[i]=this->pass[i]; - } - json["Wifi"+String(i)]["pass"]=this->pass[i].c_str(); - - } - else - { - - this->ssid[i]=this->ssid[i]; - this->pass[i]=this->pass[i]; - - } - json["Wifi"+String(i)]["ssid"]=this->ssid[i].c_str(); - json["Wifi"+String(i)]["pass"]=this->pass[i].c_str(); - - } - - configFile.close(); - configFile = SPIFFS.open("/wifiConfig.json", "w"); - if(!configFile) - { - - Serial.println( F("failed to write to /wifiConfig.json") ); - - return(false); - } - - json.printTo(configFile); - configFile.close(); - - #if DEBUG == 1 - - Serial.println( F("saved configuration is :") ); - json.printTo(Serial); - Serial.println(); - - #endif - #if DEBUG == 0 - Serial.println( F("Configuration loaded : OK")); - #endif - return(true); - } - } - + Serial.println("Reading Wifi Configuration.."); + delay(100); +#endif + + // read config file + // update data + File configFile = SPIFFS.open("/wifiConfig.json", "r"); + + if (!configFile) { + + Serial.println(F("failed to read /wifiConfig.json")); + Serial.println(); + + return (false); + } else { + size_t size = configFile.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + configFile.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + + Serial.println(F("failed to parse json ")); + Serial.println(); + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("configuration json is ")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); +#endif + // wifiCount + if (json["wifiCount"].success()) { + this->wifiCount = json["wifiCount"]; + } else { + this->wifiCount = this->wifiCount; + } + json["wifiCount"] = this->wifiCount; + + // AP timeOut + if (json["timeOut"].success()) { + this->timeOut = json["timeOut"]; + } else { + this->timeOut = this->timeOut; + } + json["timeOut"] = this->timeOut; + + // nomad + if (json["nomad"].success()) { + this->nomad = json["nomad"]; + } else { + this->nomad = this->nomad; + } + json["nomad"] = this->nomad; + + // Wifis SSID and PASS + for (int i = 0; i < this->wifiCount; i++) { + if (json["Wifi" + String(i)].success()) { + + if (json["Wifi" + String(i)]["ssid"].success()) { + const char *tempSsid = json["Wifi" + String(i)]["ssid"]; + this->ssid[i] = tempSsid; + } else { + this->ssid[i] = this->ssid[i]; + } + json["Wifi" + String(i)]["ssid"] = this->ssid[i].c_str(); + + if (json["Wifi" + String(i)]["pass"].success()) { + const char *tempPass = json["Wifi" + String(i)]["pass"]; + this->pass[i] = tempPass; + } else { + this->pass[i] = this->pass[i]; + } + json["Wifi" + String(i)]["pass"] = this->pass[i].c_str(); + + } else { + + this->ssid[i] = this->ssid[i]; + this->pass[i] = this->pass[i]; + } + json["Wifi" + String(i)]["ssid"] = this->ssid[i].c_str(); + json["Wifi" + String(i)]["pass"] = this->pass[i].c_str(); + } + + configFile.close(); + configFile = SPIFFS.open("/wifiConfig.json", "w"); + if (!configFile) { + + Serial.println(F("failed to write to /wifiConfig.json")); + + return (false); + } + + json.printTo(configFile); + configFile.close(); + +#if DEBUG == 1 + + Serial.println(F("saved configuration is :")); + json.printTo(Serial); + Serial.println(); + +#endif +#if DEBUG == 0 + Serial.println(F("Configuration loaded : OK")); +#endif + return (true); + } + } } /** -* CoolWifi::config(ssid array, pass array, number of wifis, AP timeout,nomad flag ); -* This method is provided to configure the Wifi without SPIFFS -* -* \return true if successfull, false otherwise -*/ -bool CoolWifi::config(String ssid[],String pass[],int wifiNumber, int APTimeOut,bool nomad) -{ - -#if DEBUG == 1 - - Serial.println("Entering CoolWifi.config(), no SPIFFS variant ") ; - + * CoolWifi::config(ssid array, pass array, number of wifis, AP + *timeout,nomad flag ); This method is provided to configure the Wifi without + *SPIFFS + * + * \return true if successfull, false otherwise + */ +bool CoolWifi::config(String ssid[], String pass[], int wifiNumber, + int APTimeOut, bool nomad) { + +#if DEBUG == 1 + + Serial.println("Entering CoolWifi.config(), no SPIFFS variant "); + #endif - - if(wifiNumber>50) - { - - #if DEBUG == 1 - - Serial.println("the limit of WiFis is 50 " ); - - #endif - return(false); - } - - this->wifiCount=wifiNumber; - - this->timeOut=APTimeOut; - - this->nomad=nomad; - - for(int i=0;issid[i]=ssid[i]; - - this->pass[i]=pass[i]; - } - - return(true); -} + if (wifiNumber > 50) { + +#if DEBUG == 1 + + Serial.println("the limit of WiFis is 50 "); + +#endif + return (false); + } + + this->wifiCount = wifiNumber; + this->timeOut = APTimeOut; + + this->nomad = nomad; + + for (int i = 0; i < wifiNumber; i++) { + this->ssid[i] = ssid[i]; + + this->pass[i] = pass[i]; + } + + return (true); +} /** -* CoolWifi::printConf(): -* This method is provided to print the -* configuration to the Serial Monitor -*/ -void CoolWifi::printConf() -{ + * CoolWifi::printConf(): + * This method is provided to print the + * configuration to the Serial Monitor + */ +void CoolWifi::printConf() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering CoolWifi.printConf()") ); - Serial.println(); + Serial.println(F("Entering CoolWifi.printConf()")); + Serial.println(); #endif - - Serial.println(F("Wifi configuration ")); - - - Serial.println(F("wifiCount : ")); - Serial.println(this->wifiCount); - - for(int i=0;iwifiCount;i++) - { - Serial.print(F("SSID")); - Serial.print(i); - Serial.println(F(" : ")); - Serial.println(this->ssid[i]); - - - //Serial.print("PASS"); - //Serial.print(i); - //Serial.println(" : "); - - //Serial.print(F("PASS")); - //Serial.print(i); - //Serial.println(F(" : ")); - - //Serial.println(this->pass[i]); - - } - - Serial.println(F("timeOut : ")); - Serial.println(this->timeOut); - - Serial.println(F("nomad : ")); - Serial.println(this->nomad); - - Serial.println(); + Serial.println(F("Wifi configuration ")); + + Serial.println(F("wifiCount : ")); + Serial.println(this->wifiCount); + + for (int i = 0; i < this->wifiCount; i++) { + Serial.print(F("SSID")); + Serial.print(i); + Serial.println(F(" : ")); + Serial.println(this->ssid[i]); + + // Serial.print("PASS"); + // Serial.print(i); + // Serial.println(" : "); + // Serial.print(F("PASS")); + // Serial.print(i); + // Serial.println(F(" : ")); + + // Serial.println(this->pass[i]); + } + + Serial.println(F("timeOut : ")); + Serial.println(this->timeOut); + + Serial.println(F("nomad : ")); + Serial.println(this->nomad); + + Serial.println(); } /** -* CoolWifi::addWifi(ssid,pass) -* This method is provided to add new WiFi -* detected by the WiFiManager to -* the jsonConfig(if used ) -* -* \return true if successfull , false otherwise -*/ -bool CoolWifi::addWifi( String ssid , String pass ) -{ + * CoolWifi::addWifi(ssid,pass) + * This method is provided to add new WiFi + * detected by the WiFiManager to + * the jsonConfig(if used ) + * + * \return true if successfull , false otherwise + */ +bool CoolWifi::addWifi(String ssid, String pass) { + +#if DEBUG == 1 + + Serial.println("Entering CoolWifi.addWifi() "); + +#endif + + this->wifiCount++; + if (this->wifiCount >= 50) { + +#if DEBUG == 1 + + Serial.println("You have reached the limit of 50"); + return (false); + +#endif + } + + this->ssid[this->wifiCount - 1] = ssid; + this->pass[this->wifiCount - 1] = pass; + + // read config file + // update data + File configFile = SPIFFS.open("/wifiConfig.json", "r"); + + if (!configFile) { #if DEBUG == 1 - - Serial.println("Entering CoolWifi.addWifi() ") ; - -#endif - - this->wifiCount++; - if( this->wifiCount >=50) - { - - #if DEBUG == 1 - - Serial.println("You have reached the limit of 50"); - return(false); - - #endif - - } - - this->ssid[this->wifiCount-1]=ssid; - this->pass[this->wifiCount-1]=pass; - - //read config file - //update data - File configFile = SPIFFS.open("/wifiConfig.json", "r"); - - if (!configFile) - { - - #if DEBUG == 1 - - Serial.println( F("failed to read /wifiConfig.json") ); - Serial.println(); - - #endif - } - else - { - size_t size = configFile.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - configFile.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json ") ); - Serial.println(); - - #endif - } - else - { - - #if DEBUG == 1 - - Serial.println( F("configuration json is ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - //wifiCount - if(json["wifiCount"].success() ) - { - json["wifiCount"]=this->wifiCount; - } - else - { - this->wifiCount=this->wifiCount; - } - json["wifiCount"]=this->wifiCount; - - - //AP timeOut - if(json["timeOut"].success() ) - { - this->timeOut=json["timeOut"]; - } - else - { - this->timeOut=this->timeOut; - - } - json["timeOut"]=this->timeOut; - - - //new Wifi SSID and PASS - JsonObject& newWifi = json.createNestedObject( "Wifi"+String( this->wifiCount-1 ) ); - - newWifi["ssid"] =this->ssid[this->wifiCount-1]; - newWifi["pass"] = this->pass[this->wifiCount-1]; - - - configFile.close(); - configFile = SPIFFS.open("/wifiConfig.json", "w"); - if(!configFile) - { - - #if DEBUG == 1 - - Serial.println( F("failed to write to /wifiConfig.json") ); - - #endif - - } - - json.printTo(configFile); - configFile.close(); - - #if DEBUG == 1 - - Serial.println( F("saved configuration is :") ); - json.printTo(Serial); - Serial.println(); - - #endif - - return(true); - } - } - - - return(true); - + + Serial.println(F("failed to read /wifiConfig.json")); + Serial.println(); + +#endif + } else { + size_t size = configFile.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + configFile.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + +#if DEBUG == 1 + + Serial.println(F("failed to parse json ")); + Serial.println(); + +#endif + } else { + +#if DEBUG == 1 + + Serial.println(F("configuration json is ")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + // wifiCount + if (json["wifiCount"].success()) { + json["wifiCount"] = this->wifiCount; + } else { + this->wifiCount = this->wifiCount; + } + json["wifiCount"] = this->wifiCount; + + // AP timeOut + if (json["timeOut"].success()) { + this->timeOut = json["timeOut"]; + } else { + this->timeOut = this->timeOut; + } + json["timeOut"] = this->timeOut; + + // new Wifi SSID and PASS + JsonObject &newWifi = + json.createNestedObject("Wifi" + String(this->wifiCount - 1)); + + newWifi["ssid"] = this->ssid[this->wifiCount - 1]; + newWifi["pass"] = this->pass[this->wifiCount - 1]; + + configFile.close(); + configFile = SPIFFS.open("/wifiConfig.json", "w"); + if (!configFile) { + +#if DEBUG == 1 + + Serial.println(F("failed to write to /wifiConfig.json")); + +#endif + } + + json.printTo(configFile); + configFile.close(); + +#if DEBUG == 1 + + Serial.println(F("saved configuration is :")); + json.printTo(Serial); + Serial.println(); + +#endif + + return (true); + } + } + + return (true); } diff --git a/src/CoolWifi.h b/src/CoolWifi.h index 92d7d57b..bc13fe39 100644 --- a/src/CoolWifi.h +++ b/src/CoolWifi.h @@ -1,108 +1,100 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef CoolWifi_H #define CoolWifi_H - #include "Arduino.h" -#include -#include #include #include - - +#include +#include /** -* \class CoolWifi -* -* \brief This class manages the WiFi connection . -* -*/ + * \class CoolWifi + * + * \brief This class manages the WiFi connection . + * + */ -class CoolWifi -{ +class CoolWifi { public: - void begin(); - - bool config(); - - bool config(String ssid[],String pass[],int wifiNumber,int APTimeOut,bool nomad); - - wl_status_t connect(); - - wl_status_t connectWifiMulti(); - - wl_status_t connectAP(); - - wl_status_t state(); - - wl_status_t disconnect(); - - void printConf(); - - bool addWifi( String ssid , String pass="" ); - - /** - * nomad Mode Flag - * in Nomad Mode , the Wifi Access Point will - * Only Lunch on Start when there's NO saved wifis ( wifiCount = 0) - * After that, the Wifi will ONLY try to connect to known Wifis via - * WifiMulti - */ - bool nomad=0; + void begin(); -private: - - /** - * ESP8266WiFiMulti instance - */ - ESP8266WiFiMulti wifiMulti; - - /** - * number of saved WiFi's - */ - int wifiCount=0; - - /** - * WiFi SSID Array - */ - String ssid[50]={"0"}; - - /** - * WiFi PASS Array - */ - String pass[50]={"0"}; - - /** - * WiFi Access Point TimeOut Value in Seconds - */ - int timeOut=0; - - - + bool config(); + + bool config(String ssid[], String pass[], int wifiNumber, int APTimeOut, + bool nomad); + wl_status_t connect(); + + wl_status_t connectWifiMulti(); + + wl_status_t connectAP(); + + wl_status_t state(); + + wl_status_t disconnect(); + + void printConf(); + + bool addWifi(String ssid, String pass = ""); + + /** + * nomad Mode Flag + * in Nomad Mode , the Wifi Access Point will + * Only Lunch on Start when there's NO saved wifis ( wifiCount = 0) + * After that, the Wifi will ONLY try to connect to known Wifis via + * WifiMulti + */ + bool nomad = 0; + +private: + /** + * ESP8266WiFiMulti instance + */ + ESP8266WiFiMulti wifiMulti; + + /** + * number of saved WiFi's + */ + int wifiCount = 0; + + /** + * WiFi SSID Array + */ + String ssid[50] = {"0"}; + + /** + * WiFi PASS Array + */ + String pass[50] = {"0"}; + + /** + * WiFi Access Point TimeOut Value in Seconds + */ + int timeOut = 0; }; #endif diff --git a/src/ExternalSensor.h b/src/ExternalSensor.h index 32a09e7d..f09ee580 100644 --- a/src/ExternalSensor.h +++ b/src/ExternalSensor.h @@ -1,891 +1,815 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef BaseExternalSensor_H #define BaseExternalSensor_H -#include "internals/CoolNDIR_I2C.h" -#include +#include "Arduino.h" +#include "internals/CoolAdafruit_ADS1015.h" +#include "internals/CoolAdafruit_CCS811.h" #include "internals/CoolAdafruit_TCS34725.h" -#include "internals/CoolAdafruit_ADS1015.h" -#include "internals/CoolAdafruit_CCS811.h" #include "internals/CoolGauge.h" -#include "Arduino.h" +#include "internals/CoolNDIR_I2C.h" +#include - #define DEBUGExternal 0 - /** -* \class BaseExternalSensor: -* \brief This class is a generic external Sensor -* it is a way to access real external sensor -* methods through run Time polymorphism -*/ -class BaseExternalSensor -{ + * \class BaseExternalSensor: + * \brief This class is a generic external Sensor + * it is a way to access real external sensor + * methods through run Time polymorphism + */ +class BaseExternalSensor { public: - /** - * BaseExternalSensor(): - * Base class generic Constructor - */ - BaseExternalSensor() - { - - #if DEBUGExternal == 1 - - Serial.println( "BaseExternalSensor Constructor" ); - Serial.println(); - - #endif - - } - - /** - * begin(): - * Base class virtual - * generic begin method - * - * \return generic value as it's not supposed - * to be used - */ - virtual uint8_t begin() - { - - #if DEBUGExternal == 1 - - Serial.println( "BaseExternalSensor.begin()" ); - Serial.println(); - - #endif - - return(-2); - } - - /** - * read(): - * Base class virtual - * generic read method - * - * \return generic value - * as it is not supposed - * to be used - */ - virtual float read() - { - - #if DEBUGExternal == 1 - - Serial.println( "BaseExternalSensor.read()" ); - Serial.println(); - - #endif - - return(-2); - } - - virtual float read(int16_t *a) - { - return(-42,42); - } - - virtual float read(int16_t *a,int16_t *b,float *c) - { - return(-42.42); - } - - virtual float read(uint32_t *a,uint32_t *b,uint32_t *c) - { - return(-42.42); - } - - virtual float read(int16_t *a,int16_t *b,int16_t *c,int16_t *d) - { - return(-42.42); - } - - virtual float read(int16_t *a,int16_t *b,int16_t *c,int16_t *d,int16_t *e,int16_t *f) - { - return(-42.42); - } - - virtual float read(int16_t *a,int16_t *b,int16_t *c,int16_t *d,int16_t *e,int16_t *f,int16_t *g,int16_t *h) - { - return(-42.42); - } - -}; + /** + * BaseExternalSensor(): + * Base class generic Constructor + */ + BaseExternalSensor() { +#if DEBUGExternal == 1 + Serial.println("BaseExternalSensor Constructor"); + Serial.println(); -/** -* \class ExternalSensor -* \brief template class External Sensor: -* Derived class from BaseExternalSensor. -* -* This is the generic Template for an external sensor -* This class works automatically with sensors that -* provide the following methods : -* - constructor(void); -* - uint8_t/bool begin(void); -* - float read(void); -* -* If your sensor doesn't provide these methods -* or is not present in the specialized templates -* feel free to implement your own specializiation, -* following the provided generic template , -* or contact us and we will be glad to expand our -* list of supported external sensors -*/ -template -class ExternalSensor : public BaseExternalSensor -{ -public : - /** - * Generic Constructor - */ - ExternalSensor() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor Constructor" ); - Serial.println(); - - #endif - - sensor(); - } - - /** - * Generic begin method - * - * - */ - virtual uint8_t begin() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor begin()" ); - Serial.println(); - - #endif - - return(sensor.begin() ); - } - - /** - * Generic read method - */ - virtual float read() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor read() " ); - Serial.println(); - - #endif - - return(sensor.read()); - } - - - -private : - - T sensor; //the sensor itself +#endif + } + + /** + * begin(): + * Base class virtual + * generic begin method + * + * \return generic value as it's not supposed + * to be used + */ + virtual uint8_t begin() { + +#if DEBUGExternal == 1 + + Serial.println("BaseExternalSensor.begin()"); + Serial.println(); + +#endif + + return (-2); + } + + /** + * read(): + * Base class virtual + * generic read method + * + * \return generic value + * as it is not supposed + * to be used + */ + virtual float read() { + +#if DEBUGExternal == 1 + + Serial.println("BaseExternalSensor.read()"); + Serial.println(); + +#endif + + return (-2); + } + + virtual float read(int16_t *a) { return (-42, 42); } + virtual float read(int16_t *a, int16_t *b, float *c) { return (-42.42); } + + virtual float read(uint32_t *a, uint32_t *b, uint32_t *c) { return (-42.42); } + + virtual float read(int16_t *a, int16_t *b, int16_t *c, int16_t *d) { + return (-42.42); + } + + virtual float read(int16_t *a, int16_t *b, int16_t *c, int16_t *d, int16_t *e, + int16_t *f) { + return (-42.42); + } + + virtual float read(int16_t *a, int16_t *b, int16_t *c, int16_t *d, int16_t *e, + int16_t *f, int16_t *g, int16_t *h) { + return (-42.42); + } }; /** -* \class ExternalSensor -* \brief NDIR_I2C Specialization Class -* This is the template specialization -* for the NDIR_I2C CO2 sensor -*/ -template<> -class ExternalSensor :public BaseExternalSensor -{ + * \class ExternalSensor + * \brief template class External Sensor: + * Derived class from BaseExternalSensor. + * + * This is the generic Template for an external sensor + * This class works automatically with sensors that + * provide the following methods: + * - constructor(void); + * - uint8_t/bool begin(void); + * - float read(void); + * + * If your sensor doesn't provide these methods + * or is not present in the specialized templates + * feel free to implement your own specialization, + * following the provided generic template, + * or contact us and we will be glad to expand our + * list of supported external sensors + */ +template class ExternalSensor : public BaseExternalSensor { public: + /** + * Generic Constructor + */ + ExternalSensor() { - /** - * ExternalSensor(I2C address): - * NDIR_I2C specific constructor - */ - ExternalSensor(uint8_t i2c_addr) - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor constructor"); - Serial.println(); - - #endif - - sensor=NDIR_I2C(i2c_addr); - } - - /** - * begin(): - * NDIR_I2C specific begin method - * - * \return true if successful, - * false otherwise - */ - virtual uint8_t begin() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor begin()" ); - Serial.println(); - - #endif - - if (sensor.begin()) - { - - #if DEBUGExternal == 1 - - Serial.println( "NDIR_I2C init : wait 10 seconds" ); - Serial.println(); - - #endif - - delay(10000); - return(true); - - } - else - { - - #if DEBUGExternal == 1 - - Serial.println( "NDIR_I2C init : fail " ); - Serial.println(); - - #endif - - return(false); - } - } - - /** - * read(): - * NDIR_I2C specific read method - * - * \return the ppm value if successful, - * else return -42 - */ - virtual float read() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor read()" ); - Serial.println(); - - #endif - - if (sensor.measure()) - { - - #if DEBUGExternal == 1 - - Serial.print( "NDIR_I2C ppm :" ); - Serial.println( (float) sensor.ppm); - - Serial.println(); - - #endif - - return( (float) sensor.ppm); - - } - - else - { - - #if DEBUGExternal == 1 - - Serial.println( "NDIR_I2C read fail " ); - Serial.println(); - - #endif - - return(-42); - } - } +#if DEBUGExternal == 1 -private: + Serial.println("ExternalSensor Constructor"); + Serial.println(); + +#endif + + sensor(); + } + + /** + * Generic begin method + */ + virtual uint8_t begin() { + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor begin()"); + Serial.println(); - NDIR_I2C sensor=NULL; +#endif + + return (sensor.begin()); + } + + /** + * Generic read method + */ + virtual float read() { + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor read() "); + Serial.println(); + +#endif + + return (sensor.read()); + } + +private: + T sensor; // the sensor itself }; /** -* \class ExternalSensor -* \brief DallasTemperature Specialization Class -* This is the template specialization -* for the Dallas Temperature sensor -*/ -template<> -class ExternalSensor :public BaseExternalSensor -{ + * \class ExternalSensor + * \brief NDIR_I2C Specialization Class + * This is the template specialization + * for the NDIR_I2C CO2 sensor + */ +template <> class ExternalSensor : public BaseExternalSensor { public: - /** - * ExternalSensor(): - * DallasTemperature specific constructor - */ - ExternalSensor(OneWire* oneWire) - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor constructor" ); - Serial.println(); - - #endif - sensor=DallasTemperature(oneWire); - } - - /** - * begin(): - * DallasTemperature specific begin method - * - * \return true if successful - */ - virtual uint8_t begin() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor begin()" ); - Serial.println(); - - #endif - - sensor.begin(); - delay(5); - sensor.getAddress(this->dallasAddress, 0); - return(true); - } - /** - * read(): - * DallasTemperature specific read method - * - * \return the temperature in °C - */ - virtual float read() - { - - sensor.requestTemperatures(); // Send the command to get temperatures - float result=(float) sensor.getTempCByIndex(0); - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor read()" ); - Serial.println(); - - Serial.print("Requesting temperature..."); - - Serial.print( "temperature : "); - Serial.print( result ); - Serial.print( "°C" ); - Serial.println(); - - #endif - - return( result ); - } + /** + * ExternalSensor(I2C address): + * NDIR_I2C specific constructor + */ + ExternalSensor(uint8_t i2c_addr) { -private: +#if DEBUGExternal == 1 + Serial.println("ExternalSensor constructor"); + Serial.println(); - DallasTemperature sensor; - DeviceAddress dallasAddress; -}; +#endif + + sensor = NDIR_I2C(i2c_addr); + } + + /** + * begin(): + * NDIR_I2C specific begin method + * + * \return true if successful, + * false otherwise + */ + virtual uint8_t begin() { + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor begin()"); + Serial.println(); + +#endif + + if (sensor.begin()) { + +#if DEBUGExternal == 1 + + Serial.println("NDIR_I2C init : wait 10 seconds"); + Serial.println(); + +#endif + + delay(10000); + return (true); + + } else { + +#if DEBUGExternal == 1 + + Serial.println("NDIR_I2C init : fail "); + Serial.println(); + +#endif + + return (false); + } + } + /** + * read(): + * NDIR_I2C specific read method + * + * \return the ppm value if successful, + * else return -42 + */ + virtual float read() { + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor read()"); + Serial.println(); + +#endif + + if (sensor.measure()) { + +#if DEBUGExternal == 1 + + Serial.print("NDIR_I2C ppm :"); + Serial.println((float)sensor.ppm); + + Serial.println(); + +#endif + + return ((float)sensor.ppm); + + } + + else { + +#if DEBUGExternal == 1 + + Serial.println("NDIR_I2C read fail "); + Serial.println(); + +#endif + + return (-42); + } + } + +private: + NDIR_I2C sensor = NULL; +}; /** -* \class ExternalSensor -* \brief Adafruit_TCS34725 Specialization Class -* This is the template specialization -* for the Adafruit RGB Sensor -*/ - -template<> -class ExternalSensor :public BaseExternalSensor -{ + * \class ExternalSensor + * \brief DallasTemperature Specialization Class + * This is the template specialization + * for the Dallas Temperature sensor + */ +template <> +class ExternalSensor : public BaseExternalSensor { public: - /** - * ExternalSensor(): - * Adafruit_TCS34725 specific constructor - */ - ExternalSensor() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor constructor" ); - Serial.println(); - - #endif - /* Initialise with default values (int time = 2.4ms, gain = 1x) */ - sensor=Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS,TCS34725_GAIN_1X); - - } - - /** - * begin(): - * Adafruit_TCS34725 specific begin method - * - * \return true if successful - */ - virtual uint8_t begin() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor begin()" ); - Serial.println(); - - #endif - - if (sensor.begin()) - { - - Serial.println("Found sensor"); - - return(true); - } - else - { - Serial.println("No TCS34725 found ... check your connections"); - - return(false); - } - - } - - /** - * read(int16_t *a,int16_t *b,int16_t *c,int16_t *d,int16_t *e,int16_t *f): - * Adafruit_TCS34725 specific read method - * - * modifies the input to R,G,B,C,lux,ColorTemp values - */ - virtual float read(int16_t *a,int16_t *b,int16_t *c,int16_t *d,int16_t *e,int16_t *f) - { - uint16_t internR,internG,internB,internC,internColorTemp,internLux; - - sensor.getRawData(&internR, &internG, &internB, &internC); - - internColorTemp=sensor.calculateColorTemperature(internR,internG,internB); - - internLux =sensor.calculateLux(internR,internG,internB); - - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor read()" ); - Serial.println(); - - Serial.print("Color Temp: "); Serial.print(internColorTemp, DEC); Serial.print(" K - "); - Serial.print("Lux: "); Serial.print(internLux, DEC); Serial.print(" - "); - Serial.print("R: "); Serial.print(internR, DEC); Serial.print(" "); - Serial.print("G: "); Serial.print(internG, DEC); Serial.print(" "); - Serial.print("B: "); Serial.print(internB, DEC); Serial.print(" "); - Serial.print("C: "); Serial.print(internC, DEC); Serial.print(" "); - Serial.println(" "); - - #endif - *a=(int16_t)internR; - *b=(int16_t)internG; - *c=(int16_t)internB; - *d=(int16_t)internC; - *e=(int16_t)internColorTemp; - *f=(int16_t)internLux; - - return( 0.0 ); - } + /** + * ExternalSensor(): + * DallasTemperature specific constructor + */ + ExternalSensor(OneWire *oneWire) { -private: +#if DEBUGExternal == 1 + Serial.println("ExternalSensor constructor"); + Serial.println(); - Adafruit_TCS34725 sensor; +#endif + sensor = DallasTemperature(oneWire); + } -}; + /** + * begin(): + * DallasTemperature specific begin method + * + * \return true if successful + */ + virtual uint8_t begin() { +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor begin()"); + Serial.println(); + +#endif + + sensor.begin(); + delay(5); + sensor.getAddress(this->dallasAddress, 0); + return (true); + } + /** + * read(): + * DallasTemperature specific read method + * + * \return the temperature in °C + */ + virtual float read() { + + sensor.requestTemperatures(); // Send the command to get temperatures + float result = (float)sensor.getTempCByIndex(0); +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor read()"); + Serial.println(); + + Serial.print("Requesting temperature..."); + + Serial.print("temperature : "); + Serial.print(result); + Serial.print("°C"); + Serial.println(); + +#endif + + return (result); + } + +private: + DallasTemperature sensor; + DeviceAddress dallasAddress; +}; /** -* \class ExternalSensor -* \brief Adafruit_CCS881 Specialization Class -* This is the template specialization -* for the Adafruit VOC/eCO2 Sensor -*/ - -template<> -class ExternalSensor :public BaseExternalSensor -{ + * \class ExternalSensor + * \brief Adafruit_TCS34725 Specialization Class + * This is the template specialization + * for the Adafruit RGB Sensor + */ + +template <> +class ExternalSensor : public BaseExternalSensor { public: - /** - * ExternalSensor(): - * Adafruit_CCS881 specific constructor - */ - ExternalSensor(uint8_t i2c_addr) - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor constructor" ); - Serial.println(); - - #endif - - sensor=Adafruit_CCS811(); - - if(!sensor.begin(i2c_addr)){ - Serial.println("Failed to start sensor! Please check your wiring."); - } - - } - - virtual uint8_t begin() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor begin()" ); - Serial.println(); - - #endif - - //calibrate temperature sensor - while(!sensor.available()); - float T = sensor.calculateTemperature(); - sensor.setTempOffset(T - 25.0); - - } - - virtual float read(int16_t *a,int16_t *b,float *c) - { - uint16_t internC,internV; - float internT; - - if(sensor.available()){ - internT = sensor.calculateTemperature(); - if(!sensor.readData()){ - internC = sensor.geteCO2(); - internV = sensor.getTVOC(); - } - } - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor read()" ); - Serial.println(); - - Serial.print("C02 : "); Serial.print(internC); Serial.println(" ppm "); - Serial.print("VOT : "); Serial.print(internV); Serial.println(" ppb "); - Serial.print("Temp: "); Serial.print(internT); Serial.println(" °C "); - Serial.println(" "); - - #endif - *a=(int16_t)internC; - *b=(int16_t)internV; - *c=internT; - return( 0.0 ); - } + /** + * ExternalSensor(): + * Adafruit_TCS34725 specific constructor + */ + ExternalSensor() { -private: +#if DEBUGExternal == 1 - Adafruit_CCS811 sensor; + Serial.println("ExternalSensor constructor"); + Serial.println(); -}; +#endif + /* Initialise with default values (int time = 2.4ms, gain = 1x) */ + sensor = + Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); + } + + /** + * begin(): + * Adafruit_TCS34725 specific begin method + * + * \return true if successful + */ + virtual uint8_t begin() { + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor begin()"); + Serial.println(); + +#endif + if (sensor.begin()) { + Serial.println("Found sensor"); + return (true); + } else { + Serial.println("No TCS34725 found ... check your connections"); + return (false); + } + } + + /** + * read(int16_t *a,int16_t *b,int16_t *c,int16_t *d,int16_t *e,int16_t *f): + * Adafruit_TCS34725 specific read method + * + * modifies the input to R,G,B,C,lux,ColorTemp values + */ + virtual float read(int16_t *a, int16_t *b, int16_t *c, int16_t *d, int16_t *e, + int16_t *f) { + uint16_t internR, internG, internB, internC, internColorTemp, internLux; + + sensor.getRawData(&internR, &internG, &internB, &internC); + + internColorTemp = + sensor.calculateColorTemperature(internR, internG, internB); + + internLux = sensor.calculateLux(internR, internG, internB); + +#if DEBUGExternal == 1 + Serial.println("ExternalSensor read()"); + Serial.println(); + Serial.print("Color Temp: "); + Serial.print(internColorTemp, DEC); + Serial.print(" K - "); + Serial.print("Lux: "); + Serial.print(internLux, DEC); + Serial.print(" - "); + Serial.print("R: "); + Serial.print(internR, DEC); + Serial.print(" "); + Serial.print("G: "); + Serial.print(internG, DEC); + Serial.print(" "); + Serial.print("B: "); + Serial.print(internB, DEC); + Serial.print(" "); + Serial.print("C: "); + Serial.print(internC, DEC); + Serial.print(" "); + Serial.println(" "); +#endif + *a = (int16_t)internR; + *b = (int16_t)internG; + *c = (int16_t)internB; + *d = (int16_t)internC; + *e = (int16_t)internColorTemp; + *f = (int16_t)internLux; + + return (0.0); + } + +private: + Adafruit_TCS34725 sensor; +}; /** -* \class ExternalSensor -* \brief Adafruit_ADS1015 Specialization Class -* This is the template specialization -* for the Adafruit Analog I2C Interface -*/ - -template<> -class ExternalSensor :public BaseExternalSensor -{ + * \class ExternalSensor + * \brief Adafruit_CCS881 Specialization Class + * This is the template specialization + * for the Adafruit VOC/eCO2 Sensor + */ + +template <> class ExternalSensor : public BaseExternalSensor { public: - /** - * ExternalSensor(): - * Adafruit_ADS1015 specific constructor - */ - ExternalSensor(uint8_t i2c_addr) - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor constructor" ); - Serial.println(); - - #endif - - sensor=Adafruit_ADS1015(i2c_addr); - - } - - /** - * begin(): - * Adafruit_ADS1015 specific begin method - * - * \return true if successful - */ - virtual uint8_t begin() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor begin()" ); - Serial.println(); - - #endif - - sensor.begin(); - return(true); - - - } - - /** - * read(uint16_t *a,uint16_t *b,uint16_t *c,uint16_t *d,uint16_t *e,uint16_t *f): - * Adafruit_ADS1015 specific read method - * - * modifies the input variables to channel0..3 and differential01 ,23 values - */ - virtual float read(int16_t *a,int16_t *b,int16_t *c,int16_t *d,int16_t *e,int16_t *f,int16_t *g,int16_t *h) - { - uint16_t channel0,channel1,channel2,channel3; - uint16_t gain0, gain1, gain2, gain3; - - channel0=sensor.readADC_SingleEnded(0); - gain0=sensor.getGain(); - channel1=sensor.readADC_SingleEnded(1); - gain1=sensor.getGain(); - channel2=sensor.readADC_SingleEnded(2); - gain2=sensor.getGain(); - channel3=sensor.readADC_SingleEnded(3); - gain3=sensor.getGain(); - - - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor read()" ); - Serial.println(); - - Serial.print("Channel 0 : "); Serial.print(channel0, DEC); - Serial.print("Channel 1 : "); Serial.print(channel1, DEC); - Serial.print("Channel 2 : "); Serial.print(channel2, DEC); - Serial.print("Channel 3 : "); Serial.print(channel3, DEC); - Serial.println(" "); - - #endif - *a=(int16_t)channel0; - *b=(int16_t)gain0; - *c=(int16_t)channel1; - *d=(int16_t)gain1; - *e=(int16_t)channel2; - *f=(int16_t)gain2; - *g=(int16_t)channel3; - *h=(int16_t)gain3; - - return( 0.0 ); - } + /** + * ExternalSensor(): + * Adafruit_CCS881 specific constructor + */ + ExternalSensor(uint8_t i2c_addr) { -private: +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor constructor"); + Serial.println(); + +#endif + + sensor = Adafruit_CCS811(); + if (!sensor.begin(i2c_addr)) { + Serial.println("Failed to start sensor! Please check your wiring."); + } + } - Adafruit_ADS1015 sensor; + virtual uint8_t begin() { +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor begin()"); + Serial.println(); + +#endif + + // calibrate temperature sensor + while (!sensor.available()) + ; + float T = sensor.calculateTemperature(); + sensor.setTempOffset(T - 25.0); + } + + virtual float read(int16_t *a, int16_t *b, float *c) { + uint16_t internC, internV; + float internT; + + if (sensor.available()) { + internT = sensor.calculateTemperature(); + if (!sensor.readData()) { + internC = sensor.geteCO2(); + internV = sensor.getTVOC(); + } + } + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor read()"); + Serial.println(); + + Serial.print("C02 : "); + Serial.print(internC); + Serial.println(" ppm "); + Serial.print("VOT : "); + Serial.print(internV); + Serial.println(" ppb "); + Serial.print("Temp: "); + Serial.print(internT); + Serial.println(" °C "); + Serial.println(" "); + +#endif + *a = (int16_t)internC; + *b = (int16_t)internV; + *c = internT; + return (0.0); + } + +private: + Adafruit_CCS811 sensor; }; /** -* \class ExternalSensor -* \brief Adafruit_ADS1115 Specialization Class -* This is the template specialization -* for the Adafruit Analog I2C Interface -*/ - -template<> -class ExternalSensor :public BaseExternalSensor -{ + * \class ExternalSensor + * \brief Adafruit_ADS1015 Specialization Class + * This is the template specialization + * for the Adafruit Analog I2C Interface + */ + +template <> class ExternalSensor : public BaseExternalSensor { public: - /** - * ExternalSensor(): - * Adafruit_ADS1115 specific constructor - */ - ExternalSensor(uint8_t i2c_addr) - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor constructor" ); - Serial.println(); - - #endif - - sensor=Adafruit_ADS1115(i2c_addr); - - } - - /** - * begin(): - * Adafruit_ADS1115 specific begin method - * - * \return true if successful - */ - virtual uint8_t begin() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor begin()" ); - Serial.println(); - - #endif - - sensor.begin(); - return(true); - - - } - - /** - * read(uint16_t *a,uint16_t *b,uint16_t *c,uint16_t *d,uint16_t *e,uint16_t *f): - * Adafruit_ADS1115 specific read method - * - * modifies the input variables to channel0..3 and differential01 ,23 values - */ - virtual float read(int16_t *a,int16_t *b,int16_t *c,int16_t *d,int16_t *e,int16_t *f,int16_t *g,int16_t *h) - { - uint16_t channel0,channel1,channel2,channel3; - uint16_t gain0, gain1, gain2, gain3; - - channel0=sensor.readADC_SingleEnded(0); - gain0=sensor.getGain(); - channel1=sensor.readADC_SingleEnded(1); - gain1=sensor.getGain(); - channel2=sensor.readADC_SingleEnded(2); - gain2=sensor.getGain(); - channel3=sensor.readADC_SingleEnded(3); - gain3=sensor.getGain(); - - - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor read()" ); - Serial.println(); - - Serial.print("Channel 0 : "); Serial.print(channel0, DEC); - Serial.print("Channel 1 : "); Serial.print(channel1, DEC); - Serial.print("Channel 2 : "); Serial.print(channel2, DEC); - Serial.print("Channel 3 : "); Serial.print(channel3, DEC); - Serial.println(" "); - - #endif - *a=(int16_t)channel0; - *b=(int16_t)gain0; - *c=(int16_t)channel1; - *d=(int16_t)gain1; - *e=(int16_t)channel2; - *f=(int16_t)gain2; - *g=(int16_t)channel3; - *h=(int16_t)gain3; - - return( 0.0 ); - } + /** + * ExternalSensor(): + * Adafruit_ADS1015 specific constructor + */ + ExternalSensor(uint8_t i2c_addr) { -private: +#if DEBUGExternal == 1 + Serial.println("ExternalSensor constructor"); + Serial.println(); - Adafruit_ADS1115 sensor; +#endif + + sensor = Adafruit_ADS1015(i2c_addr); + } + + /** + * begin(): + * Adafruit_ADS1015 specific begin method + * + * \return true if successful + */ + virtual uint8_t begin() { +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor begin()"); + Serial.println(); + +#endif + + sensor.begin(); + return (true); + } + + /** + * read(uint16_t *a,uint16_t *b,uint16_t *c,uint16_t *d,uint16_t + **e,uint16_t *f): Adafruit_ADS1015 specific read method + * + * modifies the input variables to channel0..3 and differential01 ,23 + *values + */ + virtual float read(int16_t *a, int16_t *b, int16_t *c, int16_t *d, int16_t *e, + int16_t *f, int16_t *g, int16_t *h) { + uint16_t channel0, channel1, channel2, channel3; + uint16_t gain0, gain1, gain2, gain3; + + channel0 = sensor.readADC_SingleEnded(0); + gain0 = sensor.getGain(); + channel1 = sensor.readADC_SingleEnded(1); + gain1 = sensor.getGain(); + channel2 = sensor.readADC_SingleEnded(2); + gain2 = sensor.getGain(); + channel3 = sensor.readADC_SingleEnded(3); + gain3 = sensor.getGain(); + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor read()"); + Serial.println(); + + Serial.print("Channel 0 : "); + Serial.print(channel0, DEC); + Serial.print("Channel 1 : "); + Serial.print(channel1, DEC); + Serial.print("Channel 2 : "); + Serial.print(channel2, DEC); + Serial.print("Channel 3 : "); + Serial.print(channel3, DEC); + Serial.println(" "); + +#endif + *a = (int16_t)channel0; + *b = (int16_t)gain0; + *c = (int16_t)channel1; + *d = (int16_t)gain1; + *e = (int16_t)channel2; + *f = (int16_t)gain2; + *g = (int16_t)channel3; + *h = (int16_t)gain3; + + return (0.0); + } + +private: + Adafruit_ADS1015 sensor; }; -template<> -class ExternalSensor :public BaseExternalSensor -{ +/** + * \class ExternalSensor + * \brief Adafruit_ADS1115 Specialization Class + * This is the template specialization + * for the Adafruit Analog I2C Interface + */ + +template <> class ExternalSensor : public BaseExternalSensor { public: - /** - * ExternalSensor(): - * CoolGauge specific constructor - */ - ExternalSensor() - { - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor constructor" ); - Serial.println(); - - #endif - - sensor=Gauges(); - } - - virtual uint8_t begin() - {} - - virtual float read(uint32_t *a,uint32_t *b,uint32_t *c) - { - uint32_t A, B, C; - - A = sensor.readGauge1(); - B = sensor.readGauge2(); - C = sensor.readGauge3(); - - #if DEBUGExternal == 1 - - Serial.println( "ExternalSensor read()" ); - Serial.println(); - - Serial.print("Gauge 1 : "); Serial.print(A); - Serial.print("Gauge 2 : "); Serial.print(B); - Serial.print("Gauge 3 : "); Serial.print(C); - Serial.println(" "); - - #endif - *a=A; - *b=B; - *c=C; - return( 0.0 ); - } + /** + * ExternalSensor(): + * Adafruit_ADS1115 specific constructor + */ + ExternalSensor(uint8_t i2c_addr) { + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor constructor"); + Serial.println(); + +#endif + + sensor = Adafruit_ADS1115(i2c_addr); + } + + /** + * begin(): + * Adafruit_ADS1115 specific begin method + * + * \return true if successful + */ + virtual uint8_t begin() { + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor begin()"); + Serial.println(); + +#endif + + sensor.begin(); + return (true); + } + + /** + * read(uint16_t *a,uint16_t *b,uint16_t *c,uint16_t *d,uint16_t + **e,uint16_t *f): Adafruit_ADS1115 specific read method + * + * modifies the input variables to channel0..3 and differential01 ,23 + *values + */ + virtual float read(int16_t *a, int16_t *b, int16_t *c, int16_t *d, int16_t *e, + int16_t *f, int16_t *g, int16_t *h) { + uint16_t channel0, channel1, channel2, channel3; + uint16_t gain0, gain1, gain2, gain3; + + channel0 = sensor.readADC_SingleEnded(0); + gain0 = sensor.getGain(); + channel1 = sensor.readADC_SingleEnded(1); + gain1 = sensor.getGain(); + channel2 = sensor.readADC_SingleEnded(2); + gain2 = sensor.getGain(); + channel3 = sensor.readADC_SingleEnded(3); + gain3 = sensor.getGain(); + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor read()"); + Serial.println(); + + Serial.print("Channel 0 : "); + Serial.print(channel0, DEC); + Serial.print("Channel 1 : "); + Serial.print(channel1, DEC); + Serial.print("Channel 2 : "); + Serial.print(channel2, DEC); + Serial.print("Channel 3 : "); + Serial.print(channel3, DEC); + Serial.println(" "); + +#endif + *a = (int16_t)channel0; + *b = (int16_t)gain0; + *c = (int16_t)channel1; + *d = (int16_t)gain1; + *e = (int16_t)channel2; + *f = (int16_t)gain2; + *g = (int16_t)channel3; + *h = (int16_t)gain3; + + return (0.0); + } private: + Adafruit_ADS1115 sensor; +}; - Gauges sensor; +template <> class ExternalSensor : public BaseExternalSensor { +public: + /** + * ExternalSensor(): + * CoolGauge specific constructor + */ + ExternalSensor() { + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor constructor"); + Serial.println(); + +#endif + sensor = Gauges(); + } + + virtual uint8_t begin() {} + + virtual float read(uint32_t *a, uint32_t *b, uint32_t *c) { + uint32_t A, B, C; + + A = sensor.readGauge1(); + B = sensor.readGauge2(); + C = sensor.readGauge3(); + +#if DEBUGExternal == 1 + + Serial.println("ExternalSensor read()"); + Serial.println(); + + Serial.print("Gauge 1 : "); + Serial.print(A); + Serial.print("Gauge 2 : "); + Serial.print(B); + Serial.print("Gauge 3 : "); + Serial.print(C); + Serial.println(" "); + +#endif + *a = A; + *b = B; + *c = C; + return (0.0); + } + +private: + Gauges sensor; }; #endif diff --git a/src/ExternalSensors.cpp b/src/ExternalSensors.cpp index fcf35df7..14c9717d 100644 --- a/src/ExternalSensors.cpp +++ b/src/ExternalSensors.cpp @@ -1,567 +1,483 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #include "FS.h" -#include"Arduino.h" +#include "Arduino.h" -#include"OneWire.h" +#include "OneWire.h" -#include"ExternalSensors.h" +#include "ExternalSensors.h" #include "ArduinoJson.h" -#include"ExternalSensor.h" +#include "ExternalSensor.h" #define DEBUG 0 OneWire oneWire(0); - /** -* ExternalSensors::begin(): -* This method is provided to initialise -* the external sensors. -*/ -void ExternalSensors::begin() -{ + * ExternalSensors::begin(): + * This method is provided to initialise + * the external sensors. + */ +void ExternalSensors::begin() { #if DEBUG == 1 - Serial.println( F("Enter ExternalSensors.begin()") ); - Serial.println(); - -#endif - - for(int i=0;i< this->sensorsNumber ; i++) - { - if( (sensors[i].reference) == "NDIR_I2C" ) - { - std::unique_ptr< ExternalSensor > sensorCO2(new ExternalSensor( this->sensors[i].address) ); - - - sensors[i].exSensor= sensorCO2.release(); - sensors[i].exSensor->begin(); - sensors[i].exSensor->read(); - - } - else if( (sensors[i].reference) == "DallasTemperature") - { - - std::unique_ptr< ExternalSensor > dallasTemp(new ExternalSensor (&oneWire)); - - sensors[i].exSensor=dallasTemp.release(); - sensors[i].exSensor->begin(); - sensors[i].exSensor->read(); - } - else if( (sensors[i].reference) == "Adafruit_TCS34725") - { - int16_t r, g, b, c, colorTemp, lux; - - std::unique_ptr< ExternalSensor > rgbSensor(new ExternalSensor ()); - - sensors[i].exSensor=rgbSensor.release(); - sensors[i].exSensor->begin(); - sensors[i].exSensor->read(&r,&g,&b,&c,&colorTemp,&lux); - } - - else if( (sensors[i].reference) == "Adafruit_CCS811") - { - int16_t C02, VOT; - float Temp; - - std::unique_ptr< ExternalSensor > aqSensor(new ExternalSensor (this->sensors[i].address)); - sensors[i].exSensor=aqSensor.release(); - sensors[i].exSensor->begin(); - sensors[i].exSensor->read(&C02, &VOT, &Temp); - } - - else if( (sensors[i].reference) == "Adafruit_ADS1015") - { - int16_t channel0, channel1, channel2, channel3, diff01, diff23; - int16_t gain0, gain1, gain2, gain3; - - std::unique_ptr< ExternalSensor > analogI2C(new ExternalSensor (this->sensors[i].address)); - - sensors[i].exSensor=analogI2C.release(); - sensors[i].exSensor->begin(); - sensors[i].exSensor->read(&channel0, &gain0, &channel1, &gain1, &channel2, &gain2, &channel3, &gain3); - - } - - else if( (sensors[i].reference) == "Adafruit_ADS1115") - { - //Serial.print("ADS1115 !!! address: "); - //Serial.println(this->sensors[i].address); - int16_t channel0, channel1, channel2, channel3, diff01, diff23; - int16_t gain0, gain1, gain2, gain3; - - std::unique_ptr< ExternalSensor > analogI2C(new ExternalSensor (this->sensors[i].address)); - - sensors[i].exSensor=analogI2C.release(); - //sensors[i].exSensor->begin(); - sensors[i].exSensor->read(&channel0, &gain0, &channel1, &gain1, &channel2, &gain2, &channel3, &gain3); - } - - else if( (sensors[i].reference) == "CoolGauge") - { - uint32_t A, B, C; - - std::unique_ptr< ExternalSensor > gauge(new ExternalSensor ()); - - sensors[i].exSensor=gauge.release(); - //sensors[i].exSensor->begin(); - sensors[i].exSensor->read(&A, &B, &C); - } - - } + Serial.println(F("Enter ExternalSensors.begin()")); + Serial.println(); + +#endif + + for (int i = 0; i < this->sensorsNumber; i++) { + if ((sensors[i].reference) == "NDIR_I2C") { + std::unique_ptr> sensorCO2( + new ExternalSensor(this->sensors[i].address)); + + sensors[i].exSensor = sensorCO2.release(); + sensors[i].exSensor->begin(); + sensors[i].exSensor->read(); + + } else if ((sensors[i].reference) == "DallasTemperature") { + + std::unique_ptr> dallasTemp( + new ExternalSensor(&oneWire)); + + sensors[i].exSensor = dallasTemp.release(); + sensors[i].exSensor->begin(); + sensors[i].exSensor->read(); + } else if ((sensors[i].reference) == "Adafruit_TCS34725") { + int16_t r, g, b, c, colorTemp, lux; + + std::unique_ptr> rgbSensor( + new ExternalSensor()); + + sensors[i].exSensor = rgbSensor.release(); + sensors[i].exSensor->begin(); + sensors[i].exSensor->read(&r, &g, &b, &c, &colorTemp, &lux); + } + + else if ((sensors[i].reference) == "Adafruit_CCS811") { + int16_t C02, VOT; + float Temp; + + std::unique_ptr> aqSensor( + new ExternalSensor(this->sensors[i].address)); + sensors[i].exSensor = aqSensor.release(); + sensors[i].exSensor->begin(); + sensors[i].exSensor->read(&C02, &VOT, &Temp); + } + + else if ((sensors[i].reference) == "Adafruit_ADS1015") { + int16_t channel0, channel1, channel2, channel3, diff01, diff23; + int16_t gain0, gain1, gain2, gain3; + + std::unique_ptr> analogI2C( + new ExternalSensor(this->sensors[i].address)); + + sensors[i].exSensor = analogI2C.release(); + sensors[i].exSensor->begin(); + sensors[i].exSensor->read(&channel0, &gain0, &channel1, &gain1, &channel2, + &gain2, &channel3, &gain3); + + } + + else if ((sensors[i].reference) == "Adafruit_ADS1115") { + // Serial.print("ADS1115 !!! address: "); + // Serial.println(this->sensors[i].address); + int16_t channel0, channel1, channel2, channel3, diff01, diff23; + int16_t gain0, gain1, gain2, gain3; + + std::unique_ptr> analogI2C( + new ExternalSensor(this->sensors[i].address)); + + sensors[i].exSensor = analogI2C.release(); + // sensors[i].exSensor->begin(); + sensors[i].exSensor->read(&channel0, &gain0, &channel1, &gain1, &channel2, + &gain2, &channel3, &gain3); + } + + else if ((sensors[i].reference) == "CoolGauge") { + uint32_t A, B, C; + + std::unique_ptr> gauge( + new ExternalSensor()); + + sensors[i].exSensor = gauge.release(); + // sensors[i].exSensor->begin(); + sensors[i].exSensor->read(&A, &B, &C); + } + } } /** -* ExternalSensors::read(): -* This method is provided to -* read the data from the external sensors -* -* \return json string that contains the -* sensors data -*/ -String ExternalSensors::read() -{ + * ExternalSensors::read(): + * This method is provided to + * read the data from the external sensors + * + * \return json string that contains the + * sensors data + */ +String ExternalSensors::read() { + +#if DEBUG == 1 + + Serial.println(F("Entering ExternalSensors.read()")); + Serial.println(); + +#endif + + String data; + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.createObject(); + + if (!root.success()) { #if DEBUG == 1 - Serial.println( F("Entering ExternalSensors.read()") ); - Serial.println(); - -#endif - - String data; - DynamicJsonBuffer jsonBuffer ; - JsonObject& root = jsonBuffer.createObject(); - - if(!root.success() ) - { - - #if DEBUG == 1 - - Serial.println( F("failed to create json ") ); - - #endif - - return("00"); - } - else - { - if(sensorsNumber>0) - { - for(int i=0;iread(&r,&g,&b,&c,&colorTemp,&lux); - - root[sensors[i].kind0] = r; - root[sensors[i].kind1] = g; - root[sensors[i].kind2] = b; - root[sensors[i].kind3] = c; - } - else if(sensors[i].reference=="Adafruit_CCS811") - { - int16_t C, V ; - float T; - - sensors[i].exSensor->read(&C,&V,&T); - - root[sensors[i].kind0] = C; - root[sensors[i].kind1] = V; - root[sensors[i].kind2] = T; - } - else if((sensors[i].reference=="Adafruit_ADS1015" ) || (sensors[i].reference=="Adafruit_ADS1115" ) ) - { - int16_t channel0, channel1, channel2, channel3, diff01, diff23; - int16_t gain0, gain1, gain2, gain3; - - sensors[i].exSensor->read(&channel0, &gain0, &channel1, &gain1, &channel2, &gain2, &channel3, &gain3); - gain0 = gain0/512; - gain1 = gain1/512; - gain2 = gain2/512; - gain3 = gain3/512; - root["0_" + sensors[i].kind0] = channel0; - root["G0_" + sensors[i].kind0] = gain0; - root["1_" + sensors[i].kind1] = channel1; - root["G1_" + sensors[i].kind1] = gain1; - root["2_" + sensors[i].kind2] = channel2; - root["G2_" + sensors[i].kind2] = gain2; - root["3_" + sensors[i].kind3] = channel3; - root["G3_" + sensors[i].kind3] = gain3; - } - else if(sensors[i].reference=="CoolGauge") - { - uint32_t A, B, C; - - sensors[i].exSensor->read(&A, &B, &C); - - root[sensors[i].kind0] = A; - root[sensors[i].kind1] = B; - root[sensors[i].kind2] = C; - } - else - { - root[sensors[i].type]=sensors[i].exSensor->read(); - } - } - - #if DEBUG == 1 - else - { - Serial.println(F("null pointer ")); - } - #endif - } - } - - root.printTo(data); - - #if DEBUG == 1 - - Serial.println( F("sensors data :") ); - Serial.println(data); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - return(data); - } + Serial.println(F("failed to create json ")); + +#endif + + return ("00"); + } else { + if (sensorsNumber > 0) { + for (int i = 0; i < sensorsNumber; i++) { + if (sensors[i].exSensor != NULL) { + if (sensors[i].reference == "Adafruit_TCS34725") { + int16_t r, g, b, c, colorTemp, lux; + + sensors[i].exSensor->read(&r, &g, &b, &c, &colorTemp, &lux); + + root[sensors[i].kind0] = r; + root[sensors[i].kind1] = g; + root[sensors[i].kind2] = b; + root[sensors[i].kind3] = c; + } else if (sensors[i].reference == "Adafruit_CCS811") { + int16_t C, V; + float T; + + sensors[i].exSensor->read(&C, &V, &T); + + root[sensors[i].kind0] = C; + root[sensors[i].kind1] = V; + root[sensors[i].kind2] = T; + } else if ((sensors[i].reference == "Adafruit_ADS1015") || + (sensors[i].reference == "Adafruit_ADS1115")) { + int16_t channel0, channel1, channel2, channel3, diff01, diff23; + int16_t gain0, gain1, gain2, gain3; + + sensors[i].exSensor->read(&channel0, &gain0, &channel1, &gain1, + &channel2, &gain2, &channel3, &gain3); + gain0 = gain0 / 512; + gain1 = gain1 / 512; + gain2 = gain2 / 512; + gain3 = gain3 / 512; + root["0_" + sensors[i].kind0] = channel0; + root["G0_" + sensors[i].kind0] = gain0; + root["1_" + sensors[i].kind1] = channel1; + root["G1_" + sensors[i].kind1] = gain1; + root["2_" + sensors[i].kind2] = channel2; + root["G2_" + sensors[i].kind2] = gain2; + root["3_" + sensors[i].kind3] = channel3; + root["G3_" + sensors[i].kind3] = gain3; + } else if (sensors[i].reference == "CoolGauge") { + uint32_t A, B, C; + + sensors[i].exSensor->read(&A, &B, &C); + + root[sensors[i].kind0] = A; + root[sensors[i].kind1] = B; + root[sensors[i].kind2] = C; + } else { + root[sensors[i].type] = sensors[i].exSensor->read(); + } + } +#if DEBUG == 1 + else { + Serial.println(F("null pointer ")); + } +#endif + } + } + + root.printTo(data); + +#if DEBUG == 1 + + Serial.println(F("sensors data :")); + Serial.println(data); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + return (data); + } } /** -* ExternalSensors::config(): -* This method is provided to configure -* the externalSensors through a configuration -* file -* -* \return true if successful,false otherwise -*/ -bool ExternalSensors::config() -{ - //read config file - //update data - File externalSensorsConfig = SPIFFS.open("/externalSensorsConfig.json", "r"); - - if (!externalSensorsConfig) - { - - #if DEBUG == 1 - - Serial.println( F("failed to read /externalSensorsConfig.json") ); - Serial.println(); - - #endif - - return(false); - } - else - { - size_t size = externalSensorsConfig.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - externalSensorsConfig.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - - if (!json.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json") ); - Serial.println(); - - #endif - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("configuration json is : ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - if(json["sensorsNumber"]!=NULL) - { - this->sensorsNumber = json["sensorsNumber"]; - - - - for(int i=0;isensors[i].reference =sensorJson["reference"].as(); - } - else - { - this->sensors[i].reference=this->sensors[i].reference; - - } - sensorJson["reference"]=this->sensors[i].reference; - - - if(sensorJson["type"].success() ) - { - this->sensors[i].type=sensorJson["type"].as(); - } - else - { - this->sensors[i].type=this->sensors[i].type; - - } - sensorJson["type"]=this->sensors[i].type; - - if(sensorJson["address"].success() ) - { - this->sensors[i].address=sensorJson["address"]; - } - else - { - this->sensors[i].address=this->sensors[i].address; - - } - sensorJson["address"]=this->sensors[i].address; - - - - if(sensorJson["kind0"].success() ) - { - this->sensors[i].kind0=sensorJson["kind0"].as(); - } - else - { - this->sensors[i].kind0=this->sensors[i].kind0; - - } - sensorJson["kind0"]=this->sensors[i].kind0; - - - if(sensorJson["kind1"].success() ) - { - this->sensors[i].kind1=sensorJson["kind1"].as(); - } - else - { - this->sensors[i].kind1=this->sensors[i].kind1; - - } - sensorJson["kind1"]=this->sensors[i].kind1; - - if(sensorJson["kind2"].success() ) - { - this->sensors[i].kind2=sensorJson["kind2"].as(); - } - else - { - this->sensors[i].kind2=this->sensors[i].kind2; - - } - sensorJson["kind2"]=this->sensors[i].kind2; - - if(sensorJson["kind3"].success() ) - { - this->sensors[i].kind3=sensorJson["kind3"].as(); - } - else - { - this->sensors[i].kind3=this->sensors[i].kind3; - - } - sensorJson["sensor3"]=this->sensors[i].kind3; - - } - else - { - this->sensors[i]=this->sensors[i]; - } - - json[name]["reference"]=this->sensors[i].reference; - json[name]["type"]=this->sensors[i].type; - json[name]["address"]=this->sensors[i].address; - json[name]["kind0"]=this->sensors[i].kind0; - json[name]["kind1"]=this->sensors[i].kind1; - json[name]["kind2"]=this->sensors[i].kind2; - json[name]["kind3"]=this->sensors[i].kind3; - } - - } - else - { - this->sensorsNumber=this->sensorsNumber; - } - json["sensorsNumber"]=this->sensorsNumber; - - externalSensorsConfig.close(); - externalSensorsConfig = SPIFFS.open("/externalSensorsConfig.json", "w"); - - if(!externalSensorsConfig) - { - - #if DEBUG == 1 - - Serial.println( F("failed to write to /externalSensorsConfig.json") ); - Serial.println(); - - #endif - - return(false); - } - - json.printTo(externalSensorsConfig); - externalSensorsConfig.close(); - - #if DEBUG == 1 - - Serial.println( F("saved configuration is : ") ); - json.printTo(Serial); - Serial.println(); - - #endif - - return(true); - } - } - + * ExternalSensors::config(): + * This method is provided to configure + * the externalSensors through a configuration + * file + * + * \return true if successful,false otherwise + */ +bool ExternalSensors::config() { + // read config file + // update data + File externalSensorsConfig = SPIFFS.open("/externalSensorsConfig.json", "r"); + + if (!externalSensorsConfig) { + +#if DEBUG == 1 + + Serial.println(F("failed to read /externalSensorsConfig.json")); + Serial.println(); +#endif + return (false); + } else { + size_t size = externalSensorsConfig.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + externalSensorsConfig.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + + if (!json.success()) { + +#if DEBUG == 1 + + Serial.println(F("failed to parse json")); + Serial.println(); + +#endif + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("configuration json is : ")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + if (json["sensorsNumber"] != NULL) { + this->sensorsNumber = json["sensorsNumber"]; + + for (int i = 0; i < sensorsNumber; i++) { + String name = "sensor" + String(i); + + if (json[name].success()) { + JsonObject &sensorJson = json[name]; + + if (sensorJson["reference"].success()) { + this->sensors[i].reference = sensorJson["reference"].as(); + } else { + this->sensors[i].reference = this->sensors[i].reference; + } + sensorJson["reference"] = this->sensors[i].reference; + + if (sensorJson["type"].success()) { + this->sensors[i].type = sensorJson["type"].as(); + } else { + this->sensors[i].type = this->sensors[i].type; + } + sensorJson["type"] = this->sensors[i].type; + + if (sensorJson["address"].success()) { + this->sensors[i].address = sensorJson["address"]; + } else { + this->sensors[i].address = this->sensors[i].address; + } + sensorJson["address"] = this->sensors[i].address; + + if (sensorJson["kind0"].success()) { + this->sensors[i].kind0 = sensorJson["kind0"].as(); + } else { + this->sensors[i].kind0 = this->sensors[i].kind0; + } + sensorJson["kind0"] = this->sensors[i].kind0; + + if (sensorJson["kind1"].success()) { + this->sensors[i].kind1 = sensorJson["kind1"].as(); + } else { + this->sensors[i].kind1 = this->sensors[i].kind1; + } + sensorJson["kind1"] = this->sensors[i].kind1; + + if (sensorJson["kind2"].success()) { + this->sensors[i].kind2 = sensorJson["kind2"].as(); + } else { + this->sensors[i].kind2 = this->sensors[i].kind2; + } + sensorJson["kind2"] = this->sensors[i].kind2; + + if (sensorJson["kind3"].success()) { + this->sensors[i].kind3 = sensorJson["kind3"].as(); + } else { + this->sensors[i].kind3 = this->sensors[i].kind3; + } + sensorJson["sensor3"] = this->sensors[i].kind3; + + } else { + this->sensors[i] = this->sensors[i]; + } + + json[name]["reference"] = this->sensors[i].reference; + json[name]["type"] = this->sensors[i].type; + json[name]["address"] = this->sensors[i].address; + json[name]["kind0"] = this->sensors[i].kind0; + json[name]["kind1"] = this->sensors[i].kind1; + json[name]["kind2"] = this->sensors[i].kind2; + json[name]["kind3"] = this->sensors[i].kind3; + } + + } else { + this->sensorsNumber = this->sensorsNumber; + } + json["sensorsNumber"] = this->sensorsNumber; + + externalSensorsConfig.close(); + externalSensorsConfig = SPIFFS.open("/externalSensorsConfig.json", "w"); + + if (!externalSensorsConfig) { + +#if DEBUG == 1 + + Serial.println(F("failed to write to /externalSensorsConfig.json")); + Serial.println(); + +#endif + + return (false); + } + + json.printTo(externalSensorsConfig); + externalSensorsConfig.close(); + +#if DEBUG == 1 + + Serial.println(F("saved configuration is : ")); + json.printTo(Serial); + Serial.println(); + +#endif + + return (true); + } + } } /** -* ExternalSensors::config(String reference[],String type[],uint8_t address[],int sensorsNumber): -* This method is provided to configure -* the externalSensors without a configuration -* file -* -* \return true if successful,false otherwise -*/ -bool ExternalSensors::config(String reference[],String type[],uint8_t address[],int sensorsNumber) -{ + * ExternalSensors::config(String reference[],String type[],uint8_t + *address[],int sensorsNumber): This method is provided to configure the + *externalSensors without a configuration file + * + * \return true if successful,false otherwise + */ +bool ExternalSensors::config(String reference[], String type[], + uint8_t address[], int sensorsNumber) { + +#if DEBUG == 1 + + Serial.println(F("Entering ExternalSensors.conf(reference[], type[], " + "address[], sensorsNumber)")); + Serial.println(); + +#endif + if (sensorsNumber > 50) { #if DEBUG == 1 - Serial.println( F("Entering ExternalSensors.conf(reference[], type[], address[], sensorsNumber)") ); - Serial.println(); - -#endif - if(sensorsNumber>50) - { - - #if DEBUG == 1 - - Serial.println(F("you can't add more than 50 sensors")); - - #endif - - return(false); - } - - this->sensorsNumber=sensorsNumber; - - for(int i=0;isensors[i].reference=reference[i]; - - this->sensors[i].type=type[i]; - - this->sensors[i].address=address[i]; - - } - - return(true); + Serial.println(F("you can't add more than 50 sensors")); + +#endif + + return (false); + } + + this->sensorsNumber = sensorsNumber; + + for (int i = 0; i < sensorsNumber; i++) { + + this->sensors[i].reference = reference[i]; + this->sensors[i].type = type[i]; + + this->sensors[i].address = address[i]; + } + + return (true); } /** -* ExternalSensors::printConf(): -* This method is provided to print the -* configuration to the Serial Monitor -*/ -void ExternalSensors::printConf() -{ + * ExternalSensors::printConf(): + * This method is provided to print the + * configuration to the Serial Monitor + */ +void ExternalSensors::printConf() { #if DEBUG == 1 - Serial.println( F("Entering ExternalSensors.printConf()") ); - Serial.println(); - -#endif - - Serial.println("External Sensors configuration "); - - Serial.print("sensorsNumber : "); - Serial.println(sensorsNumber); - - for(int i=0;isensors[i].reference); - - Serial.print("sensor "); - Serial.print(i); - Serial.print(" type : "); - Serial.println(this->sensors[i].type); - - Serial.print("sensor "); - Serial.print(i); - Serial.print(" address : "); - Serial.println(this->sensors[i].address); - - } -} + Serial.println(F("Entering ExternalSensors.printConf()")); + Serial.println(); + +#endif + + Serial.println("External Sensors configuration "); + Serial.print("sensorsNumber : "); + Serial.println(sensorsNumber); + + for (int i = 0; i < sensorsNumber; i++) { + Serial.print("sensor "); + Serial.print(i); + Serial.print(" reference : "); + Serial.println(this->sensors[i].reference); + + Serial.print("sensor "); + Serial.print(i); + Serial.print(" type : "); + Serial.println(this->sensors[i].type); + + Serial.print("sensor "); + Serial.print(i); + Serial.print(" address : "); + Serial.println(this->sensors[i].address); + } +} diff --git a/src/ExternalSensors.h b/src/ExternalSensors.h index 44d45e8e..7e7c0f0e 100644 --- a/src/ExternalSensors.h +++ b/src/ExternalSensors.h @@ -1,87 +1,82 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef ExternalSensors_H #define ExternalSensors_H +#include "Arduino.h" -#include"Arduino.h" - -#include"ExternalSensor.h" +#include "ExternalSensor.h" /** -* \class ExternalSensors -* \brief This class handles the external sensors -* run time defintion , configuartion and actions -* -*/ -class ExternalSensors -{ + * \class ExternalSensors + * \brief This class handles the external sensors + * run time defintion , configuartion and actions + * + */ +class ExternalSensors { public: + void begin(); - void begin(); + String read(); - String read (); + bool config(); - bool config(); - - bool config(String reference[],String type[],uint8_t address[],int sensorsNumber); + bool config(String reference[], String type[], uint8_t address[], + int sensorsNumber); - void printConf(); + void printConf(); private: - - /** - * Array of 50 External Sensors - * - * An External Sensor is described by : - * - * sensor.reference : the sensor's reference ( NDIR_I2C...) - * - * sensor.type : the sensor's Type ( CO2 , Temperature , .... ) - * - * sensor.address : the sensor's Address if it has one - * - * sensor.exSensor : pointer to the dynmacially instanciated sensor - */ - struct sensor - { - String reference=""; - String type=""; - uint8_t address=0; - BaseExternalSensor *exSensor=NULL; - String kind0="0"; - String kind1="0"; - String kind2="0"; - String kind3="0"; - }sensors[50]; - -/** -* External Sensors Number -* Maximum is 50 -*/ -int sensorsNumber=0; + /** + * Array of 50 External Sensors + * + * An External Sensor is described by : + * + * sensor.reference : the sensor's reference ( NDIR_I2C...) + * + * sensor.type : the sensor's Type ( CO2 , Temperature , .... ) + * + * sensor.address : the sensor's Address if it has one + * + * sensor.exSensor : pointer to the dynmacially instanciated sensor + */ + struct sensor { + String reference = ""; + String type = ""; + uint8_t address = 0; + BaseExternalSensor *exSensor = NULL; + String kind0 = "0"; + String kind1 = "0"; + String kind2 = "0"; + String kind3 = "0"; + } sensors[50]; + /** + * External Sensors Number + * Maximum is 50 + */ + int sensorsNumber = 0; }; #endif diff --git a/src/Irene3000.cpp b/src/Irene3000.cpp index 528a5ce8..37ed98ac 100644 --- a/src/Irene3000.cpp +++ b/src/Irene3000.cpp @@ -1,861 +1,751 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #include "FS.h" -#include #include "ArduinoJson.h" -#include -#include "CoolBoardLed.h" //CoolBoard Led Manager +#include "CoolBoardLed.h" //CoolBoard Led Manager +#include #include - - - +#include #define DEBUG 0 - /** -* Irene3000::begin(): -* This method is provided to start the -* Irene3000 -*/ -void Irene3000::startADC() -{ - this->ads.begin(); - - delay(2000); + * Irene3000::begin(): + * This method is provided to start the + * Irene3000 + */ +void Irene3000::startADC() { + this->ads.begin(); + + delay(2000); } /** -* Irene3000::begin(): -* This method is provided to start the -* Irene3000 -*/ -void Irene3000::begin() -{ - this->ads.begin(); - - delay(2000); - -#if DEBUG == 1 - - - Serial.println( F("Entering Irene3000.begin()") ); - Serial.println(); - - - - -#endif - Serial.println(F("Press the button to calibrate the Ph probe ")); - - - delay(2000); - - int bValue=this->readButton(); - if (bValue >= 65000) bValue = 0; - - while(bValue<500) - { - bValue=this->readButton(); - delay(10); - } - - if( bValue > 20000 ) - { - - - - Serial.println(F("calibrating the Ph probe " ) ); - - Serial.println(F("ph7 calibration for 25 seconds")); - - delay(10000); - - this->calibratepH7(); - - delay(15000); - - this->calibratepH7(); - - delay(1000); - Serial.println(F("ph 7 calibration ok")); - Serial.println(); - - - Serial.println(F("ph 4 calibration for 25 seconds")); - - delay(10000); - - this->calibratepH4(); - - delay(15000); - - this->calibratepH4(); - - delay(1000); - - Serial.println(F("ph 4 calibration ok")); - Serial.println(); - - - - this->saveParams(); - - } + * Irene3000::begin(): + * This method is provided to start the + * Irene3000 + */ +void Irene3000::begin() { + this->ads.begin(); + + delay(2000); + +#if DEBUG == 1 + + Serial.println(F("Entering Irene3000.begin()")); + Serial.println(); + +#endif + Serial.println(F("Press the button to calibrate the Ph probe ")); + + delay(2000); + + int bValue = this->readButton(); + if (bValue >= 65000) + bValue = 0; + + while (bValue < 500) { + bValue = this->readButton(); + delay(10); + } + + if (bValue > 20000) { + + Serial.println(F("calibrating the Ph probe ")); + + Serial.println(F("ph7 calibration for 25 seconds")); + + delay(10000); + + this->calibratepH7(); + + delay(15000); + + this->calibratepH7(); + + delay(1000); + Serial.println(F("ph 7 calibration ok")); + Serial.println(); + + Serial.println(F("ph 4 calibration for 25 seconds")); + + delay(10000); + + this->calibratepH4(); + + delay(15000); + + this->calibratepH4(); + + delay(1000); + + Serial.println(F("ph 4 calibration ok")); + Serial.println(); + + this->saveParams(); + } } /** -* Irene3000:read(): -* This method is provided to read -* the Irene3000 sensors data -* -* \return json string of the sensors -* data -*/ -String Irene3000::read() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering Irene3000.read()") ); - Serial.println(); - -#endif - - String data; - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.createObject(); - if( !( root.success()) ) - { - - #if DEBUG == 1 - - Serial.println( F("failed to create json") ); - - #endif - - return(""); - } - - - if(waterTemp.active) - { - root["waterTemp"] = this->readTemp(); - - if(phProbe.active) - { - root["ph"] =this->readPh( root["waterTemp"].as() ) ; - } - - } - - if(adc2.active) - { - root[adc2.type] =this->readADSChannel2(adc2.gain); - } - - root.printTo(data); - -#if DEBUG == 1 - - Serial.println( F("Irene data : ") ); - Serial.println(data); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - -#endif - - return(data); - - + * Irene3000:read(): + * This method is provided to read + * the Irene3000 sensors data + * + * \return json string of the sensors + * data + */ +String Irene3000::read() { + +#if DEBUG == 1 + + Serial.println(F("Entering Irene3000.read()")); + Serial.println(); + +#endif + + String data; + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.createObject(); + if (!(root.success())) { + +#if DEBUG == 1 + + Serial.println(F("failed to create json")); + +#endif + + return (""); + } + + if (waterTemp.active) { + root["waterTemp"] = this->readTemp(); + + if (phProbe.active) { + root["ph"] = this->readPh(root["waterTemp"].as()); + } + } + if (adc2.active) { + root[adc2.type] = this->readADSChannel2(adc2.gain); + } + + root.printTo(data); + +#if DEBUG == 1 + + Serial.println(F("Irene data : ")); + Serial.println(data); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + return (data); } /** -* Irene3000::config(): -* This method is provided to configure the -* Irene3000 shield through a configuration file -* -* \return true if successful,false otherwise -*/ -bool Irene3000::config() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering Irene3000.config()") ); - Serial.println(); - -#endif - - File irene3000Config = SPIFFS.open("/irene3000Config.json", "r"); - - if (!irene3000Config) - { - - #if DEBUG == 1 - - Serial.println( F("failed to read /irene3000Config.json") ); - Serial.println(); - - #endif - - return(false); - } - else - { - size_t size = irene3000Config.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - uint16_t tempGain; - irene3000Config.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json ") ); - Serial.println(); - - #endif - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("read configuration file ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - - #endif - if(json["waterTemp"]["active"].success() ) - { - this->waterTemp.active = json["waterTemp"]["active"]; - } - else - { - this->waterTemp.active=this->waterTemp.active; - } - json["waterTemp"]["active"]=this->waterTemp.active; - - - if(json["phProbe"]["active"].success()) - { - this->phProbe.active=json["phProbe"]["active"]; - } - else - { - this->phProbe.active=this->phProbe.active; - } - json["phProbe"]["active"]=this->phProbe.active; - - - if(json["adc2"]["active"].success() ) - { - this->adc2.active=json["adc2"]["active"]; - } - else - { - this->adc2.active=this->adc2.active; - } - json["adc2"]["active"]=this->adc2.active; - - - if(json["adc2"]["gain"].success() ) - { - tempGain=json["adc2"]["gain"]; - this->adc2.gain=this->gainConvert(tempGain); - } - else - { - this->adc2.gain=this->adc2.gain; - } - json["adc2"]["gain"]=this->adc2.gain; - - - if(json["adc2"]["type"].success() ) - { - this->adc2.type=json["adc2"]["type"].as(); - } - else - { - this->adc2.type=this->adc2.type; - } - json["adc2"]["type"]=this->adc2.type; - - - if(json["pH7Cal"].success()) - { - this->params.pH7Cal=json["pH7Cal"]; - } - else - { - this->params.pH7Cal=this->params.pH7Cal; - } - json["pH7Cal"]=this->params.pH7Cal; - - - if(json["pH4Cal"].success()) - { - this->params.pH4Cal=json["pH4Cal"]; - } - else - { - this->params.pH4Cal=this->params.pH4Cal; - } - json["pH4Cal"]=this->params.pH4Cal; - - - if(json["pHStep"].success()) - { - this->params.pHStep=json["pHStep"]; - } - else - { - this->params.pHStep=this->params.pHStep; - } - json["pHStep"]=this->params.pHStep; - - - irene3000Config.close(); - irene3000Config = SPIFFS.open("/irene3000Config.json", "w"); - - if(!irene3000Config) - { - - #if DEBUG == 1 - - Serial.println( F("failed to write to /irene3000Config.json") ); - Serial.println(); - - #endif - - return(false); - } - - json.printTo(irene3000Config); - irene3000Config.close(); - - #if DEBUG == 1 - - Serial.println( F("saved configuration file :") ); - json.printTo(Serial); - Serial.println(); - - #endif - - return(true); - } - } + * Irene3000::config(): + * This method is provided to configure the + * Irene3000 shield through a configuration file + * + * \return true if successful,false otherwise + */ +bool Irene3000::config() { + +#if DEBUG == 1 + + Serial.println(F("Entering Irene3000.config()")); + Serial.println(); + +#endif + + File irene3000Config = SPIFFS.open("/irene3000Config.json", "r"); + + if (!irene3000Config) { + +#if DEBUG == 1 + + Serial.println(F("failed to read /irene3000Config.json")); + Serial.println(); + +#endif + + return (false); + } else { + size_t size = irene3000Config.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + uint16_t tempGain; + irene3000Config.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + +#if DEBUG == 1 + Serial.println(F("failed to parse json ")); + Serial.println(); + +#endif + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("read configuration file ")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + if (json["waterTemp"]["active"].success()) { + this->waterTemp.active = json["waterTemp"]["active"]; + } else { + this->waterTemp.active = this->waterTemp.active; + } + json["waterTemp"]["active"] = this->waterTemp.active; + + if (json["phProbe"]["active"].success()) { + this->phProbe.active = json["phProbe"]["active"]; + } else { + this->phProbe.active = this->phProbe.active; + } + json["phProbe"]["active"] = this->phProbe.active; + + if (json["adc2"]["active"].success()) { + this->adc2.active = json["adc2"]["active"]; + } else { + this->adc2.active = this->adc2.active; + } + json["adc2"]["active"] = this->adc2.active; + + if (json["adc2"]["gain"].success()) { + tempGain = json["adc2"]["gain"]; + this->adc2.gain = this->gainConvert(tempGain); + } else { + this->adc2.gain = this->adc2.gain; + } + json["adc2"]["gain"] = this->adc2.gain; + + if (json["adc2"]["type"].success()) { + this->adc2.type = json["adc2"]["type"].as(); + } else { + this->adc2.type = this->adc2.type; + } + json["adc2"]["type"] = this->adc2.type; + + if (json["pH7Cal"].success()) { + this->params.pH7Cal = json["pH7Cal"]; + } else { + this->params.pH7Cal = this->params.pH7Cal; + } + json["pH7Cal"] = this->params.pH7Cal; + + if (json["pH4Cal"].success()) { + this->params.pH4Cal = json["pH4Cal"]; + } else { + this->params.pH4Cal = this->params.pH4Cal; + } + json["pH4Cal"] = this->params.pH4Cal; + + if (json["pHStep"].success()) { + this->params.pHStep = json["pHStep"]; + } else { + this->params.pHStep = this->params.pHStep; + } + json["pHStep"] = this->params.pHStep; + + irene3000Config.close(); + irene3000Config = SPIFFS.open("/irene3000Config.json", "w"); + + if (!irene3000Config) { + +#if DEBUG == 1 + + Serial.println(F("failed to write to /irene3000Config.json")); + Serial.println(); + +#endif + + return (false); + } + + json.printTo(irene3000Config); + irene3000Config.close(); + +#if DEBUG == 1 + + Serial.println(F("saved configuration file :")); + json.printTo(Serial); + Serial.println(); + +#endif + + return (true); + } + } } /** -* Irene3000::printConf(): -* This method is provided to print the configuration -* to the Serial Monitor -*/ -void Irene3000::printConf() -{ + * Irene3000::printConf(): + * This method is provided to print the configuration + * to the Serial Monitor + */ +void Irene3000::printConf() { + +#if DEBUG == 1 -#if DEBUG == 1 + Serial.println(F("Entering Irene3000.printConf()")); + Serial.println(); - Serial.println( F("Entering Irene3000.printConf()") ); - Serial.println(); +#endif -#endif + Serial.println("Irene Configuration "); - Serial.println("Irene Configuration "); + Serial.print("waterTemp.active : "); + Serial.println(waterTemp.active); - Serial.print("waterTemp.active : "); - Serial.println(waterTemp.active); + Serial.print("waterTemp.gain : "); + Serial.println(waterTemp.gain, HEX); - Serial.print("waterTemp.gain : "); - Serial.println(waterTemp.gain,HEX); + Serial.print("phProbe.active : "); + Serial.println(phProbe.active); - Serial.print("phProbe.active : "); - Serial.println(phProbe.active); + Serial.print("phProbe.gain : "); + Serial.println(phProbe.gain, HEX); - Serial.print("phProbe.gain : "); - Serial.println(phProbe.gain,HEX); - - Serial.print("adc2.active : "); - Serial.println(adc2.active); + Serial.print("adc2.active : "); + Serial.println(adc2.active); - Serial.print("adc2.gain : "); - Serial.println(adc2.gain,HEX); + Serial.print("adc2.gain : "); + Serial.println(adc2.gain, HEX); - Serial.print("adc2.type : "); - Serial.println(adc2.type); + Serial.print("adc2.type : "); + Serial.println(adc2.type); - Serial.println(); + Serial.println(); } /** -* Irene3000::readButton(): -* This method is provided to read the -* Irene3000 button -* -* \return the button value -*/ -int Irene3000::readButton() -{ + * Irene3000::readButton(): + * This method is provided to read the + * Irene3000 button + * + * \return the button value + */ +int Irene3000::readButton() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering Irene3000.readButton()" ) ); - Serial.println(); + Serial.println(F("Entering Irene3000.readButton()")); + Serial.println(); -#endif +#endif - this->setGain(GAIN_TWOTHIRDS); - int result =this->ads.readADC_SingleEnded(button); + this->setGain(GAIN_TWOTHIRDS); + int result = this->ads.readADC_SingleEnded(button); #if DEBUG == 1 - - Serial.println( F("button value : ") ); - Serial.println( result ); -#endif + Serial.println(F("button value : ")); + Serial.println(result); + +#endif - return( result ); - + return (result); } /** -* Irene3000::setGain(gain): -* This method is provided to set the -* ADS chip gain -*/ -void Irene3000::setGain(adsGain_t gain) -{ + * Irene3000::setGain(gain): + * This method is provided to set the + * ADS chip gain + */ +void Irene3000::setGain(adsGain_t gain) { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering Irene3000.setGain()") ); - Serial.println(); + Serial.println(F("Entering Irene3000.setGain()")); + Serial.println(); #endif - this->ads.setGain(gain); + this->ads.setGain(gain); } /** -* Irene3000::readADSChannel2(gain): -* This method is provided to read from -* the ADS channel 2 . -* ADS Channel 2 is free and the user can connect -* another analog sensor to it. -* -* \return the ADS Channel 2 value -*/ -int Irene3000::readADSChannel2(adsGain_t gain) -{ + * Irene3000::readADSChannel2(gain): + * This method is provided to read from + * the ADS channel 2 . + * ADS Channel 2 is free and the user can connect + * another analog sensor to it. + * + * \return the ADS Channel 2 value + */ +int Irene3000::readADSChannel2(adsGain_t gain) { + +#if DEBUG == 1 -#if DEBUG == 1 - - Serial.println( F("Entering Irene3000.readADSChannel2()") ); - Serial.println(); + Serial.println(F("Entering Irene3000.readADSChannel2()")); + Serial.println(); #endif - this->setGain(gain); - int result = this->ads.readADC_SingleEnded(freeAdc); -#if DEBUG == 1 - - Serial.println( F("adc2 value : ") ); - Serial.println( ); - Serial.println(); + this->setGain(gain); + int result = this->ads.readADC_SingleEnded(freeAdc); +#if DEBUG == 1 + + Serial.println(F("adc2 value : ")); + Serial.println(); + Serial.println(); #endif - return( result ) ; + return (result); } /** -* Irene3000::readPh(double t): -* This method is provided to read the PH probe -* note that for the best results, PH must be -* correlated to Temperature. -* -* \return the PH probe value -*/ -float Irene3000::readPh(double t) -{ + * Irene3000::readPh(double t): + * This method is provided to read the PH probe + * note that for the best results, PH must be + * correlated to Temperature. + * + * \return the PH probe value + */ +float Irene3000::readPh(double t) { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering Irene3000.readPh()") ); - Serial.println(); + Serial.println(F("Entering Irene3000.readPh()")); + Serial.println(); -#endif +#endif - this->setGain(GAIN_FOUR); - int adcR=ads.readADC_SingleEnded(ph); - double Voltage = REFERENCE_VOLTAGE_GAIN_4 * ( adcR ) / ADC_MAXIMUM_VALUE; + this->setGain(GAIN_FOUR); + int adcR = ads.readADC_SingleEnded(ph); + double Voltage = REFERENCE_VOLTAGE_GAIN_4 * (adcR) / ADC_MAXIMUM_VALUE; - float miliVolts = Voltage * 1000; - float temporary = ((((vRef * (float)params.pH7Cal) / 32767) * 1000) - miliVolts) / opampGain; - - float phT=7 - (temporary / params.pHStep); + float miliVolts = Voltage * 1000; + float temporary = + ((((vRef * (float)params.pH7Cal) / 32767) * 1000) - miliVolts) / + opampGain; -#if DEBUG == 1 + float phT = 7 - (temporary / params.pHStep); - Serial.println( F("ph is : ") ); - Serial.println( phT ) ; +#if DEBUG == 1 -#endif - if(isnan(phT) ) - { - return(-42); - } + Serial.println(F("ph is : ")); + Serial.println(phT); - return(phT); +#endif + if (isnan(phT)) { + return (-42); + } + return (phT); } /** -* Irene3000::readTemp(gain): -* This method is provided to read -* the Temeperature probe -* -* \return the Temperature probe value -*/ -double Irene3000::readTemp() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering Irene3000.readTemp()") ); - Serial.println(); - -#endif - - const double A = 3.9083E-3; - const double B = -5.775E-7; - double T; - - this->setGain(GAIN_EIGHT); - - double adc0 = ads.readADC_SingleEnded(temp); - - - double R = ( ( adc0 * V_GAIN_8 ) / 0.095 ) / 1000 ; - - T = 0.0 - A; - T += sqrt((A * A) - 4.0 * B * (1.0 - R)); - T /= (2.0 * B); - - if (T > 0 && T < 200) - { - - #if DEBUG == 1 - - Serial.print( F(" temperature : ") ); - Serial.println(T); - Serial.println(); - - #endif - if(isnan(T)) - { - return(-300); - } - - return T; - } - else - { - T = 0.0 - A; - T -= sqrt((A * A) - 4.0 * B * (1.0 - R)); - T /= (2.0 * B); - - #if DEBUG == 1 - - Serial.println( F("temperature : ") ); - Serial.println(T); - Serial.println(); - - #endif - if(isnan(T)) - { - return(-400); - } - - return T; - } + * Irene3000::readTemp(gain): + * This method is provided to read + * the Temeperature probe + * + * \return the Temperature probe value + */ +double Irene3000::readTemp() { -} +#if DEBUG == 1 + Serial.println(F("Entering Irene3000.readTemp()")); + Serial.println(); -/** -* Irene3000::calibratepH7(): -* This method is provided to calibrate the -* PH probe to 7 -*/ -void Irene3000::calibratepH7() -{ +#endif -#if DEBUG == 1 + const double A = 3.9083E-3; + const double B = -5.775E-7; + double T; - Serial.println( F("Entering Irene3000.calibratepH7() ") ); - Serial.println(); - - + this->setGain(GAIN_EIGHT); -#endif - delay(1000); + double adc0 = ads.readADC_SingleEnded(temp); - this->setGain(GAIN_FOUR); - - this->params.pH7Cal = ads.readADC_SingleEnded(ph); - - this->calcpHSlope(); + double R = ((adc0 * V_GAIN_8) / 0.095) / 1000; + T = 0.0 - A; + T += sqrt((A * A) - 4.0 * B * (1.0 - R)); + T /= (2.0 * B); -} + if (T > 0 && T < 200) { -/** -* Irene3000::calibratepH4(): -* This method is provided to calibrate the -* PH probe to 4 -*/ -void Irene3000::calibratepH4() -{ +#if DEBUG == 1 + + Serial.print(F(" temperature : ")); + Serial.println(T); + Serial.println(); -#if DEBUG == 1 +#endif + if (isnan(T)) { + return (-300); + } + + return T; + } else { + T = 0.0 - A; + T -= sqrt((A * A) - 4.0 * B * (1.0 - R)); + T /= (2.0 * B); + +#if DEBUG == 1 + + Serial.println(F("temperature : ")); + Serial.println(T); + Serial.println(); + +#endif + if (isnan(T)) { + return (-400); + } - Serial.println( F("Entering Irene3000.calibraph4()") ); - Serial.println(); + return T; + } +} -#endif - delay(1000); +/** + * Irene3000::calibratepH7(): + * This method is provided to calibrate the + * PH probe to 7 + */ +void Irene3000::calibratepH7() { - this->setGain(GAIN_FOUR); +#if DEBUG == 1 - this->params.pH4Cal = ads.readADC_SingleEnded(ph); + Serial.println(F("Entering Irene3000.calibratepH7() ")); + Serial.println(); - this->calcpHSlope(); +#endif + delay(1000); + this->setGain(GAIN_FOUR); + this->params.pH7Cal = ads.readADC_SingleEnded(ph); + this->calcpHSlope(); } /** -* Irene3000::calcpHSlop(): -* This method is provided to calculate -* th PH slope -*/ -void Irene3000::calcpHSlope () -{ + * Irene3000::calibratepH4(): + * This method is provided to calibrate the + * PH probe to 4 + */ +void Irene3000::calibratepH4() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering Irene3000.calcpHSlope()") ); - Serial.println(); + Serial.println(F("Entering Irene3000.calibraph4()")); + Serial.println(); -#endif +#endif + delay(1000); + + this->setGain(GAIN_FOUR); - params.pHStep = ((((vRef * (float)(params.pH7Cal - params.pH4Cal)) / 32767) * 1000) / opampGain) / 3; + this->params.pH4Cal = ads.readADC_SingleEnded(ph); - + this->calcpHSlope(); } /** -* Irene3000::resetParams(): -* This method is provided to reset -* the PH configuration, -* assuming Ideal configuration -*/ -void Irene3000::resetParams(void) -{ + * Irene3000::calcpHSlop(): + * This method is provided to calculate + * th PH slope + */ +void Irene3000::calcpHSlope() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering Irene3000.resetParams()") ); - Serial.println(); + Serial.println(F("Entering Irene3000.calcpHSlope()")); + Serial.println(); -#endif +#endif - //Restore to default set of parameters! - params.WriteCheck = Write_Check; - params.pH7Cal = 16384; //assume ideal probe and amp conditions 1/2 of 4096 - params.pH4Cal = 8192; //using ideal probe slope we end up this many 12bit units away on the 4 scale - params.pHStep = 59.16;//ideal probe slope + params.pHStep = + ((((vRef * (float)(params.pH7Cal - params.pH4Cal)) / 32767) * 1000) / + opampGain) / + 3; +} +/** + * Irene3000::resetParams(): + * This method is provided to reset + * the PH configuration, + * assuming Ideal configuration + */ +void Irene3000::resetParams(void) { + +#if DEBUG == 1 + + Serial.println(F("Entering Irene3000.resetParams()")); + Serial.println(); + +#endif + // Restore to default set of parameters! + params.WriteCheck = Write_Check; + params.pH7Cal = 16384; // assume ideal probe and amp conditions 1/2 of 4096 + params.pH4Cal = 8192; // using ideal probe slope we end up this many 12bit + // units away on the 4 scale + params.pHStep = 59.16; // ideal probe slope } /** -* Irene3000::gainConvert( gain : { 0.67 ,1,2,4,8,16 } ) -* This method is provided to convert the gain to -* Internal Constants -* -* \return internal representation of the ADS gain -*/ -adsGain_t Irene3000::gainConvert(uint16_t tempGain) -{ - -#if DEBUG == 1 - - Serial.println( F("Entering Irene3000.gainConvert()") ); - Serial.println(); - -#endif - - switch(tempGain) - { - case(1): return (GAIN_ONE); - case(2) : return(GAIN_TWO); - case(4): return(GAIN_FOUR) ; - case(8):return(GAIN_EIGHT) ; - case(16):return(GAIN_SIXTEEN); - default: return(GAIN_TWOTHIRDS); - } - - return(GAIN_ONE); + * Irene3000::gainConvert( gain : { 0.67 ,1,2,4,8,16 } ) + * This method is provided to convert the gain to + * Internal Constants + * + * \return internal representation of the ADS gain + */ +adsGain_t Irene3000::gainConvert(uint16_t tempGain) { -} +#if DEBUG == 1 + Serial.println(F("Entering Irene3000.gainConvert()")); + Serial.println(); -bool Irene3000::saveParams() -{ - -#if DEBUG == 1 - - Serial.println(F("Entering Irene3000.saveParams()")); - Serial.println(); - - Serial.println(F("saving new params")); -#endif - - //open file - File irene3000Config = SPIFFS.open("/irene3000Config.json", "r"); - - if (!irene3000Config) - { - - #if DEBUG == 1 - - Serial.println( F("failed to read /irene3000Config.json") ); - Serial.println(); - - #endif - - return(false); - } - else - { - //read file into json - size_t size = irene3000Config.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - irene3000Config.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json ") ); - Serial.println(); - - #endif - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("read configuration file ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - - #endif - - //add params to json - json["pH7Cal"]=this->params.pH7Cal; - json["pH4Cal"]=this->params.pH4Cal; - json["pHStep"]=this->params.pHStep; - - irene3000Config.close(); - - - //save json to file - irene3000Config = SPIFFS.open("/irene3000Config.json", "w"); - - if(!irene3000Config) - { - - #if DEBUG == 1 - - Serial.println( F("failed to write to /irene3000Config.json") ); - Serial.println(); - - #endif - - return(false); - } - - json.printTo(irene3000Config); - irene3000Config.close(); - - #if DEBUG == 1 - - Serial.println( F("saved configuration file :") ); - json.printTo(Serial); - Serial.println(); - - #endif - - return(true); - } - } +#endif + switch (tempGain) { + case (1): + return (GAIN_ONE); + case (2): + return (GAIN_TWO); + case (4): + return (GAIN_FOUR); + case (8): + return (GAIN_EIGHT); + case (16): + return (GAIN_SIXTEEN); + default: + return (GAIN_TWOTHIRDS); + } + + return (GAIN_ONE); } +bool Irene3000::saveParams() { + +#if DEBUG == 1 + + Serial.println(F("Entering Irene3000.saveParams()")); + Serial.println(); + + Serial.println(F("saving new params")); +#endif + + // open file + File irene3000Config = SPIFFS.open("/irene3000Config.json", "r"); + + if (!irene3000Config) { + +#if DEBUG == 1 + + Serial.println(F("failed to read /irene3000Config.json")); + Serial.println(); + +#endif + + return (false); + } else { + // read file into json + size_t size = irene3000Config.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + irene3000Config.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + +#if DEBUG == 1 + + Serial.println(F("failed to parse json ")); + Serial.println(); + +#endif + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("read configuration file ")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + // add params to json + json["pH7Cal"] = this->params.pH7Cal; + json["pH4Cal"] = this->params.pH4Cal; + json["pHStep"] = this->params.pHStep; + + irene3000Config.close(); + + // save json to file + irene3000Config = SPIFFS.open("/irene3000Config.json", "w"); + + if (!irene3000Config) { + +#if DEBUG == 1 + + Serial.println(F("failed to write to /irene3000Config.json")); + Serial.println(); + +#endif + + return (false); + } + + json.printTo(irene3000Config); + irene3000Config.close(); + +#if DEBUG == 1 + + Serial.println(F("saved configuration file :")); + json.printTo(Serial); + Serial.println(); + +#endif + + return (true); + } + } +} diff --git a/src/Irene3000.h b/src/Irene3000.h index d8db478d..a181f122 100644 --- a/src/Irene3000.h +++ b/src/Irene3000.h @@ -1,40 +1,38 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef IRENE3000_H #define IRENE3000_H -#include "internals/CoolAdafruit_ADS1015.h" +#include "internals/CoolAdafruit_ADS1015.h" #include - -#define ADC_MAXIMUM_VALUE 32767 -#define REFERENCE_VOLTAGE_GAIN_4 1.024 +#define ADC_MAXIMUM_VALUE 32767 +#define REFERENCE_VOLTAGE_GAIN_4 1.024 #define V_GAIN_2 0.0625 #define V_GAIN_4 0.03125 #define V_GAIN_8 0.015625 -#define Write_Check 0x1234 +#define Write_Check 0x1234 #define button 0 #define temp 1 @@ -42,108 +40,101 @@ #define ph 3 /** -* \class Irene3000 -* \brief This class is provided to manage -* the Irene3000 Ph/Temperature Shield -* -*/ -class Irene3000 -{ + * \class Irene3000 + * \brief This class is provided to manage + * the Irene3000 Ph/Temperature Shield + * + */ +class Irene3000 { public: + void startADC(); - void startADC(); - - void begin(); + void begin(); - bool config(); + bool config(); - bool ireneButton(); + bool ireneButton(); - void printConf(); + void printConf(); - String read(); + String read(); - int readButton(); + int readButton(); - void setGain(adsGain_t gain); + void setGain(adsGain_t gain); - int readADSChannel2(adsGain_t gain); + int readADSChannel2(adsGain_t gain); - float readPh( double t ); + float readPh(double t); - double readTemp(); + double readTemp(); - void resetParams(); + void resetParams(); - void calibratepH7(); + void calibratepH7(); - void calibratepH4(); + void calibratepH4(); - void calcpHSlope (); + void calcpHSlope(); - adsGain_t gainConvert(uint16_t tempGain); - - bool saveParams(); + adsGain_t gainConvert(uint16_t tempGain); + bool saveParams(); private: - - /** - * ADS1115 instance - */ - Adafruit_ADS1115 ads; - - /** - * Led handler instance - */ - CoolBoardLed coolBoardLed; - - /** - * PH Sensor Calibration parameters - * - * pH7Cal : PH 7 Calibration value - * - * pH4Cal : PH 4 Calibration value - * - * pHStep : PH Slope Step value - * - */ - struct parameters_T - { - unsigned int WriteCheck=0; - int pH7Cal, pH4Cal=0; - float pHStep=1; - }params; - - /** - * Irene sensors state struct - * - * active : if the related sensor is active or not - * - * gain : what is the used gain for the related sensor (used only for the free channel) - * - * type : the type of the used sensor(used only for the free channel ) - * - */ - struct state - { - bool active=0; - adsGain_t gain=GAIN_TWOTHIRDS; - String type=""; - - } waterTemp, phProbe,adc2; - - /** - * vRef constant used for ADC conversion - */ - const float vRef = 1.024; - - /** - * opAmpGain constant used for ADC conversion - */ - const float opampGain = 5.25; - + /** + * ADS1115 instance + */ + Adafruit_ADS1115 ads; + + /** + * Led handler instance + */ + CoolBoardLed coolBoardLed; + + /** + * PH Sensor Calibration parameters + * + * pH7Cal : PH 7 Calibration value + * + * pH4Cal : PH 4 Calibration value + * + * pHStep : PH Slope Step value + * + */ + struct parameters_T { + unsigned int WriteCheck = 0; + int pH7Cal, pH4Cal = 0; + float pHStep = 1; + } params; + + /** + * Irene sensors state struct + * + * active : if the related sensor is active or not + * + * gain : what is the used gain for the related sensor (used only for the + *free channel) + * + * type : the type of the used sensor(used only for the free channel ) + * + */ + struct state { + bool active = 0; + adsGain_t gain = GAIN_TWOTHIRDS; + String type = ""; + + } waterTemp, phProbe, adc2; + + /** + * vRef constant used for ADC conversion + */ + const float vRef = 1.024; + + /** + * opAmpGain constant used for ADC conversion + */ + const float opampGain = 5.25; }; - #endif diff --git a/src/Jetpack.cpp b/src/Jetpack.cpp index d0c2c26f..f49f9069 100644 --- a/src/Jetpack.cpp +++ b/src/Jetpack.cpp @@ -1,1782 +1,1578 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ - -#include "FS.h" #include "Arduino.h" #include "ArduinoJson.h" +#include "FS.h" #include "Jetpack.h" - #define DEBUG 0 +/** + * Jetpack::begin(): + * This method is provided to + * initialise the pin that control + * the Jetpack shield + */ +void Jetpack::begin() { + +#if DEBUG == 1 + + Serial.println(F("Entering Jetpack.begin() ")); + Serial.println(); + +#endif + + pinMode(EnI2C, OUTPUT); + pinMode(dataPin, OUTPUT); + pinMode(clockPin, OUTPUT); +} /** -* Jetpack::begin(): -* This method is provided to -* initialise the pin that control -* the Jetpack shield -*/ -void Jetpack::begin() -{ - -#if DEBUG == 1 - - Serial.println( F("Entering Jetpack.begin() ") ); - Serial.println(); + * Jetpack::write(action): + * This method is provided to write + * the given action to the entire Jetpack + * action is a Byte (8 bits ), each bit goes + * to an output. + * MSBFirst + */ +void Jetpack::write(byte action) { + +#if DEBUG == 1 + + Serial.println(F("Entering Jetpack.write()")); + Serial.println(); + + Serial.println(F("writing this action : ")); + Serial.println(action, BIN); + Serial.println(); #endif - pinMode(EnI2C,OUTPUT); - pinMode(dataPin,OUTPUT); - pinMode(clockPin,OUTPUT); - - + this->action = action; -} + digitalWrite(EnI2C, LOW); + shiftOut(dataPin, clockPin, MSBFIRST, this->action); + + digitalWrite(EnI2C, HIGH); +} /** -* Jetpack::write(action): -* This method is provided to write -* the given action to the entire Jetpack -* action is a Byte (8 bits ), each bit goes -* to an output. -* MSBFirst -*/ -void Jetpack::write(byte action) -{ + * Jetpack::writeBit(pin,state): + * This method is provided to write + * the given state to the given pin + */ +void Jetpack::writeBit(byte pin, bool state) { #if DEBUG == 1 - Serial.println( F("Entering Jetpack.write()") ); - Serial.println(); + Serial.println(F("Entering Jetpack.writeBit() ")); - Serial.println( F("writing this action : ") ); - Serial.println(action,BIN); - Serial.println(); + Serial.print(F("Writing ")); + Serial.print(state); -#endif + Serial.print(F("to pin N°")); + Serial.print(pin); - this->action=action; + Serial.println(); - - digitalWrite(EnI2C, LOW); - - shiftOut(dataPin, clockPin, MSBFIRST, this->action); +#endif - digitalWrite(EnI2C, HIGH); + bitWrite(this->action, pin, state); + digitalWrite(EnI2C, LOW); + shiftOut(dataPin, clockPin, MSBFIRST, this->action); + digitalWrite(EnI2C, HIGH); } - + /** -* Jetpack::writeBit(pin,state): -* This method is provided to write -* the given state to the given pin -*/ -void Jetpack::writeBit(byte pin,bool state) -{ + * Jetpack::doAction(sensor data ): + * This method is provided to automate the Jetpack. + * + * The result action is the result of + * checking the different flags of an actor + * (actif , temporal ,inverted, primaryType + * and secondaryType ) and the corresponding + * call to the appropriate helping method + * + * \return a string of the current Jetpack state + * + */ +String Jetpack::doAction(const char *data, int hour, int minute) { -#if DEBUG == 1 +#if DEBUG == 1 + + Serial.println(F("Entering Jetpack.doAction()")); + Serial.println(); + + Serial.println(F("input data is :")); + Serial.println(data); + Serial.println(); - Serial.println( F("Entering Jetpack.writeBit() ") ); + Serial.print(F("Hour from Coolboard.cpp : ")); + Serial.println(hour); + Serial.print(F("Minute from Coolboard.cpp : ")); + Serial.println(minute); - Serial.print( F("Writing ") ); - Serial.print(state); +#endif + + // input json buffer and object + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.parseObject(data); - Serial.print( F("to pin N°") ); - Serial.print(pin); + // output json buffer and object + String jetpackState; + DynamicJsonBuffer jsonBufferOutput; + JsonObject &rootOutput = jsonBufferOutput.createObject(); - Serial.println(); + if (!root.success()) { + +#if DEBUG == 1 + + Serial.println(F("failed to parse json object ")); + Serial.println(); #endif - bitWrite(this->action, pin, state); - digitalWrite(EnI2C, LOW); - - shiftOut(dataPin, clockPin, MSBFIRST, this->action); + } else if (!rootOutput.success()) { - digitalWrite(EnI2C, HIGH); +#if DEBUG == 1 -} + Serial.println(F("failed to create output json object")); + Serial.println(); +#endif -/** -* Jetpack::doAction(sensor data ): -* This method is provided to automate the Jetpack. -* -* The result action is the result of -* checking the different flags of an actor -* (actif , temporal ,inverted, primaryType -* and secondaryType ) and the corresponding -* call to the appropriate helping method -* -* \return a string of the current Jetpack state -* -*/ -String Jetpack::doAction( const char* data, int hour, int minute ) -{ - -#if DEBUG == 1 - - Serial.println( F("Entering Jetpack.doAction()") ); - Serial.println(); - - Serial.println( F("input data is :") ); - Serial.println(data); - Serial.println(); - - Serial.print( F("Hour from Coolboard.cpp : ")); - Serial.println(hour); - Serial.print( F("Minute from Coolboard.cpp : ")); - Serial.println(minute); - -#endif - - //input json buffer and object - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.parseObject(data); - - //output json buffer and object - String jetpackState; - DynamicJsonBuffer jsonBufferOutput ; - JsonObject& rootOutput = jsonBufferOutput.createObject(); - - if (!root.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse json object ") ); - Serial.println(); - - #endif - - } - else if(!rootOutput.success()) - { - - #if DEBUG == 1 - - Serial.println(F("failed to create output json object")); - Serial.println(); - #endif - - } - else - { - - #if DEBUG == 1 - - Serial.println( F("created Json object :") ); - root.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - - //invert the current action state for each actor - //if the value is outside the limits - for(int i=0;i<8;i++) - { - //check if actor is actif - if(this->actors[i].actif==1) - { - //normal actor - if(this->actors[i].temporal == 0) - { - //not inverted actor - if(this->actors[i].inverted==0) - { - this->normalAction(i,root[this->actors[i].primaryType].as()); - - } - //inverted actor - else if(this->actors[i].inverted==1) - { - this->invertedAction(i,root[this->actors[i].primaryType].as()); - } - } - //temporal actor - else if(this->actors[i].temporal == 1 ) - { - //hour actor - if(this->actors[i].secondaryType=="hour") - { - //mixed hour actor - if(root[this->actors[i].primaryType].success() ) - { - this->mixedHourAction(i, hour, root[this->actors[i].primaryType].as()); - } - //normal hour actor - else - { - this->hourAction(i, hour); - } - - } - //minute actor - else if(this->actors[i].secondaryType=="minute") - { - //mixed minute actor - if(root[this->actors[i].primaryType].success() ) - { - this->mixedMinuteAction(i,root[this->actors[i].secondaryType].as(),root[this->actors[i].primaryType].as()); - } - //normal minute actor - else - { - this->minuteAction(i,root[this->actors[i].secondaryType].as()); - } - } - //hourMinute actor - else if(this->actors[i].secondaryType=="hourMinute") - { - //mixed hourMinute actor - if(root[this->actors[i].primaryType].success() ) - { - this->mixedHourMinuteAction(i, hour, minute,root[this->actors[i].primaryType].as()); - } - //normal hourMinute actor - else - { - this->hourMinuteAction(i, hour, minute); - } - } - //normal temporal actor - else if(this->actors[i].secondaryType=="") - { - //mixed temporal actor - if(root[this->actors[i].primaryType].success() ) - { - this->mixedTemporalActionOn(i,root[this->actors[i].primaryType].as()); - } - //normal temporal actor - else - { - this->temporalActionOff(i); - } - - } - - } - } - //inactif actor - else if(this->actors[i].actif == 0 ) - { - //temporal actor - if(this->actors[i].temporal==1) - { - //mixed temporal actor - if(root[this->actors[i].primaryType].success() ) - { - this->mixedTemporalActionOff(i,root[this->actors[i].primaryType].as()); - } - //normal temporal actor - else - { - this->temporalActionOn(i); - } - } - } - /*if(this->action == 1) - { - rootOutput[String("Act")+String(i)]=true; - } - else rootOutput[String("Act")+String(i)]=false;*/ - - rootOutput[String("Act") + String(i)] = (bitRead(this->action, i) == 1); - } - - this->write(this->action); - - } - - rootOutput.printTo(jetpackState); - - return(jetpackState); + } else { + +#if DEBUG == 1 + + Serial.println(F("created Json object :")); + root.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + // invert the current action state for each actor + // if the value is outside the limits + for (int i = 0; i < 8; i++) { + // check if actor is actif + if (this->actors[i].actif == 1) { + // normal actor + if (this->actors[i].temporal == 0) { + // not inverted actor + if (this->actors[i].inverted == 0) { + this->normalAction(i, + root[this->actors[i].primaryType].as()); + + } + // inverted actor + else if (this->actors[i].inverted == 1) { + this->invertedAction(i, + root[this->actors[i].primaryType].as()); + } + } + // temporal actor + else if (this->actors[i].temporal == 1) { + // hour actor + if (this->actors[i].secondaryType == "hour") { + // mixed hour actor + if (root[this->actors[i].primaryType].success()) { + this->mixedHourAction( + i, hour, root[this->actors[i].primaryType].as()); + } + // normal hour actor + else { + this->hourAction(i, hour); + } + + } + // minute actor + else if (this->actors[i].secondaryType == "minute") { + // mixed minute actor + if (root[this->actors[i].primaryType].success()) { + this->mixedMinuteAction( + i, root[this->actors[i].secondaryType].as(), + root[this->actors[i].primaryType].as()); + } + // normal minute actor + else { + this->minuteAction(i, + root[this->actors[i].secondaryType].as()); + } + } + // hourMinute actor + else if (this->actors[i].secondaryType == "hourMinute") { + // mixed hourMinute actor + if (root[this->actors[i].primaryType].success()) { + this->mixedHourMinuteAction( + i, hour, minute, + root[this->actors[i].primaryType].as()); + } + // normal hourMinute actor + else { + this->hourMinuteAction(i, hour, minute); + } + } + // normal temporal actor + else if (this->actors[i].secondaryType == "") { + // mixed temporal actor + if (root[this->actors[i].primaryType].success()) { + this->mixedTemporalActionOn( + i, root[this->actors[i].primaryType].as()); + } + // normal temporal actor + else { + this->temporalActionOff(i); + } + } + } + } + // inactif actor + else if (this->actors[i].actif == 0) { + // temporal actor + if (this->actors[i].temporal == 1) { + // mixed temporal actor + if (root[this->actors[i].primaryType].success()) { + this->mixedTemporalActionOff( + i, root[this->actors[i].primaryType].as()); + } + // normal temporal actor + else { + this->temporalActionOn(i); + } + } + } + /*if(this->action == 1) + { + rootOutput[String("Act")+String(i)]=true; + } + else rootOutput[String("Act")+String(i)]=false;*/ + + rootOutput[String("Act") + String(i)] = (bitRead(this->action, i) == 1); + } + + this->write(this->action); + } + + rootOutput.printTo(jetpackState); + + return (jetpackState); } /** -* Jetpack::config(): -* This method is provided to configure the -* Jetpack with a configuration file -* -* \return true if successful,false otherwise -*/ -bool Jetpack::config() -{ + * Jetpack::config(): + * This method is provided to configure the + * Jetpack with a configuration file + * + * \return true if successful,false otherwise + */ +bool Jetpack::config() { -#if DEBUG == 1 +#if DEBUG == 1 - Serial.println( F("Entering Jetpack.config() ") ); - Serial.println(); + Serial.println(F("Entering Jetpack.config() ")); + Serial.println(); #endif - File jetPackConfig = SPIFFS.open("/jetPackConfig.json", "r"); - - if (!jetPackConfig) - { - - #if DEBUG == 1 - - Serial.println( F("failed to read /jetPackConfig.json ") ); - Serial.println(); - - #endif - - return(false); - } - else - { - size_t size = jetPackConfig.size(); - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - jetPackConfig.readBytes(buf.get(), size); - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - if (!json.success()) - { - - #if DEBUG == 1 - - Serial.println( F("failed to parse jetpack config json from file ") ); - Serial.println(); - - #endif - - return(false); - } - else - { - - #if DEBUG == 1 - - Serial.println( F("read configuration file : ") ); - json.printTo(Serial); - Serial.println(); - - Serial.print(F("jsonBuffer size: ")); - Serial.println(jsonBuffer.size()); - Serial.println(); - - - #endif - - for(int i=0;i<8;i++) - { - if(json[String("Act")+String(i)].success()) - { - //parsing actif key - if(json[String("Act")+String(i)]["actif"].success() ) - { - this->actors[i].actif=json[String("Act")+String(i)]["actif"]; - } - else - { - this->actors[i].actif=this->actors[i].actif; - } - json[String("Act")+String(i)]["actif"]=this->actors[i].actif; - - //parsing temporal key - if(json[String("Act")+String(i)]["temporal"].success() ) - { - this->actors[i].temporal=json[String("Act")+String(i)]["temporal"]; - } - else - { - this->actors[i].temporal=this->actors[i].temporal; - } - json[String("Act")+String(i)]["temporal"]=this->actors[i].temporal; - - //parsing inverted key - if(json[String("Act")+String(i)]["inverted"].success() ) - { - this->actors[i].inverted=json[String("Act")+String(i)]["inverted"]; - } - else - { - this->actors[i].inverted=this->actors[i].inverted; - } - json[String("Act")+String(i)]["inverted"]=this->actors[i].inverted; - - //parsing inverted key - if(json[String("Act")+String(i)]["inverted"].success() ) - { - this->actors[i].inverted=json[String("Act")+String(i)]["inverted"]; - } - else - { - this->actors[i].inverted=this->actors[i].inverted; - } - json[String("Act")+String(i)]["inverted"]=this->actors[i].inverted; - - //parsing low key - if(json[String("Act")+String(i)]["low"].success() ) - { - this->actors[i].rangeLow=json[String("Act")+String(i)]["low"][0]; - this->actors[i].timeLow=json[String("Act")+String(i)]["low"][1]; - this->actors[i].hourLow=json[String("Act")+String(i)]["low"][2]; - this->actors[i].minuteLow=json[String("Act")+String(i)]["low"][3]; - } - else - { - this->actors[i].rangeLow=this->actors[i].rangeLow; - this->actors[i].timeLow=this->actors[i].timeLow; - this->actors[i].hourLow=this->actors[i].hourLow; - this->actors[i].minuteLow=this->actors[i].minuteLow; - } - json[String("Act")+String(i)]["low"][0]=this->actors[i].rangeLow; - json[String("Act")+String(i)]["low"][1]=this->actors[i].timeLow; - json[String("Act")+String(i)]["low"][2]=this->actors[i].hourLow; - json[String("Act")+String(i)]["low"][3]=this->actors[i].minuteLow; - - //parsing high key - if(json[String("Act")+String(i)]["high"].success() ) - { - this->actors[i].rangeHigh=json[String("Act")+String(i)]["high"][0]; - this->actors[i].timeHigh=json[String("Act")+String(i)]["high"][1]; - this->actors[i].hourHigh=json[String("Act")+String(i)]["high"][2]; - this->actors[i].minuteHigh=json[String("Act")+String(i)]["high"][3]; - } - else - { - this->actors[i].rangeHigh=this->actors[i].rangeHigh; - this->actors[i].timeHigh=this->actors[i].timeHigh; - this->actors[i].hourHigh=this->actors[i].hourHigh; - this->actors[i].minuteHigh=this->actors[i].minuteHigh; - } - json[String("Act")+String(i)]["high"][0]=this->actors[i].rangeHigh; - json[String("Act")+String(i)]["high"][1]=this->actors[i].timeHigh; - json[String("Act")+String(i)]["high"][2]=this->actors[i].hourHigh; - json[String("Act")+String(i)]["high"][3]=this->actors[i].minuteHigh; - - //parsing type key - if(json[String("Act")+String(i)]["type"].success() ) - { - this->actors[i].primaryType=json[String("Act")+String(i)]["type"][0].as(); - this->actors[i].secondaryType=json[String("Act")+String(i)]["type"][1].as(); - - } - else - { - this->actors[i].primaryType=this->actors[i].primaryType; - this->actors[i].secondaryType=this->actors[i].secondaryType; - } - json[String("Act")+String(i)]["type"][0]=this->actors[i].primaryType; - json[String("Act")+String(i)]["type"][1]=this->actors[i].secondaryType; - - - - - - - } - else - { - this->actors[i]=this->actors[i]; - } - - json[String("Act")+String(i)]["actif"]=this->actors[i].actif; - json[String("Act")+String(i)]["temporal"]=this->actors[i].temporal; - json[String("Act")+String(i)]["inverted"]=this->actors[i].inverted; - - json[String("Act")+String(i)]["low"][0]=this->actors[i].rangeLow; - json[String("Act")+String(i)]["low"][1]=this->actors[i].timeLow; - json[String("Act")+String(i)]["low"][2]=this->actors[i].hourLow; - json[String("Act")+String(i)]["low"][3]=this->actors[i].minuteLow; - - json[String("Act")+String(i)]["high"][0]=this->actors[i].rangeHigh; - json[String("Act")+String(i)]["high"][1]=this->actors[i].timeHigh; - json[String("Act")+String(i)]["high"][2]=this->actors[i].hourHigh; - json[String("Act")+String(i)]["high"][3]=this->actors[i].minuteHigh; - - json[String("Act")+String(i)]["type"][0]=this->actors[i].primaryType; - json[String("Act")+String(i)]["type"][1]=this->actors[i].secondaryType; - - - - } - - - jetPackConfig.close(); - jetPackConfig = SPIFFS.open("/jetPackConfig.json", "w"); - if(!jetPackConfig) - { - - #if DEBUG == 1 - - Serial.println(F("failed to write to /jetPackConfig.json ") ); - Serial.println(); - - #endif - - return(false); - } - - json.printTo(jetPackConfig); - jetPackConfig.close(); - - #if DEBUG == 1 - - Serial.println(F("saved configuration : ")); - json.printTo(Serial ); - Serial.println(); - - #endif - - return(true); - } - } - + File jetPackConfig = SPIFFS.open("/jetPackConfig.json", "r"); + + if (!jetPackConfig) { + +#if DEBUG == 1 + + Serial.println(F("failed to read /jetPackConfig.json ")); + Serial.println(); + +#endif + return (false); + } else { + size_t size = jetPackConfig.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + jetPackConfig.readBytes(buf.get(), size); + DynamicJsonBuffer jsonBuffer; + JsonObject &json = jsonBuffer.parseObject(buf.get()); + if (!json.success()) { + +#if DEBUG == 1 + + Serial.println(F("failed to parse jetpack config json from file ")); + Serial.println(); + +#endif + + return (false); + } else { + +#if DEBUG == 1 + + Serial.println(F("read configuration file : ")); + json.printTo(Serial); + Serial.println(); + + Serial.print(F("jsonBuffer size: ")); + Serial.println(jsonBuffer.size()); + Serial.println(); + +#endif + + for (int i = 0; i < 8; i++) { + if (json[String("Act") + String(i)].success()) { + // parsing actif key + if (json[String("Act") + String(i)]["actif"].success()) { + this->actors[i].actif = json[String("Act") + String(i)]["actif"]; + } else { + this->actors[i].actif = this->actors[i].actif; + } + json[String("Act") + String(i)]["actif"] = this->actors[i].actif; + + // parsing temporal key + if (json[String("Act") + String(i)]["temporal"].success()) { + this->actors[i].temporal = + json[String("Act") + String(i)]["temporal"]; + } else { + this->actors[i].temporal = this->actors[i].temporal; + } + json[String("Act") + String(i)]["temporal"] = + this->actors[i].temporal; + + // parsing inverted key + if (json[String("Act") + String(i)]["inverted"].success()) { + this->actors[i].inverted = + json[String("Act") + String(i)]["inverted"]; + } else { + this->actors[i].inverted = this->actors[i].inverted; + } + json[String("Act") + String(i)]["inverted"] = + this->actors[i].inverted; + + // parsing inverted key + if (json[String("Act") + String(i)]["inverted"].success()) { + this->actors[i].inverted = + json[String("Act") + String(i)]["inverted"]; + } else { + this->actors[i].inverted = this->actors[i].inverted; + } + json[String("Act") + String(i)]["inverted"] = + this->actors[i].inverted; + + // parsing low key + if (json[String("Act") + String(i)]["low"].success()) { + this->actors[i].rangeLow = + json[String("Act") + String(i)]["low"][0]; + this->actors[i].timeLow = json[String("Act") + String(i)]["low"][1]; + this->actors[i].hourLow = json[String("Act") + String(i)]["low"][2]; + this->actors[i].minuteLow = + json[String("Act") + String(i)]["low"][3]; + } else { + this->actors[i].rangeLow = this->actors[i].rangeLow; + this->actors[i].timeLow = this->actors[i].timeLow; + this->actors[i].hourLow = this->actors[i].hourLow; + this->actors[i].minuteLow = this->actors[i].minuteLow; + } + json[String("Act") + String(i)]["low"][0] = this->actors[i].rangeLow; + json[String("Act") + String(i)]["low"][1] = this->actors[i].timeLow; + json[String("Act") + String(i)]["low"][2] = this->actors[i].hourLow; + json[String("Act") + String(i)]["low"][3] = this->actors[i].minuteLow; + + // parsing high key + if (json[String("Act") + String(i)]["high"].success()) { + this->actors[i].rangeHigh = + json[String("Act") + String(i)]["high"][0]; + this->actors[i].timeHigh = + json[String("Act") + String(i)]["high"][1]; + this->actors[i].hourHigh = + json[String("Act") + String(i)]["high"][2]; + this->actors[i].minuteHigh = + json[String("Act") + String(i)]["high"][3]; + } else { + this->actors[i].rangeHigh = this->actors[i].rangeHigh; + this->actors[i].timeHigh = this->actors[i].timeHigh; + this->actors[i].hourHigh = this->actors[i].hourHigh; + this->actors[i].minuteHigh = this->actors[i].minuteHigh; + } + json[String("Act") + String(i)]["high"][0] = + this->actors[i].rangeHigh; + json[String("Act") + String(i)]["high"][1] = this->actors[i].timeHigh; + json[String("Act") + String(i)]["high"][2] = this->actors[i].hourHigh; + json[String("Act") + String(i)]["high"][3] = + this->actors[i].minuteHigh; + + // parsing type key + if (json[String("Act") + String(i)]["type"].success()) { + this->actors[i].primaryType = + json[String("Act") + String(i)]["type"][0].as(); + this->actors[i].secondaryType = + json[String("Act") + String(i)]["type"][1].as(); + + } else { + this->actors[i].primaryType = this->actors[i].primaryType; + this->actors[i].secondaryType = this->actors[i].secondaryType; + } + json[String("Act") + String(i)]["type"][0] = + this->actors[i].primaryType; + json[String("Act") + String(i)]["type"][1] = + this->actors[i].secondaryType; + + } else { + this->actors[i] = this->actors[i]; + } + + json[String("Act") + String(i)]["actif"] = this->actors[i].actif; + json[String("Act") + String(i)]["temporal"] = this->actors[i].temporal; + json[String("Act") + String(i)]["inverted"] = this->actors[i].inverted; + + json[String("Act") + String(i)]["low"][0] = this->actors[i].rangeLow; + json[String("Act") + String(i)]["low"][1] = this->actors[i].timeLow; + json[String("Act") + String(i)]["low"][2] = this->actors[i].hourLow; + json[String("Act") + String(i)]["low"][3] = this->actors[i].minuteLow; + + json[String("Act") + String(i)]["high"][0] = this->actors[i].rangeHigh; + json[String("Act") + String(i)]["high"][1] = this->actors[i].timeHigh; + json[String("Act") + String(i)]["high"][2] = this->actors[i].hourHigh; + json[String("Act") + String(i)]["high"][3] = this->actors[i].minuteHigh; + + json[String("Act") + String(i)]["type"][0] = + this->actors[i].primaryType; + json[String("Act") + String(i)]["type"][1] = + this->actors[i].secondaryType; + } + + jetPackConfig.close(); + jetPackConfig = SPIFFS.open("/jetPackConfig.json", "w"); + if (!jetPackConfig) { + +#if DEBUG == 1 + + Serial.println(F("failed to write to /jetPackConfig.json ")); + Serial.println(); + +#endif + + return (false); + } + + json.printTo(jetPackConfig); + jetPackConfig.close(); + +#if DEBUG == 1 + + Serial.println(F("saved configuration : ")); + json.printTo(Serial); + Serial.println(); + +#endif + + return (true); + } + } } /** -* Jetpack::printConf(): -* This method is provided to -* print the configuration to the -* Serial Monitor -*/ -void Jetpack::printConf() -{ - -#if DEBUG == 1 - - Serial.println( F("Enter Jetpack.printConf() ") ); - Serial.println(); - -#endif - Serial.println(F( "Jetpack configuration " ) ) ; - - for(int i=0;i<8;i++) - { - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" actif :")); - Serial.println(this->actors[i].actif); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" temporal :")); - Serial.println(this->actors[i].temporal); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" inverted :")); - Serial.println(this->actors[i].inverted); - - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" primary Type :")); - Serial.println(this->actors[i].primaryType); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" secondary Type :")); - Serial.println(this->actors[i].secondaryType); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" range Low :")); - Serial.println(this->actors[i].rangeLow); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" time Low :")); - Serial.println(this->actors[i].timeLow); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" hour low:")); - Serial.println(this->actors[i].hourLow); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" minute low:")); - Serial.println(this->actors[i].minuteLow); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" range High:")); - Serial.println(this->actors[i].rangeHigh); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" time High:")); - Serial.println(this->actors[i].timeHigh); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" hour high:")); - Serial.println(this->actors[i].hourHigh); - - Serial.print(F("actor N°")); - Serial.print(i); - Serial.print(F(" minute high:")); - Serial.println(this->actors[i].minuteHigh); - - Serial.println(); - - } - - Serial.println(); + * Jetpack::printConf(): + * This method is provided to + * print the configuration to the + * Serial Monitor + */ +void Jetpack::printConf() { + +#if DEBUG == 1 + + Serial.println(F("Enter Jetpack.printConf() ")); + Serial.println(); + +#endif + Serial.println(F("Jetpack configuration ")); + + for (int i = 0; i < 8; i++) { + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" actif :")); + Serial.println(this->actors[i].actif); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" temporal :")); + Serial.println(this->actors[i].temporal); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" inverted :")); + Serial.println(this->actors[i].inverted); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" primary Type :")); + Serial.println(this->actors[i].primaryType); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" secondary Type :")); + Serial.println(this->actors[i].secondaryType); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" range Low :")); + Serial.println(this->actors[i].rangeLow); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" time Low :")); + Serial.println(this->actors[i].timeLow); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" hour low:")); + Serial.println(this->actors[i].hourLow); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" minute low:")); + Serial.println(this->actors[i].minuteLow); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" range High:")); + Serial.println(this->actors[i].rangeHigh); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" time High:")); + Serial.println(this->actors[i].timeHigh); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" hour high:")); + Serial.println(this->actors[i].hourHigh); + + Serial.print(F("actor N°")); + Serial.print(i); + Serial.print(F(" minute high:")); + Serial.println(this->actors[i].minuteHigh); + + Serial.println(); + } + + Serial.println(); } /** -* Jetpack::normalAction(actorNumber , measured value): -* This method is provided to -* handle normal actors. -* it changes the action according to wether the -* measured value is: > rangeHigh ( deactivate actor) -* or < rangeLow (activate actor ) -*/ -void Jetpack::normalAction(int actorNumber,float measurment) -{ + * Jetpack::normalAction(actorNumber , measured value): + * This method is provided to + * handle normal actors. + * it changes the action according to wether the + * measured value is: > rangeHigh ( deactivate actor) + * or < rangeLow (activate actor ) + */ +void Jetpack::normalAction(int actorNumber, float measurment) { #if DEBUG == 1 - - Serial.print(F("none inverted Actor N° : ")); - Serial.println(actorNumber); - Serial.print(F("measured value : ")); - Serial.println(measurment); + Serial.print(F("none inverted Actor N° : ")); + Serial.println(actorNumber); - Serial.print(F("high range : ")); - Serial.println(this->actors[actorNumber].rangeHigh); + Serial.print(F("measured value : ")); + Serial.println(measurment); - Serial.print(F("low range : ")); - Serial.println(this->actors[actorNumber].rangeLow); + Serial.print(F("high range : ")); + Serial.println(this->actors[actorNumber].rangeHigh); + + Serial.print(F("low range : ")); + Serial.println(this->actors[actorNumber].rangeLow); #endif - //measured value lower than minimum range : activate actor - if(measurment < this->actors[actorNumber].rangeLow) - { - bitWrite( this->action , actorNumber , 1) ; + // measured value lower than minimum range : activate actor + if (measurment < this->actors[actorNumber].rangeLow) { + bitWrite(this->action, actorNumber, 1); - #if DEBUG == 1 +#if DEBUG == 1 - Serial.println(F("actor ON ")); - - #endif - - } - //measured value higher than maximum range : deactivate actor - else if(measurment > this->actors[actorNumber].rangeHigh) - { - bitWrite( this->action , actorNumber , 0) ; + Serial.println(F("actor ON ")); - #if DEBUG == 1 +#endif - Serial.println(F("actor OFF ")); - - #endif - - } + } + // measured value higher than maximum range : deactivate actor + else if (measurment > this->actors[actorNumber].rangeHigh) { + bitWrite(this->action, actorNumber, 0); +#if DEBUG == 1 -} + Serial.println(F("actor OFF ")); +#endif + } +} /** -* Jetpack::invertedAction(actorNumber , measured value): -* This method is provided to -* handle inverted actors. -* it changes the action according to wether the -* measured value is: -* > rangeHigh (activate actor) -* < rangeLow ( deactivate actor ) -*/ -void Jetpack::invertedAction(int actorNumber,float measurment) -{ + * Jetpack::invertedAction(actorNumber , measured value): + * This method is provided to + * handle inverted actors. + * it changes the action according to wether the + * measured value is: + * > rangeHigh (activate actor) + * < rangeLow ( deactivate actor ) + */ +void Jetpack::invertedAction(int actorNumber, float measurment) { #if DEBUG == 1 - - Serial.print(F("inverted Actor N° : ")); - Serial.println(actorNumber); - Serial.print(F("measured value : ")); - Serial.println(measurment); + Serial.print(F("inverted Actor N° : ")); + Serial.println(actorNumber); - Serial.print(F("high range : ")); - Serial.println(this->actors[actorNumber].rangeHigh); + Serial.print(F("measured value : ")); + Serial.println(measurment); - Serial.print(F("low range : ")); - Serial.println(this->actors[actorNumber].rangeLow); + Serial.print(F("high range : ")); + Serial.println(this->actors[actorNumber].rangeHigh); -#endif + Serial.print(F("low range : ")); + Serial.println(this->actors[actorNumber].rangeLow); - //measured value lower than minimum range : deactivate actor - if(measurment < this->actors[actorNumber].rangeLow) - { - bitWrite( this->action , actorNumber , 0) ; +#endif - #if DEBUG == 1 + // measured value lower than minimum range : deactivate actor + if (measurment < this->actors[actorNumber].rangeLow) { + bitWrite(this->action, actorNumber, 0); - Serial.println(F("actor OFF ")); - - #endif +#if DEBUG == 1 - } - //measured value higher than maximum range : activate actor - else if(measurment > this->actors[actorNumber].rangeHigh) - { - bitWrite( this->action , actorNumber , 1) ; + Serial.println(F("actor OFF ")); - #if DEBUG == 1 +#endif - Serial.println(F("actor ON ")); - - #endif + } + // measured value higher than maximum range : activate actor + else if (measurment > this->actors[actorNumber].rangeHigh) { + bitWrite(this->action, actorNumber, 1); - } +#if DEBUG == 1 + Serial.println(F("actor ON ")); +#endif + } } /** -* Jetpack::temporalActionOff(actorNumber ): -* This method is provided to -* handle temporal actors. -* it changes the action according to: -* -* currentTime - startTime > timeHigh : deactivate actor -* -*/ -void Jetpack::temporalActionOff(int actorNumber) -{ + * Jetpack::temporalActionOff(actorNumber ): + * This method is provided to + * handle temporal actors. + * it changes the action according to: + * + * currentTime - startTime > timeHigh : deactivate actor + * + */ +void Jetpack::temporalActionOff(int actorNumber) { #if DEBUG == 1 - Serial.println(F("temporalActionOff")); - Serial.print(F("temporal Actor N° : ")); - Serial.println(actorNumber); + Serial.println(F("temporalActionOff")); + Serial.print(F("temporal Actor N° : ")); + Serial.println(actorNumber); - Serial.print(F("millis : ")); - Serial.println(millis()); + Serial.print(F("millis : ")); + Serial.println(millis()); - Serial.print(F("actif Time : ")); - Serial.println(this->actors[actorNumber].actifTime); - - Serial.print(F("high time : ")); - Serial.println(this->actors[actorNumber].timeHigh); + Serial.print(F("actif Time : ")); + Serial.println(this->actors[actorNumber].actifTime); + Serial.print(F("high time : ")); + Serial.println(this->actors[actorNumber].timeHigh); #endif - - if( ( millis()- this->actors[actorNumber].actifTime ) >= ( this->actors[actorNumber].timeHigh ) || this->actors[actorNumber].actifTime == 0) - { - //stop the actor - bitWrite( this->action , actorNumber , 0) ; - //make the actor inactif: - this->actors[actorNumber].actif=0; + if ((millis() - this->actors[actorNumber].actifTime) >= + (this->actors[actorNumber].timeHigh) || + this->actors[actorNumber].actifTime == 0) { + // stop the actor + bitWrite(this->action, actorNumber, 0); - //start the low timer - this->actors[actorNumber].inactifTime=millis(); + // make the actor inactif: + this->actors[actorNumber].actif = 0; - #if DEBUG == 1 + // start the low timer + this->actors[actorNumber].inactifTime = millis(); - Serial.println(F("actor OFF ")); - - #endif - - } -} +#if DEBUG == 1 + Serial.println(F("actor OFF ")); + +#endif + } +} /** -* Jetpack::mixedTemporalActionOff(actorNumber, measured value ): -* This method is provided to -* handle mixed temporal actors. -* it changes the action according to: -* -* currentTime - startTime >= timeHigh : -* measured value >= rangeHigh : deactivate actor -* measured value < rangeHigh : activate actor -*/ -void Jetpack::mixedTemporalActionOff(int actorNumber,float measurment) -{ + * Jetpack::mixedTemporalActionOff(actorNumber, measured value ): + * This method is provided to + * handle mixed temporal actors. + * it changes the action according to: + * + * currentTime - startTime >= timeHigh : + * measured value >= rangeHigh : deactivate actor + * measured value < rangeHigh : activate actor + */ +void Jetpack::mixedTemporalActionOff(int actorNumber, float measurment) { #if DEBUG == 1 - - Serial.print("mixed Temporal Actor N° : "); - Serial.println(actorNumber); - Serial.print("measured value : "); - Serial.println(measurment); + Serial.print("mixed Temporal Actor N° : "); + Serial.println(actorNumber); - Serial.print("high range : "); - Serial.println(this->actors[actorNumber].rangeHigh); + Serial.print("measured value : "); + Serial.println(measurment); - Serial.print("time high : "); - Serial.println(this->actors[actorNumber].timeHigh); + Serial.print("high range : "); + Serial.println(this->actors[actorNumber].rangeHigh); - Serial.print("actif Time : "); - Serial.println(this->actors[actorNumber].actifTime); + Serial.print("time high : "); + Serial.println(this->actors[actorNumber].timeHigh); - Serial.print(F("millis : ")); - Serial.println(millis()); + Serial.print("actif Time : "); + Serial.println(this->actors[actorNumber].actifTime); -#endif - if( ( millis()- this->actors[actorNumber].actifTime ) >= ( this->actors[actorNumber].timeHigh ) ) - { - if( measurment >= this->actors[actorNumber].rangeHigh ) - { - //stop the actor - bitWrite( this->action , actorNumber , 0) ; + Serial.print(F("millis : ")); + Serial.println(millis()); - //make the actor inactif: - this->actors[actorNumber].actif=0; +#endif + if ((millis() - this->actors[actorNumber].actifTime) >= + (this->actors[actorNumber].timeHigh)) { + if (measurment >= this->actors[actorNumber].rangeHigh) { + // stop the actor + bitWrite(this->action, actorNumber, 0); - //start the low timer - this->actors[actorNumber].inactifTime=millis(); + // make the actor inactif: + this->actors[actorNumber].actif = 0; - #if DEBUG == 1 + // start the low timer + this->actors[actorNumber].inactifTime = millis(); - Serial.print(F("actor was on for at least ")); - Serial.print(this->actors[actorNumber].timeHigh); - Serial.println(F(" ms ")); +#if DEBUG == 1 - Serial.print(measurment); - Serial.print(F(" > " )); - Serial.println(this->actors[actorNumber].rangeHigh); + Serial.print(F("actor was on for at least ")); + Serial.print(this->actors[actorNumber].timeHigh); + Serial.println(F(" ms ")); - - Serial.println(F("actor OFF ")); + Serial.print(measurment); + Serial.print(F(" > ")); + Serial.println(this->actors[actorNumber].rangeHigh); - #endif + Serial.println(F("actor OFF ")); - } - else - { - bitWrite( this->action , actorNumber , 1) ; +#endif - #if DEBUG == 1 - - Serial.print(F("actor was on for at least ")); - Serial.print(this->actors[actorNumber].timeHigh); - Serial.println(F(" ms ")); + } else { + bitWrite(this->action, actorNumber, 1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actors[actorNumber].rangeHigh); +#if DEBUG == 1 - Serial.println(F("actor ON ")); + Serial.print(F("actor was on for at least ")); + Serial.print(this->actors[actorNumber].timeHigh); + Serial.println(F(" ms ")); - #endif + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actors[actorNumber].rangeHigh); - } - } + Serial.println(F("actor ON ")); +#endif + } + } } +/** + * Jetpack::temporalActionOn(actorNumber ): + * This method is provided to + * handle temporal actors. + * it changes the action according to : + * + * currentTime - stopTime > timeLow : activate actor + * + */ +void Jetpack::temporalActionOn(int actorNumber) { + +#if DEBUG == 1 + Serial.println(F("temporalActionOn")); + Serial.print(F("temporal Actor N° : ")); + Serial.println(actorNumber); + + Serial.print(F("millis : ")); + Serial.println(millis()); + + Serial.print(F("inactif Time : ")); + Serial.println(this->actors[actorNumber].inactifTime); + + Serial.print(F("low time : ")); + Serial.println(this->actors[actorNumber].timeLow); + +#endif + + if ((millis() - this->actors[actorNumber].inactifTime) >= + (this->actors[actorNumber].timeLow)) { + // start the actor + bitWrite(this->action, actorNumber, 1); + + // make the actor actif: + this->actors[actorNumber].actif = 1; + + // start the low timer + this->actors[actorNumber].actifTime = millis(); + +#if DEBUG == 1 + + Serial.println(F("actor ON ")); + +#endif + } +} /** -* Jetpack::temporalActionOn(actorNumber ): -* This method is provided to -* handle temporal actors. -* it changes the action according to : -* -* currentTime - stopTime > timeLow : activate actor -* -*/ -void Jetpack::temporalActionOn(int actorNumber) -{ + * Jetpack::mixedTemporalActionOn(actorNumber, measured value ): + * This method is provided to + * handle mixed temporal actors. + * it changes the action according to : + * + * currentTime - stopTime > timeLow : + * measured value >= rangeLow : deactivate actor + * measured value < rangeLow : activate actor + * + */ +void Jetpack::mixedTemporalActionOn(int actorNumber, float measurment) { #if DEBUG == 1 - Serial.println(F("temporalActionOn")); - Serial.print(F("temporal Actor N° : ")); - Serial.println(actorNumber); - Serial.print(F("millis : ")); - Serial.println(millis()); + Serial.print(F("mixed Temporal Actor N° : ")); + Serial.println(actorNumber); + + Serial.print(F("measured value : ")); + Serial.println(measurment); - Serial.print(F("inactif Time : ")); - Serial.println(this->actors[actorNumber].inactifTime); + Serial.print(F("low range : ")); + Serial.println(this->actors[actorNumber].rangeLow); - Serial.print(F("low time : ")); - Serial.println(this->actors[actorNumber].timeLow); + Serial.print(F("time low : ")); + Serial.println(this->actors[actorNumber].timeLow); + Serial.print(F("inactif Time : ")); + Serial.println(this->actors[actorNumber].inactifTime); + + Serial.print(F("millis : ")); + Serial.println(millis()); #endif - if( ( millis() - this->actors[actorNumber].inactifTime ) >= ( this->actors[actorNumber].timeLow ) ) - { - //start the actor - bitWrite( this->action , actorNumber , 1) ; + if ((millis() - this->actors[actorNumber].inactifTime) >= + (this->actors[actorNumber].timeLow)) { + if (measurment < this->actors[actorNumber].rangeLow) { + // start the actor + bitWrite(this->action, actorNumber, 1); + + // make the actor actif: + this->actors[actorNumber].actif = 1; + + // start the low timer + this->actors[actorNumber].actifTime = millis(); - //make the actor actif: - this->actors[actorNumber].actif=1; +#if DEBUG == 1 + + Serial.print(F("actor was off for at least ")); + Serial.print(this->actors[actorNumber].timeLow); + Serial.println(F(" ms ")); + + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actors[actorNumber].rangeLow); + + Serial.println(F("actor ON ")); + +#endif - //start the low timer - this->actors[actorNumber].actifTime=millis(); + } else { + bitWrite(this->action, actorNumber, 0); - #if DEBUG == 1 +#if DEBUG == 1 - Serial.println(F("actor ON ")); + Serial.print(F("actor was off for at least ")); + Serial.print(this->actors[actorNumber].timeLow); + Serial.println(F(" ms ")); - #endif + Serial.print(measurment); + Serial.print(F(" > ")); + Serial.println(this->actors[actorNumber].rangeLow); - } + Serial.println(F("actor OFF ")); +#endif + } + } } /** -* Jetpack::mixedTemporalActionOn(actorNumber, measured value ): -* This method is provided to -* handle mixed temporal actors. -* it changes the action according to : -* -* currentTime - stopTime > timeLow : -* measured value >= rangeLow : deactivate actor -* measured value < rangeLow : activate actor -* -*/ -void Jetpack::mixedTemporalActionOn(int actorNumber,float measurment) -{ + * Jetpack::hourAction(actorNumber, current hour ): + * This method is provided to + * handle hour actors. + * it changes the action according to: + * + * hour >= hourLow : deactivate the actor + * hour >= hourHigh : activate the actor + * + */ +void Jetpack::hourAction(int actorNumber, int hour) { #if DEBUG == 1 - - Serial.print(F("mixed Temporal Actor N° : ")); - Serial.println(actorNumber); - Serial.print(F("measured value : ")); - Serial.println(measurment); + Serial.print(F("hour Actor N° : ")); + Serial.println(actorNumber); - Serial.print(F("low range : ")); - Serial.println(this->actors[actorNumber].rangeLow); + Serial.print(F(" hour : ")); + Serial.println(hour); - Serial.print(F("time low : ")); - Serial.println(this->actors[actorNumber].timeLow); + Serial.print(F("high hour : ")); + Serial.println(this->actors[actorNumber].hourHigh); - Serial.print(F("inactif Time : ")); - Serial.println(this->actors[actorNumber].inactifTime); - - Serial.print(F("millis : ")); - Serial.println(millis()); + Serial.print(F("low hour : ")); + Serial.println(this->actors[actorNumber].hourLow); + Serial.print(F("inverted Flag : ")); + Serial.println(this->actors[actorNumber].inverted); + Serial.println(); #endif - if( ( millis() - this->actors[actorNumber].inactifTime ) >= ( this->actors[actorNumber].timeLow ) ) - { - if( measurment < this->actors[actorNumber].rangeLow ) - { - //start the actor - bitWrite( this->action , actorNumber , 1) ; + if (this->actors[actorNumber].hourHigh < this->actors[actorNumber].hourLow) { + // stop the actor + if (hour >= this->actors[actorNumber].hourLow || + hour < this->actors[actorNumber].hourHigh) { + if (this->actors[actorNumber].inverted) { + bitWrite(this->action, actorNumber, 1); + } else { + bitWrite(this->action, actorNumber, 0); + } - //make the actor actif: - this->actors[actorNumber].actif=1; +#if DEBUG == 1 - //start the low timer - this->actors[actorNumber].actifTime=millis(); + Serial.println(F("Daymode")); + Serial.println(F("actor OFF ")); - #if DEBUG == 1 +#endif - Serial.print(F("actor was off for at least ")); - Serial.print(this->actors[actorNumber].timeLow); - Serial.println(F(" ms ")); + } + // starting the actor + else { + if (this->actors[actorNumber].inverted) { + bitWrite(this->action, actorNumber, 0); + } else { + bitWrite(this->action, actorNumber, 1); + } - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actors[actorNumber].rangeLow); - - Serial.println(F("actor ON ")); - - #endif +#if DEBUG == 1 - } - else - { - bitWrite( this->action , actorNumber , 0) ; + Serial.println(F("DayMode")); + Serial.println(F("actor ON ")); - #if DEBUG == 1 +#endif + } + } else { + // stop the actor in Nght mode ie a light that is on over night + if (hour >= this->actors[actorNumber].hourLow && + hour < this->actors[actorNumber].hourHigh) { + if (this->actors[actorNumber].inverted) { + bitWrite(this->action, actorNumber, 1); + } else { + bitWrite(this->action, actorNumber, 0); + } - Serial.print(F("actor was off for at least ")); - Serial.print(this->actors[actorNumber].timeLow); - Serial.println(F(" ms ")); +#if DEBUG == 1 + + Serial.println(F("Nightmode")); + Serial.println(F("Actor OFF ")); - Serial.print(measurment); - Serial.print(F(" > " )); - Serial.println(this->actors[actorNumber].rangeLow); +#endif - Serial.println(F("actor OFF ")); - - #endif + } + // starting the actor + else { + if (this->actors[actorNumber].inverted) { + bitWrite(this->action, actorNumber, 0); + } else { + bitWrite(this->action, actorNumber, 1); + } - } +#if DEBUG == 1 - } + Serial.println(F("Hightmode")); + Serial.println(F("Actor ON ")); - +#endif + } + } } - /** -* Jetpack::hourAction(actorNumber, current hour ): -* This method is provided to -* handle hour actors. -* it changes the action according to: -* -* hour >= hourLow : deactivate the actor -* hour >= hourHigh : activate the actor -* -*/ -void Jetpack::hourAction(int actorNumber, int hour) -{ + * Jetpack::mixedHourAction(actorNumber, current hour, measured value ): + * This method is provided to + * handle mixed hour actors. + * it changes the action according to : + * + * hour >= hourLow : + * -measuredValue >= rangeHigh : deactivate actor + * -measured < rangeHigh : activate actor + * + * hour >= hourHigh : + * -measuredValue < rangeLow : activate actor + * -measuredValue >=rangeLow : activate actor + */ +void Jetpack::mixedHourAction(int actorNumber, int hour, float measurment) { #if DEBUG == 1 - - Serial.print(F("hour Actor N° : ")); - Serial.println(actorNumber); - Serial.print(F(" hour : ")); - Serial.println(hour); + Serial.print(F("mixed hour Actor N° : ")); + Serial.println(actorNumber); - Serial.print(F("high hour : ")); - Serial.println(this->actors[actorNumber].hourHigh); + Serial.print(F(" hour : ")); + Serial.println(hour); - Serial.print(F("low hour : ")); - Serial.println(this->actors[actorNumber].hourLow); + Serial.print(F("high hour : ")); + Serial.println(this->actors[actorNumber].hourHigh); - Serial.print(F("inverted Flag : ")); - Serial.println(this->actors[actorNumber].inverted); - Serial.println(); -#endif + Serial.print(F("low hour : ")); + Serial.println(this->actors[actorNumber].hourLow); - if (this->actors[actorNumber].hourHigh < this->actors[actorNumber].hourLow) - { - //stop the actor - if(hour >= this->actors[actorNumber].hourLow || hour < this->actors[actorNumber].hourHigh) - { - if (this->actors[actorNumber].inverted) - { - bitWrite( this->action , actorNumber , 1) ; - } - else - { - bitWrite( this->action , actorNumber , 0) ; - } - - #if DEBUG == 1 - - Serial.println(F("Daymode")); - Serial.println(F("actor OFF ")); - - #endif - - } - //starting the actor - else - { - if (this->actors[actorNumber].inverted) - { - bitWrite( this->action , actorNumber , 0) ; - } - else - { - bitWrite( this->action , actorNumber , 1) ; - } - - #if DEBUG == 1 - - Serial.println(F("DayMode")); - Serial.println(F("actor ON ")); - - #endif - - } - } - else - { - //stop the actor in Nght mode ie a light that is on over night - if(hour >= this->actors[actorNumber].hourLow && hour < this->actors[actorNumber].hourHigh) - { - if (this->actors[actorNumber].inverted) - { - bitWrite( this->action , actorNumber , 1) ; - } - else - { - bitWrite( this->action , actorNumber , 0) ; - } - - #if DEBUG == 1 - - Serial.println(F("Nightmode")); - Serial.println(F("Actor OFF ")); - - #endif - - } - //starting the actor - else - { - if (this->actors[actorNumber].inverted) - { - bitWrite( this->action , actorNumber , 0) ; - } - else - { - bitWrite( this->action , actorNumber , 1) ; - } - - #if DEBUG == 1 - - Serial.println(F("Hightmode")); - Serial.println(F("Actor ON ")); - - #endif - - } - } -} + Serial.print(F("measured value : ")); + Serial.println(measurment); + Serial.print(F("high range : ")); + Serial.println(this->actors[actorNumber].rangeHigh); -/** -* Jetpack::mixedHourAction(actorNumber, current hour, measured value ): -* This method is provided to -* handle mixed hour actors. -* it changes the action according to : -* -* hour >= hourLow : -* -measuredValue >= rangeHigh : deactivate actor -* -measured < rangeHigh : activate actor -* -* hour >= hourHigh : -* -measuredValue < rangeLow : activate actor -* -measuredValue >=rangeLow : activate actor -*/ -void Jetpack::mixedHourAction(int actorNumber,int hour, float measurment) -{ + Serial.print(F("low range : ")); + Serial.println(this->actors[actorNumber].rangeLow); + +#endif + + if (measurment <= this->actors[actorNumber].rangeLow && + this->actors[actorNumber].failsave == true) { + this->actors[actorNumber].failsave = false; + Serial.println("!!! Reset Failsave !!!"); + } + + else if (measurment >= this->actors[actorNumber].rangeHigh && + this->actors[actorNumber].failsave == false) { + this->actors[actorNumber].failsave = true; + Serial.println("!!! Engage Failsave !!!"); + } + + if (this->actors[actorNumber].hourHigh < this->actors[actorNumber].hourLow) { + // stop the actor + if ((hour >= this->actors[actorNumber].hourLow || + hour < this->actors[actorNumber].hourHigh) || + this->actors[actorNumber].failsave == true) { + if (this->actors[actorNumber].inverted) { + bitWrite(this->action, actorNumber, 1); + } else { + bitWrite(this->action, actorNumber, 0); + } #if DEBUG == 1 - - Serial.print(F("mixed hour Actor N° : ")); - Serial.println(actorNumber); - Serial.print(F(" hour : ")); - Serial.println(hour); + Serial.println(F("Daymode")); + Serial.println(F("Onboard Actor OFF ")); + Serial.print(F("failsave Flag : ")); + Serial.println(this->actors[actorNumber].failsave); - Serial.print(F("high hour : ")); - Serial.println(this->actors[actorNumber].hourHigh); +#endif - Serial.print(F("low hour : ")); - Serial.println(this->actors[actorNumber].hourLow); + } + // starting the actor + else if (this->actors[actorNumber].failsave == false) { + if (this->actors[actorNumber].inverted) { + bitWrite(this->action, actorNumber, 0); + } else { + bitWrite(this->action, actorNumber, 1); + } - Serial.print(F("measured value : ")); - Serial.println(measurment); +#if DEBUG == 1 + + Serial.println(F("DayMode")); + Serial.println(F("Onboard Actor ON ")); + +#endif + } + } else { + // stop the actor in Nght mode ie a light that is on over night + if ((hour >= this->actors[actorNumber].hourLow && + hour < this->actors[actorNumber].hourHigh) || + this->actors[actorNumber].failsave == true) { + if (this->actors[actorNumber].inverted) { + bitWrite(this->action, actorNumber, 1); + } else { + bitWrite(this->action, actorNumber, 0); + } - Serial.print(F("high range : ")); - Serial.println(this->actors[actorNumber].rangeHigh); +#if DEBUG == 1 - Serial.print(F("low range : ")); - Serial.println(this->actors[actorNumber].rangeLow); + Serial.println(F("Nightmode")); + Serial.println(F("Onboard Actor OFF ")); #endif - if (measurment <= this->actors[actorNumber].rangeLow && this->actors[actorNumber].failsave == true) - { - this->actors[actorNumber].failsave = false; - Serial.println("!!! Reset Failsave !!!"); - } - - else if (measurment >= this->actors[actorNumber].rangeHigh && this->actors[actorNumber].failsave == false) - { - this->actors[actorNumber].failsave = true; - Serial.println("!!! Engage Failsave !!!"); - } - - - if (this->actors[actorNumber].hourHigh < this->actors[actorNumber].hourLow) - { - //stop the actor - if((hour >= this->actors[actorNumber].hourLow || hour < this->actors[actorNumber].hourHigh) || this->actors[actorNumber].failsave == true) - { - if (this->actors[actorNumber].inverted) - { - bitWrite( this->action , actorNumber , 1) ; - } - else - { - bitWrite( this->action , actorNumber , 0) ; - } - - #if DEBUG == 1 - - Serial.println(F("Daymode")); - Serial.println(F("Onboard Actor OFF ")); - Serial.print(F("failsave Flag : ")); - Serial.println(this->actors[actorNumber].failsave); - - #endif - - } - //starting the actor - else if (this->actors[actorNumber].failsave == false) - { - if (this->actors[actorNumber].inverted) - { - bitWrite( this->action , actorNumber , 0) ; - } - else - { - bitWrite( this->action , actorNumber , 1) ; - } - - #if DEBUG == 1 - - Serial.println(F("DayMode")); - Serial.println(F("Onboard Actor ON ")); - - #endif - - } - } - else - { - //stop the actor in Nght mode ie a light that is on over night - if((hour >= this->actors[actorNumber].hourLow && hour < this->actors[actorNumber].hourHigh) || this->actors[actorNumber].failsave == true) - { - if (this->actors[actorNumber].inverted) - { - bitWrite( this->action , actorNumber , 1) ; - } - else - { - bitWrite( this->action , actorNumber , 0) ; - } - - #if DEBUG == 1 - - Serial.println(F("Nightmode")); - Serial.println(F("Onboard Actor OFF ")); - - #endif - - } - //starting the actor - else if (this->actors[actorNumber].failsave == false) - { - if (this->actors[actorNumber].inverted) - { - bitWrite( this->action , actorNumber , 0) ; - } - else - { - bitWrite( this->action , actorNumber , 1) ; - } - - #if DEBUG == 1 - - Serial.println(F("Nightmode")); - Serial.println(F("Onboard Actor ON ")); - - #endif - - } - } + } + // starting the actor + else if (this->actors[actorNumber].failsave == false) { + if (this->actors[actorNumber].inverted) { + bitWrite(this->action, actorNumber, 0); + } else { + bitWrite(this->action, actorNumber, 1); + } + +#if DEBUG == 1 + + Serial.println(F("Nightmode")); + Serial.println(F("Onboard Actor ON ")); + +#endif + } + } } /** -* Jetpack::minteAction(actorNumber, current minute ): -* This method is provided to -* handle minute actors. -* it changes the action according to: -* -* minute >= minuteLow : deactivate the actor -* minute >= minuteHigh : activate the actor -* -*/ -void Jetpack::minuteAction(int actorNumber,int minute) -{ + * Jetpack::minteAction(actorNumber, current minute ): + * This method is provided to + * handle minute actors. + * it changes the action according to: + * + * minute >= minuteLow : deactivate the actor + * minute >= minuteHigh : activate the actor + * + */ +void Jetpack::minuteAction(int actorNumber, int minute) { #if DEBUG == 1 - - Serial.print(F("minute Actor N° : ")); - Serial.println(actorNumber); - Serial.print(F(" minute : ")); - Serial.println(minute); + Serial.print(F("minute Actor N° : ")); + Serial.println(actorNumber); - Serial.print(F("high minute : ")); - Serial.println(this->actors[actorNumber].minuteHigh); + Serial.print(F(" minute : ")); + Serial.println(minute); - Serial.print(F("low minute : ")); - Serial.println(this->actors[actorNumber].minuteLow); + Serial.print(F("high minute : ")); + Serial.println(this->actors[actorNumber].minuteHigh); -#endif - - //stop the actor - if(minute <= this->actors[actorNumber].minuteLow) - { - bitWrite( this->action , actorNumber , 0) ; + Serial.print(F("low minute : ")); + Serial.println(this->actors[actorNumber].minuteLow); - #if DEBUG == 1 +#endif - Serial.println(F("actor OFF ")); + // stop the actor + if (minute <= this->actors[actorNumber].minuteLow) { + bitWrite(this->action, actorNumber, 0); - #endif +#if DEBUG == 1 - } - //starting the actor - else if(minute >= this->actors[actorNumber].minuteHigh) - { - bitWrite( this->action , actorNumber , 1) ; + Serial.println(F("actor OFF ")); - #if DEBUG == 1 +#endif - Serial.println(F("actor ON ")); + } + // starting the actor + else if (minute >= this->actors[actorNumber].minuteHigh) { + bitWrite(this->action, actorNumber, 1); - #endif +#if DEBUG == 1 - } + Serial.println(F("actor ON ")); -} +#endif + } +} /** -* Jetpack::mixedMinuteAction(actorNumber, current minute, measured value ): -* This method is provided to -* handle mixed minute actors. -* it changes the action according to : -* -* minute >= minuteLow : -* -measuredValue >= rangeHigh : deactivate actor -* -measured < rangeHigh : activate actor -* -* minute >= minuteHigh : -* -measuredValue < rangeLow : activate actor -* -measuredValue >=rangeLow : activate actor -*/ -void Jetpack::mixedMinuteAction(int actorNumber,int minute,float measurment) -{ + * Jetpack::mixedMinuteAction(actorNumber, current minute, measured value + *): This method is provided to handle mixed minute actors. it changes the + *action according to : + * + * minute >= minuteLow : + * -measuredValue >= rangeHigh : deactivate actor + * -measured < rangeHigh : activate actor + * + * minute >= minuteHigh : + * -measuredValue < rangeLow : activate actor + * -measuredValue >=rangeLow : activate actor + */ +void Jetpack::mixedMinuteAction(int actorNumber, int minute, float measurment) { #if DEBUG == 1 - - Serial.print(F("mixed minute Actor N° : ")); - Serial.println(actorNumber); - Serial.print(F(" minute : ")); - Serial.println(minute); + Serial.print(F("mixed minute Actor N° : ")); + Serial.println(actorNumber); + + Serial.print(F(" minute : ")); + Serial.println(minute); - Serial.print(F("high minute : ")); - Serial.println(this->actors[actorNumber].minuteHigh); + Serial.print(F("high minute : ")); + Serial.println(this->actors[actorNumber].minuteHigh); - Serial.print(F("low minute : ")); - Serial.println(this->actors[actorNumber].minuteLow); + Serial.print(F("low minute : ")); + Serial.println(this->actors[actorNumber].minuteLow); - Serial.print(F("measured value : ")); - Serial.println(measurment); + Serial.print(F("measured value : ")); + Serial.println(measurment); - Serial.print(F("high range : ")); - Serial.println(this->actors[actorNumber].rangeHigh); + Serial.print(F("high range : ")); + Serial.println(this->actors[actorNumber].rangeHigh); - Serial.print(F("low range : ")); - Serial.println(this->actors[actorNumber].rangeLow); + Serial.print(F("low range : ")); + Serial.println(this->actors[actorNumber].rangeLow); #endif - //stop the actor - if(minute <= this->actors[actorNumber].minuteLow) - { - if( measurment > this->actors[actorNumber].rangeHigh ) - { - bitWrite( this->action , actorNumber , 0) ; + // stop the actor + if (minute <= this->actors[actorNumber].minuteLow) { + if (measurment > this->actors[actorNumber].rangeHigh) { + bitWrite(this->action, actorNumber, 0); - #if DEBUG == 1 +#if DEBUG == 1 - Serial.print(measurment); - Serial.print(F(" > " )); - Serial.println(this->actors[actorNumber].rangeHigh); + Serial.print(measurment); + Serial.print(F(" > ")); + Serial.println(this->actors[actorNumber].rangeHigh); - Serial.println(F("actor OFF ")); + Serial.println(F("actor OFF ")); - #endif - - } - else - { - bitWrite( this->action , actorNumber , 1) ; +#endif - #if DEBUG == 1 + } else { + bitWrite(this->action, actorNumber, 1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actors[actorNumber].rangeHigh); +#if DEBUG == 1 - Serial.println(F("actor ON ")); + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actors[actorNumber].rangeHigh); - #endif - - } - } - //starting the actor - else if(minute >= this->actors[actorNumber].minuteHigh) - { - if( measurment < this->actors[actorNumber].rangeLow ) - { - bitWrite( this->action , actorNumber , 1) ; + Serial.println(F("actor ON ")); - #if DEBUG == 1 +#endif + } + } + // starting the actor + else if (minute >= this->actors[actorNumber].minuteHigh) { + if (measurment < this->actors[actorNumber].rangeLow) { + bitWrite(this->action, actorNumber, 1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actors[actorNumber].rangeLow); +#if DEBUG == 1 - Serial.println(F("actor ON ")); + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actors[actorNumber].rangeLow); - #endif + Serial.println(F("actor ON ")); - } - else - { - bitWrite( this->action , actorNumber , 0) ; - - #if DEBUG == 1 +#endif - Serial.print(measurment); - Serial.print(F(" > " )); - Serial.println(this->actors[actorNumber].rangeLow); + } else { + bitWrite(this->action, actorNumber, 0); - Serial.println(F("actor OFF ")); +#if DEBUG == 1 - #endif - - } + Serial.print(measurment); + Serial.print(F(" > ")); + Serial.println(this->actors[actorNumber].rangeLow); - } + Serial.println(F("actor OFF ")); +#endif + } + } } - /** -* Jetpack::minteAction(actorNumber, current hour,current minute ): -* This method is provided to -* handle hour minute actors. -* it changes the action according to: -* -* hour == hourLow : -* minute >= minuteLow : deactivate the actor -* -* hour > hourLow : deactivate the actor -* -* hour == hourHigh : -* minute >= minteHigh : activate the actor -* -* hour > hourHigh : activate the actor -*/ -void Jetpack::hourMinuteAction(int actorNumber,int hour,int minute) -{ + * Jetpack::minteAction(actorNumber, current hour,current minute ): + * This method is provided to + * handle hour minute actors. + * it changes the action according to: + * + * hour == hourLow : + * minute >= minuteLow : deactivate the actor + * + * hour > hourLow : deactivate the actor + * + * hour == hourHigh : + * minute >= minteHigh : activate the actor + * + * hour > hourHigh : activate the actor + */ +void Jetpack::hourMinuteAction(int actorNumber, int hour, int minute) { #if DEBUG == 1 - Serial.print(F("hourMinute Actor N° : ")); - Serial.println(actorNumber); + Serial.print(F("hourMinute Actor N° : ")); + Serial.println(actorNumber); - Serial.print(F(" hour : ")); - Serial.println(hour); - Serial.print(F(" minute : ")); - Serial.println(minute); + Serial.print(F(" hour : ")); + Serial.println(hour); + Serial.print(F(" minute : ")); + Serial.println(minute); - Serial.print(F("high hour : ")); - Serial.println(this->actors[actorNumber].hourHigh); + Serial.print(F("high hour : ")); + Serial.println(this->actors[actorNumber].hourHigh); - Serial.print(F("high minute : ")); - Serial.println(this->actors[actorNumber].minuteHigh); + Serial.print(F("high minute : ")); + Serial.println(this->actors[actorNumber].minuteHigh); - Serial.print(F("low hour : ")); - Serial.println(this->actors[actorNumber].hourLow); + Serial.print(F("low hour : ")); + Serial.println(this->actors[actorNumber].hourLow); - Serial.print(F("low minute : ")); - Serial.println(this->actors[actorNumber].minuteLow); + Serial.print(F("low minute : ")); + Serial.println(this->actors[actorNumber].minuteLow); #endif - //stop the actor - if(hour==this->actors[actorNumber].hourLow) - { - if(minute>= this->actors[actorNumber].minuteLow) - { - bitWrite( this->action , actorNumber , 0) ; - #if DEBUG == 1 - - Serial.println(F("actor OFF ")); + // stop the actor + if (hour == this->actors[actorNumber].hourLow) { + if (minute >= this->actors[actorNumber].minuteLow) { + bitWrite(this->action, actorNumber, 0); +#if DEBUG == 1 - #endif - } - } - else if(hour > this->actors[actorNumber].hourLow) - { + Serial.println(F("actor OFF ")); - bitWrite( this->action , actorNumber , 0) ; - #if DEBUG == 1 +#endif + } + } else if (hour > this->actors[actorNumber].hourLow) { - Serial.println(F("actor OFF ")); + bitWrite(this->action, actorNumber, 0); +#if DEBUG == 1 - #endif - - } - //start the actor - else if(hour==this->actors[actorNumber].hourHigh) - { - if(minute>= this->actors[actorNumber].minuteHigh) - { - bitWrite( this->action , actorNumber , 1) ; + Serial.println(F("actor OFF ")); - #if DEBUG == 1 +#endif - Serial.println(F("actor ON ")); + } + // start the actor + else if (hour == this->actors[actorNumber].hourHigh) { + if (minute >= this->actors[actorNumber].minuteHigh) { + bitWrite(this->action, actorNumber, 1); - #endif - } - } - else if(hour > this->actors[actorNumber].hourHigh) - { +#if DEBUG == 1 - bitWrite( this->action , actorNumber , 1) ; + Serial.println(F("actor ON ")); - #if DEBUG == 1 +#endif + } + } else if (hour > this->actors[actorNumber].hourHigh) { - Serial.println(F("actor ON ")); + bitWrite(this->action, actorNumber, 1); - #endif +#if DEBUG == 1 - } + Serial.println(F("actor ON ")); - +#endif + } } /** -* Jetpack::minteAction(actorNumber, current hour,current minute , measured Value ): -* This method is provided to -* handle hour minute actors. -* it changes the action according to: -* -* hour == hourLow : -* minute >= minuteLow : -* measuredValue >= rangeHigh : deactivate actor -* measuredValue < rangeHigh : activate actor -* -* hour > hourLow : -* measuredValue >= rangeHigh : deactivate actor -* measuredValue < rangeHigh : activate actor -* -* hour == hourHigh : -* minute >= minteHigh : -* measuredValue >= rangeLow : deactivate actor -* measuredValue < rangeLow : activate actor -* -* hour > hourHigh : -* measuredValue >= rangeLow : deactivate actor -* measuredValue < rangeLow : activate actor -* -*/ -void Jetpack::mixedHourMinuteAction(int actorNumber,int hour,int minute ,float measurment) -{ + * Jetpack::minteAction(actorNumber, current hour,current minute , measured + *Value ): This method is provided to handle hour minute actors. it changes the + *action according to: + * + * hour == hourLow : + * minute >= minuteLow : + * measuredValue >= rangeHigh : deactivate actor + * measuredValue < rangeHigh : activate actor + * + * hour > hourLow : + * measuredValue >= rangeHigh : deactivate actor + * measuredValue < rangeHigh : activate actor + * + * hour == hourHigh : + * minute >= minteHigh : + * measuredValue >= rangeLow : deactivate actor + * measuredValue < rangeLow : activate actor + * + * hour > hourHigh : + * measuredValue >= rangeLow : deactivate actor + * measuredValue < rangeLow : activate actor + * + */ +void Jetpack::mixedHourMinuteAction(int actorNumber, int hour, int minute, + float measurment) { #if DEBUG == 1 - - Serial.print(F("hourMinute Actor N° : ")); - Serial.println(actorNumber); - - Serial.print(F(" hour : ")); - Serial.println(hour); - Serial.print(F(" minute : ")); - Serial.println(minute); - Serial.print(F("high hour : ")); - Serial.println(this->actors[actorNumber].hourHigh); + Serial.print(F("hourMinute Actor N° : ")); + Serial.println(actorNumber); - Serial.print(F("high minute : ")); - Serial.println(this->actors[actorNumber].minuteHigh); + Serial.print(F(" hour : ")); + Serial.println(hour); + Serial.print(F(" minute : ")); + Serial.println(minute); - Serial.print(F("low hour : ")); - Serial.println(this->actors[actorNumber].hourLow); + Serial.print(F("high hour : ")); + Serial.println(this->actors[actorNumber].hourHigh); - Serial.print(F("low minute : ")); - Serial.println(this->actors[actorNumber].minuteLow); + Serial.print(F("high minute : ")); + Serial.println(this->actors[actorNumber].minuteHigh); - Serial.print(F("measured value : ")); - Serial.println(measurment); + Serial.print(F("low hour : ")); + Serial.println(this->actors[actorNumber].hourLow); - Serial.print(F("high range : ")); - Serial.println(this->actors[actorNumber].rangeHigh); + Serial.print(F("low minute : ")); + Serial.println(this->actors[actorNumber].minuteLow); - Serial.print(F("low range : ")); - Serial.println(this->actors[actorNumber].rangeLow); + Serial.print(F("measured value : ")); + Serial.println(measurment); -#endif - //stop the actor - if(hour==this->actors[actorNumber].hourLow) - { - if(minute>= this->actors[actorNumber].minuteLow) - { - if( measurment >= this->actors[actorNumber].rangeHigh ) - { - bitWrite( this->action , actorNumber , 0) ; + Serial.print(F("high range : ")); + Serial.println(this->actors[actorNumber].rangeHigh); - #if DEBUG == 1 + Serial.print(F("low range : ")); + Serial.println(this->actors[actorNumber].rangeLow); - Serial.print(measurment); - Serial.print(F(" >= " )); - Serial.println(this->actors[actorNumber].rangeHigh); +#endif + // stop the actor + if (hour == this->actors[actorNumber].hourLow) { + if (minute >= this->actors[actorNumber].minuteLow) { + if (measurment >= this->actors[actorNumber].rangeHigh) { + bitWrite(this->action, actorNumber, 0); - Serial.println(F("actor OFF ")); +#if DEBUG == 1 - #endif + Serial.print(measurment); + Serial.print(F(" >= ")); + Serial.println(this->actors[actorNumber].rangeHigh); - } - else - { - bitWrite( this->action , actorNumber , 1) ; + Serial.println(F("actor OFF ")); - #if DEBUG == 1 +#endif - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actors[actorNumber].rangeHigh); + } else { + bitWrite(this->action, actorNumber, 1); - Serial.println(F("actor ON ")); +#if DEBUG == 1 - #endif - - } - } - } - else if(hour > this->actors[actorNumber].hourLow) - { + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actors[actorNumber].rangeHigh); - if( measurment >= this->actors[actorNumber].rangeHigh ) - { - bitWrite( this->action , actorNumber , 0) ; + Serial.println(F("actor ON ")); - #if DEBUG == 1 +#endif + } + } + } else if (hour > this->actors[actorNumber].hourLow) { - Serial.print(measurment); - Serial.print(F(" >= " )); - Serial.println(this->actors[actorNumber].rangeHigh); + if (measurment >= this->actors[actorNumber].rangeHigh) { + bitWrite(this->action, actorNumber, 0); - Serial.println(F("actor OFF ")); +#if DEBUG == 1 - #endif + Serial.print(measurment); + Serial.print(F(" >= ")); + Serial.println(this->actors[actorNumber].rangeHigh); - } - else - { - bitWrite( this->action , actorNumber , 1) ; + Serial.println(F("actor OFF ")); - #if DEBUG == 1 +#endif - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actors[actorNumber].rangeHigh); + } else { + bitWrite(this->action, actorNumber, 1); - Serial.println(F("actor ON ")); +#if DEBUG == 1 - #endif - - } + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actors[actorNumber].rangeHigh); + Serial.println(F("actor ON ")); - } - //start the actor - else if(hour==this->actors[actorNumber].hourHigh) - { - if(minute>= this->actors[actorNumber].minuteHigh) - { - if( measurment < this->actors[actorNumber].rangeLow ) - { - bitWrite( this->action , actorNumber , 1) ; +#endif + } - #if DEBUG == 1 + } + // start the actor + else if (hour == this->actors[actorNumber].hourHigh) { + if (minute >= this->actors[actorNumber].minuteHigh) { + if (measurment < this->actors[actorNumber].rangeLow) { + bitWrite(this->action, actorNumber, 1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actors[actorNumber].rangeLow); +#if DEBUG == 1 - Serial.println(F("actor ON ")); + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actors[actorNumber].rangeLow); - #endif + Serial.println(F("actor ON ")); - } - else - { - bitWrite( this->action , actorNumber , 0) ; +#endif - #if DEBUG == 1 + } else { + bitWrite(this->action, actorNumber, 0); - Serial.print(measurment); - Serial.println(F(" > " )); - Serial.print(this->actors[actorNumber].rangeLow); +#if DEBUG == 1 - Serial.println(F("actor OFF ")); + Serial.print(measurment); + Serial.println(F(" > ")); + Serial.print(this->actors[actorNumber].rangeLow); - #endif - - } - } - } - else if(hour > this->actors[actorNumber].hourHigh) - { + Serial.println(F("actor OFF ")); - if( measurment < this->actors[actorNumber].rangeLow ) - { - bitWrite( this->action , actorNumber , 1) ; +#endif + } + } + } else if (hour > this->actors[actorNumber].hourHigh) { - #if DEBUG == 1 + if (measurment < this->actors[actorNumber].rangeLow) { + bitWrite(this->action, actorNumber, 1); - Serial.print(measurment); - Serial.print(F(" < " )); - Serial.println(this->actors[actorNumber].rangeLow); +#if DEBUG == 1 - Serial.println(F("actor ON ")); + Serial.print(measurment); + Serial.print(F(" < ")); + Serial.println(this->actors[actorNumber].rangeLow); - #endif + Serial.println(F("actor ON ")); - } - else - { - bitWrite( this->action , actorNumber , 0) ; +#endif - #if DEBUG == 1 + } else { + bitWrite(this->action, actorNumber, 0); - Serial.print(measurment); - Serial.println(F(" > " )); - Serial.print(this->actors[actorNumber].rangeLow); +#if DEBUG == 1 - Serial.println(F("actor OFF ")); + Serial.print(measurment); + Serial.println(F(" > ")); + Serial.print(this->actors[actorNumber].rangeLow); - #endif - - } - - } + Serial.println(F("actor OFF ")); +#endif + } + } } - - diff --git a/src/Jetpack.h b/src/Jetpack.h index 59876722..094aa0e1 100644 --- a/src/Jetpack.h +++ b/src/Jetpack.h @@ -1,221 +1,223 @@ /** -* Copyright (c) 2018 La Cool Co SAS -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -* -*/ - + * Copyright (c) 2018 La Cool Co SAS + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ #ifndef Jetpack_H #define Jetpack_H #include "Arduino.h" - /** -* \class Jetpack -* \brief This class manages the Jetpack shield -* -*/ -class Jetpack -{ + * \class Jetpack + * \brief This class manages the Jetpack shield + * + */ +class Jetpack { public: + void begin(); // starts the Jetpack - void begin(); //starts the Jetpack + void write(byte action); // writes to the Jetpack - void write(byte action); //writes to the Jetpack - - void writeBit(byte pin,bool state); //writes to a single pin of the Jetpack - - String doAction(const char* data, int hour, int minute ); + void writeBit(byte pin, bool state); // writes to a single pin of the Jetpack - void normalAction(int actorNumber,float measurment); + String doAction(const char *data, int hour, int minute); - void invertedAction(int actorNumber,float measurment); + void normalAction(int actorNumber, float measurment); - void temporalActionOff(int actorNumber); + void invertedAction(int actorNumber, float measurment); - void temporalActionOn(int actorNumber); + void temporalActionOff(int actorNumber); - void mixedTemporalActionOff(int actorNumber,float measurment); + void temporalActionOn(int actorNumber); - void mixedTemporalActionOn(int actorNumber,float measurment); + void mixedTemporalActionOff(int actorNumber, float measurment); - void hourAction(int actorNumber, int hour); + void mixedTemporalActionOn(int actorNumber, float measurment); - void mixedHourAction(int actorNumber,int hour, float measurment); + void hourAction(int actorNumber, int hour); - void minuteAction(int actorNumber,int minute); + void mixedHourAction(int actorNumber, int hour, float measurment); - void mixedMinuteAction(int actorNumber,int minute,float measurment); + void minuteAction(int actorNumber, int minute); - void hourMinuteAction(int actorNumber,int hour,int minute); + void mixedMinuteAction(int actorNumber, int minute, float measurment); - void mixedHourMinuteAction(int actorNumber,int hour,int minute ,float measurment); + void hourMinuteAction(int actorNumber, int hour, int minute); - bool config(); + void mixedHourMinuteAction(int actorNumber, int hour, int minute, + float measurment); - void printConf(); + bool config(); - /** - * the Jetpack's Action - */ - byte action= B00000000; + void printConf(); -private: + /** + * the Jetpack's Action + */ + byte action = B00000000; - /** - * the Actor's state - */ - struct state{ - - /** - * actif flag - * set to 1 when using an actor - * set to 0 otherwise - */ - bool actif=0; - - /** - * temporal flag - * set to 1 to declare a temporal actor - * set to 0 otherwise - */ - bool temporal=0; - - /** - * inverted flag - * set to 1 to declare an inverted actor - * set to 0 to declare a normal actor - */ - bool inverted=0; - - /** - * "type":["temperature","hour"] - * - * the primary type is related to the sensor's type (type[0]) - * - * if both types are present and valid, the actor is a mixedActor - */ - String primaryType=""; - - /** - * "type":["temperature","hour"] - * - * the secondary type if present is hour or minute or hourMinute (type[1]) - * - * if both types are present and valid, the actor is a mixedActor - */ - String secondaryType=""; - - /** - * "low":[20,5000,18,10] - * - * rangeLow : this is the minimum at which the actor becomes actif (low[0]) - * - */ - int rangeLow=0; - - /** - * "low":[20,5000,18,10] - * - * timeLow : this is the time the actor is off in temporal mode (low[1]) (ms) - */ - unsigned long timeLow=0; - - /** - * "low":[20,5000,18,10] - * - * hour low :this is the hour when to turn off the actor in temporal/hour(hourMinute) mode (low[2] ) - */ - int hourLow=0; - - /** - * "low":[20,5000,18,10] - * - * minute low :this is the minute when to turn off the actor in temporal/minute(hourMinute) mode (low[3]) - */ - int minuteLow=0; - - - /** - * "high":[30,2000,17,1] - * - * rangeHigh : this is the maximum at which the actor becomes inactif(high[0]) - */ - int rangeHigh=0; - - /** - * "high":[30,2000,17,1] - * - * timeHigh : this is the time the actor is on in temporal mode(high[1]) (ms) - */ - unsigned long timeHigh=0; - - /** - * "high":[30,2000,17,1] - * - * hourHigh : this is the hour when to turn on the actor in temporal/hour(hourMinute) mode(high[2]) - */ - int hourHigh=0; - - /** - * "high":[30,2000,17,1] - * - * minuteHigh : this is the minute when to turn on the actor in temporal/minute(hourMinute) mode (high[3]) - */ - int minuteHigh=0; - - /** - * actifTime : period of Time spent actif , used in Temporal mode - * in ms - */ - unsigned long actifTime=0; - - /** - * inactifTime : period of Time spent inactif , used in Temporal mode - * in ms - */ - unsigned long inactifTime=0; - - /** - * failsave : Switches of the actor if a metric rises above high treshold in mixed mode - */ - bool failsave = false ; - - }actors[8]; - - /** - * clock pin for the shift register - */ - const int clockPin = 4; - - /** - * data pin for the shift register - */ - const int dataPin = 15; - - /** - * I2C Enable pin - */ - const int EnI2C=5; - +private: + /** + * the Actor's state + */ + struct state { + + /** + * actif flag + * set to 1 when using an actor + * set to 0 otherwise + */ + bool actif = 0; + + /** + * temporal flag + * set to 1 to declare a temporal actor + * set to 0 otherwise + */ + bool temporal = 0; + + /** + * inverted flag + * set to 1 to declare an inverted actor + * set to 0 to declare a normal actor + */ + bool inverted = 0; + + /** + * "type":["temperature","hour"] + * + * the primary type is related to the sensor's type (type[0]) + * + * if both types are present and valid, the actor is a mixedActor + */ + String primaryType = ""; + + /** + * "type":["temperature","hour"] + * + * the secondary type if present is hour or minute or hourMinute (type[1]) + * + * if both types are present and valid, the actor is a mixedActor + */ + String secondaryType = ""; + + /** + * "low":[20,5000,18,10] + * + * rangeLow : this is the minimum at which the actor becomes actif (low[0]) + * + */ + int rangeLow = 0; + + /** + * "low":[20,5000,18,10] + * + * timeLow : this is the time the actor is off in temporal mode (low[1]) + *(ms) + */ + unsigned long timeLow = 0; + + /** + * "low":[20,5000,18,10] + * + * hour low :this is the hour when to turn off the actor in + *temporal/hour(hourMinute) mode (low[2] ) + */ + int hourLow = 0; + + /** + * "low":[20,5000,18,10] + * + * minute low :this is the minute when to turn off the actor in + *temporal/minute(hourMinute) mode (low[3]) + */ + int minuteLow = 0; + + /** + * "high":[30,2000,17,1] + * + * rangeHigh : this is the maximum at which the actor becomes + *inactif(high[0]) + */ + int rangeHigh = 0; + + /** + * "high":[30,2000,17,1] + * + * timeHigh : this is the time the actor is on in temporal mode(high[1]) + *(ms) + */ + unsigned long timeHigh = 0; + + /** + * "high":[30,2000,17,1] + * + * hourHigh : this is the hour when to turn on the actor in + *temporal/hour(hourMinute) mode(high[2]) + */ + int hourHigh = 0; + + /** + * "high":[30,2000,17,1] + * + * minuteHigh : this is the minute when to turn on the actor in + *temporal/minute(hourMinute) mode (high[3]) + */ + int minuteHigh = 0; + + /** + * actifTime : period of Time spent actif , used in Temporal mode + * in ms + */ + unsigned long actifTime = 0; + + /** + * inactifTime : period of Time spent inactif , used in Temporal mode + * in ms + */ + unsigned long inactifTime = 0; + + /** + * failsave : Switches of the actor if a metric rises above high treshold + *in mixed mode + */ + bool failsave = false; + + } actors[8]; + + /** + * clock pin for the shift register + */ + const int clockPin = 4; + + /** + * data pin for the shift register + */ + const int dataPin = 15; + + /** + * I2C Enable pin + */ + const int EnI2C = 5; }; #endif diff --git a/src/internals/extras/WiFiManager.template.html b/src/internals/extras/WiFiManager.template.html deleted file mode 100644 index 8050f91c..00000000 --- a/src/internals/extras/WiFiManager.template.html +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - {v} - - - - - - - - - - -
- - - - - - - - -




- - -
{v} {r}%
-
PMisa 100%
- -
PMisa 8%
- - -
{v} {r}%
- - -


- - -
- - -
- - -
- - -
Credentials Saved
Trying to connect ESP to network.
If it fails reconnect to AP to try again
- - - - - - -
- - - diff --git a/src/internals/extras/parse.js b/src/internals/extras/parse.js deleted file mode 100644 index 97a3e384..00000000 --- a/src/internals/extras/parse.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -const fs = require('fs'); - -console.log('starting'); - -const inFile = 'WiFiManager.template.html'; -const outFile = 'template.h'; - -const defineRegEx = //gm; -console.log('parsing', inFile); - -fs.readFile(inFile, 'utf8', function (err,data) { - if (err) { - return console.log(err); - } - //console.log(data); - - let defines = data.match(defineRegEx); - - //console.log(defines); - var stream = fs.createWriteStream(outFile); - stream.once('open', function(fd) { - for (const i in defines) { - - const start = defines[i]; - const end = start.replace('