Note: I have updated this post, due to resolving part of the issue.
I am using the LMIC library by @matthijskooijman to transmit temperature and humidity data from a DHT11 sensor. The microcontroller I am using is a TTGO ESP32 SX1276 without the OLED. For some reason, when I do a transmission, by using the do_send(&sendjob)
function, my console shows:
3rd. Downlink (which I have not specified anything for)
2nd. Uplink
1st. Connects
Please ignore the additional bytes I am sending. I’m clearing this up at the moment and doing some testing.
These are the downlink details in the application console:
These are the downlink details in the gateway console:
Please note that these details are for a different time point, but seeings all downlinks are blank in the application console, I’m assuming they are the same.
This means that two transmissions are occurring, one from the node to the gateway and the other way around. In my code, I make the ESP32 deep sleep, which means that the board should only go through that command once. I have specified this in the TX_COMPLETE
case.
What I want to occur is for only a single uplink, meaning I want the downlink disabled as this is additional wasted airtime, eating into the maximum number of transmissions I can perform.
But, first would anyone know what is causing these two transmission?
Here is my code:
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <dhtnew.h>
//Here we use pin IO14 of ESP32 to read data
DHTNEW dht(14);
// Temperature
float temp_raw;
float temp_multiplied;
int temp_adjusted;
// Humidity
float humid_raw;
float humid_multiplied;
int humid_adjusted;
// Combined Array
static uint8_t combined_array[4];
// APPEUI needs to be in little endian / least-significant-byte (lsb) format
// This is found by clicking on < > next to "Device EUI" and then the two arrows icon
static const u1_t PROGMEM APPEUI[8] = { };
void os_getArtEui (u1_t* buf) {
memcpy_P(buf, APPEUI, 8);
}
// DEVEUI needs to be in little endian / least-significant-byte (lsb) format
// This is found by clicking on < > next to "Device EUI" and then the two arrows icon
static const u1_t PROGMEM DEVEUI[8] = { };
void os_getDevEui (u1_t* buf) {
memcpy_P(buf, DEVEUI, 8);
}
// APPKEY needs to be in big endian / most significant byte format (msb) format
// This is found by clicking on < > next to "Device EUI"
static const u1_t PROGMEM APPKEY[16] = { };
void os_getDevKey (u1_t* buf) {
memcpy_P(buf, APPKEY, 16);
}
static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations)
const unsigned TX_INTERVAL = 5;
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = 18,
.rxtx = LMIC_UNUSED_PIN,
.rst = 14,
.dio = {26, 33, 32},
};
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"));
// Disable link check validation (automatically enabled
// during join, but not supported by TTN at this time)
LMIC_setLinkCheckMode(0);
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.txrxFlags & TXRX_ACK) {
Serial.println(F("Received ack"));
}
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
}
//Schedule next transmission
delay(10);
esp_deep_sleep(10000000);
//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) {
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
// Prepare upstream data transmission at the next possible time
// Temperature
LMIC_setTxData2(1, combined_array, sizeof(combined_array)+4, 0);
//FIRST 1 is port number
Serial.println(F("Packet queued"));
}
// Next TX is scheduled after TX_COMPLETE event
}
void setup() {
Serial.begin(115200);
SPI.begin(5, 19, 27);
Serial.println("The sensor just started up! #Finallyitsworking!");
// Call read to get data from sensor
dht.read();
// Read temperature as Celsius (the default)
temp_raw = dht.temperature;
temp_multiplied = temp_raw*10;
temp_adjusted = int(temp_multiplied);
Serial.print("To show you the sensor is working, this is the temperature: ");
Serial.print(temp_adjusted/10);
Serial.println(" *C");
// Read humidity as a percentage
humid_raw = dht.humidity;
humid_multiplied = humid_raw*10;
humid_adjusted = int(humid_multiplied);
Serial.print("To show you that the sensor is working, this is the humidity: ");
Serial.print(humid_adjusted/10);
Serial.println(" %");
// Combining both temperature and humidity arrays, ready for tranmission
memcpy(&combined_array, &temp_adjusted, sizeof(temp_adjusted));
memcpy(&combined_array + sizeof(temp_adjusted), &humid_adjusted, sizeof(humid_adjusted));
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded
LMIC_reset();
// Start job (sending automatically starts OTAA too)
do_send(&sendjob);
}
void loop()
{
os_runloop_once();
}
And here is my console output from the ESP32 waking to deep sleep:
rst:0x5 (DEEPSLEEP_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:952
load:0x40078000,len:6084
load:0x40080000,len:7936
entry 0x40080310
The sensor just started up! #Finallyitsworking!
To show you the sensor is working, this is the temperature: 25 *C
To show you that the sensor is working, this is the humidity: 59 %
Packet queued
4898: EV_JOINING
1146049: EV_JOINED
1431693: EV_TXCOMPLETE (includes waiting for RX windows)
Received ack
ets Jun 8 2016 00:22:57