Hello,
I’ve been trying to get started with my first node since this spring.
My garage is a bit further away and the status of it’s doors is what I want to monitor. I have a TTN Indoor Gateway running at the house. I’m familiar with Arduino through MySensors for home automation but that’s as far as I’m able to code - or understand it.
I decided to build my first node with an Arduino Pro Mini 3.3V and RFM95 module. I have microswitches that monitor the garage doors. I built the node per: https://www.thethingsnetwork.org/labs/story/build-the-cheapest-possible-node-yourself
And borrowed and modified code from: https://github.com/tijnonlijn/RFM-node/blob/master/PIR_example.ino
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
// LoRaWAN NwkSKey, network session key
static const PROGMEM u1_t NWKSKEY[16] = { NOT MY REAL KEYS };
// LoRaWAN AppSKey, application session key
static const u1_t PROGMEM APPSKEY[16] = { NOT MY REAL KEYS EITHER };
// LoRaWAN end-device address (DevAddr)
static const u4_t DEVADDR = 0xFUBARTOO ; // <-- Change this address for every node!
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
// HERE YOUR CODE!!
int Door1State = LOW;
int Door1StateChanged = 0;
int Door1Pin = 8;
int Door2State = LOW;
int Door2StateChanged = 0;
int Door2Pin = 9;
static uint8_t mydata[] = "0 , 0";
// END HERE !!
static osjob_t sendjob;
const unsigned TX_INTERVAL = 60;
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = 6,
.rxtx = LMIC_UNUSED_PIN,
.rst = 5,
.dio = {2, 3, 4},
};
#define DEBUG
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
Serial.println(F("EV_JOINED"));
break;
case EV_RFU1:
Serial.println(F("EV_RFU1"));
break;
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
break;
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
if(LMIC.dataLen) {
// data received in rx slot after tx
Serial.print(F("Data Received: "));
Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
Serial.println();
}
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
default:
Serial.println(F("Unknown event"));
break;
}
}
void do_send(osjob_t* j){
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
Serial.println(F("Packet queued"));
}
}
void setup() {
Serial.begin(57600);
Serial.println(F("Starting"));
os_init();
LMIC_reset();
#ifdef PROGMEM
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);
#else
LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
#endif
LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI); // g-band
LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK, DR_FSK), BAND_MILLI); // g2-band
LMIC_setLinkCheckMode(0);
LMIC_setDrTxpow(DR_SF7,14);
do_send(&sendjob);
}
void loop() {
if(digitalRead(Door1Pin) == HIGH){
if (Door1State == LOW) {
Serial.println("Door1 open");
Door1State = HIGH;
mydata[0] = '1';
Door1StateChanged = 1;
}
}
else if (digitalRead(Door1Pin) == LOW) {
if (Door1State == HIGH) {
Serial.println("Door1 closed");
Door1State = LOW;
mydata[0] = '0';
Door1StateChanged = 1;
}
}
if (Door1StateChanged == 1) {
Serial.print("Door1Pin:");
Serial.println(digitalRead(Door1Pin));
Door1StateChanged = 0;
}
if(digitalRead(Door2Pin) == HIGH){
if (Door2State == LOW) {
Serial.println("Door2 open");
Door2State = HIGH;
mydata[1] = '1';
Door2StateChanged = 1;
}
}
else if (digitalRead(Door2Pin) == LOW) {
if (Door2State == HIGH) {
Serial.println("Door2 closed");
Door2State = LOW;
mydata[1] = '0';
Door2StateChanged = 1;
}
}
if (Door2StateChanged == 1) {
Serial.print("Door2Pin:");
Serial.println(digitalRead(Door2Pin));
Door1StateChanged = 0;
}
os_runloop_once();
}
My first and foremost problem is that Arduino IDE 1.8.10 doesn’t compile.
I have problems selecting and installing LMIC -library and what other lib’s are needed.
I have researched the forum, but can’t find a good answer or at least one that is current. Most LMIC librarys say “modified” from IBM LMIC - but IDE says “invalid library” or if I move the files to the library the compiler turns up a LOT of errors.
Can someone help me make the code above work or give me some pointers?