Stage 1: Building Your Sensor Node
Stage 1: Building Your Sensor Node
Configure the LoRaWAN® Node Moisture Sensor
- In the Arduino IDE, create a new sketch: File > New.
- Remove the starting code so that you are left with an empty file.
- Copy and paste the following code into the sketch:
#include <lmic.h>
#include <hal/hal.h> #include <SPI.h> #define SensorPin A0 int sensorValue = -1;
// Pin mapping -- set your pin numbers here. These are for the Dragino shield.
const lmic_pinmap lmic_pins = { .nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = {2, 6, 7},
};
// Insert Device EUI here
// Insert Application EUI here
// Insert App Key here
// Schedule uplink to send every TX_INTERVAL seconds
void do_send(osjob_t* j){ // Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
// Prepare upstream data transmission at the next possible time.
sensorValue = analogRead(SensorPin);
Serial.println("Reading is: ");
Serial.println(sensorValue);
// int -> byte array
byte payload[2];
payload[0] = lowByte(sensorValue);
payload[1] = highByte(sensorValue);
// transmit packet at the next available slot. The parameters are:
// - FPort, the port used to send the packet -- port 1
// - the payload to send
// - the size of the payload
// - if we want an acknowledgement (ack), costing 1 downlink message; 0 means we do not want an ack
LMIC_setTxData2(1, payload, sizeof(payload), 0);
Serial.println(F("Payload queued"));
}
}
static osjob_t sendjob;
void setup() { Serial.begin(9600);
Serial.println(F("Starting"));
// Initalizes the LIMIC library,
os_init();
// Resets the MAC state -- this removes sessions, meaning the device must repeat the join process each time it is started.
LMIC_reset();
// Disable link check validation -- this is used to periodically verify network connectivity. Not needed in this tutorial.
LMIC_setLinkCheckMode(0);
// Set data rate to Spreading Factor 7 and transmit power to 14 dBi for uplinks
LMIC_setDrTxpow(DR_SF7, 14);
// Start job
do_send(&sendjob);
}
void loop() { |
- Since you are setting up a private instance, use the sample code as it is, with the default values provided. Note, there are two values in this script which are configurable for each device you want to register: DEVEUI and APPKEY.
DEVEUI, Device Extended-Unique-Identifier (EUI), is a globally unique 64-bit identifier that uniquely identifies your end device. In the Arduino code it is written in least significant bit (LSB) format.
APPKEY, Application Key, is an Advanced Encryption Standard (AES) 128-bit root key unique to the end device. The AppKey is used by both the end device and ChirpStack to derive the session keys during the OTAA process, and to verify the integrity of the join request and join accept messages sent during the OTAA process. In the Arduino code it is written in most significant bit (MSB) format.
The APPEUI is not required by the ChirpStack network server. It is required by the LMIC library, so each byte is set to zero in the sample code.
- Connect the Arduino to the computer’s USB port using the USB-B cable. You should see a power LED illuminate on the Arduino and on the shield.
- At the Arduino IDE menu, select Tools > Port. You need to select the serial port that the Arduino is connected to. Select the port which has the model name of your Arduino board alongside it. If none of the ports are labeled, disconnect the Arduino board and reopen the menu; the entry which disappears should be your board. Reconnect the board, then select the entry which had disappeared.
- At the top of the sketch window, click the Upload icon (right arrow) to the right of the Verify icon. This will upload the sketch to the device.
When the upload is complete, the message Done uploading will display at the bottom of the sketch file window, confirming the upload has taken place.
- At the menu, select Tools > Serial Monitor. The Serial Monitor window will open.
- Select the 9600 baud option from the drop-down menu at the bottom right of the Serial Monitor.
- If the code uploaded successfully, you will see logging in the Serial Monitor that shows the attempted communication of the device.