What you need to do is program the Arduino to do EXACTLY the same thing as the Serial Tool when you issue commands.
So you need to connect the Arduino to the RAK811 serial port and use the RAK supplied library that will do most of the coding work for you:
There are three things to pay real attention to:
You will need to MANUALLY set the Baud rate of the RAK811 to 9600 BEFORE you start - the Arduino can’t talk at 115200 - you aren’t sending much info over the serial link, so 9600 isn’t as slow as it sounds.
The block that sets the working mode in the Arduino script needs to be removed - when you set the working mode, the RAK module will need restarting / resetting - which isn’t allowed for in the library so it will just stop there. Set this manually whilst you are setting the baud rate. You only need to do this once as the module will remember the settings you set.
You will need some sort of level convertor if you have an Arduino Uno, Nano or indeed other that runs at 5V as the RAK811 runs at 3.3V and you don’t want to release the magic smoke.
If you have the Wisnode/Arduino Shield, the library provides all the information you need, including what jumpers to set depending on which version you have. If you just have a breakout modules, here’s mine:
You’ll see that I have in fact wired in a reset and that I’m so rich, I live life to the edge and don’t bother with a level converter. Please look at the RAK811 breakout module info and an Arduino Nano pinout to figure out the micro-details of the connections.
I have a question. How do you this?
I mean, I only take out the #define WORK_MODE LoRaWAN from the code? or I have to modify something from the library?
“The block that sets the working mode in the Arduino script needs to be removed - when you set the working mode, the RAK module will need restarting / resetting - which isn’t allowed for in the library so it will just stop there. Set this manually whilst you are setting the baud rate. You only need to do this once as the module will remember the settings you set.”
I edited the previous answer to add the float conversion for you. This would a way to solve your problem.
Float -> char -> add another char with something you may want, like sensor name -> merge them -> print(buffer, hex)
Please, note the this is was added inside the RAK811.cpp file, you need to create your own function for this.
Whilst this does move things forward, it will have rendered the library specific only to this sensor.
The code should be put in the main ino and the hex string then passed to the rk_sendData.
Additionally, sending payloads as strings is considered wasteful - conversion to an integer (usually by multiplying by 10, 100 or 1000 etc as appropriate) and then byte packing / shifting is strongly encouraged. As far as the TTN forumites think, any other way is wrong, a heinous crime and bad for the network.
I definitely agree with @nmcc regarding floats. The best approach is to multiply it with 10, 100, etc. then do the conversion on the decoder side of the network server. It is just a simple javascript. But @SWoto’s code above can work as well and you can easily add char or string on your payload. However, if you are only going to send just float values, @nmcc advise is really recommended.
Sadly all too easy - I’ve yet to come across a really good reason for sending plain text in a LoRaWAN payload.
Conversely, I have a very messed up set of utilities I use to bit-pack so if, for example I only need 0-15, I only use 4 bits and the other 4 are used for another value - it handles all the fun of crossing byte boundaries. Perhaps I should clean it up and release it.
I think with what i’m going to say I might be stepping outside the topic question, though I’m not sure because it is related with the replies. Mods, please correct me if I’m wrong.
@nmcc, @carlrowan, regarding the discussion about sending it as int or float and the usage of plain text, if you have three sensors (assume temperature, humidity and pressure) how would you send it with LoRaWAN? Three frames with the value as int, or try to fit it in a frame separated by some character?
No, no, no, no, never, ever. Never ever think like that again or the LoRaWAN SWAT team will visit.
That implies three transmissions - three times you use you battery.
Try? Do or do not, there is no try!
The LoRaWAN SWAT team won’t come for this, they just send the LoRaWAN police around to fine you 100 batteries - as this will add to battery burn.
OK, I’ll stop encouraging a search using the forum, TTN forum or Mistress Google and go for the direct answer using Arduino code:
#define PAYLOAD_SIZE 6
void setup() {
Serial.begin(9600);
Serial.println("LoRaWAN using RAK library payload demo");
int data[PAYLOAD_SIZE]; // binary data structure with enough bytes for the payload
int temperature = 20;
int humidity = 35;
int pressure = 935;
/* This is where you get your readings
// Get your readings
temperature = getTheTemperature();
humidity = getTheHumidity();
pressure = getThePressure();
*/
// Transfer to the binary data structure
data[0] = highByte(temperature);
data[1] = lowByte(temperature);
data[2] = highByte(humidity);
data[3] = lowByte(humidity);
data[4] = highByte(pressure);
data[5] = lowByte(pressure);
// Convert to a HEX string
String payload = "";
for(int c = 0; c < PAYLOAD_SIZE; c++) {
if(data[c] < 0x10) { // If it is less that 10, we'll need to put the 0 in as the Arduino String function doesn't pad with leading 0's
payload += '0';
}
payload += String(data[c], HEX);
}
payload.toUpperCase(); // Converts in place, just to add to new programmer confusion
Serial.print("Payload as Hex string: ");
Serial.println(payload);
// RAKLoRa.rk_sendData(1, payload) // This is where we send the payload
// Note, buffer is a reserved word in Arduino, so I don't use buffer, I use payload, but the RAK examples do, your mileage may vary
}
void loop() {
}