PlatformIO LoraWan project code

The Main.cpp class has my HeltecLora class on it.
It instantiates the class the calls hl.Setup(), and hl.Loop();

You need to create a folder in your libraries folder called HeltecLora and place HeltecLora.h and Heltec.cpp in it.

All of the action takes place in this HeltecLora.cpp method
void HeltecLora::prepareTxFrame( uint8_t port )
Comment out my DHT temp humid code, but you can use the battery code as that came from heltec.
Add your devEui, appEui and appKey down at the bottom of this file.

Let me know if you have any platformio questions. I have finally figured out how to run code from it rather than Arduino. I think it is way better.

platformio.ini

[env:cubecell_AB02_lcd]
platform = heltec-cubecell
board = cubecell_board_plus
framework = arduino
monitor_speed = 115200
board_build.mcu = asr6502
board_build.f_cpu = 48000000L

build_src_filter = 
	+<main.cpp>
	;+<bat3.cpp>
	;+<HeltecLora.cpp>

lib_extra_dirs =
	c:\source\PlatformIO\_Tps-Libraries
	c:\source\PlatformIO\_Libraries

build_flags = -Wl,-Map,output.map
board_build.arduino.lorawan.region = US915
board_build.arduino.lorawan.class = CLASS_A
board_build.arduino.lorawan.netmode = OTAA
board_build.arduino.lorawan.uplinkmode = UNCONFIRMED
board_build.arduino.lorawan.rgb = ACTIVE
board_build.arduino.lorawan.debug_level = FREQ_AND_DIO ; debug_level can be NONE, FREQ, FREQ_AND_DIO
board_build.arduino.lorawan.adr = OFF
board_build.arduino.lorawan.at_support = OFF
board_build.arduino.lorawan.net_reserve = OFF

[env:cubecell_AB02A_ant]
platform = heltec-cubecell
board = cubecell_node
board_build.mcu = asr6502
board_build.f_cpu = 48000000L
framework = arduino
monitor_speed = 115200
build_src_filter = 
	+<main.cpp>
	;+<flowmeter.cpp>
	;+<HeltecLora.cpp>

lib_extra_dirs =
	c:\source\PlatformIO\_Tps-Libraries
	c:\source\PlatformIO\_Libraries

build_flags = -Wl,-Map,output.map
board_build.arduino.lorawan.region = US915
board_build.arduino.lorawan.class = CLASS_A
board_build.arduino.lorawan.netmode = OTAA
board_build.arduino.lorawan.uplinkmode = UNCONFIRMED
board_build.arduino.lorawan.rgb = DEACTIVE
board_build.arduino.lorawan.debug_level = FREQ_AND_DIO ; debug_level can be NONE, FREQ, FREQ_AND_DIO
board_build.arduino.lorawan.adr = OFF
board_build.arduino.lorawan.at_support = OFF
board_build.arduino.lorawan.net_reserve = OFF

[env:cubecell_AB01]
platform = heltec-cubecell
board = cubecell_board
framework = arduino
monitor_speed = 115200
board_build.mcu = asr6502
board_build.f_cpu = 48000000L

build_src_filter = 
	+<main.cpp>
	;+<bat3.cpp>
	;+<HeltecLora.cpp>

lib_extra_dirs =
	c:\source\PlatformIO\_Tps-Libraries
	c:\source\PlatformIO\_Libraries

build_flags = -Wl,-Map,output.map
board_build.arduino.lorawan.region = US915
board_build.arduino.lorawan.class = CLASS_A
board_build.arduino.lorawan.netmode = OTAA
board_build.arduino.lorawan.uplinkmode = UNCONFIRMED
board_build.arduino.lorawan.rgb = ACTIVE
board_build.arduino.lorawan.debug_level = FREQ_AND_DIO ; debug_level can be NONE, FREQ, FREQ_AND_DIO
board_build.arduino.lorawan.adr = OFF
board_build.arduino.lorawan.at_support = OFF
board_build.arduino.lorawan.net_reserve = OFF

main.cpp

#include <Arduino.h>
#include "HeltecLora.h"

int minBetweenUpload = 15;

HeltecLora hl(minBetweenUpload);

void setup() {
  Serial.begin(115200);
  delay(1000);
  hl.setup();
}

void loop() {
  hl.loop();
  delay(1000);
}

HeltecLora.h

#ifndef HeltecLora_h
#define HeltecLora_h

	#include "Arduino.h"

	class HeltecLora {
		public:
			HeltecLora(int);
			void setup();
			void loop();
		private:
			void prepareTxFrame( uint8_t );
	};


#endif

HeltecLora.cpp

#include "LoRaWan_APP.h"
#include "Arduino.h"
#include "HeltecLora.h"
#include "TpsConfigDHT.h"

int minutesBetweenUpload ;
extern uint32_t UpLinkCounter;

///////////////////////////
//-------------->> All other properties are at the bottom of this file.
//////////////////////////

HeltecLora::HeltecLora(int minBetweenUpload){  //Constructor

	minutesBetweenUpload = minBetweenUpload;
}

void HeltecLora::setup(){
	dht.begin();
	boardInitMcu();
	deviceState = DEVICE_STATE_INIT;
	LoRaWAN.ifskipjoin();

}
void HeltecLora::loop(){


	switch( deviceState )
	{
		case DEVICE_STATE_INIT:
		{
			printDevParam();
			LoRaWAN.init(loraWanClass, loraWanRegion);
			deviceState = DEVICE_STATE_JOIN;

			// This is a runtime API that is in the Heltec github
			// but has not yet made it to the Arduino install
			// version. (1.0.0) It's here for future reference
			// LoRaWAN.setDataRateForNoADR(DR_3);

			break;
		}
		case DEVICE_STATE_JOIN:
		{
			LoRaWAN.join();
			break;
		}
		case DEVICE_STATE_SEND:
		{
			// Comment out this warning if you "really" do want
			// to enable ADR
			if (loraWanAdr == true)
			{
				Serial.println(">>>> WARNING: ADR is enabled.\n\tThis may reduce the datarate/Spreading Factor after about 100 uplinks");
			}
 
			prepareTxFrame( appPort );
			LoRaWAN.send();
			deviceState = DEVICE_STATE_CYCLE;
			// the following is experimental but does seem to
			// re-initialize the connection correctly. See the note
			// at extern UpLinkCounter above
			if (UpLinkCounter == 65534)
			{
				// force a rejoin
				deviceState = DEVICE_STATE_INIT;
			}
			break;
		}
		case DEVICE_STATE_CYCLE:
		{
			// Schedule next packet transmission
			txDutyCycleTime = appTxDutyCycle + randr( 0, APP_TX_DUTYCYCLE_RND );
			LoRaWAN.cycle(txDutyCycleTime);
			deviceState = DEVICE_STATE_SLEEP;
			break;
		}
		case DEVICE_STATE_SLEEP:
		{
			LoRaWAN.sleep();
			break;
		}
		default:
		{
			deviceState = DEVICE_STATE_INIT;
			break;
		}
	}
}
void HeltecLora::prepareTxFrame( uint8_t port )
{
	float temperature = readDHTTemperature(); //this method prints T to Serial
	float humidity = readDHTHumidity(); //this method prints H to Serial
	uint16_t batteryVoltage = getBatteryVoltage();
;
	delay(100);

	unsigned char *puc;

	puc = (unsigned char *)(&temperature);
	appDataSize = 10;
	appData[0] = puc[0];
	appData[1] = puc[1];
	appData[2] = puc[2];
	appData[3] = puc[3];


	puc = (unsigned char *)(&humidity);
	appData[4] = puc[0];
	appData[5] = puc[1];
	appData[6] = puc[2];
	appData[7] = puc[3];

	appData[8] = (uint8_t)(batteryVoltage>>8);
	appData[9] = (uint8_t)batteryVoltage;

	Serial.print("T=");
	Serial.print(temperature);
	Serial.print("C, RH=");
	Serial.print(humidity);
	Serial.print("%,");
	Serial.print("BatteryVoltage:");
	Serial.println(batteryVoltage);

}

/*the application data transmission duty cycle.  value in [ms].*/
uint32_t appTxDutyCycle = 1000 * 60 * minutesBetweenUpload;
//uint32_t appTxDutyCycle = 1000 * 10 * minutesBetweenUpload;

//AB02-A ant //
uint8_t devEui[] = { 0x60, 0x81, ...};

//AB02 lcd
//uint8_t devEui[] = { 0x60, 0x81... };

uint8_t appEui[] = { 0x60, 0x81, ...};
uint8_t appKey[] = { 0x92, 0x2E, ...};

/* ABP para*/
uint8_t nwkSKey[] = { 0x15, 0xb1, ...};
uint8_t appSKey[] = { 0xd7, 0x2c, ...};
uint32_t devAddr =  ( uint32_t )0x007e6ae1;

/*LoraWan channelsmask, default channels 0-7*/ 
uint16_t userChannelsMask[6]={ 0xFF00,0x0000,0x0000,0x0000,0x0000,0x0000 };

/* NOTE: It is very important that these variables be defined correctly in the Platformio.ini file*/
/*LoraWan region, select in arduino IDE tools*/
LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
/*LoraWan Class, Class A and Class C are supported*/
DeviceClass_t  loraWanClass = LORAWAN_CLASS;
/*OTAA or ABP*/
bool overTheAirActivation = LORAWAN_NETMODE;
/*ADR enable*/
bool loraWanAdr = LORAWAN_ADR;
/* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */
bool keepNet = LORAWAN_NET_RESERVE;
/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = LORAWAN_UPLINKMODE;
/* Application port */
uint8_t appPort = 2;
/*!
* Number of trials to transmit the frame, if the LoRaMAC layer did not
* receive an acknowledgment. The MAC performs a datarate adaptation,
* according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
* to the following table:
*
* Transmission nb | Data Rate
* ----------------|-----------
* 1 (first)       | DR
* 2               | DR
* 3               | max(DR-1,0)
* 4               | max(DR-1,0)
* 5               | max(DR-2,0)
* 6               | max(DR-2,0)
* 7               | max(DR-3,0)
* 8               | max(DR-3,0)
*
* Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
* the datarate, in case the LoRaMAC layer did not receive an acknowledgment
*/
uint8_t confirmedNbTrials = 4;

// This variable, defined in the runtime, tracks the number of uplinks sent.
// When this count reaches 65,535 the connection must be re-established.
// If you do not the device will continue to send data to the network but
// it will not make it to the Helium console.
// The value is tested below and a reconnection is forced if the counter
//  equals 65534.
// refer to FCnt on this documentation page:
// https://developer.helium.com/longfi/mac-commands-fopts-adr