I am trying to get Home Assistant to manipulate my Arduino through MQTT communication. I have writing to HA solved but when I receive messages from HA they are pointers. I need/would like to have these as variables which I can use to compare and or adjust times things run. Is this possible? I am using the ArduinoHA.h library.
here is my full 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);
Serial.print("length is: ");
Serial.println(length);
while( length-- ) {
Serial.print(*(const char *)payload);
++payload;
}
Serial.println();
//char message[5]={0x00};
//for(int i=0;i<length;i++){ // I added the moustaches. Fairly sure they were excluded in error
// message[i]=(char)payload[i];
// Serial.print(message[i]);
// }
//int myInt = 0;
//for (int i = 0; i < length; i++) {
// myInt *= 10; // new position, multiply the before by 10
// myInt += payload[i] - '0';
//}
}
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", "ooghuluxooHidohvahpee0Sool3iXahsoo3cheiThei3Iekieth2iquedoosh0Ga");
}
void loop() {
currentMillis = millis();
sendToServer();
mqtt.loop();
//Serial.println(myInt);
}
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);
}
}
I have tried a lot of things. From this post
I tried some of the tricks in there but I get very odd results. I have commented out two attempts I have made that I guess seem promising..
//char message[5]={0x00};
//for(int i=0;i<length;i++){ // I added the moustaches. Fairly sure they were excluded in error
// message[i]=(char)payload[i];
// Serial.print(message[i]);
// }
//int myInt = 0;
//for (int i = 0; i < length; i++) {
// myInt *= 10; // new position, multiply the before by 10
// myInt += payload[i] - '0';
//}
When I uncomment either of these, it compiles without a problem but both times in the serial output screen it produces something similar to this.
*wm:AutoConnect
*wm:Connecting to SAVED AP: Roth
*wm:connectTimeout not set, ESP waitForConnectResult...
*wm:AutoConnect: SUCCESS
*wm:STA IP Address: 192.168.1.145
Connected to the broker!
New message on topic!: hourslight
Data: 20
20
2
20
63
63
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Exception (3):
epc1=0x402010c8 epc2=0x00000000 epc3=0x00000000 excvaddr=0x40000000 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffdc0 end: 3fffffd0 offset: 0150
3fffff10: 0000000e 00000000 3ffefb54 3ffefbb2
3fffff20: 3ffefba7 3ffeec50 3ffefb54 40213a7e
3fffff30: 00000002 0000000d 0000000f 000f45f8
3fffff40: 00000001 3ffefb54 3ffefb54 00000001
3fffff50: 00000001 00000d35 3ffefb54 4020d45c
3fffff60: 00000002 3ffefbb2 3ffefba7 000ebc63
3fffff70: 3ffefb0a 3ffefb54 3ffefb01 3ffefb54
3fffff80: 00000003 0000000a 00000010 00000004
3fffff90: 3fffdad0 3ffeec50 3ffeec50 3ffeef14
3fffffa0: 3fffdad0 00000000 3ffeec50 4020cd2e
3fffffb0: 00000000 00000000 3ffeeee8 402118bc
3fffffc0: feefeffe feefeffe 3fffdab0 40100da9
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3424, room 16
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8
tail 0
chksum 0x2b
csum 0x2b
v00058700
~ld
⸮⸮⸮e⸮g⸮;⸮⸮n|⸮l⸮l`c⸮⸮|r⸮d⸮g⸮⸮o⸮
It will just repeat that over and over again. Which I don't understand why/how it would do that seeing as it is not in the void loop. Anyway, any help in converting these pointers to something I can maybe better use in my code will be much appreciated!