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

View File

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

View File

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

View File

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