Make an integer from a pointer

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!

this block here increments the pointer, leaves it pointing to the end..
use a temp pointer and leave payload intact or move this block to the end..

good luck.. ~q

replace while block with this, see what it prints..

char message[length+1]={0x00};
uint8_t* p = payload;
for (int i = 0; i < length;i++){
        Serial.print(*(const char *)p);
         message[i]= *p;
         p++;
}
Serial.println();
Serial.println(message);

Thank you for the help!
I replaced that and it threw this error.
invalid conversion from 'const uint8_t*' {aka 'const unsigned char*'} to 'uint8_t*' {aka 'unsigned char*'} [-fpermissive]

which is one I have gotten with just about everything else I have tried with this thing. sigh

strange..
but been a long day, starting to see double..

code in sim working..

maybe i missed something..

sorry.. ~q

never mind, error shows up when I moved it to esp32..
to fix..

const uint8_t* p = payload;//change me..

here's the esp32 sim, added a 32 bit int payload test too..
ESP32 Payload

~q

Oh my gosh!!! thank you so much! this has been killing me for so long! I was playing around with your first one and was thinking the board might be making a difference... but didn't think it could actually make a difference. Glad you sussed that out! I want need to compare and assign this to different values. This was still throwing an error because it is a char and not an int, but I used this
test = atoi(message);
and that got me exactly what I was looking for.
Thank you again!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.