code style

This commit is contained in:
Timo Volkmann 2020-06-30 23:14:17 +02:00
parent 32b6850814
commit 861d8b527b
5 changed files with 53 additions and 81 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"cquery.cacheDirectory": "${workspaceFolder}/.vscode/cquery_cached_index/"
}

View File

@ -1,9 +1,8 @@
#include <header.h>
#include <MQTT.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <MQTT.h>
#include <header.h>
extern "C" extern "C" {
{
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/timers.h" #include "freertos/timers.h"
} }
@ -26,45 +25,36 @@ AutoConnectConfig Config;
// WiFi.config(Config); // WiFi.config(Config);
MQTTClient mqttClient; MQTTClient mqttClient;
void connectWiFi() void connectWiFi() {
{
Serial.println("Start WiFi..."); Serial.println("Start WiFi...");
if (Portal.begin()) if (Portal.begin()) {
{
digitalWrite(PIN_LED_G, HIGH); digitalWrite(PIN_LED_G, HIGH);
} }
} }
void mqttLoop(void *parameter) void mqttLoop(void *parameter) {
{
bool x; bool x;
do { do {
x = mqttClient.loop(); x = mqttClient.loop();
delay(50); delay(50);
} while (mqttClient.connected()); } while (mqttClient.connected());
if (!mqttClient.connected()) if (!mqttClient.connected()) {
{
Serial.println("Disconnected from MQTT."); Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) if (WiFi.isConnected()) {
{
xTimerStart(mqttReconnectTimer, 0); xTimerStart(mqttReconnectTimer, 0);
} }
} }
vTaskDelete(NULL); vTaskDelete(NULL);
} }
void connectMQTT() void connectMQTT() {
{
Serial.println("Connecting to MQTT..."); Serial.println("Connecting to MQTT...");
mqttClient.begin(MQTT_HOST, MQTT_PORT, client); mqttClient.begin(MQTT_HOST, MQTT_PORT, client);
mqttClient.connect(MQTT_DEVICE_ID); mqttClient.connect(MQTT_DEVICE_ID);
if (mqttClient.connected()) if (mqttClient.connected()) {
{
Serial.println("Connected!"); Serial.println("Connected!");
} } else {
else
{
Serial.println("NOT Connected!"); Serial.println("NOT Connected!");
} }
mqttClient.subscribe(MQTT_PATH_SUB, 2); mqttClient.subscribe(MQTT_PATH_SUB, 2);
@ -80,67 +70,57 @@ void connectMQTT()
//xTimerStart(mqttProcessingTimer, 0); //xTimerStart(mqttProcessingTimer, 0);
} }
void WiFiEvent(WiFiEvent_t event) void WiFiEvent(WiFiEvent_t event) {
{
Serial.printf("[WiFi-event] event: %d\n", event); Serial.printf("[WiFi-event] event: %d\n", event);
switch (event) switch (event) {
{ case SYSTEM_EVENT_STA_GOT_IP:
case SYSTEM_EVENT_STA_GOT_IP: Serial.println("WiFi connected");
Serial.println("WiFi connected"); Serial.println("IP address: ");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
Serial.println(WiFi.localIP()); connectMQTT();
connectMQTT(); break;
break; case SYSTEM_EVENT_STA_DISCONNECTED:
case SYSTEM_EVENT_STA_DISCONNECTED: digitalWrite(PIN_LED_G, LOW);
digitalWrite(PIN_LED_G, LOW); xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi // xTimerStop(mqttProcessingTimer, 0);
// xTimerStop(mqttProcessingTimer, 0); xTimerStart(wifiReconnectTimer, 0);
xTimerStart(wifiReconnectTimer, 0); break;
break;
} }
} }
void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload_length) void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload_length) {
{
Serial.print("Message arrived ["); Serial.print("Message arrived [");
Serial.print(topic); Serial.print(topic);
Serial.print("] "); Serial.print("] ");
for (int i = 0; i < payload_length; i++) for (int i = 0; i < payload_length; i++) {
{
Serial.print((char)payload[i]); Serial.print((char)payload[i]);
} }
Serial.println(); Serial.println();
if (strcmp(topic, MQTT_VALVE_COMMAND) == 0) if (strcmp(topic, MQTT_VALVE_COMMAND) == 0) {
{
Serial.println("toggling valve..."); Serial.println("toggling valve...");
Serial.println(topic); Serial.println(topic);
toggleValve(); toggleValve();
} }
if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) {
{
Serial.println("receiving soil thresholds..."); Serial.println("receiving soil thresholds...");
Serial.println(topic); Serial.println(topic);
Serial.println(payload); Serial.println(payload);
// const int capacity = JSON_OBJECT_SIZE(3) + 2 * JSON_OBJECT_SIZE(1); // const int capacity = JSON_OBJECT_SIZE(3) + 2 * JSON_OBJECT_SIZE(1);
StaticJsonDocument<1024> doc; StaticJsonDocument<1024> doc;
DeserializationError err = deserializeJson(doc, payload); DeserializationError err = deserializeJson(doc, payload);
if (err == DeserializationError::Ok) if (err == DeserializationError::Ok) {
{
int fc = doc["fc"]; int fc = doc["fc"];
int pwp = doc["pwp"]; int pwp = doc["pwp"];
int sat = doc["sat"]; int sat = doc["sat"];
setSoilProperties(fc, pwp, sat); setSoilProperties(fc, pwp, sat);
} } else {
else
{
Serial.println(err.c_str()); Serial.println(err.c_str());
} }
} }
} }
void setupConnections() void setupConnections() {
{
Serial.println(); Serial.println();
Serial.println(); Serial.println();
@ -158,7 +138,6 @@ void setupConnections()
connectWiFi(); connectWiFi();
} }
void publishMessage(const char *topic, const char *msg) void publishMessage(const char *topic, const char *msg) {
{
mqttClient.publish(topic, msg, true, 1); mqttClient.publish(topic, msg, true, 1);
} }

View File

@ -3,13 +3,12 @@
BH1750 lightMeter; BH1750 lightMeter;
void setupLightSensor() { void setupLightSensor() {
Wire.begin(); Wire.begin();
lightMeter.begin(); lightMeter.begin();
Serial.println("Sensor started..."); Serial.println("Sensor started...");
} }
float readLightSensorValue() float readLightSensorValue() {
{ float intensity = lightMeter.readLightLevel();
float intensity = lightMeter.readLightLevel(); return intensity;
return intensity;
} }

View File

@ -3,13 +3,13 @@
*/ */
#include <header.h> #include <header.h>
#include <string> #include <string>
unsigned long sensorReadTimer = 0; unsigned long sensorReadTimer = 0;
bool valveOpen = false; bool valveOpen = false;
void setup() void setup() {
{
Serial.begin(115200); Serial.begin(115200);
sensorReadTimer = millis(); sensorReadTimer = millis();
@ -29,17 +29,13 @@ void setup()
Serial.println(); Serial.println();
} }
void loop() void loop() {
{
// read sensors // read sensors
if (millis() - sensorReadTimer >= FREQUENCY) if (millis() - sensorReadTimer >= FREQUENCY) {
{
readSensors(); readSensors();
sensorReadTimer = millis(); sensorReadTimer = millis();
} }
// toggle valve // toggle valve
if (valveOpen) { if (valveOpen) {
} }
} }

View File

@ -1,8 +1,8 @@
#include <header.h> #include <header.h>
#include <string> #include <string>
//using namespace std; //using namespace std;
extern "C" extern "C" {
{
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
} }
@ -21,8 +21,7 @@ int permanentWiltingPoint = 25;
// Boden vollständig gesättigt bei (Prozent): Standard ist Humus // Boden vollständig gesättigt bei (Prozent): Standard ist Humus
int soilSaturation = 69; int soilSaturation = 69;
void readSensors() void readSensors() {
{
float lxValue = readLightSensorValue(); float lxValue = readLightSensorValue();
Serial.print("Light intensity: "); Serial.print("Light intensity: ");
Serial.print(lxValue); Serial.print(lxValue);
@ -64,18 +63,15 @@ bool closeValve() {
return false; return false;
} }
void valveTask(void *parameter) void valveTask(void *parameter) {
{
unsigned long valveTimeoutTimer = millis(); unsigned long valveTimeoutTimer = millis();
bool valveOpen = openValve(); bool valveOpen = openValve();
while (valveOpen) while (valveOpen) {
{
delay(500); delay(500);
int mstValue = readCapacitiveSoilMoistureSensor(); int mstValue = readCapacitiveSoilMoistureSensor();
if (millis() - valveTimeoutTimer >= MAX_VALVE_TIMEOUT) if (millis() - valveTimeoutTimer >= MAX_VALVE_TIMEOUT) {
{
valveOpen = closeValve(); valveOpen = closeValve();
} }
// if (mstValue > 80) { // if (mstValue > 80) {
@ -85,8 +81,7 @@ void valveTask(void *parameter)
vTaskDelete(NULL); vTaskDelete(NULL);
} }
void toggleValve() void toggleValve() {
{
xTaskCreate( xTaskCreate(
valveTask, /* Task function. */ valveTask, /* Task function. */
"valveTask", /* String with name of task. */ "valveTask", /* String with name of task. */