Hallo, I want to use two HTCC-AB01 for a simple peer to peer connection.
Can somebody provide me a simpel code?
Where can I get the Library LoRaWan_App.h?
Greetings begraf
2 HTCC-AB01 for a simple P2P connection
hi,
please refer the links:
Project folder:
pingpong code:
LoRaWan_App.h:
This is my initial test code for HTCC-AM02. I use PlatformIO as development environment. #define TX for transmitter and #define RX for receiver. I guess it works also with HTCC-AB01.
#include "LoRaWan_APP.h"
#include "Arduino.h"
#define TX
//#define RX
#define RF_FREQUENCY 868000000 // Hz
#define TX_OUTPUT_POWER 14 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz]
#define LORA_SPREADING_FACTOR 7 // [SF7]
#define LORA_CODINGRATE 4 // [4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON 1
#define LORA_IQ_INVERSION_ON 0
#define FDEV 0 // LoRa: always 0
#define BAND_WIDTH_AFC 0 // LoRa: always 0
#define CRC_ON 0
#define FREQ_HOP_ON 0
#define HOP_PERIOD 0
#define LORA_RX_CONTINOUS 0
#define LORA_SYNC_WORD 0x12
#define RX_TIMEOUT_VALUE 1000
#define TX_TIMEOUT_VALUE 3000
#define BUFFER_SIZE 2
int16_t txNumber;
bool packetSent = false;
bool rxTimeout = false;
bool packetReceived = false;
static RadioEvents_t RadioEvents;
void OnTxDone(void);
void OnTxTimeout(void);
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
void OnRxTimeout(void);
void setup()
{
boardInitMcu();
Serial.begin(115200);
txNumber = 0;
RadioEvents.TxDone = OnTxDone;
RadioEvents.TxTimeout = OnTxTimeout;
RadioEvents.RxDone = OnRxDone;
RadioEvents.RxTimeout = OnRxTimeout;
Radio.Init(&RadioEvents);
Radio.SetChannel(RF_FREQUENCY);
Radio.SetSyncWord(LORA_SYNC_WORD);
Radio.SetPublicNetwork(false);
Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, FDEV, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
CRC_ON, FREQ_HOP_ON, HOP_PERIOD, LORA_IQ_INVERSION_ON, TX_TIMEOUT_VALUE);
Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE, BAND_WIDTH_AFC, LORA_PREAMBLE_LENGTH,
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, BUFFER_SIZE,
CRC_ON, FREQ_HOP_ON, HOP_PERIOD, LORA_IQ_INVERSION_ON, LORA_RX_CONTINOUS);
}
void loop()
{
uint8_t bufPayload[BUFFER_SIZE], len;
#ifdef TX
txNumber++;
bufPayload[0] = 0xAA; bufPayload[1] = 0xBB; len = BUFFER_SIZE;
Serial.printf("\r\nTx packet #%d: [%02X] [%02X] len=%d",txNumber, bufPayload[0], bufPayload[1], len);
packetSent = false;
Radio.Send(bufPayload, len);
do {
Radio.IrqProcess();
} while(!packetSent);
delay(500);
#endif
#ifdef RX
rxTimeout = false;
packetReceived = false;
Radio.Rx(RX_TIMEOUT_VALUE); // set radio to continuous reception mode
while (!packetReceived) {
Radio.IrqProcess();
if (rxTimeout) break; // 1000 ms rx window in use => if no rx go further only every 1000 ms
}
#endif
} // end loop()
void OnTxDone( void )
{
Serial.printf("\r\nTX #%d done.\r\n", txNumber);
packetSent = true;
}
void OnTxTimeout( void )
{
Serial.printf("\r\n TX #%d Timeout...\r\n", txNumber);
packetSent = true;
}
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
{
//memcpy(RxPacket, payload, BUFFER_SIZE);
Serial.printf(" <RX done ");
Serial.printf("\r\n <RxDone [%02X] [%02X] RSSI/SNR %d/%d, len=%d\r\n", payload[0], payload[1], rssi, snr, size);
rxTimeout = false;
packetReceived = true;
}
void OnRxTimeout(void)
{
Serial.println(" <RX Timeout.");
rxTimeout = true;
}
yes, the anwser is Yes.
The Pingpongs code, which I got. is working fine.
Now I have to add the deep sleep function.
Can I merge the two examples?
Thanks for your support
I want to send sensor datas every hour in a P2P Mode. I use twoHTCC-AB01.
Due to your support I can send LoRa datas with the code out of the CubeCell examples and I have the LowPower_WakeUpByTimer code running.
If I mix the two codes, the sleep Funktion is not working any more:
See code
Heltec Automation Send communication test example
*
- Function:
-
- Send data from a CubeCell device over hardware
- this project also realess in GitHub:
- https://github.com/HelTecAutomation/ASR650x-Arduino
- */
#include “LoRaWan_APP.h”
#include “Arduino.h”
/*
- set LoraWan_RGB to 1,the RGB active in loraWan
- RGB red means sending;
- RGB green means received done;
*/
#ifndef LoraWan_RGB
#define LoraWan_RGB 0
#endif
#define RF_FREQUENCY 868400000 // Hz
#define TX_OUTPUT_POWER 14 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7…SF12]
#define LORA_CODINGRATE 1 // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 30 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
static RadioEvents_t RadioEvents;
double txNumber;
int16_t rssi,rxSize;
void DoubleToString( char *str, double double_num,unsigned int len);
/*
- part wake up
*/
#define timetillsleep 5000
#define timetillwakeup 50000
static TimerEvent_t sleep;
static TimerEvent_t wakeUp;
uint8_t lowpower=1;
void onSleep()
{
Serial.printf(“Going into lowpower mode, %d ms later wake up.\r\n”,timetillwakeup);
lowpower=1;
//timetillwakeup ms later wake up;
TimerSetValue( &wakeUp, timetillwakeup );
TimerStart( &wakeUp );
}
void onWakeUp()
{
Serial.printf(“Woke up, %d ms later into lowpower mode.\r\n”,timetillsleep);
lowpower=0;
//timetillsleep ms later into lowpower mode;
TimerSetValue( &sleep, timetillsleep );
TimerStart( &sleep );
}
void setup() {
boardInitMcu( );
Serial.begin(115200);
txNumber=0;
rssi=0;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
/*
- part wake up
*/
Radio.Sleep( );
TimerInit( &sleep, onSleep );
TimerInit( &wakeUp, onWakeUp );
onSleep();
}
void loop()
{
delay(1000);
txNumber += 0.01;
sprintf(txpacket,"%s",“Hello world number”); //start a package
// sprintf(txpacket+strlen(txpacket),"%d",txNumber); //add to the end of package
DoubleToString(txpacket,txNumber,3); //add to the end of package
turnOnRGB(COLOR_SEND,0); //change rgb color
Serial.printf("\r\nsending packet “%s” , length %d\r\n",txpacket, strlen(txpacket));
Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
}
/**
- @brief Double To String
- @param str: Array or pointer for storing strings
- @param double_num: Number to be converted
- @param len: Fractional length to keep
-
@retval None
*/
void DoubleToString( char *str, double double_num,unsigned int len) {
double fractpart, intpart;
fractpart = modf(double_num, &intpart);
fractpart = fractpart * (pow(10,len));
sprintf(str + strlen(str),"%d", (int)(intpart)); //Integer part
sprintf(str + strlen(str), “.%d”, (int)(fractpart)); //Decimal part
if(lowpower){
//note that lowPowerHandler() runs six times before the mcu goes into lowpower mode;
lowPowerHandler();
}
}
can somebody support me?
Thanks
YES, you can refer the factory test code:
it is including:RGB test + pingpong test + external OLED test + lowpower test(Use USE_KEYS enter the lowpower mode)
Thanks for the Information!!
Can you explain more in Detail, what I should do with “USE_KEYS” ?
begraf
Hi,
today I want to share the code of my project with you:
P2P Communication with two CubeCellHTCC AB01.
They exchange the information of a distance, measured by an Ultrasonic sensor and the Vortage of the battery.
Comments are welcome.
/*
- Heltec Automation
- Function:
-
- Measure a distance with a US sensor DYP A06-V1.1 similar to HC SR04
-
- Send data from a CubeCell to another CubeCell
-
- Measure voltage
-
- go for 1 min in Sleep
*/
- go for 1 min in Sleep
#include “LoRaWan_APP.h”
#include “Arduino.h”
int txNumber=0;
long dauer=0;
int voltage =0;
// Hier wird die Zeitdauer abgespeichert die die Ultraschallwelle braucht
long entfernung=0; // Hier wird die Entfernung vom Hindernis abgespeichert
int i=0;
/*
- set LoraWan_RGB to 1,the RGB active in loraWan
- RGB red means sending;
- RGB green means received done;
*/
#ifndef LoraWan_RGB
#define LoraWan_RGB 0
#endif
#define RF_FREQUENCY 868400000 // Hz
#define TX_OUTPUT_POWER 20 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7…SF12]
#define LORA_CODINGRATE 1 // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 30 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
static RadioEvents_t RadioEvents;
int16_t rssi,rxSize;
void DoubleToString( char *str, double double_num,unsigned int len);
#define timetillsleep 1000
#define timetillwakeup 60000
static TimerEvent_t sleep;
static TimerEvent_t wakeUp;
uint8_t lowpower=1;
void onSleep()
{
lowpower=1;
//timetillwakeup ms later wake up;
TimerSetValue( &wakeUp, timetillwakeup );
TimerStart( &wakeUp );
}
void onWakeUp()
{
txNumber ++;
i=0;
sprintf(txpacket,"XXX"); //start a package
sprintf(txpacket+strlen(txpacket)," %d",voltage);
sprintf(txpacket+strlen(txpacket)," %d",dauer);
sprintf(txpacket+strlen(txpacket)," %d",txNumber);
// sprintf(txpacket+strlen(txpacket),"%d",txNumber); //add to the end of package
turnOnRGB(COLOR_SEND,0); //change rgb color
Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
lowpower=0;
//timetillsleep ms later into lowpower mode;
TimerSetValue( &sleep, timetillsleep );
TimerStart( &sleep );
}
void setup() {
pinMode(GPIO1, OUTPUT); // Trigger Pin als Ausgang definieren
pinMode(GPIO2, INPUT); // Echo Pin als Eingang defnieren
boardInitMcu( );
rssi=0;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
Radio.Sleep( );
TimerInit( &sleep, onSleep );
TimerInit( &wakeUp, onWakeUp );
onSleep();
Serial.printf("\r\nsending packet “%s” , length %d\r\n",txpacket, strlen(txpacket));
}
void loop()
{
i=(i+1);
if (i<3)
{
digitalWrite(GPIO1, LOW); // Den Trigger auf LOW setzen
delay(20); // 20 Millisekunden warten
digitalWrite(GPIO1, HIGH); // Den Trigger auf HIGH setzen
delay(20); // 20 Millisekunden warten
digitalWrite(GPIO1, LOW); // Trigger auf LOW setzen
dauer = pulseIn(GPIO2, HIGH); // Die Zeit messen bis die Ultraschallwelle zurĂĽckkommt
uint16_t voltage = getBatteryVoltage();
}
if(lowpower){
turnOffRGB();
//note that lowPowerHandler() runs six times before the mcu goes into lowpower mode;
lowPowerHandler();
}
}
additional Moment, if you want to reach 7uA in Sleep modus including LoRa:
put in Radio.Sleep () in the void onSleep():
void onSleep()
{
lowpower=1;
//timetillwakeup ms later wake up;
Radio.Sleep( );
TimerSetValue( &wakeUp, timetillwakeup );
TimerStart( &wakeUp );
}
Thanks for your sharing!
Hi,
I use a CubeCell AB01 to collect Ultrasonic datas and send them each 30 min.
This is working fine with 7uA in the sleep mode.
#define timetillsleep 1000
#define timetillwakeup 3600000
Now I want to extend the sleeping period to 60 min. This doesn’t work.
Where is the mistake?
Thanks for supporting me!