-1

I'm new to a little new to programming, how do I store a variable in message? I'm trying to make a wireless temperature sensor using LoRa, with an Arduino Uno + Dragino shield. The results to be displayed on the things network. Everything else is working fine. Why do I get the error written below? {temp} does not work either.

CODE:

int temp = 25;

// Payload to send (uplink)

static uint8_t message[] = temp;

Error:

HelloWorld1:77: error: initializer fails to determine size of 'message'

   static uint8_t message[] = temp;
                  ^

HelloWorld1:77: error: array must be initialized with a brace-enclosed initializer

Multiple libraries were found for "lmic.h"
 Used: C:\Users\\Documents\Arduino\libraries\arduino-lmic-master
 Not used: C:\Program Files (x86)\Arduino\libraries\arduino-lmic-master
exit status 1
initializer fails to determine size of 'message'
gre_gor
  • 6,669
  • 9
  • 47
  • 52

1 Answers1

1

The compiler has to know the size of the array when you declare it. It can find it out either directly from the value in [] (e.g. uint8_t message [2]) either, if there isn’t any value there, from the length of a braced-enclosed initializer, i.e. the list of comma-separated values inside a { } that you may assign to the array at declaration.

That aside, you can’t directly store an int value (2 bytes, signed) in an uint8_t (1 byte, unsigned). Since (I suppose) you need to transmit data as an uint8_t array you can do as follows:

int temp = 25;
// store temp in a uint8_t array with two elements (needed to store two bytes)
uint8_t message[2];
 message[0] = temp >> 8; // Most significant byte, MSB
 message[1] = temp; // Least significant byte, LSB

or

int temp = 25;
// store temp in a uint8_t array with two elements (needed to store two bytes)
uint8_t message[2] = {(temp >> 8), temp};

 message[0] = temp >> 8; // Most significant byte, MSB
 message[1] = temp; // Least significant byte, LSB

Theb transmit message, and on the receiver “reconvert” it to an int: temp = (message[0] << 8) | message[1];.

noearchimede
  • 188
  • 9