# Need help c++ programming esp8266

**URL:** <https://community.openhab.org/t/need-help-c-programming-esp8266/49800>\
**Category:** Home Automation\
**Tags:** esp8266, programming\
**Created:** [August 21, 2018, 10:51am UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800 "2018-08-21T10:51:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aymeric106](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aymeric106](https://community.openhab.org/u/aymeric106)\
**Post date:** [August 21, 2018, 10:51am UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800/1 "2018-08-21T10:51:47Z")

</div>

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

I’m reworking an example of [GitHub - crankyoldgit/IRremoteESP8266: Infrared remote library for ESP8266/ESP32: send and receive infrared signals with multiple protocols. Based on: https://github.com/shirriff/Arduino-IRremote/](https://github.com/markszabo/IRremoteESP8266) to fit my need.  
I’m using the example as a base [IRremoteESP8266/examples/IRMQTTServer at master · crankyoldgit/IRremoteESP8266 · GitHub](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

```auto
#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**

```auto
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

```auto

// 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 😀

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [August 21, 2018, 10:58am UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800/2 "2018-08-21T10:58:17Z")

</div>

```c++
If (callback_str =="SAMSUNG_POWER_ON") code=SAMSUNG_POWER_ON

```

---

<div class="post-metadata">

**Author:** ![aymeric106](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aymeric106](https://community.openhab.org/u/aymeric106)\
**Post date:** [August 21, 2018, 11:07am UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800/3 "2018-08-21T11:07:11Z")

</div>

Yes, but there’s 150 lines in samsung\_code.h …  
…

I can do that but …

```auto
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
...

```

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [August 21, 2018, 11:19am UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800/4 "2018-08-21T11:19:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aymeric106](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aymeric106](https://community.openhab.org/u/aymeric106)\
**Post date:** [August 21, 2018, 9:22pm UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800/5 "2018-08-21T21:22:51Z")

</div>

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 ? 🤨 )

[https://projetsdiy.fr/esp8266-webserveur-partie4-arduinojson-charger-enregistrer-fichiers-spiffs/](https://projetsdiy.fr/esp8266-webserveur-partie4-arduinojson-charger-enregistrer-fichiers-spiffs/)

Aymeric

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [August 21, 2018, 10:27pm UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800/6 "2018-08-21T22:27:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aymeric106](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aymeric106](https://community.openhab.org/u/aymeric106)\
**Post date:** [August 22, 2018, 7:47am UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800/7 "2018-08-22T07:47:31Z")

</div>

It’s basically a tweaked example here

> <https://github.com/crankyoldgit/IRremoteESP8266/blob/master/examples/IRMQTTServer/IRMQTTServer.ino>

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

```auto
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 🤣 ! 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 …)

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [August 22, 2018, 7:51am UTC](https://community.openhab.org/t/need-help-c-programming-esp8266/49800/8 "2018-08-22T07:51:49Z")

</div>

> [@aymeric106](#):
>
> I have another idea to feed my fish

Poor fish!! 😆
