implemented valve control tasks
This commit is contained in:
parent
8ad568ea1b
commit
4d685c8edf
@ -57,19 +57,13 @@ void WiFiEvent(WiFiEvent_t event)
|
|||||||
void onMqttConnect(bool sessionPresent)
|
void onMqttConnect(bool sessionPresent)
|
||||||
{
|
{
|
||||||
Serial.println("Connected to MQTT.");
|
Serial.println("Connected to MQTT.");
|
||||||
|
|
||||||
Serial.print("Session present: ");
|
Serial.print("Session present: ");
|
||||||
Serial.println(sessionPresent);
|
Serial.println(sessionPresent);
|
||||||
uint16_t packetIdSub = mqttClient.subscribe("smartgarden/test", 2);
|
|
||||||
Serial.print("Subscribing at QoS 2, packetId: ");
|
uint16_t packetIdSub = mqttClient.subscribe(MQTT_PATH_SUB, 2);
|
||||||
Serial.println(packetIdSub);
|
Serial.print("subscribed to: ");
|
||||||
mqttClient.publish("test/lol", 0, true, "test 1");
|
Serial.println(MQTT_PATH_SUB);
|
||||||
Serial.println("Publishing at QoS 0");
|
|
||||||
uint16_t packetIdPub1 = mqttClient.publish("smartgarden/test", 1, true, "test 2");
|
|
||||||
Serial.print("Publishing at QoS 1, packetId: ");
|
|
||||||
Serial.println(packetIdPub1);
|
|
||||||
uint16_t packetIdPub2 = mqttClient.publish("smartgarden/test", 2, true, "test 3");
|
|
||||||
Serial.print("Publishing at QoS 2, packetId: ");
|
|
||||||
Serial.println(packetIdPub2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
||||||
@ -84,9 +78,9 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
|||||||
|
|
||||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos)
|
void onMqttSubscribe(uint16_t packetId, uint8_t qos)
|
||||||
{
|
{
|
||||||
Serial.println("Subscribe acknowledged.");
|
Serial.print("Subscribe acknowledged:");
|
||||||
Serial.print(" packetId: ");
|
Serial.print(" packetId: ");
|
||||||
Serial.println(packetId);
|
Serial.print(packetId);
|
||||||
Serial.print(" qos: ");
|
Serial.print(" qos: ");
|
||||||
Serial.println(qos);
|
Serial.println(qos);
|
||||||
}
|
}
|
||||||
@ -100,33 +94,26 @@ void onMqttUnsubscribe(uint16_t packetId)
|
|||||||
|
|
||||||
void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
|
void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
|
||||||
{
|
{
|
||||||
Serial.println("Publish received.");
|
Serial.print("Publish received: ");
|
||||||
Serial.print(" topic: ");
|
Serial.print(" topic: ");
|
||||||
Serial.println(topic);
|
Serial.println(topic);
|
||||||
Serial.print(" qos: ");
|
|
||||||
Serial.println(properties.qos);
|
if (strcmp(topic, "smartgarden/commands") == 0 ) {
|
||||||
Serial.print(" dup: ");
|
Serial.println("executing command...");
|
||||||
Serial.println(properties.dup);
|
Serial.println(topic);
|
||||||
Serial.print(" retain: ");
|
toggleValve();
|
||||||
Serial.println(properties.retain);
|
}
|
||||||
Serial.print(" len: ");
|
|
||||||
Serial.println(len);
|
|
||||||
Serial.print(" index: ");
|
|
||||||
Serial.println(index);
|
|
||||||
Serial.print(" total: ");
|
|
||||||
Serial.println(total);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onMqttPublish(uint16_t packetId)
|
void onMqttPublish(uint16_t packetId)
|
||||||
{
|
{
|
||||||
Serial.println("Publish acknowledged.");
|
Serial.print("Publish acknowledged: ");
|
||||||
Serial.print(" packetId: ");
|
Serial.print(" packetId: ");
|
||||||
Serial.println(packetId);
|
Serial.println(packetId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupConnections()
|
void setupConnections()
|
||||||
{
|
{
|
||||||
pinMode(PIN_LED_G, OUTPUT);
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
|
|||||||
@ -30,7 +30,8 @@
|
|||||||
#define VALUE_AIR 3500
|
#define VALUE_AIR 3500
|
||||||
|
|
||||||
// Ventil
|
// Ventil
|
||||||
#define PIN_VENTIL 26
|
#define PIN_VALVE 32
|
||||||
|
#define MAX_VALVE_TIMEOUT 10000
|
||||||
|
|
||||||
// LED
|
// LED
|
||||||
#define PIN_LED_R 2
|
#define PIN_LED_R 2
|
||||||
@ -44,6 +45,8 @@
|
|||||||
#define MQTT_TOPIC_BASE_SUB "smartgarden/commands"
|
#define MQTT_TOPIC_BASE_SUB "smartgarden/commands"
|
||||||
#define MQTT_TOPIC_BASE_PUB "smartgarden/updates"
|
#define MQTT_TOPIC_BASE_PUB "smartgarden/updates"
|
||||||
#define MQTT_PATH_PUB MQTT_TOPIC_BASE_PUB "/" MQTT_DEVICE_ID "/"
|
#define MQTT_PATH_PUB MQTT_TOPIC_BASE_PUB "/" MQTT_DEVICE_ID "/"
|
||||||
|
#define MQTT_PATH_SUB MQTT_TOPIC_BASE_SUB "/#"
|
||||||
|
// MQTT_DEVICE_ID "/#"
|
||||||
|
|
||||||
// PUBLISH FREQUENCY (MS)
|
// PUBLISH FREQUENCY (MS)
|
||||||
#define FREQUENCY 3000
|
#define FREQUENCY 3000
|
||||||
@ -64,3 +67,7 @@ extern float readTemperature();
|
|||||||
// mqtt & wifi
|
// mqtt & wifi
|
||||||
extern void setupConnections();
|
extern void setupConnections();
|
||||||
extern void publishMessage(const char *topic, const char *msg);
|
extern void publishMessage(const char *topic, const char *msg);
|
||||||
|
|
||||||
|
// sensors
|
||||||
|
void readSensors();
|
||||||
|
void toggleValve();
|
||||||
55
src/main.cpp
55
src/main.cpp
@ -4,20 +4,22 @@
|
|||||||
|
|
||||||
#include <header.h>
|
#include <header.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
//using namespace std;
|
|
||||||
|
|
||||||
#define MQTT_MOISTURE MQTT_PATH_PUB "moisture"
|
unsigned long sensorReadTimer = 0;
|
||||||
#define MQTT_TEMPERATURE MQTT_PATH_PUB "temperature"
|
bool valveOpen = false;
|
||||||
#define MQTT_HUMIDITY MQTT_PATH_PUB "humidity"
|
|
||||||
#define MQTT_BRIGHTNESS MQTT_PATH_PUB "brightness"
|
|
||||||
|
|
||||||
unsigned long pingTimer = 0;
|
|
||||||
char buffer[16];
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
pingTimer = millis();
|
sensorReadTimer = millis();
|
||||||
|
|
||||||
|
pinMode(PIN_VALVE, OUTPUT);
|
||||||
|
pinMode(PIN_LED_R, OUTPUT);
|
||||||
|
pinMode(PIN_LED_G, OUTPUT);
|
||||||
|
pinMode(PIN_LED_B, OUTPUT);
|
||||||
|
|
||||||
|
digitalWrite(PIN_VALVE, LOW);
|
||||||
|
|
||||||
setupConnections();
|
setupConnections();
|
||||||
setupLightSensor();
|
setupLightSensor();
|
||||||
setupTemperatureSensor();
|
setupTemperatureSensor();
|
||||||
@ -29,34 +31,15 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
if (millis() - pingTimer >= FREQUENCY)
|
// read sensors
|
||||||
|
if (millis() - sensorReadTimer >= FREQUENCY)
|
||||||
{
|
{
|
||||||
float lxValue = readLightSensorValue();
|
readSensors();
|
||||||
Serial.print("Light intensity: ");
|
sensorReadTimer = millis();
|
||||||
Serial.print(lxValue);
|
}
|
||||||
Serial.println(" lx");
|
// toggle valve
|
||||||
sprintf(buffer, "%f", lxValue);
|
if (valveOpen) {
|
||||||
publishMessage(MQTT_BRIGHTNESS, buffer);
|
|
||||||
|
|
||||||
int mstValue = readCapacitiveSoilMoistureSensor();
|
|
||||||
Serial.print("Soil moisture: ");
|
|
||||||
Serial.println(mstValue);
|
|
||||||
sprintf(buffer, "%i", mstValue);
|
|
||||||
publishMessage(MQTT_MOISTURE, buffer);
|
|
||||||
|
|
||||||
float humidityValue = readHumidity();
|
|
||||||
Serial.print("Humidity: ");
|
|
||||||
Serial.println(humidityValue);
|
|
||||||
sprintf(buffer, "%f", humidityValue);
|
|
||||||
publishMessage(MQTT_HUMIDITY, buffer);
|
|
||||||
|
|
||||||
float temperatureValue = readTemperature();
|
|
||||||
Serial.print("Temperature: ");
|
|
||||||
Serial.println(temperatureValue);
|
|
||||||
sprintf(buffer, "%f", temperatureValue);
|
|
||||||
publishMessage(MQTT_TEMPERATURE, buffer);
|
|
||||||
Serial.print("\n");
|
|
||||||
|
|
||||||
pingTimer = millis();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
90
src/peripherals.cpp
Normal file
90
src/peripherals.cpp
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#include <header.h>
|
||||||
|
#include <string>
|
||||||
|
//using namespace std;
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MQTT_MOISTURE MQTT_PATH_PUB "moisture"
|
||||||
|
#define MQTT_TEMPERATURE MQTT_PATH_PUB "temperature"
|
||||||
|
#define MQTT_HUMIDITY MQTT_PATH_PUB "humidity"
|
||||||
|
#define MQTT_BRIGHTNESS MQTT_PATH_PUB "brightness"
|
||||||
|
|
||||||
|
char buffer[16];
|
||||||
|
|
||||||
|
void readSensors()
|
||||||
|
{
|
||||||
|
float lxValue = readLightSensorValue();
|
||||||
|
Serial.print("Light intensity: ");
|
||||||
|
Serial.print(lxValue);
|
||||||
|
Serial.println(" lx");
|
||||||
|
sprintf(buffer, "%f", lxValue);
|
||||||
|
publishMessage(MQTT_BRIGHTNESS, buffer);
|
||||||
|
|
||||||
|
int mstValue = readCapacitiveSoilMoistureSensor();
|
||||||
|
Serial.print("Soil moisture: ");
|
||||||
|
Serial.println(mstValue);
|
||||||
|
sprintf(buffer, "%i", mstValue);
|
||||||
|
publishMessage(MQTT_MOISTURE, buffer);
|
||||||
|
|
||||||
|
float humidityValue = readHumidity();
|
||||||
|
Serial.print("Humidity: ");
|
||||||
|
Serial.println(humidityValue);
|
||||||
|
sprintf(buffer, "%f", humidityValue);
|
||||||
|
publishMessage(MQTT_HUMIDITY, buffer);
|
||||||
|
|
||||||
|
float temperatureValue = readTemperature();
|
||||||
|
Serial.print("Temperature: ");
|
||||||
|
Serial.println(temperatureValue);
|
||||||
|
sprintf(buffer, "%f", temperatureValue);
|
||||||
|
publishMessage(MQTT_TEMPERATURE, buffer);
|
||||||
|
Serial.print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool openValve() {
|
||||||
|
digitalWrite(PIN_VALVE, HIGH);
|
||||||
|
digitalWrite(PIN_LED_G, LOW);
|
||||||
|
digitalWrite(PIN_LED_B, HIGH);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool closeValve() {
|
||||||
|
digitalWrite(PIN_VALVE, LOW);
|
||||||
|
digitalWrite(PIN_LED_G, HIGH);
|
||||||
|
digitalWrite(PIN_LED_B, LOW);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void valveTask(void *parameter)
|
||||||
|
{
|
||||||
|
unsigned long valveTimeoutTimer = millis();
|
||||||
|
bool valveOpen = openValve();
|
||||||
|
|
||||||
|
while (valveOpen)
|
||||||
|
{
|
||||||
|
delay(500);
|
||||||
|
int mstValue = readCapacitiveSoilMoistureSensor();
|
||||||
|
|
||||||
|
if (millis() - valveTimeoutTimer >= MAX_VALVE_TIMEOUT)
|
||||||
|
{
|
||||||
|
valveOpen = closeValve();
|
||||||
|
}
|
||||||
|
// if (mstValue > 80) {
|
||||||
|
// valveOpen = closeValve();
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void toggleValve()
|
||||||
|
{
|
||||||
|
xTaskCreate(
|
||||||
|
valveTask, /* Task function. */
|
||||||
|
"valveTask", /* String with name of task. */
|
||||||
|
10000, /* Stack size in bytes. */
|
||||||
|
NULL, /* Parameter passed as input of the task */
|
||||||
|
1, /* Priority of the task. */
|
||||||
|
NULL); /* Task handle. */
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user