Skip to content

Commit

Permalink
Merge branch 'main' into FixCompilerWarningsJSONParsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Martinius79 authored Nov 1, 2024
2 parents f5f0a4b + 1487152 commit e0d98ad
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 190 deletions.
3 changes: 2 additions & 1 deletion EleksTubeHAX_pio/src/Backlights.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Backlights: public Adafruit_NeoPixel {
void adjustIntensity(int16_t adj);
uint8_t getIntensity() { return config->intensity; }

bool dimming = false;
void setDimming(bool dim) { dimming = dim; pattern_needs_init = true; }

// Helper methods
uint32_t phaseToColor(uint16_t phase);
Expand All @@ -72,6 +72,7 @@ class Backlights: public Adafruit_NeoPixel {
const uint8_t max_intensity = 8; // 0 to 7

private:
bool dimming = false;
bool pattern_needs_init;
bool off;

Expand Down
115 changes: 50 additions & 65 deletions EleksTubeHAX_pio/src/Mqtt_client_ips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ double round1(double value) {
void sendToBroker(const char* topic, const char* message) {
if (MQTTclient.connected()) {
char topicArr[100];
sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic);
snprintf(topicArr, sizeof(topicArr), "%s/%s", MQTT_CLIENT, topic);
MQTTclient.publish(topicArr, message, true);
#ifdef DEBUG_OUTPUT // long output
Serial.print("Sending to MQTT: ");
Expand Down Expand Up @@ -220,7 +220,8 @@ void MqttReportState(bool force) {
MQTTclient.publish(topic, buffer, true);
LastSentBackPowerState = MqttStatusBackPower;
LastSentBackBrightness = MqttStatusBackBrightness;
strcpy(LastSentBackPattern, MqttStatusBackPattern);
strncpy(LastSentBackPattern, MqttStatusBackPattern, sizeof(LastSentBackPattern) - 1);
LastSentBackPattern[sizeof(LastSentBackPattern) - 1] = '\0';
LastSentBackColorPhase = MqttStatusBackColorPhase;

Serial.print("TX MQTT: ");
Expand Down Expand Up @@ -349,9 +350,9 @@ void MqttStart() {
}

#ifndef MQTT_HOME_ASSISTANT
char subscibeTopic[100];
sprintf(subscibeTopic, "%s/#", MQTT_CLIENT);
MQTTclient.subscribe(subscibeTopic); //Subscribes to all messages send to the device
char subscribeTopic[100];
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/#", MQTT_CLIENT);
MQTTclient.subscribe(subscribeTopic); //Subscribes to all messages send to the device

sendToBroker("report/online", "true"); // Reports that the device is online
sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version
Expand All @@ -361,27 +362,27 @@ void MqttStart() {
#endif

#ifdef MQTT_HOME_ASSISTANT
char subscibeTopic[100];
sprintf(subscibeTopic, "%s/main/set", MQTT_CLIENT);
MQTTclient.subscribe(subscibeTopic);
char subscribeTopic[100];
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/main/set", MQTT_CLIENT);
MQTTclient.subscribe(subscribeTopic);

sprintf(subscibeTopic, "%s/back/set", MQTT_CLIENT);
MQTTclient.subscribe(subscibeTopic);
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/back/set", MQTT_CLIENT);
MQTTclient.subscribe(subscribeTopic);

sprintf(subscibeTopic, "%s/use_twelve_hours/set", MQTT_CLIENT);
MQTTclient.subscribe(subscibeTopic);
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/use_twelve_hours/set", MQTT_CLIENT);
MQTTclient.subscribe(subscribeTopic);

sprintf(subscibeTopic, "%s/blank_zero_hours/set", MQTT_CLIENT);
MQTTclient.subscribe(subscibeTopic);
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/blank_zero_hours/set", MQTT_CLIENT);
MQTTclient.subscribe(subscribeTopic);

sprintf(subscibeTopic, "%s/pulse_bpm/set", MQTT_CLIENT);
MQTTclient.subscribe(subscibeTopic);
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/pulse_bpm/set", MQTT_CLIENT);
MQTTclient.subscribe(subscribeTopic);

sprintf(subscibeTopic, "%s/breath_bpm/set", MQTT_CLIENT);
MQTTclient.subscribe(subscibeTopic);
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/breath_bpm/set", MQTT_CLIENT);
MQTTclient.subscribe(subscribeTopic);

sprintf(subscibeTopic, "%s/rainbow_duration/set", MQTT_CLIENT);
MQTTclient.subscribe(subscibeTopic);
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/rainbow_duration/set", MQTT_CLIENT);
MQTTclient.subscribe(subscribeTopic);
#endif
}
#endif
Expand Down Expand Up @@ -423,31 +424,23 @@ void callback(char* topic, byte* payload, unsigned int length) { // A new messa
int commandNumber = 10;
char* command[commandNumber];
commandNumber = splitCommand(topic, command, commandNumber);

#ifndef MQTT_HOME_ASSISTANT

char message[length + 1];
sprintf(message, "%c", (char)payload[0]);
for (int i = 1; i < length; i++) {
sprintf(message, "%s%c", message, (char)payload[i]);
}
#ifdef DEBUG_OUTPUT
Serial.print("\t Message: ");
Serial.println(message);
#else
Serial.print("MQTT RX: ");
Serial.print(command[0]);
Serial.print("/");
Serial.print(command[1]);
Serial.print("/");
Serial.println(message);
#endif
strncpy(message, (char*)payload, length);
message[length] = '\0';

if (commandNumber < 2) {
// otherwise code below crashes on the strmp on non-initialized pointers in command[] array
Serial.println("Number of command in MQTT message < 2!");
return;
Serial.println("Detected number of commands in MQTT message is lower then 2! -> Ignoring message because it is not valid!");
return;
}


Serial.println();
Serial.print("RX MQTT: ");
Serial.print(topic);
Serial.print(" ");
Serial.println(message);

#ifndef MQTT_HOME_ASSISTANT
//------------------Decide what to do depending on the topic and message---------------------------------
if (strcmp(command[0], "directive") == 0 && strcmp(command[1], "powerState") == 0) { // Turn On or OFF
if (strcmp(message, "ON") == 0) {
Expand All @@ -467,15 +460,6 @@ void callback(char* topic, byte* payload, unsigned int length) { // A new messa
#endif

#ifdef MQTT_HOME_ASSISTANT
char message[length + 1];
sprintf(message, "%c", (char)payload[0]);
for (int i = 1; i < length; i++) {
sprintf(message, "%s%c", message, (char)payload[i]);
}
Serial.print("RX MQTT: ");
Serial.print(topic);
Serial.print(" ");
Serial.println(message);
if (strcmp(command[0], "main") == 0 && strcmp(command[1], "set") == 0) {
JsonDocument doc;
deserializeJson(doc, payload, length);
Expand Down Expand Up @@ -507,10 +491,11 @@ void callback(char* topic, byte* payload, unsigned int length) { // A new messa
MqttCommandBackBrightness = doc["brightness"];
MqttCommandBackBrightnessReceived = true;
}
if(doc["effect"].is<const char*>()) {
strcpy(MqttCommandBackPattern, doc["effect"]);
if(doc["effect"].is<const char*>()) {
strncpy(MqttCommandBackPattern, doc["effect"], sizeof(MqttCommandBackPattern) - 1);
MqttCommandBackPattern[sizeof(MqttCommandBackPattern) - 1] = '\0';
MqttCommandBackPatternReceived = true;
}
}
if(doc["color"].is<JsonObject>()) {
MqttCommandBackColorPhase = backlights.hueToPhase(doc["color"]["h"]);
MqttCommandBackColorPhaseReceived = true;
Expand Down Expand Up @@ -585,16 +570,16 @@ void MqttLoopInFreeTime(){
}

void MqttReportBattery() {
char message2[5];
sprintf(message2, "%d", MqttStatusBattery);
sendToBroker("report/battery", message2);
}
char message[5];
snprintf(message, sizeof(message), "%d", MqttStatusBattery);
sendToBroker("report/battery", message);
}

void MqttReportStatus() {
if (LastSentStatus != MqttStatusState) {
char message2[5];
sprintf(message2, "%d", MqttStatusState);
sendToBroker("report/setpoint", message2);
char message[5];
snprintf(message, sizeof(message), "%d", MqttStatusState);
sendToBroker("report/setpoint", message);
LastSentStatus = MqttStatusState;
}
}
Expand All @@ -620,7 +605,7 @@ void MqttReportWiFiSignal() {
int SignalLevel = WiFi.RSSI();
// ignore deviations smaller than 3 dBm
if (abs(SignalLevel - LastSentSignalLevel) > 2) {
sprintf(signal, "%d", SignalLevel);
snprintf(signal, sizeof(signal), "%d", SignalLevel);
sendToBroker("report/signal", signal); // Reports the signal strength
LastSentSignalLevel = SignalLevel;
}
Expand All @@ -635,17 +620,17 @@ void MqttReportNotification(String message) {
// send only different notification, do not re-send same notifications!
if (NotificationChecksum != LastNotificationChecksum) {
// string to char array
char msg2[message.length() + 1];
strcpy(msg2, message.c_str());
char msg2[message.length() + 1];
strncpy(msg2, message.c_str(), sizeof(msg2) - 1);
sendToBroker("report/notification", msg2);
LastNotificationChecksum = NotificationChecksum;
}
}

void MqttReportGraphic(bool force) {
if (force || MqttStatusGraphic != LastSentGraphic) {
char graphic[2] = "";
sprintf(graphic, "%i", MqttStatusGraphic);
char graphic[3]; // Increased size to accommodate null terminator
snprintf(graphic, sizeof(graphic), "%i", MqttStatusGraphic);
sendToBroker("graphic", graphic); // Reports the signal strength

LastSentGraphic = MqttStatusGraphic;
Expand Down
25 changes: 13 additions & 12 deletions EleksTubeHAX_pio/src/WiFi_WPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ void WifiBegin() {
} else {
// data is saved, connect now
// WiFi credentials are known, connect
tfts.println("Joining wifi");
tfts.println("Joining WiFi");
tfts.println(stored_config.config.wifi.ssid);
Serial.print("Joining wifi ");
Serial.print("Joining WiFi ");
Serial.println(stored_config.config.wifi.ssid);

// https://stackoverflow.com/questions/48024780/esp32-wps-reconnect-on-power-on
Expand All @@ -121,18 +121,20 @@ void WifiBegin() {
}
}
}
#else ////NO WPS -- Hard coded credentials
#else //NO WPS -- Try using hard coded credentials

WiFi.begin(WIFI_SSID, WIFI_PASSWD);
WiFi.onEvent(WiFiEvent);
unsigned long StartTime = millis();
while ((WiFi.status() != WL_CONNECTED)) {
delay(500);
tfts.print(".");
Serial.print(".");
tfts.print(">");
Serial.print(">");
if ((millis() - StartTime) > (WIFI_CONNECT_TIMEOUT_SEC * 1000)) {
Serial.println("\r\nWiFi connection timeout!");
tfts.setTextColor(TFT_RED, TFT_BLACK);
tfts.println("\nTIMEOUT!");
tfts.setTextColor(TFT_WHITE, TFT_BLACK);
Serial.println("\r\nWiFi connection timeout!");
WifiState = disconnected;
return; // exit loop, exit procedure, continue clock startup
}
Expand All @@ -142,10 +144,9 @@ void WifiBegin() {


WifiState = connected;

tfts.println("\n Connected!");
tfts.println(WiFi.localIP());

tfts.println("\nConnected! IP:");
tfts.println(WiFi.localIP());
Serial.println("");
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Expand All @@ -168,7 +169,7 @@ void WiFiStartWps() {
sprintf(stored_config.config.wifi.ssid, "");
sprintf(stored_config.config.wifi.password, "");
stored_config.config.wifi.WPS_connected = 0x11; // invalid = different than 0x55
Serial.print("Saving config.");
Serial.println(""); Serial.print("Saving config! Triggered from WPS start...");
stored_config.save();
Serial.println(" Done.");

Expand Down Expand Up @@ -199,13 +200,13 @@ void WiFiStartWps() {
Serial.print(".");
}
tfts.setTextColor(TFT_WHITE, TFT_BLACK);
Serial.print("Saving config.");
sprintf(stored_config.config.wifi.ssid, "%s", WiFi.SSID());
sprintf(stored_config.config.wifi.ssid, "%s", WiFi.SSID());
// memset(stored_config.config.wifi.ssid, '\0', sizeof(stored_config.config.wifi.ssid));
// strcpy(stored_config.config.wifi.ssid, WiFi.SSID());

sprintf(stored_config.config.wifi.password, ""); // can't save a password from WPS
stored_config.config.wifi.WPS_connected = StoredConfig::valid;
Serial.println(); Serial.print("Saving config! Triggered from WPS success...");
stored_config.save();
Serial.println(" WPS finished.");
}
Expand Down
Loading

0 comments on commit e0d98ad

Please sign in to comment.