Skip to content

Instantly share code, notes, and snippets.

@oksbwn
Last active December 27, 2020 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oksbwn/dbfe260880f706cdeba961f11a77dc5a to your computer and use it in GitHub Desktop.
Save oksbwn/dbfe260880f706cdeba961f11a77dc5a to your computer and use it in GitHub Desktop.
TTGo ESP32 Test Codes
#include <U8x8lib.h>
//OLED Declaration
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
void setup() {
//setup the display
u8x8.begin();
u8x8.setFont(u8x8_font_pxplusibmcgathin_r);
u8x8.drawString(0, 0, "TTGO ESP32 Test");
}
void loop() {
}
//Library used is : https://github.com/mcci-catena/arduino-lmic
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#define DISABLE_PING 0
#define DISABLE_BEACONS 0
static uint8_t NWKSKEY[16] = { 0xF1, 0xD6, 0x3A, 0xD2, 0xC9, 0x87, 0x65, 0x8C, 0xAF, 0x7A, 0x3A, 0xB1, 0x40, 0x1B, 0x96, 0x8A };
static uint8_t APPSKEY[16] = { 0x20, 0x87, 0x15, 0xE2, 0x5E, 0x64, 0x91, 0x04, 0xB5, 0x5F, 0xA6, 0x64, 0x11, 0xBF, 0xBB, 0x7E };
static const u4_t DEVADDR = 0x260115AC; // <-- Change this address for every node!
void os_getArtEui(u1_t * buf) {}
void os_getDevEui(u1_t * buf) {}
void os_getDevKey(u1_t * buf) {}
static uint8_t mydata[] = "Hello, from WGLabz!";
static osjob_t sendjob;
const unsigned TX_INTERVAL = 600;
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = 18, // CS PIN
.rxtx = LMIC_UNUSED_PIN,
.rst = 14, // reset pin
.dio = {
26,
33,
32
}
};
void onEvent(ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
if(ev == 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.print(F("Received "));
Serial.print(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
Serial.println(F("Data is "));
// Change the following codes to process incoming data !!
for(int counter = 0; counter < LMIC.dataLen; counter++){
Serial.print(LMIC.frame[LMIC.dataBeg + counter], HEX);
}
Serial.println(F(" "));
}
// Schedule next transmission
os_setTimedCallback( & sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
}
else{
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
}
}
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.
LMIC_setTxData2(1, mydata, sizeof(mydata) - 1, 0);
Serial.println(F("Packet queued"));
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
// pinMode(13, OUTPUT);
while (!Serial); // wait for Serial to be initialized
Serial.begin(115200);
delay(100); // per sample code on RF_95 test
Serial.println(F("Starting"));
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
LMIC_setSession(0x13, DEVADDR, NWKSKEY, APPSKEY);
LMIC_setupChannel(0, 865062500, DR_RANGE_MAP(DR_SF7, DR_SF12), BAND_CENTI); // g-band
LMIC_setupChannel(1, 865402500, DR_RANGE_MAP(DR_SF7, DR_SF12), BAND_CENTI); // g-band
LMIC_setupChannel(2, 865985000, DR_RANGE_MAP(DR_SF7, DR_SF12), BAND_CENTI); // g-band
LMIC_setupChannel(3, 866550000, DR_RANGE_MAP(DR_SF10, DR_SF10), BAND_CENTI); // g-ban
// Disable link check validation
LMIC_setLinkCheckMode(0);
// Setting up the Downlink data reate. TTN uses SF9 for its RX2 window.
LMIC.dn2Dr = DR_SF10;
// Set data rate and transmit power for uplink
LMIC_setDrTxpow(DR_SF7, 14);
do_send( & sendjob);
}
void loop() {
unsigned long now;
now = millis();
if ((now & 512) != 0) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
os_runloop_once();
}
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <U8x8lib.h>
#define DISABLE_PING 0
#define DISABLE_BEACONS 0
static uint8_t NWKSKEY[16] = { 0xB8, 0xB8, 0x64, 0xFC, 0xBA, 0x11, 0xF5, 0xAB, 0x83, 0x9A, 0x94, 0x90, 0xB2, 0x17, 0x92, 0x8B };
static uint8_t APPSKEY[16] = { 0x40, 0xBC, 0x7B, 0xFF, 0xFD, 0x64, 0xF5, 0xB8, 0x70, 0x35, 0x34, 0x91, 0x96, 0xAF, 0xD4, 0x0A };
static const u4_t DEVADDR = 0x260118D7; // <-- Change this address for every node!
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
U8X8_SSD1306_128X64_NONAME_SW_I2C display_(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
static uint8_t mydata[] = "Hello from WGLabz";
static osjob_t sendjob;
const unsigned TX_INTERVAL = 60;
// Pin mapping for the module
const lmic_pinmap lmic_pins = {
.nss = 18, // CS PIN
.rxtx = LMIC_UNUSED_PIN,
.rst = 14, // reset pin
.dio = {
26,
33,
32
}
};
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
display_.drawString(0, 3, "Data Sent");
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"));
display_.drawString(0, 4, "Data received");
Serial.println(F("Data is "));
// Change the following codes to process incoming data !!
for (int counter = 0; counter < LMIC.dataLen; counter++) {
Serial.print(LMIC.frame[LMIC.dataBeg + counter], HEX);
}
Serial.println(F(" "));
}else
display_.drawString(0, 4, " ");
// 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;
case EV_TXSTART:
Serial.println(F("EV_TXSTART"));
break;
case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED"));
break;
case EV_RXSTART:
/* do not print anything -- it wrecks timing */
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
break;
}
}
void do_send(osjob_t* j){
// Check if there is not a current TX/RX job running
display_.clear();
display_.drawString(0, 0, "TTGO ESP32 LoRa Test");
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
// Prepare upstream data transmission at the next possible time.
LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
Serial.println(F("Packet queued"));
display_.drawString(0, 2, "Packet Queued");
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
// pinMode(13, OUTPUT);
while (!Serial); // wait for Serial to be initialized
Serial.begin(115200);
delay(100); // per sample code on RF_95 test
Serial.println(F("Starting"));
display_.begin();
display_.setFont(u8x8_font_pxplusibmcgathin_r);
display_.drawString(0, 0, "TTGO ESP32 LoRa Test");
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
LMIC_setSession (0x13, DEVADDR, NWKSKEY, APPSKEY);
#if defined(CFG_in866)
Serial.println(F("INDIAN LoRa Band selected."));
display_.drawString(0, 1, "Using IN866 Band");
#endif
// 3 Channels are already defined and comes into play when you set in866 as region
LMIC_setupChannel(3, 866550000, DR_RANGE_MAP(DR_SF10, DR_SF10), BAND_CENTI); // g-ban
// 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
LMIC_setDrTxpow(DR_SF7,14);
// Start job
do_send(&sendjob);
}
void loop() {
unsigned long now;
now = millis();
if ((now & 512) != 0) {
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
os_runloop_once();
}
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <U8x8lib.h>
#define DISABLE_PING 0
#define DISABLE_BEACONS 0
static const u1_t PROGMEM APPEUI[8] = {};
void os_getArtEui(u1_t * buf) { memcpy_P(buf, APPEUI, 8);}
static const u1_t PROGMEM DEVEUI[8] = { };
void os_getDevEui(u1_t * buf) { memcpy_P(buf, DEVEUI, 8);}
static const u1_t PROGMEM APPKEY[16] = { };
void os_getDevKey(u1_t * buf) { memcpy_P(buf, APPKEY, 16);}
U8X8_SSD1306_128X64_NONAME_SW_I2C display_(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
static uint8_t mydata[] = "Hello, from WGLabz!";
static osjob_t sendjob;
const unsigned TX_INTERVAL = 60;
const lmic_pinmap lmic_pins = {
.nss = 18, // CS PIN
.rxtx = LMIC_UNUSED_PIN,
.rst = 14, // reset pin
.dio = {
26,
33,
32
}
};
void printHex2(unsigned v) {
v &= 0xff;
if (v < 16)
Serial.print('0');
Serial.print(v, HEX);
}
void onEvent(ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch (ev) {
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
Serial.println(F("EV_JOINED")); {
u4_t netid = 0;
devaddr_t devaddr = 0;
u1_t nwkKey[16];
u1_t artKey[16];
LMIC_getSessionKeys( & netid, & devaddr, nwkKey, artKey);
Serial.print("netid: ");
Serial.println(netid, DEC);
Serial.print("devaddr: ");
Serial.println(devaddr, HEX);
Serial.print("AppSKey: ");
for (size_t i = 0; i < sizeof(artKey); ++i) {
if (i != 0)
Serial.print(" ");
printHex2(artKey[i]);
}
Serial.println("");
Serial.print("NwkSKey: ");
for (size_t i = 0; i < sizeof(nwkKey); ++i) {
if (i != 0)
Serial.print(" ");
printHex2(nwkKey[i]);
}
Serial.println();
display_.drawString(0, 5, "Network Joined");
}
// Disable link check validation (automatically enabled
// during join, but because slow data rates change max TX
// size, we don't use it in this example.
LMIC_setLinkCheckMode(0);
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)"));
display_.drawString(0, 3, "Data Sent");
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if (LMIC.dataLen) {
Serial.print(F("Received "));
Serial.print(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
display_.drawString(0, 4, "Data received");
Serial.println(F("Data is "));
// Change the following codes to process incoming data !!
for (int counter = 0; counter < LMIC.dataLen; counter++) {
Serial.print(LMIC.frame[LMIC.dataBeg + counter], HEX);
}
Serial.println(F(" "));
}
else
display_.drawString(0, 4, " ");
// 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;
case EV_TXSTART:
Serial.println(F("EV_TXSTART"));
break;
case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED"));
break;
case EV_JOIN_TXCOMPLETE:
Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
break;
}
}
void do_send(osjob_t * j) {
display_.clear();
display_.drawString(0, 0, "TTGO ESP32 LoRa Test");
// 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.
LMIC_setTxData2(1, mydata, sizeof(mydata) - 1, 0);
Serial.println(F("Packet queued"));
display_.drawString(0, 2, "Packet Queued");
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
delay(5000);
while (!Serial)
;
Serial.begin(115200);
Serial.println(F("Starting"));
display_.begin();
display_.setFont(u8x8_font_pxplusibmcgathin_r);
display_.drawString(0, 0, "TTGO ESP32 LoRa Test");
#ifdef CFG_in866
Serial.println(F("Module Configured for Inidian LoRa band (865-867 MHz)"));
display_.drawString(0, 1, "Using IN866 Band");
#endif
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
// LMIC_setClockError(MAX_CLOCK_ERROR * 5 / 100);
do_send( & sendjob);
}
void loop() {
os_runloop_once();
}
@oksbwn
Copy link
Author

oksbwn commented May 13, 2020

The region settings (IN866) is done for TTN_OTTA_Test_IN868.ino sketch using the lmic_project_config.h config file located mostly in C:\Users\user\Documents\Arduino\libraries\MCCI_LoRaWAN_LMIC_library\project_config. The content of the file to set INDIA as region would look like,

// project-specific definitions
//#define CFG_eu868 1
// #define CFG_us915 1
//#define CFG_au915 1
//#define CFG_as923 1
// #define LMIC_COUNTRY_CODE LMIC_COUNTRY_CODE_JP	/* for as923-JP */
//#define CFG_kr920 1
#define CFG_in866 1
#define CFG_sx1276_radio 1
//#define LMIC_USE_INTERRUPTS

All the above code works and tested with the TTGO ESP32 LoRa module with OLED. Link to the module is 2Pcs LILYGO TTGO LORA32 868Mhz ESP32 LoRa OLED 0.96 Inch Blue Display bluetooth WIFI ESP-32 Development Board Module With Antenna

HTB1lBvnSFXXXXXEapXXq6xXFXXX4

@oksbwn
Copy link
Author

oksbwn commented May 19, 2020

Used Libraries:

  1. u8g2 - - - - - -- - - - -- - - -- - -- - - Download used Version
  2. arduino-lmic - - - - - -- -- - - - - - - -Download used Version

@jaantambal
Copy link

Excellent, a query what would be the configuration for US915

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment