Thanks for your prompt reply!
You mean my e-mail address? To be honest, I’m not comfortable with posting my e-mail address in a public forum.
Here’s the code I’m using:
#include <AltSoftSerial.h>
#include <Adafruit_Sensor.h>
//#include <Adafruit_BME280.h>
#include <lmic.h>
#include <hal/hal.h>
#include <LowPower.h> // from Rocket Scream Electronics, 1.6.0
#include "Wire.h" // for communication with I2C devices (here: BME280)
#include "SparkFunBME280.h"
#define ALTITUDE 347.0 // Altitude in Binningen, CH
//Adafruit_BME280 bme; // I2C
#include <TheThingsNetwork.h>
#include <CayenneLPP.h> // 1.0.0
#define debugSerial Serial
#define debugPrintLn(...) { if (debugSerial) debugSerial.println(__VA_ARGS__); }
#define debugPrint(...) { if (debugSerial) debugSerial.print(__VA_ARGS__); }
#define debugFlush() { if (debugSerial) debugSerial.flush(); }
float pres;
float hum;
float temp;
//float alt;
BME280 bme;
const int interval = 600; // ~600 seconds (was originally 60s in Oli's Sketch)
static const u1_t PROGMEM NWKSKEY[16] = { /*_Update from TTN_*/ }; // MSB
static const u1_t PROGMEM APPSKEY[16] = { /*_Update from TTN_*/ }; // MSB
static const u4_t DEVADDR = 0x/*_Update from TTN_*/;
void os_getArtEui(u1_t* buf) { }
void os_getDevEui(u1_t* buf) { }
void os_getDevKey(u1_t* buf) { }
static osjob_t sendjob;
bool LMIC_transmitted = false;
int LMIC_event_Timeout = 0;
// Pin mappingm, Dragino board
const lmic_pinmap lmic_pins = {
.nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = { 2, 6, 7 },
};
void onEvent(ev_t ev) {
debugPrint(os_getTime());
debugPrint(": ");
switch (ev) {
case EV_TXCOMPLETE:
debugPrintLn(F("EV_TXC"));
if (LMIC.txrxFlags & TXRX_ACK) {
debugPrintLn(F("R ACK")); // Received ack
}
if (LMIC.dataLen) {
debugPrintLn(F("R "));
debugPrintLn(LMIC.dataLen);
debugPrintLn(F(" bytes")); // of payload
}
LMIC_transmitted = true;
break;
default:
break;
}
}
/*
uint16_t hwCPUVoltage(void)
{
// Measure Vcc against 1.1V Vref
ADMUX = (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1));
ADCSRA |= _BV(ADEN); // enable the ADC
// Vref settle
delay(20);
// Do conversion
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
// return Vcc in mV
return (1125300UL) / ADC;
}
double hwCPUTemperature(void)
{
unsigned int wADC;
double t;
// Set the internal reference and mux
ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
ADCSRA |= _BV(ADEN); // enable the ADC
// Vref settle
delay(20);
// Do conversion
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
// Reading register "ADCW" takes care of how to read ADCL and ADCH.
wADC = ADCW;
// The offset of 324.31 could be wrong. It is just an indication.
t = (wADC - 324.31) / 1.22;
// The returned temperature is in degrees Celsius.
return (t);
}
*/
void do_send(osjob_t* j) {
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
debugPrintLn(F("OP_TXRXPEND, not sending"));
}
else {
debugPrintLn(F("Measurements:"));
Serial.println(temp);
Serial.println(hum);
Serial.println(pres);
//Serial.println(alt);
CayenneLPP lpp(51); // create a buffer of 51 bytes to store the payload
lpp.reset(); // clear the buffer
lpp.addTemperature(1, temp); // on channel 1, add temperature, value 22.5�C
lpp.addRelativeHumidity(2, hum);
lpp.addBarometricPressure(3, pres);
LMIC_setTxData2(1, (xref2u1_t)lpp.getBuffer(), lpp.getSize(), 0);
debugPrintLn(F("Packet queued"));
}
}
void setup() {
bmeSetup();
debugSerial.begin(115200);
//randomSeed(analogRead(0));
debugPrintLn(F("Starting"));
// 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.
// On AVR, these values are stored in flash and only copied to RAM
// once. Copy them to a temporary buffer here, LMIC_setSession will
// copy them into a buffer of its own again.
uint8_t appskey[sizeof(APPSKEY)];
uint8_t nwkskey[sizeof(NWKSKEY)];
(void)memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
(void)memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
LMIC_setSession(0x1, DEVADDR, nwkskey, appskey);
// Set up the channels used by the Things Network, which corresponds
// to the defaults of most gateways. Without this, only three base
// channels from the LoRaWAN specification are used, which certainly
// works, so it is good for debugging, but can overload those
// frequencies, so be sure to configure the full frequency range of
// your network here (unless your network autoconfigures them).
// Setting up channels should happen after LMIC_setSession, as that
// configures the minimal channel set.
// NA-US channels 0-71 are configured automatically
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
// TTN defines an additional channel at 869.525Mhz using SF9 for class B
// devices' ping slots. LMIC does not have an easy way to define set this
// frequency and support for class B is spotty and untested, so this
// frequency is not configured here.
// Disable link check validation
LMIC_setLinkCheckMode(0);
// TTN uses SF9 for its RX2 window.
LMIC.dn2Dr = DR_SF9;
// Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
LMIC_setDrTxpow(DR_SF7, 14);
//disable all channels except channel 0 (868.1 MHz) for communication with single channel gateway
for (int i=1;i<=8;i++)LMIC_disableChannel(i);
}
void loop() {
// Start job
readSENSORS();
do_send(&sendjob);
debugPrintLn(F("W")); // waiting for transmission
LMIC_event_Timeout = 60 * 100; // 60 * 100 times 10mSec = 60 seconds
while (LMIC_event_Timeout-- && !LMIC_transmitted) {
os_runloop_once();
delay(10);
}
if (!LMIC_event_Timeout) {
debugPrintLn(F("ETO, msg not tx"));
}
LMIC_transmitted = false;
LMIC_event_Timeout = 0;
debugPrintLn(F("S")); // sleeping
debugFlush();
for (int i = 0; i < interval; i += 8) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_ON);
}
debugPrintLn("-");
}
/*
float getTemperature()
{
temperature = bme.readTemperature();
}
float getHumidity()
{
humidity = bme.readHumidity();
}
float getPressure()
{
pressure = bme.readPressure();
pressure = bme.seaLevelForAltitude(ALTITUDE,pressure);
pressure = pressure/100.0F;
}
*/
void bmeSetup()
{
bme.settings.commInterface = I2C_MODE;
bme.settings.I2CAddress =0x76;
bme.settings.runMode = 0;
// 0, Sleep mode
// 1 or 2, Forced mode
// 3, Normal mode
bme.settings.tStandby = 0;
// 0, 0.5ms
// 1, 62.5ms
// 2, 125ms
// 3, 250ms
// 4, 500ms
// 5, 1000ms
// 6, 10ms
// 7, 20ms
bme.settings.filter = 0;
// 0, filter off
// 1, coefficients = 2
// 2, coefficients = 4
// 3, coefficients = 8
// 4, coefficients = 16
bme.settings.tempOverSample = 1;
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16
bme.settings.pressOverSample = 1;
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16
bme.settings.humidOverSample = 1;
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16
bme.begin();
delay(10);
}
// -----------------------------------------------------------------------
void readSENSORS()
{
rTEMP();
}
// -----------------------------------------------------------------------
void rTEMP()
{
bmeForceRead();
temp = bme.readTempC();
hum = bme.readFloatHumidity();
pres = bme.readFloatPressure()/100.0F;;
//alt = bme.readFloatAltitudeMeters();
}
// -----------------------------------------------------------------------
void bmeForceRead() {
// set the BME280 sensor in "forced mode" to force a reading.
// after the reading the sensor will go back to sleep mode as set before.
uint8_t value = bme.readRegister(BME280_CTRL_MEAS_REG);
value = (value & 0xFC) + 0x01;
bme.writeRegister(BME280_CTRL_MEAS_REG, value);
// Measurement Time (as per BME280 datasheet section 9.1)
// T_max(ms) = 1.25
// + (2.3 * T_oversampling)
// + (2.3 * P_oversampling + 0.575)
// + (2.4 * H_oversampling + 0.575)
// ~ 9.3ms for current settings
delay(10);
}
// -----------------------------------------------------------------------