Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. How to convert received messages to integers? (MQTT)

How to convert received messages to integers? (MQTT)

Scheduled Pinned Locked Moved Troubleshooting
6 Posts 3 Posters 3.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ikkeTI Offline
    ikkeTI Offline
    ikkeT
    wrote on last edited by
    #1

    Hi,

    I'm puzzled how message receive happens. I am building a infrared transmitter. I decoded the NEC signals from original remote, they look like this (hex): 0xF7C03F. This is power on command.

    Now I want to send those signals over MQTT, which mysgw transfers over NRF24L01+ to arduino. But how to convert messages so that they look alike at both ends? is that number too big to send (3 bytes)?

    As I send that exact command from arduino:

    #define CMD_ON        0xF7C03F
    ...
    send(msg_ir.set(CMD_ON));
    

    it looks like this on MQTT looking from mosquitto_sub:
    MySensorsGW/out/7/1/1/0/32 16236607

    When I send that back:
    mosquitto_pub -h droidcam.ikenet -p 1883 -t MySensorsGW/in/7/1/1/0/32 -m 16236607

    the code:

    uint32_t ircode;
    ircode = atoi( message.data );
    ...
            Serial.print( "V_IR_SEND data invalid: " );
            Serial.print( message.data );
            Serial.print( ", ircode: " );
            Serial.println( ircode );
    

    prints it out like this:
    V_IR_SEND data invalid: 16236607, ircode: 4294950975

    the numbers don't match. 16236607 is not the original CMD_ON code.

    This is the receive function:

    // IR remote command codes
    #define CMD_BRIGHTER  0xF700FF
    #define CMD_DIMMER    0xF7807F
    #define CMD_OFF       0xF740BF
    #define CMD_ON        0xF7C03F
    #define CMD_FLASH     0xF7D02F
    #define CMD_STROBE    0xF7F00F
    #define CMD_FADE      0xF7C837
    #define CMD_SMOOTH    0xF7E817
    #define CMD_RED       0xF720DF
    #define CMD_RED1      0xF710EF
    #define CMD_RED2      0xF730CF
    #define CMD_RED3      0xF708F7
    #define CMD_RED4      0xF728D7
    #define CMD_GREEN     0xF7A05F
    #define CMD_GREEN1    0xF7906F
    #define CMD_GREEN2    0xF7B04F
    #define CMD_GREEN3    0xF78877
    #define CMD_GREEN4    0xF7A857
    #define CMD_BLUE1     0xF7609F
    #define CMD_BLUE      0xF750AF
    #define CMD_BLUE2     0xF7708F
    #define CMD_BLUE3     0xF748B7
    #define CMD_BLUE4     0xF76897
    
    void receive(const MyMessage &message)
    {
      uint32_t ircode;
      Serial.print( "Received something of type: " );
      Serial.println( message.type );
    
    	if (message.type == V_IR_SEND) {
    		Serial.println( "V_IR_SEND command received..." );
    
    		ircode = atoi( message.data );
        //ircode = message.data;
        switch (ircode) {
          case CMD_BRIGHTER:
          case CMD_DIMMER:
          case CMD_OFF:
          case CMD_ON:
          case CMD_FLASH:
          case CMD_STROBE:
          case CMD_FADE:
          case CMD_SMOOTH:
          case CMD_RED:
          case CMD_RED1:
          case CMD_RED2:
          case CMD_RED3:
          case CMD_RED4:
          case CMD_GREEN:
          case CMD_GREEN1:
          case CMD_GREEN2:
          case CMD_GREEN3:
          case CMD_GREEN4:
          case CMD_BLUE1:
          case CMD_BLUE:
          case CMD_BLUE2:
          case CMD_BLUE3:
          case CMD_BLUE4:
          {
            Serial.print( "V_IR_SEND code received: ");
            Serial.println( message.data );
            send_ircode(ircode);
            ack_ir_to_controller(ircode);
            break;
          }
          default:
          {
            Serial.print( "V_IR_SEND data invalid: " );
            Serial.print( message.data );
            Serial.print( ", ircode: " );
            Serial.println( ircode );
    			  return;
    		   }
        }
      }
      for (int i=0; i<5; i++) {
        digitalWrite(LED_PIN, HIGH);
        wait(100);
        digitalWrite(LED_PIN, LOW);
        wait(100);
      }
    }
    

    I suppose it has to do with data length and auto conversion of variables. It perhaps worked if I use smaller numbers to set the IR code value. But however, I'd like to understand how this is supposed to work?

    BR,
    -ikke

    PS, this is IR controlled LED light bulb from LIDL, and commands do work with the given codes.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hek
      Admin
      wrote on last edited by
      #2

      #define CMD_ON 0xF7C03F

      It might help if you add a L to indicate long on the defines.

      Like this
      #define CMD_ON 0xF7C03FL

      1 Reply Last reply
      1
      • ikkeTI Offline
        ikkeTI Offline
        ikkeT
        wrote on last edited by
        #3

        Thanks, now it looks different, doesn't still work though. Perhaps the atoi doesn't do longs properly?

        Received something of type: 32
        V_IR_SEND command received...
        V_IR_SEND data invalid: 16236607, ircode: 4294950975
        

        that turns out to be:

        echo "obase=16;4294950975"|bc
        FFFFC03F
        

        How does it come up with that? Need to hurry to work now, later...

        1 Reply Last reply
        0
        • ikkeTI Offline
          ikkeTI Offline
          ikkeT
          wrote on last edited by
          #4

          The point in the above is, the Serial.print converts it correct, the atoi doesn't.

          A 1 Reply Last reply
          0
          • ikkeTI ikkeT

            The point in the above is, the Serial.print converts it correct, the atoi doesn't.

            A Offline
            A Offline
            AWI
            Hero Member
            wrote on last edited by AWI
            #5

            @ikkeT
            The ato.. functions are very basic and do only what they are supposed to do:

            • atoi parses to int
            • atof parses to float
            • atol parses to long

            You can also consider the str.... functions (e.g. strtoul()) as a more robust alternative.

            Replacing the defines by const will help with type checking.
            i.e. replace #define CMD_BLUE4 0xF76897ul (you can add UL to indicate a constant is unsigned long which can hold 4 bytes 0xFFFFFFFF) with const unsigned long CMD_BLUE4 = 0xF76897ul ;

            1 Reply Last reply
            1
            • ikkeTI Offline
              ikkeTI Offline
              ikkeT
              wrote on last edited by
              #6

              Excellent, thank you. It's too long since I've done serious coding, I didn't remember that. atoi -> atol fixed it.

              1 Reply Last reply
              0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              20

              Online

              12.0k

              Users

              11.2k

              Topics

              113.4k

              Posts


              Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • MySensors
              • OpenHardware.io
              • Categories
              • Recent
              • Tags
              • Popular