Last active
March 11, 2020 22:17
-
-
Save RouNNdeL/c6ac33e5fb072d46fae1fc75370a4022 to your computer and use it in GitHub Desktop.
Lora sesnor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <MKRWAN.h> | |
#include "secrets.h" | |
LoRaModem modem; | |
#define ITERATION_DELAY 120000 // 1 min | |
#define DEBUG 0 | |
#if DEBUG | |
#define PRINT(s) Serial.print(s) | |
#define PRINT_BASE(s, b) Serial.print(s, b) | |
#define PRINTLN(s) Serial.println(s) | |
#else | |
#define PRINT(s) | |
#define PRINTLN(s) | |
#define PRINT_BASE(s, b) | |
#endif | |
int iterator; | |
ulong previousMillis = 0; | |
void message(); | |
void setup() { | |
#if DEBUG | |
Serial.begin(115200); | |
while(!Serial); | |
#endif | |
PRINTLN("Starting modem"); | |
if (!modem.begin(EU868)) { | |
PRINTLN("Failed to start module"); | |
for (;;); | |
} | |
PRINT("Your module version is: "); | |
PRINTLN(modem.version()); | |
PRINT("Your device EUI is: "); | |
PRINTLN(modem.deviceEUI()); | |
PRINTLN("Attempting to OTAA"); | |
while (!(modem.joinOTAA(SECRET_APP_EUI, SECRET_APP_KEY))) { | |
PRINTLN("Unable to OTAA, retrying..."); | |
} | |
PRINTLN("Successfully joined by OTAA"); | |
modem.minPollInterval(60); | |
} | |
void loop() { | |
ulong currentMillis = millis(); | |
if ((ulong) (currentMillis - previousMillis) >= ITERATION_DELAY || previousMillis == 0) { | |
previousMillis = currentMillis; | |
iterator++; | |
PRINTLN("Message iterator: "+String(iterator)); | |
message(); | |
} | |
} | |
void message() { | |
uint16_t temperature = analogRead(A0); | |
uint16_t light = analogRead(A5); | |
String payload = String(temperature) + "," + String(light); | |
PRINTLN(payload); | |
modem.beginPacket(); | |
modem.print(payload); | |
uint8_t err = modem.endPacket(true); | |
if (err > 0) { | |
PRINTLN("Message sent correctly!"); | |
} else { | |
PRINTLN("Error sending message: "+String(err, 16)); | |
} | |
delay(1000); | |
if (!modem.available()) { | |
PRINTLN("No downlink."); | |
return; | |
} | |
uint8_t rcv[64]; | |
uint8_t i = 0; | |
while (modem.available()) { | |
rcv[i++] = (uint8_t) modem.read(); | |
} | |
PRINT("Received: "); | |
for (uint8_t j = 0; j < i; j++) { | |
PRINT_BASE(rcv[j] >> 4, HEX); | |
PRINT_BASE(rcv[j] & 0xF, HEX); | |
PRINT(" "); | |
} | |
PRINTLN(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SECRET_APP_EUI "<APP EUI>" | |
#define SECRET_APP_KEY "<SECRET APP KEY>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment