Hello,
I took the SendReceive example from the Arduino IDE (2.0) menu and modified it in a way that I can send temperature and humidity values to The Things Network. So at first I took fixed values to check the connection and my payload formatter. Here is the script:
/**
- This is an example of joining, sending and receiving data via LoRaWAN using a more minimal interface.
- The example is configured for OTAA, set your keys into the variables below.
- The example will upload a counter value periodically, and will print any downlink messages.
- please disable AT_SUPPORT in tools menu
- David Brodrick.
*/
#include “LoRaWanMinimal_APP.h”
#include “Arduino.h”
/*
- set LoraWan_RGB to Active,the RGB active in loraWan
- RGB red means sending;
- RGB purple means joined done;
- RGB blue means RxWindow1;
- RGB yellow means RxWindow2;
- RGB green means received done;
*/
//Set these OTAA parameters to match your app/node in TTN
uint8_t devEui[] = {0x70, 0xB3, …};
uint8_t appEui[] = {0x49, 0x85, …};
uint8_t appKey[] = {0xA5, 0xCE, …};
uint16_t userChannelsMask[6] = {0x00FF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
static uint8_t counter = 0;
// Declare appData array for payload
uint8_t appData[4];
///////////////////////////////////////////////////
//Some utilities for going into low power mode
TimerEvent_t sleepTimer;
//Records whether our sleep/low power timer expired
bool sleepTimerExpired;
static void wakeUp()
{
sleepTimerExpired = true;
}
static void lowPowerSleep(uint32_t sleeptime)
{
sleepTimerExpired = false;
TimerInit(&sleepTimer, &wakeUp);
TimerSetValue(&sleepTimer, sleeptime);
TimerStart(&sleepTimer);
//Low power handler also gets interrupted by other timers
//So wait until our timer had expired
while (!sleepTimerExpired) lowPowerHandler();
TimerStop(&sleepTimer);
}
///////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
if (ACTIVE_REGION == LORAMAC_REGION_AU915) {
// TTN uses sub-band 2 in AU915
LoRaWAN.setSubBand2();
}
LoRaWAN.begin(LORAWAN_CLASS, ACTIVE_REGION);
// Enable ADR
LoRaWAN.setAdaptiveDR(true);
while (1) {
Serial.print("Joining… ");
LoRaWAN.joinOTAA(appEui, appKey, devEui);
if (!LoRaWAN.isJoined()) {
//In this example we just loop until we’re joined, but you could
//also go and start doing other things and try again later
Serial.println(“JOIN FAILED! Sleeping for 30 seconds”);
lowPowerSleep(30000);
} else {
Serial.println(“JOINED”);
break;
}
}
}
///////////////////////////////////////////////////
void loop()
{
//Counter is just some dummy data we send for the example
counter++;
//In this demo we use a timer to go into low power mode to kill some time.
//You might be collecting data or doing something more interesting instead.
lowPowerSleep(15000);
//Now send the data. The parameters are “data size, data pointer, port, request ack”
Serial.printf("\nSending packet with counter=%d\n", counter);
//Here we send confirmed packed (ACK requested) only for the first five (remember there is a fair use policy)
bool requestack = counter<5?true:false;
prepareTxFrame();
if (LoRaWAN.send(1, appData, 2, requestack)) {
Serial.println(“Send OK”);
} else {
Serial.println(“Send FAILED”);
}
}
///////////////////////////////////////////////////
//Example of handling downlink data
void downLinkDataHandle(McpsIndication_t *mcpsIndication)
{
Serial.printf(“Received downlink: %s, RXSIZE %d, PORT %d, DATA: “,mcpsIndication->RxSlot?“RXWIN2”:“RXWIN1”,mcpsIndication->BufferSize,mcpsIndication->Port);
for(uint8_t i=0;iBufferSize;i++) {
Serial.printf(”%02X”,mcpsIndication->Buffer[i]);
}
Serial.println();
}
/* Prepares the payload of the frame */
static void prepareTxFrame()
{
/*appData size is LORAWAN_APP_DATA_MAX_SIZE which is defined in “commissioning.h”.
*appDataSize max value is LORAWAN_APP_DATA_MAX_SIZE.
*if enabled AT, don’t modify LORAWAN_APP_DATA_MAX_SIZE, it may cause system hanging or failure.
*if disabled AT, LORAWAN_APP_DATA_MAX_SIZE can be modified, the max value is reference to lorawan region and SF.
*for example, if use REGION_CN470,
*the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in “RegionCN470.h”.
*/
float temperature = 20.30; // Example
float humidity = 65.2; // Example
int int_temp = temperature * 100; // Convert to int
int int_hum = humidity * 10; // Convert to int
appData[0] = int_temp >> 8;
appData[1] = int_temp;
appData[2] = int_hum >> 8;
appData[3] = int_hum;
}
The connection to TTN is working and I can send data. But with the following payload formatter:
function decodeUplink(input) {
var temp = input.bytes[0] << 8 | input.bytes[1];
var hum = input.bytes[2] << 8 | input.bytes[3];
return {
data: {
temperature: temp/100,
humidity: hum/10
}
};
}
I get the following live data:
{ humidity: 0, temperature: 17.92 } … instead of the above mentioned values 65.2 and 20.30
What is wrong with my code? Can anyone help?
Thank’s Martin