#define WAIT_SECS 3600
#if defined(AVR)
#include <avr/pgmspace.h>
#include <Arduino.h>
#elif defined(ARDUINO_ARCH_ESP8266)
#include <ESP.h>
#elif defined(MKL26Z64)
#include <Arduino.h>
#else
#error Unknown architecture in aes.cpp
#endif
#include “lmic.h”
#include “hal/hal.h”
#include <SPI.h>
#include <CayenneLPP.h>
//---------------------------------------------------------
// Sensor declarations
//---------------------------------------------------------
// Frame Counter
int count=0;
// LoRaWAN Application identifier (AppEUI)
// Not used in this example
static const u1_t APPEUI[8] PROGMEM = { 0x47, 0x0C, 0x03, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 }; //msb
// LoRaWAN DevEUI, unique device ID (LSBF)
// Not used in this example
static const u1_t DEVEUI[8] PROGMEM ={ 0x3F, 0x40, 0xC0, 0xBB, 0x97, 0x19, 0x06, 0x25 }; //msb
// LoRaWAN NwkSKey, network session key
// Use this key for The Things Network
unsigned char NwkSkey[16] = { 0x1D, 0x1C, 0x75, 0x2A, 0xF4, 0xFA, 0xF6, 0x45, 0x93, 0x60, 0x13, 0x1F, 0xCF, 0xCB, 0xE6, 0x7E };
// LoRaWAN AppSKey, application session key
// Use this key to get your data decrypted by The Things Network
unsigned char AppSkey[16] = { 0xE5, 0x71, 0x1F, 0x94, 0x05, 0xA3, 0x31, 0x21, 0x8F, 0x56, 0x51, 0x1B, 0x86, 0x6C, 0xF4, 0x75 };
// LoRaWAN end-device address (DevAddr)
// See Learn | The Things Network
// LoRaWAN end-device address (DevAddr)
//#define msbf4_read(p) (u4_t)((u4_t)(p)[0]<<24 | (u4_t)(p)[1]<<16 | (p)[2]<<8 | (p)[3])
//unsigned char DevAddr[4] = { 0x26, 0x01, 0x14, 0x50 }; // ← Change this address for every node!
static const u4_t DevAddr = 0x026011450 ;
// ----------------------------------------------------------------------------
// DECLARATION PIN CAPTEUR US
// ----------------------------------------------------------------------------
#define ECHOPIN 2// Pin to receive echo pulse
#define TRIGPIN 3// Pin to send trigger pulse
// ----------------------------------------------------------------------------
// POUR RECUPERER LA DISTANCE AVEC LE CAPTEUR ULTRASON
// ----------------------------------------------------------------------------
float getDist(){
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW); // Send pin low again
int distance = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
distance= distance/58;
return distance;
}
// ----------------------------------------------------------------------------
// APPLICATION CALLBACKS
// ----------------------------------------------------------------------------
// provide application router ID (8 bytes, LSBF)
void os_getArtEui (u1_t* buf) {
memcpy(buf, APPEUI, 8);
}
// provide device ID (8 bytes, LSBF)
void os_getDevEui (u1_t* buf) {
memcpy(buf, DEVEUI, 8);
}
// provide device key (16 bytes)
void os_getDevKey (u1_t* buf) {
memcpy(buf, NwkSkey, 16);
}
int debug=1;
static osjob_t sendjob;
// Pin mapping
// These settings should be set to the GPIO pins of the device
// you want to run the LMIC stack on.
//
#define LMIC_UNUSED_PIN 0
lmic_pinmap pins = {
.nss = 10, // Connected to pin D10
.rxtx = LMIC_UNUSED_PIN, // For placeholder only, Do not connected on RFM92/RFM95
.rst = 5, // Needed on RFM92/RFM95? (probably not)
.dio = {4,8,LMIC_UNUSED_PIN}, // Specify pin numbers for DIO0, 1, 2
// connected to D4, D8, D7
};
//
void onEvent (ev_t ev) {
//debug_event(ev);
switch(ev) {
// scheduled data sent (optionally data received)
// note: this includes the receive window!
case EV_TXCOMPLETE:
// use this event to keep track of actual transmissions
Serial.print("EV_TXCOMPLETE, time: ");
Serial.println(millis() / 1000);
if(LMIC.dataLen) { // data received in rx slot after tx
//debug_buf(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
Serial.println("Data Received");
}
break;
default:
break;
}
}
void do_send(osjob_t* j){
delay(1); // XXX delay is added for Serial
Serial.print("Time: ");
Serial.println(millis() / 1000);
// Show TX channel (channel numbers are local to LMIC)
Serial.print("Send, txCnhl: “);
Serial.print(LMIC.txChnl);
Serial.print(” Send, freq: ");
Serial.println(LMIC.freq);
Serial.print("Opmode check: ");
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
}
else {
Serial.println("ready to send: ");
float distance= getDist();
Serial.println (distance);
CayenneLPP lpp(20);
lpp.reset();
lpp.addDistance(1, distance);
//LMIC_setTxData2 (1, mydata, sizeof(mydata), 0);
LMIC_setTxData2(1, lpp.getBuffer(), lpp.getSize(), 0);
}
// Schedule a timed job to run at the given timestamp (absolute system time)
os_setTimedCallback(j, os_getTime()+sec2osticks(WAIT_SECS), do_send);
}
// ====================================================================
// SETUP
//
void setup() {
Serial.begin(9600);
// pour le capteur us :
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
digitalWrite(ECHOPIN, HIGH);
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
// Set static session parameters. Instead of dynamically establishing a session
// by joining the network, precomputed session parameters are be provided.
LMIC_setSession (0x1, DevAddr, (uint8_t*)NwkSkey, (uint8_t*)AppSkey);
// Disable data rate adaptation
LMIC_setAdrMode(0);
// Disable link check validation
LMIC_setLinkCheckMode(0);
// Disable beacon tracking
LMIC_disableTracking ();
// Stop listening for downstream data (periodical reception)
LMIC_stopPingable();
// Set data rate and transmit power (note: txpow seems to be ignored by the library)
LMIC_setDrTxpow(DR_SF7,14);
//
// Disable all but chan 0 for Single Channel Node(SCN) operation. Used with SCGateway
// for( int i=1; i<72; i++ )
// LMIC_disableChannel(i);
#if defined(AVR)
Serial.println(“AVR arch”);
#elif defined(ARDUINO_ARCH_ESP8266)
Serial.println(“ESP arch”);
#elif defined(MKL26Z64)
Serial.println(“Teensy arch”);
#else
Serial.println(“WARNING. Unknown Arch”);
#endif
}
// ================================================================
// LOOP
//
void loop() {
do_send(&sendjob);
while(1) {
os_runloop_once();
delay(100);
}
}