Skip to content

Instantly share code, notes, and snippets.

@wero1414
Last active March 19, 2018 15:59
Show Gist options
  • Save wero1414/149f8452a724223114b22b4603cf9a33 to your computer and use it in GitHub Desktop.
Save wero1414/149f8452a724223114b22b4603cf9a33 to your computer and use it in GitHub Desktop.
/*******************************************************************************
Created by Eduardo Contreras @ Electronic Cats 2016
Based on Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
PLEASE REFER TO THIS LMIC LIBRARY https://github.com/things-nyc/arduino-lmic
LoRaShield
In this example you can send GPS information (Compatible with the L80)
or a temperature read by the ADC
Example
*******************************************************************************/
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#define _USE_GPS_
#ifdef _USE_GPS_
#include <TinyGPS++.h>
TinyGPSPlus gps;
double gps_lat = 0;
double gps_lng = 0;
double gps_alt = 0;
bool gpsEncoded = false;
#endif
#define debugSerial Serial
unsigned long previousMillis = 0;
static const PROGMEM u1_t NWKSKEY[16] ={0x12,0x88,0x12,0x81,0x27,0x18,0x29,0x1F,0xFF,0xF9,0x23,0x18,0x12,0x91,0x02,0x91};
static const u1_t PROGMEM APPSKEY[16] ={0x19,0x28,0x12,0x12,0x91,0x02,0x01,0x09,0x28,0x48,0x48,0x39,0x30,0x33,0x33,0x23};
static const u4_t DEVADDR =0x01000000;
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 20;
// Pin mapping for RFM9X
const lmic_pinmap lmic_pins = {
.nss = A0,
.rxtx = LMIC_UNUSED_PIN,
.rst = A4,
.dio = {2, A3, LMIC_UNUSED_PIN},
};
void debug_char(u1_t b) {
debugSerial.write(b);
}
void debug_hex (u1_t b) {
debug_char("0123456789ABCDEF"[b >> 4]);
debug_char("0123456789ABCDEF"[b & 0xF]);
}
void debug_buf (const u1_t* buf, u2_t len) {
while (len--) {
debug_hex(*buf++);
debug_char(' ');
}
debug_char('\r');
debug_char('\n');
}
void onEvent (ev_t ev) {
switch (ev) {
case EV_TXCOMPLETE:
// indicating radio TX complete
digitalWrite(5, LOW);
debugSerial.println(F("[LMIC] Radio TX complete (included RX windows)"));
if (LMIC.txrxFlags & TXRX_ACK)
debugSerial.println(F("[LMIC] Received ack"));
if (LMIC.dataLen) {
debugSerial.print(F("[LMIC] Received "));
debugSerial.print(LMIC.dataLen);
debugSerial.println(F(" bytes of payload"));
debug_buf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
}
break;
default:
debugSerial.println(F("[LMIC] Unknown event"));
break;
}
}
void do_send(osjob_t* j, uint8_t *mydata1, uint16_t len) {
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
debugSerial.println(F("[LMIC] OP_TXRXPEND, not sending"));
} else {
// Prepare upstream data transmission at the next possible time
LMIC_setTxData2(1, mydata1, len, 0);
}
}
void setup() {
debugSerial.begin(9600);
debugSerial.println(F("[INFO] LoRa Demo Node 1 Demonstration"));
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(A0,OUTPUT);
pinMode(A3,INPUT);
pinMode(A4,OUTPUT);
os_init();
LMIC_reset();
uint8_t appskey[sizeof(APPSKEY)];
uint8_t nwkskey[sizeof(NWKSKEY)];
memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
for (int channel=0; channel<72; ++channel) {
LMIC_disableChannel(channel);
}
//SF TTN
/*
LMIC_enableChannel(0);
LMIC_enableChannel(1);
LMIC_enableChannel(2); //904.3Mhz
LMIC_enableChannel(3);
LMIC_enableChannel(4);
LMIC_enableChannel(5);
LMIC_enableChannel(6);
LMIC_enableChannel(7);
LMIC_enableChannel(8);
*/
//Home
LMIC_enableChannel(48);
LMIC_enableChannel(49);
LMIC_enableChannel(50);
LMIC_enableChannel(51);
LMIC_enableChannel(52);
LMIC_enableChannel(53);
LMIC_enableChannel(54);
LMIC_enableChannel(55);
LMIC_enableChannel(70);
LMIC_setLinkCheckMode(0);
LMIC_setAdrMode(false);
LMIC_setDrTxpow(DR_SF7, 14); //SF7
previousMillis = millis();
}
void loop() {
if (millis() > previousMillis + TX_INTERVAL * 1000) { //Start Job at every TX_INTERVAL*1000
getInfoAndSend();
previousMillis = millis();
}
os_runloop_once();
}
void getInfoAndSend() {
uint8_t len = 0; //Bug of len
uint8_t mydata[11];
uint8_t cnt = 0;
uint8_t ch = 0;
debugSerial.println(F("[INFO] Collecting info"));
// GPS
#ifdef _USE_GPS_// Temperature
len += 11; // GPS
GPS_loop();
if (gpsEncoded) {
digitalWrite(6, LOW);
debugSerial.println(F("[INFO] Collecting GPS info"));
debugSerial.print(F("[INFO] Lat:")); debugSerial.println(String(gps_lat, 6));
debugSerial.print(F("[INFO] Lng:")); debugSerial.println(String(gps_lng, 6));
debugSerial.print(F("[INFO] Alt:")); debugSerial.println(gps_alt);
long lat = round(gps_lat * 10000);
long lng = round(gps_lng * -10000);
long alt = round(gps_alt * 100);
ch = ch + 1;
mydata[cnt++] = ch;
mydata[cnt++] = 0x88;
mydata[cnt++] = lat >> 16;
mydata[cnt++] = lat >> 8;
mydata[cnt++] = lat;
mydata[cnt++] = lng >> 16;
mydata[cnt++] = lng >> 8;
mydata[cnt++] = lng;
mydata[cnt++] = alt >> 16;
mydata[cnt++] = alt >> 8;
mydata[cnt++] = alt;
}
else{
digitalWrite(6, HIGH);
ch = ch + 1;
mydata[cnt++] = ch;
mydata[cnt++] = 0x88;
mydata[cnt++] = 0;
mydata[cnt++] = 0;
mydata[cnt++] = 0;
mydata[cnt++] = 0;
mydata[cnt++] = 0;
mydata[cnt++] = 0;
mydata[cnt++] = 0;
mydata[cnt++] = 0;
mydata[cnt++] = 0;
}
#endif
if (cnt == len) {
debugSerial.println(F("[LMIC] Start Radio TX"));
// indicating start radio TX
digitalWrite(5, HIGH);
for(int i;i<sizeof(mydata);i++){
debugSerial.print(mydata[i]);
}
debugSerial.println();
do_send(&sendjob, mydata, sizeof(mydata));
}
else
debugSerial.println(F("[ERROR] Data stack incorrect"));
}
#ifdef _USE_GPS_
void GPS_loop() {
debugSerial.println(F("[INFO] Collecting GPS info"));
while (debugSerial.available() > 0) {
if (gps.encode(debugSerial.read())) {
if (gps.location.isValid() && gps.altitude.isValid()) {
// indicating GPS is successfully obtained
gpsEncoded = true;
// update the locations
gps_lat = gps.location.lat();
gps_lng = gps.location.lng();
gps_alt = gps.altitude.meters();
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment