I am sure I am just missing something stupid. But I am just trying to figure out this MQTT protocol stuff. I trying to send information back and forth from Home Assistant. I can send without any trouble. I have finally figured out how to receive messages. But the trouble is they come in strange. I am using the ArduinoHA library and an example from that for the start of my code.
https://dawidchyrzynski.github.io/arduino-home-assistant/documents/library/mqtt-advanced.html
Maybe there is something that the author assumes any reasonable person would know to add, well I don't. ![]()
here is my stripped down code
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoHA.h>
#include <WiFiClient.h>
String test2;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true;
unsigned long currentMillis = 0;
const int sendToServerInterval = 10000;
unsigned long previoussendToServer = 0;
WiFiClient client;
HADevice device("HC-12_CONTROLER");
HAMqtt mqtt(client, device);
void onMqttMessage(const char* topic, const uint8_t* payload, uint16_t length) {
// This callback is called when message from MQTT broker is received.
// Please note that you should always verify if the message's topic is the one you expect.
//if (memcmp(topic, "hourslight/attribute"), 0) {
Serial.print("New message on topic!: ");
Serial.println(topic);
Serial.print("Data: ");
Serial.println((const char*)payload);
test2 = (const char*)payload;
Serial.println(test2);
// }
}
void onMqttConnected() {
Serial.println("Connected to the broker!");
// You can subscribe to custom topic if you need
mqtt.subscribe("hourslight");
mqtt.subscribe("sundown");
mqtt.subscribe("testy");
}
void setup() {
Serial.begin(115200);
mqtt.onMessage(onMqttMessage);
mqtt.onConnected(onMqttConnected);
if (connectWIFI) {
connectToWifi();
}
//HA
byte mac[] = {0xA8, 0x93, 0x4A, 0x6C, 0x73, 0x31};
WiFi.macAddress(mac);
device.setUniqueId(mac, sizeof(mac));
device.setName("HC-12_CONTROLER");
//must put in your own local IP address your home assistant username and your HA secret code
mqtt.begin("192.168.1.130", "homeassistant", "HA pass code");
}
void loop() {
currentMillis = millis();
sendToServer();
mqtt.loop();
}
void sendToServer() {
if (currentMillis - previoussendToServer >= sendToServerInterval) {
previoussendToServer += sendToServerInterval;
if(WiFi.status()== WL_CONNECTED){
mqtt.publish("myPublishTopic", "hello");
}
else {
Serial.println("WiFi Disconnected");
}
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
//updateOLED2("Connect", "Wifi AG-", String(ESP.getChipId(), HEX));
delay(2000);
wifiManager.setTimeout(90);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
//updateOLED2("Booting", "offline", "mode");
Serial.println("failed to connect and hit timeout");
delay(6000);
}
}
So if from HA I publish on one of the subscribed channels I do get results. If I publish "1" then this is what comes back.
1lishTopicNTROLER/myNumber/cmd_t
If I publish "123456789" I get
123456789cNTROLER/myNumber/cmd_t
So the published data is replacing characters in this string, but I don't understand why it doesn't just have my published data? If I publish a string that exceeds length then I get a long scrambled thing
12345678912345678912345678912345678myNumber/configHidohvahpee0Sool3iXahsoo3cheiThei3Iekieth2iquedoosh0Ga⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
So... certainly doesn't like that. I don't need anything long, for now. I am just trying to pull a number from a helper card. I have gotten all of that working, just the received data is not what I'd expect. I have looked into parsing this string and some how working with what I have, but just seems the more efficient way would be to get what I want.
I have published multiple different ways from HA and it always attaches this extra stuff.
Help please!