Need help c++ programming esp8266

Hello,
Not really an openhab question, but maybe someone can help me:

I’m reworking an example of https://github.com/markszabo/IRremoteESP8266 to fit my need.
I’m using the example as a base https://github.com/markszabo/IRremoteESP8266/tree/master/examples/IRMQTTServer

The ESP8266 is waiting for MQTT request and send an InfraRed signal depending on the mqtt data received

What I like to do is send a infrared command as a string parameter in my MQTT request, and the ESP8266 needs to find that string in a small ‘database’ and send the correct IR.

mosquitto_pub -t "ESP8266/send_ir" -m "SAMSUNG_POWER_ON"

The context: the function receivingMQTT below is triggered every time a MQTT request is received.

Here’s what I have

samsung_code.h
// Samsung IR Code, Globalcache Format

#define SAMSUNG_POWER_OFF    "38000,1,1,173,173,21,65,21,65,21,65,21,21,21,21,21,21,21,21,21,21,21,65,21,65,21,65,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,65,21,65,21,21,21,21,21,65,21,65,21,65,21,65,21,21,21,21,21,65,21,65,21,21,21,1832"
#define SAMSUNG_POWER_ON    "38000,1,1,172,172,22,64,22,64,22,64,22,21,22,21,22,21,22,21,22,21,22,64,22,64,22,64,22,21,22,21,22,21,22,21,22,21,22,64,22,21,22,21,22,64,22,64,22,21,22,21,22,64,22,21,22,64,22,64,22,21,22,21,22,64,22,64,22,21,22,1820"
...

esp8266.ino

void receivingMQTT(String const topic_name, String const callback_str)
{
...
...
int ir_type = GLOBALCACHE; // >we are sending a GLOBALCACHE formatted string
char *code;

  debug("Receiving data by MQTT topic " + topic_name);
  debug("MQTT Payload (raw): " + callback_str);


// ->>> Here we need to affect the correct value to code
// If  (callback_str =="SAMSUNG_POWER_ON")  code="38000,1,1,173,173,21,.."
//
      // send received MQTT value by IR signal
      sendIRCode(ir_type, code,"",0, 0);
}

here’s the definition of sendIRCode, only 1st and 2nd parameters are really used in our case


// Transmit the given IR message.
//
// Args:
//   ir_type:  enum of the protocol to be sent.
//   code:     Numeric payload of the IR message. Most protocols use this.
//   code_str: The unparsed code to be sent. Used by complex protocol encodings.
//   bits:     Nr. of bits in the protocol. 0 means use the protocol's default.
//   repeat:   Nr. of times the message is to be repeated. (Not all protcols.)
void sendIRCode(int const ir_type, uint64_t const code, char const *code_str,
                uint16_t bits, uint16_t repeat)

So: how from the string “SAMSUNG_POWER_ON” find the string '“38000,1,1,173,173,21…”, and assign it to the the var 'code’

Thank you :grinning:

If  (callback_str =="SAMSUNG_POWER_ON")  code=SAMSUNG_POWER_ON

Yes, but there’s 150 lines in samsung_code.h …

I can do that but …

If  (callback_str =="SAMSUNG_POWER_ON")  code=SAMSUNG_POWER_ON
If  (callback_str =="SAMSUNG_POWER_OFF")  code=SAMSUNG_POWER_OFF
If  (callback_str =="SAMSUNG_VOLUME_UP")  code=SAMSUNG_VOLUME_UP
...

Instead of using define for each code, use one define for a json string will all your codes with the name as key.
Then use the json library to retrieve your code with the key sent in mqtt

Thank you, I didn’t use JSON actually, because i was getting low on memory with all those codes. So I choose only the code I really need.

Anyway I saw an nice tutorial about ESP8266 programming thanks to you.
(it’s in french, but something tell me you understand french, don’t you ? :face_with_raised_eyebrow: )

Aymeric

Yes but that uses JSON
It’s unlikely that you were running low on memory on an ESP module
What size is your programme?

It’s basically a tweaked example here


Added the Daikin library to control my AC, and the Samsung IR codes for my TV.

first i added the 175 lines of data like you suggest

If  (callback_str =="SAMSUNG_POWER_ON")  code=SAMSUNG_POWER_ON
If  (callback_str =="SAMSUNG_POWER_OFF")  code=SAMSUNG_POWER_OFF
...
...
If  (callback_str =="SAMSUNG_VOLUME_UP")  code=SAMSUNG_VOLUME_UP

It won’t even compile in Arduino IDE

Next I made a selection, keeping perhaps 40 codes.
I have a warning about free memory availaible for local variable.

But that’s OK, I’ll clean the code to remove all I don’t use and l’ll be fine. I spent way to much time on this :rofl: ! And all this var casting in c++ is killing me “string, char * , atoi, .c_str(), strdup,unint16 32 int,” can’t we have numeric and string …

(and now I have another idea to feed my fish with an arduino or esp and a small motor …)

Poor fish!! :laughing: