2
0
Fork 0

gym lock initial implementation

This commit is contained in:
Siwat Sirichai 2024-06-22 15:42:42 +07:00
parent 0713974488
commit 7edd4f6b48
4 changed files with 80 additions and 1 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/settings.json

View File

@ -29,6 +29,11 @@
#define GIRLS_TOILET_LIGHT_RELAY_PIN 6
#define BOYS_TOILET_LIGHT_RELAY_PIN 7
/***********************************************
* IoT Configuration *
***********************************************/
#define TOPIC_TOILET_LIGHT_LOCK "/toiletlight/lock"
#define TOPIC_GYM_LIGHT_LOCK "/gymlight/lock"
/***********************************************
* Display Configuration *

View File

@ -8,6 +8,8 @@ Button rightOnButton(&espmega.inputs, RIGHT_ON_BUTTON_PIN);
Button rightOffButton(&espmega.inputs, RIGHT_OFF_BUTTON_PIN);
Button leftOnButton(&espmega.inputs, LEFT_ON_BUTTON_PIN);
Button leftOffButton(&espmega.inputs, LEFT_OFF_BUTTON_PIN);
SmartVariable toiletLightsLock;
SmartVariable gymLightsLock;
/************************************************
* End of Global Variables *
@ -66,9 +68,38 @@ void setup()
espmega.iot->registerMqttCallback(handle_mqtt_message);
internalDisplayOTA.begin("/intdisp", espmega.display, espmega.webServer);
button_init();
smartvar_init();
ESP_LOGI("CUD IoT OS", "Initialization Complete");
}
void smartvar_init() {
// Initialize Smart Variables
ESP_LOGV("CUD IoT OS", "Initializing Smart Variables");
toiletLightsLock.begin(5);
// Construct the topic
char* toilet_lock_topic = (char*)malloc(strlen(TOPIC_TOILET_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 5);
sprintf(toilet_lock_topic, "%s%s", espmega.iot->getMqttConfig()->base_topic, TOPIC_TOILET_LIGHT_LOCK);
char* toilet_lock_set_topic = (char*)malloc(strlen(TOPIC_TOILET_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 15);
sprintf(toilet_lock_set_topic, "%s%s/set", espmega.iot->getMqttConfig()->base_topic, TOPIC_TOILET_LIGHT_LOCK);
char* toilet_lock_request_topic = (char*)malloc(strlen(TOPIC_TOILET_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 15);
sprintf(toilet_lock_request_topic, "%s%s/request", espmega.iot->getMqttConfig()->base_topic, TOPIC_TOILET_LIGHT_LOCK);
toiletLightsLock.enableIoT(espmega.iot, toilet_lock_topic);
toiletLightsLock.enableSetValue(toilet_lock_set_topic);
toiletLightsLock.enableValueRequest(toilet_lock_request_topic);
gymLightsLock.begin(5);
// Construct the topic
char* gym_lock_topic = (char*)malloc(strlen(TOPIC_GYM_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 5);
sprintf(gym_lock_topic, "%s%s", espmega.iot->getMqttConfig()->base_topic, TOPIC_GYM_LIGHT_LOCK);
char* gym_lock_set_topic = (char*)malloc(strlen(TOPIC_GYM_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 15);
sprintf(gym_lock_set_topic, "%s%s/set", espmega.iot->getMqttConfig()->base_topic, TOPIC_GYM_LIGHT_LOCK);
char* gym_lock_request_topic = (char*)malloc(strlen(TOPIC_GYM_LIGHT_LOCK) + strlen(espmega.iot->getMqttConfig()->base_topic) + 15);
sprintf(gym_lock_request_topic, "%s%s/request", espmega.iot->getMqttConfig()->base_topic, TOPIC_GYM_LIGHT_LOCK);
gymLightsLock.enableIoT(espmega.iot, gym_lock_topic);
gymLightsLock.enableSetValue(gym_lock_set_topic);
gymLightsLock.enableValueRequest(gym_lock_request_topic);
}
void button_init()
{
// Initialize buttons
@ -78,10 +109,14 @@ void button_init()
rightOnButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS);
rightOnButton.setShortPressCallback([]()
{
if (get_gym_light_lock())
return;
// Turn on right light
espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH); });
rightOnButton.setLongPressCallback([]()
{
if (get_gym_light_lock())
return;
// Turn on both lights
espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH);
espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); });
@ -92,10 +127,14 @@ void button_init()
rightOffButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS);
rightOffButton.setShortPressCallback([]()
{
if (get_gym_light_lock())
return;
// Turn off right light
espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, LOW); });
rightOffButton.setLongPressCallback([]()
{
if (get_gym_light_lock())
return;
// Turn off both lights
espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, LOW);
espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, LOW); });
@ -106,10 +145,14 @@ void button_init()
leftOnButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS);
leftOnButton.setShortPressCallback([]()
{
if (get_gym_light_lock())
return;
// Turn on left light
espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); });
leftOnButton.setLongPressCallback([]()
{
if (get_gym_light_lock())
return;
// Turn on both lights
espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH);
espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); });
@ -151,18 +194,34 @@ void handle_input_change(uint8_t pin, bool state)
switch (pin)
{
case RIGHT_STADIUM_LIGHT_SWITCH_PIN:
if (get_gym_light_lock())
{
return;
}
espmega.outputs.digitalWrite(RIGHT_STADIUM_LIGHT_RELAY_PIN,
!espmega.outputs.getState(RIGHT_STADIUM_LIGHT_RELAY_PIN));
break;
case LEFT_STADIUM_LIGHT_SWITCH_PIN:
if (get_gym_light_lock())
{
return;
}
espmega.outputs.digitalWrite(LEFT_STADIUM_LIGHT_RELAY_PIN,
!espmega.outputs.getState(LEFT_STADIUM_LIGHT_RELAY_PIN));
break;
case GIRLS_TOILET_LIGHT_SWITCH_PIN:
if (get_toilet_light_lock())
{
return;
}
espmega.outputs.digitalWrite(GIRLS_TOILET_LIGHT_RELAY_PIN,
!espmega.outputs.getState(GIRLS_TOILET_LIGHT_RELAY_PIN));
break;
case BOYS_TOILET_LIGHT_SWITCH_PIN:
if (get_toilet_light_lock())
{
return;
}
espmega.outputs.digitalWrite(BOYS_TOILET_LIGHT_RELAY_PIN,
!espmega.outputs.getState(BOYS_TOILET_LIGHT_RELAY_PIN));
break;
@ -171,6 +230,16 @@ void handle_input_change(uint8_t pin, bool state)
}
}
bool get_toilet_light_lock()
{
return toiletLightsLock.getIntValue() == 1;
}
bool get_gym_light_lock()
{
return gymLightsLock.getIntValue() == 1;
}
void handle_mqtt_message(char *topic, char *payload)
{
// Do nothing

View File

@ -12,6 +12,7 @@
#include <ESPMegaProOS.hpp>
#include <ETH.h>
#include <ESPMegaDisplayOTA.hpp>
#include <SmartVariable.hpp>
#include "button.hpp"
#include "config.hpp"
@ -23,4 +24,7 @@ void loop();
void send_stop_bytes(HardwareSerial &uart);
void handle_input_change(uint8_t pin, bool state);
void handle_mqtt_message(char *topic, char *payload);
void button_init();
void button_init();
void smartvar_init();
bool get_toilet_light_lock();
bool get_gym_light_lock();