arduino-sketches – Diff between revs 4 and 8
?pathlinks?
Rev 4 | Rev 8 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | /*************************************************************************/ |
1 | /*************************************************************************/ |
|
2 | /* Copyright (C) 2020 Wizardry and Steamworks - License: GNU GPLv3 */ |
2 | /* Copyright (C) 2022 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
3 | /*************************************************************************/ |
3 | /*************************************************************************/ |
|
Line 4... | Line 4... | |||
4 | |
4 | |
|
5 | // The AP to connect to via Wifi. |
5 | // The AP to connect to via Wifi. |
|
6 | #define STA_SSID "" |
6 | #define STA_SSID "" |
|
Line 75... | Line 75... | |||
75 | 23, 25, 26, 27, 32, 33, 34, 35, 36, 37, |
75 | 23, 25, 26, 27, 32, 33, 34, 35, 36, 37, |
|
76 | 38, 39 |
76 | 38, 39 |
|
77 | }; |
77 | }; |
|
78 | #endif |
78 | #endif |
|
Line 79... | Line 79... | |||
79 | |
79 | |
|
80 | const char* mqttSerialize(StaticJsonDocument<256> msg) { |
80 | String mqttSerialize(StaticJsonDocument<256> msg) { |
|
81 | char message[256]; |
81 | char output[256]; |
|
82 | serializeJson(msg, message); |
82 | serializeJson(msg, output, 256); |
|
83 | return (const char*) message; |
83 | return String(output); |
|
Line 84... | Line 84... | |||
84 | } |
84 | } |
|
85 | |
85 | |
|
86 | void mqttCallback(char *topic, byte *payload, unsigned int length) { |
86 | void mqttCallback(char *topic, byte *payload, unsigned int length) { |
|
Line 97... | Line 97... | |||
97 | if (error) { |
97 | if (error) { |
|
98 | Serial.println("Failed to parse MQTT payload as JSON: " + String(error.c_str())); |
98 | Serial.println("Failed to parse MQTT payload as JSON: " + String(error.c_str())); |
|
99 | return; |
99 | return; |
|
100 | } |
100 | } |
|
Line 101... | Line 101... | |||
101 | |
101 | |
|
102 | // Do not process messages with an action key. |
102 | // Do not process messages without an action key. |
|
103 | if (doc.containsKey("action")) { |
103 | if (!doc.containsKey("action")) { |
|
104 | return; |
104 | return; |
|
Line -... | Line 105... | |||
- | 105 | } |
||
- | 106 | |
||
105 | } |
107 | String action = (const char *)doc["action"]; |
|
106 | |
108 | if(action == "set") { |
|
- | 109 | String state = (const char *)doc["state"]; |
||
- | 110 | const int pin = (const int)doc["pin"]; |
||
- | 111 | |
||
- | 112 | Serial.println("Setting pin: " + String(pin) + " to state: " + String(state)); |
||
- | 113 | |
||
- | 114 | pinMode(PINS[pin], OUTPUT); |
||
- | 115 | |
||
- | 116 | if (state == "on") { |
||
- | 117 | digitalWrite(PINS[pin], HIGH); |
||
- | 118 | int status = digitalRead(PINS[pin]); |
||
- | 119 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
||
Line 107... | Line -... | |||
107 | String state = (const char *)doc["state"]; |
- | ||
108 | const int pin = (const int)doc["pin"]; |
- | ||
109 | |
- | ||
110 | Serial.println("Setting pin: " + String(pin) + " to state: " + String(state)); |
- | ||
111 | // Announce the action. |
- | ||
112 | StaticJsonDocument<256> msg; |
- | ||
113 | msg["pin"] = pin; |
- | ||
114 | msg["state"] = state; |
120 | return; |
|
115 | msg["action"] = "set"; |
- | ||
116 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg)); |
121 | } |
|
117 | pinMode(PINS[pin], OUTPUT); |
122 | |
|
118 | if (state == "on") { |
123 | digitalWrite(PINS[pin], LOW); |
|
119 | digitalWrite(PINS[pin], HIGH); |
124 | int status = digitalRead(PINS[pin]); |
|
Line -... | Line 125... | |||
- | 125 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
||
- | 126 | return; |
||
- | 127 | } |
||
- | 128 | |
||
- | 129 | if(action == "get") { |
||
120 | Serial.println("Pin set to HIGH"); |
130 | const int pin = (const int)doc["pin"]; |
|
- | 131 | |
||
- | 132 | Serial.println("Getting pin: " + String(pin) + " state."); |
||
- | 133 | |
||
- | 134 | int status = digitalRead(PINS[pin]); |
||
- | 135 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
||
- | 136 | |
||
- | 137 | // Announce the action. |
||
- | 138 | StaticJsonDocument<256> msg; |
||
- | 139 | msg["pin"] = pin; |
||
- | 140 | switch(status) { |
||
- | 141 | case 1: |
||
- | 142 | msg["state"] = "on"; |
||
- | 143 | break; |
||
121 | return; |
144 | case 0: |
|
- | 145 | msg["state"] = "off"; |
||
- | 146 | break; |
||
- | 147 | default: |
||
- | 148 | msg["state"] = "unknown"; |
||
- | 149 | break; |
||
- | 150 | } |
||
122 | } |
151 | |
|
Line 123... | Line 152... | |||
123 | |
152 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
|
124 | digitalWrite(PINS[pin], LOW); |
153 | return; |
|
125 | Serial.println("Pin set to LOW"); |
154 | } |
|
Line 126... | Line 155... | |||
126 | } |
155 | } |
|
127 | |
156 | |
|
128 | bool mqttConnect() { |
157 | bool mqttConnect() { |
|
129 | Serial.println("Attempting to connect to MQTT broker: " + String(mqtt_host)); |
158 | Serial.println("Attempting to connect to MQTT broker: " + String(mqtt_host)); |
|
130 | mqttClient.setServer(mqtt_host, mqtt_port); |
159 | mqttClient.setServer(mqtt_host, mqtt_port); |
|
131 | |
160 | |
|
132 | StaticJsonDocument<255> msg; |
161 | StaticJsonDocument<256> msg; |
|
133 | if (mqttClient.connect(MQTT_CLIENT_ID().c_str(), mqtt_username, mqtt_password)) { |
162 | if (mqttClient.connect(MQTT_CLIENT_ID().c_str(), mqtt_username, mqtt_password)) { |
|
134 | Serial.println("Established connection with MQTT broker using client ID: " + MQTT_CLIENT_ID()); |
163 | Serial.println("Established connection with MQTT broker using client ID: " + MQTT_CLIENT_ID()); |
|
135 | mqttClient.setCallback(mqttCallback); |
164 | mqttClient.setCallback(mqttCallback); |
|
136 | msg["action"] = "connected"; |
165 | msg["action"] = "connected"; |
|
137 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg)); |
166 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
|
138 | Serial.println("Attempting to subscribe to MQTT topic: " + MQTT_TOPIC()); |
167 | Serial.println("Attempting to subscribe to MQTT topic: " + MQTT_TOPIC()); |
|
139 | if (!mqttClient.subscribe(MQTT_TOPIC().c_str())) { |
168 | if (!mqttClient.subscribe(MQTT_TOPIC().c_str())) { |
|
140 | Serial.println("Failed to subscribe to MQTT topic: " + MQTT_TOPIC()); |
169 | Serial.println("Failed to subscribe to MQTT topic: " + MQTT_TOPIC()); |
|
141 | return false; |
170 | return false; |
|
142 | } |
171 | } |
|
143 | Serial.println("Subscribed to MQTT topic: " + MQTT_TOPIC()); |
172 | Serial.println("Subscribed to MQTT topic: " + MQTT_TOPIC()); |
|
144 | msg.clear(); |
173 | msg.clear(); |
|
145 | msg["action"] = "subscribed"; |
- | ||
Line 146... | Line 174... | |||
146 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg)); |
174 | msg["action"] = "subscribed"; |
|
147 | return true; |
175 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
|
Line 148... | Line 176... | |||
148 | } |
176 | return true; |
|
Line 299... | Line 327... | |||
299 | DEFAULT_CASE: |
327 | DEFAULT_CASE: |
|
300 | delay(10000); |
328 | delay(10000); |
|
301 | ESP.restart(); |
329 | ESP.restart(); |
|
302 | break; |
330 | break; |
|
303 | } |
331 | } |
|
304 | } |
332 | } |
|
305 | |
333 | |