/arduinoPinToggle/arduinoPinToggle.ino |
@@ -1,5 +1,5 @@ |
/*************************************************************************/ |
/* Copyright (C) 2020 Wizardry and Steamworks - License: GNU GPLv3 */ |
/* Copyright (C) 2022 Wizardry and Steamworks - License: GNU GPLv3 */ |
/*************************************************************************/ |
|
// The AP to connect to via Wifi. |
@@ -77,10 +77,10 @@ |
}; |
#endif |
|
const char* mqttSerialize(StaticJsonDocument<256> msg) { |
char message[256]; |
serializeJson(msg, message); |
return (const char*) message; |
String mqttSerialize(StaticJsonDocument<256> msg) { |
char output[256]; |
serializeJson(msg, output, 256); |
return String(output); |
} |
|
void mqttCallback(char *topic, byte *payload, unsigned int length) { |
@@ -99,30 +99,59 @@ |
return; |
} |
|
// Do not process messages with an action key. |
if (doc.containsKey("action")) { |
// Do not process messages without an action key. |
if (!doc.containsKey("action")) { |
return; |
} |
|
String state = (const char *)doc["state"]; |
const int pin = (const int)doc["pin"]; |
String action = (const char *)doc["action"]; |
if(action == "set") { |
String state = (const char *)doc["state"]; |
const int pin = (const int)doc["pin"]; |
|
Serial.println("Setting pin: " + String(pin) + " to state: " + String(state)); |
// Announce the action. |
StaticJsonDocument<256> msg; |
msg["pin"] = pin; |
msg["state"] = state; |
msg["action"] = "set"; |
mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg)); |
pinMode(PINS[pin], OUTPUT); |
if (state == "on") { |
digitalWrite(PINS[pin], HIGH); |
Serial.println("Pin set to HIGH"); |
return; |
Serial.println("Setting pin: " + String(pin) + " to state: " + String(state)); |
|
pinMode(PINS[pin], OUTPUT); |
|
if (state == "on") { |
digitalWrite(PINS[pin], HIGH); |
int status = digitalRead(PINS[pin]); |
Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
return; |
} |
|
digitalWrite(PINS[pin], LOW); |
int status = digitalRead(PINS[pin]); |
Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
return; |
} |
|
digitalWrite(PINS[pin], LOW); |
Serial.println("Pin set to LOW"); |
if(action == "get") { |
const int pin = (const int)doc["pin"]; |
|
Serial.println("Getting pin: " + String(pin) + " state."); |
|
int status = digitalRead(PINS[pin]); |
Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
|
// Announce the action. |
StaticJsonDocument<256> msg; |
msg["pin"] = pin; |
switch(status) { |
case 1: |
msg["state"] = "on"; |
break; |
case 0: |
msg["state"] = "off"; |
break; |
default: |
msg["state"] = "unknown"; |
break; |
} |
|
mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
return; |
} |
} |
|
bool mqttConnect() { |
@@ -129,12 +158,12 @@ |
Serial.println("Attempting to connect to MQTT broker: " + String(mqtt_host)); |
mqttClient.setServer(mqtt_host, mqtt_port); |
|
StaticJsonDocument<255> msg; |
StaticJsonDocument<256> msg; |
if (mqttClient.connect(MQTT_CLIENT_ID().c_str(), mqtt_username, mqtt_password)) { |
Serial.println("Established connection with MQTT broker using client ID: " + MQTT_CLIENT_ID()); |
mqttClient.setCallback(mqttCallback); |
msg["action"] = "connected"; |
mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg)); |
mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
Serial.println("Attempting to subscribe to MQTT topic: " + MQTT_TOPIC()); |
if (!mqttClient.subscribe(MQTT_TOPIC().c_str())) { |
Serial.println("Failed to subscribe to MQTT topic: " + MQTT_TOPIC()); |
@@ -143,12 +172,11 @@ |
Serial.println("Subscribed to MQTT topic: " + MQTT_TOPIC()); |
msg.clear(); |
msg["action"] = "subscribed"; |
mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg)); |
mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
return true; |
} |
else { |
Serial.println("Connection to MQTT broker failed with MQTT client state: " + String(mqttClient.state())); |
} |
|
Serial.println("Connection to MQTT broker failed with MQTT client state: " + String(mqttClient.state())); |
|
return false; |
} |
@@ -301,4 +329,4 @@ |
ESP.restart(); |
break; |
} |
} |
} |