I made both recommended changes to all bits of code.
Data only sends one byte for door1 now.
Data: Payload 01 Door1: 1
EDIT: I found it: removed the minus one from “sizeof(mydata) -1” in sendjob as this was for a “hello world”-sketch originally.
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
// LoRaWAN NwkSKey, network session key
// LoRaWAN AppSKey, application session key
// LoRaWAN end-device address (DevAddr)
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;
uint8_t mydata[] = { 0x00, 0x00 };
// END HERE !!
static osjob_t sendjob;
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)"));
TX_INTERVAL = 3600; // Change reporting complete, go back to longer interval
Serial.println("Transmit repeat set to 60min");
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"));
pinMode(Door1Pin, INPUT_PULLUP);
pinMode(Door2Pin, INPUT_PULLUP);
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(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 (Door1StateChanged == 1 || Door2StateChanged == 1) {
Serial.print("Door1Pin:");
Serial.println(digitalRead(Door1Pin));
Serial.print("Door2Pin:");
Serial.println(digitalRead(Door2Pin));
TX_INTERVAL = 5; // Change detected, push change faster
Serial.println("Transmiting now, 5s interval until sent");
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
Door1StateChanged = 0;
Door2StateChanged = 0;
}
delay(3000);
os_runloop_once();
}
I would gladly have the interval at 60mins (an “I’m alive”-function) and not bombard the radio. Doing triggered sends is desired, but I still haven’t figured out what exactly THIS segment of loop should be (i.e. calling do_send – the syntax for the line):
Serial.println("Transmiting now, 5s interval until sent");
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); //THIS!!
Door1StateChanged = 0;
Door2StateChanged = 0;
}