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)
|
||||
{
|
||||
Serial.println("Connected to MQTT.");
|
||||
|
||||
Serial.print("Session present: ");
|
||||
Serial.println(sessionPresent);
|
||||
uint16_t packetIdSub = mqttClient.subscribe("smartgarden/test", 2);
|
||||
Serial.print("Subscribing at QoS 2, packetId: ");
|
||||
Serial.println(packetIdSub);
|
||||
mqttClient.publish("test/lol", 0, true, "test 1");
|
||||
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);
|
||||
|
||||
uint16_t packetIdSub = mqttClient.subscribe(MQTT_PATH_SUB, 2);
|
||||
Serial.print("subscribed to: ");
|
||||
Serial.println(MQTT_PATH_SUB);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
||||
@ -84,10 +78,10 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
||||
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos)
|
||||
{
|
||||
Serial.println("Subscribe acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
Serial.print(" qos: ");
|
||||
Serial.print("Subscribe acknowledged:");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.print(packetId);
|
||||
Serial.print(" 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)
|
||||
{
|
||||
Serial.println("Publish received.");
|
||||
Serial.print(" topic: ");
|
||||
Serial.print("Publish received: ");
|
||||
Serial.print(" topic: ");
|
||||
Serial.println(topic);
|
||||
Serial.print(" qos: ");
|
||||
Serial.println(properties.qos);
|
||||
Serial.print(" dup: ");
|
||||
Serial.println(properties.dup);
|
||||
Serial.print(" retain: ");
|
||||
Serial.println(properties.retain);
|
||||
Serial.print(" len: ");
|
||||
Serial.println(len);
|
||||
Serial.print(" index: ");
|
||||
Serial.println(index);
|
||||
Serial.print(" total: ");
|
||||
Serial.println(total);
|
||||
|
||||
if (strcmp(topic, "smartgarden/commands") == 0 ) {
|
||||
Serial.println("executing command...");
|
||||
Serial.println(topic);
|
||||
toggleValve();
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId)
|
||||
{
|
||||
Serial.println("Publish acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.print("Publish acknowledged: ");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
}
|
||||
|
||||
void setupConnections()
|
||||
{
|
||||
pinMode(PIN_LED_G, OUTPUT);
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
|
||||
|
||||
11
src/header.h
11
src/header.h
@ -30,7 +30,8 @@
|
||||
#define VALUE_AIR 3500
|
||||
|
||||
// Ventil
|
||||
#define PIN_VENTIL 26
|
||||
#define PIN_VALVE 32
|
||||
#define MAX_VALVE_TIMEOUT 10000
|
||||
|
||||
// LED
|
||||
#define PIN_LED_R 2
|
||||
@ -44,6 +45,8 @@
|
||||
#define MQTT_TOPIC_BASE_SUB "smartgarden/commands"
|
||||
#define MQTT_TOPIC_BASE_PUB "smartgarden/updates"
|
||||
#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)
|
||||
#define FREQUENCY 3000
|
||||
@ -63,4 +66,8 @@ extern float readTemperature();
|
||||
|
||||
// mqtt & wifi
|
||||
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();
|
||||
59
src/main.cpp
59
src/main.cpp
@ -4,20 +4,22 @@
|
||||
|
||||
#include <header.h>
|
||||
#include <string>
|
||||
//using namespace std;
|
||||
|
||||
#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"
|
||||
|
||||
unsigned long pingTimer = 0;
|
||||
char buffer[16];
|
||||
unsigned long sensorReadTimer = 0;
|
||||
bool valveOpen = false;
|
||||
|
||||
void setup()
|
||||
{
|
||||
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();
|
||||
setupLightSensor();
|
||||
setupTemperatureSensor();
|
||||
@ -29,34 +31,15 @@ void setup()
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (millis() - pingTimer >= FREQUENCY)
|
||||
// read sensors
|
||||
if (millis() - sensorReadTimer >= FREQUENCY)
|
||||
{
|
||||
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");
|
||||
|
||||
pingTimer = millis();
|
||||
readSensors();
|
||||
sensorReadTimer = millis();
|
||||
}
|
||||
}
|
||||
// toggle valve
|
||||
if (valveOpen) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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