Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejKnebl authored Jan 28, 2024
1 parent 8c4d541 commit c1de19e
Show file tree
Hide file tree
Showing 12 changed files with 1,027 additions and 36 deletions.
136 changes: 112 additions & 24 deletions SeeeduinoLoRaWan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
THE SOFTWARE.1 USA
Modified for LoRa@VSB by Ondřej Knebl, 21. 11. 2023
Modified for LoRa@VSB by Ondřej Knebl, 20. 12. 2023
*/

#include "SeeeduinoLoRaWan.h"
Expand All @@ -48,6 +48,7 @@ void LoRaWanClass::init(void)

void LoRaWanClass::setEU433(void)
{
delay(500);
setDataRate(EU433);
delay(500);

Expand Down Expand Up @@ -79,6 +80,7 @@ void LoRaWanClass::setEU433(void)

void LoRaWanClass::setEU868(void)
{
delay(500);
setDataRate(EU868);
delay(500);

Expand Down Expand Up @@ -162,6 +164,7 @@ void LoRaWanClass::setKeysABP(char *DevAddr, char *NwkSKey, char *AppSKey)
memset(cmd, 0, 64);
sprintf(cmd, "AT+ID=DevAddr,\"%s\"\r\n", DevAddr);
sendCommand(cmd);
delay(DEFAULT_TIMEWAIT);
loraPrint(DEFAULT_DEBUGTIME);
}

Expand All @@ -170,6 +173,7 @@ void LoRaWanClass::setKeysABP(char *DevAddr, char *NwkSKey, char *AppSKey)
memset(cmd, 0, 64);
sprintf(cmd, "AT+KEY=NWKSKEY,\"%s\"\r\n", NwkSKey);
sendCommand(cmd);
delay(DEFAULT_TIMEWAIT);
loraPrint(DEFAULT_DEBUGTIME);
}

Expand All @@ -178,6 +182,7 @@ void LoRaWanClass::setKeysABP(char *DevAddr, char *NwkSKey, char *AppSKey)
memset(cmd, 0, 64);
sprintf(cmd, "AT+KEY=APPSKEY,\"%s\"\r\n", AppSKey);
sendCommand(cmd);
delay(DEFAULT_TIMEWAIT);
loraPrint(DEFAULT_DEBUGTIME);
}
}
Expand All @@ -186,15 +191,20 @@ void LoRaWanClass::setKeysABP(char *DevAddr, char *NwkSKey, char *AppSKey)
void LoRaWanClass::setDataRate(_physical_type_t physicalType)
{
char cmd[32];

if(physicalType == EU433)sendCommand("AT+DR=EU433\r\n");
else if(physicalType == EU868)sendCommand("AT+DR=EU868\r\n");
delay(DEFAULT_TIMEWAIT);

memset(cmd, 0, 32);
sprintf(cmd, "AT+DR=%s\r\n", physicalType);
sendCommand(cmd);
loraPrint(DEFAULT_DEBUGTIME);

if(physicalType == EU433)
{
sprintf(cmd, "AT+DR=%s\r\n", "EU433");
sendCommand(cmd);
delay(DEFAULT_TIMEWAIT);
loraPrint(DEFAULT_DEBUGTIME);
} else if(physicalType == EU868) {
sprintf(cmd, "AT+DR=%s\r\n", "EU868");
sendCommand(cmd);
delay(DEFAULT_TIMEWAIT);
loraPrint(DEFAULT_DEBUGTIME);
}
}


Expand Down Expand Up @@ -515,7 +525,7 @@ void LoRaWanClass::setReceiveWindowDelay(_window_delay_t command, unsigned short
void LoRaWanClass::setClassType(_class_type_t type)
{
if(type == CLASS_A)sendCommand("AT+CLASS=A\r\n");
else if(type == CLASS_C)sendCommand("AT+CLASS=B\r\n");
else if(type == CLASS_B)sendCommand("AT+CLASS=B\r\n");
else if(type == CLASS_C)sendCommand("AT+CLASS=C\r\n");
loraPrint(DEFAULT_DEBUGTIME);
}
Expand All @@ -537,15 +547,19 @@ bool LoRaWanClass::setOTAAJoin(_otaa_join_cmd_t command, unsigned char timeout)
else if(command == FORCE)sendCommand("AT+JOIN=FORCE\r\n");
delay(DEFAULT_TIMEWAIT);

memset(_buffer, 0, BEFFER_LENGTH_MAX);
readBuffer(_buffer, BEFFER_LENGTH_MAX, 1);

ptr = strstr(_buffer, "+JOIN: Joined already");
if(ptr)return true;

memset(_buffer, 0, BEFFER_LENGTH_MAX);
readBuffer(_buffer, BEFFER_LENGTH_MAX, timeout);

ptr = strstr(_buffer, "+JOIN: Join failed");
if(ptr)return false;
ptr = strstr(_buffer, "+JOIN: LoRaWAN modem is busy");
if(ptr)return false;
ptr = strstr(_buffer, "+JOIN: Network joined");
if(ptr)return true;

return true;
return false;
}


Expand All @@ -556,6 +570,13 @@ void LoRaWanClass::setDeviceLowPower(void)
}


void LoRaWanClass::setDeviceLowPowerWakeUp(void)
{
sendCommand("A");
loraPrint(DEFAULT_DEBUGTIME);
}


void LoRaWanClass::setDeviceReset()
{
sendCommand("AT+RESET\r\n");
Expand All @@ -572,17 +593,74 @@ void LoRaWanClass::setDeviceDefault(void)
}


short LoRaWanClass::getBatteryVoltage(void)
float LoRaWanClass::getBatteryVoltage(void)
{
int battery;
float batteryVoltage = 0.0;

batteryVoltage = analogRead(BATTERY_POWER_PIN);
batteryVoltage = batteryVoltage * 11.0;
batteryVoltage = batteryVoltage * 3.3; // reference voltage 3.3 V
batteryVoltage = batteryVoltage / 1024.0; // convert to voltage

pinMode(CHARGE_STATUS_PIN, OUTPUT);
digitalWrite(CHARGE_STATUS_PIN, LOW);
delay(DEFAULT_TIMEWAIT);
battery = (analogRead(BATTERY_POWER_PIN) * 3300 * 11) >> 10;
return batteryVoltage;
}


bool LoRaWanClass::getBatteryStatus(void)
{
pinMode(CHARGE_STATUS_PIN, INPUT);

return battery;
delay(DEFAULT_TIMEWAIT);
bool batteryStatus = digitalRead(CHARGE_STATUS_PIN);

return batteryStatus;
}


float LoRaWanClass::getModuleTemperatureC(void)
{
float moduleTemperatureC = 0.0;

sendCommand("AT+TEMP\r\n");

memset(_buffer, 0, BEFFER_LENGTH_MAX);
readBuffer(_buffer, BEFFER_LENGTH_MAX, 1);

if (containsSubstring(_buffer, "+TEMP:")) {
// Find the colon and parse the temperature
int colonIndex = -1;
for(int i = 0; _buffer[i] != '\0' && i < sizeof(_buffer); i++) {
if(_buffer[i] == ':') {
colonIndex = i;
break;
}
}

if (colonIndex != -1) {
sscanf(&_buffer[colonIndex + 1], "%f", &moduleTemperatureC);
}
}
return moduleTemperatureC;
}


bool LoRaWanClass::containsSubstring(const char* buffer, const char* substring)
{
const char* temp = buffer;

while (*temp) {
bool matches = true;
for (int i = 0; substring[i]; i++) {
if (temp[i] != substring[i]) {
matches = false;
break;
}
}
if (matches) {
return true;
}
temp++;
}
return false;
}


Expand Down Expand Up @@ -614,11 +692,14 @@ short LoRaWanClass::readBuffer(char *buffer, short length, unsigned char timeout
if(timerEnd - timerStart > 1000 * timeout)break;
}

#ifdef PRINT_TO_SERIAL_MONITOR
SerialUSB.print(buffer);
#endif

return i;
}


void LoRaWanClass::loraPrint(unsigned char timeout)
{
unsigned long timerStart, timerEnd;
Expand All @@ -627,7 +708,14 @@ void LoRaWanClass::loraPrint(unsigned char timeout)

while(1)
{
while(SerialLoRa.available()) SerialUSB.write(SerialLoRa.read());
while(SerialLoRa.available())
{
#ifdef PRINT_TO_SERIAL_MONITOR
SerialUSB.write(SerialLoRa.read());
#else
SerialLoRa.read();
#endif
}

timerEnd = millis();
if(timerEnd - timerStart > timeout)break;
Expand Down
29 changes: 27 additions & 2 deletions SeeeduinoLoRaWan.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.1 USA
Modified for LoRa@VSB by Ondřej Knebl, 21. 11. 2023
Modified for LoRa@VSB by Ondřej Knebl, 20. 12. 2023
*/

#ifndef _SEEEDUINOLORAWAN_H_
Expand All @@ -39,6 +39,8 @@

#define SerialLoRa Serial1

//#define PRINT_TO_SERIAL_MONITOR

#define _DEBUG_SERIAL_ 1
#define DEFAULT_TIMEOUT 5 // second
#define DEFAULT_TIMEWAIT 100 // millisecond
Expand Down Expand Up @@ -405,6 +407,13 @@ class LoRaWanClass
* \return Return null
*/
void setDeviceLowPower(void);

/**
* \brief Wake up device from low power mode
*
* \return Return null
*/
void setDeviceLowPowerWakeUp(void);

/**
* \brief Reset device
Expand All @@ -425,7 +434,23 @@ class LoRaWanClass
*
* \return Return battery voltage
*/
short getBatteryVoltage(void);
float getBatteryVoltage(void);

/**
* \brief Read battery status
*
* \return Return false while charging, return true while charge done or no battery insert
*/
bool getBatteryStatus(void);

/**
* \brief Read module temperature
*
* \return Return module temperature
*/
float getModuleTemperatureC(void);

bool containsSubstring(const char* buffer, const char* substring);

void loraPrint(unsigned char timeout);

Expand Down
83 changes: 83 additions & 0 deletions examples/433_ABP_Class_A_Hello_LoRa/433_ABP_Class_A_Hello_LoRa.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*******************************************************************************
* The Things Network - Seeeduino - EU433, Class A, ABP
*
* Copyright (c) 2023 Ondřej Knebl, LoRa@VSB
*
* Permission is hereby granted, free of charge, to anyone
* obtaining a copy of this document and accompanying files,
* to do whatever they want with them without any restriction,
* including, but not limited to, copying, modification and redistribution.
* NO WARRANTY OF ANY KIND IS PROVIDED.
*
* Hello, LoRa!, 20. 12. 2023
*******************************************************************************/

#include <SeeeduinoLoRaWan.h>
LoRaWanClass lora;

// Comment out the line #define PRINT_TO_SERIAL_MONITOR
// in "C:\Users\User\Documents\Arduino\libraries\SeeeduinoLoRaWan\SeeeduinoLoRaWan.h"
// to disable printing to Serial Monitor.

//-------------- Here change your keys --------------
#define DEV_ADDR "00000000"
#define NWK_S_KEY "00000000000000000000000000000000"
#define APP_S_KEY "00000000000000000000000000000000"
//---------------------------------------------------

const unsigned TX_INTERVAL = 300; // Transmission interval in seconds
static uint8_t mydata[] = "Hello, LoRa!";


void sendAndReceiveData() {

SerialUSB.println("Sending - Hello, LoRa!");
bool result = lora.transmitPacket(mydata, sizeof(mydata)-1);

if(result) {
SerialUSB.println("Data sent successfully!");
short length;
char buffer[256];
short rssi;

memset(buffer, 0, 256);
length = lora.receivePacket(buffer, 256, &rssi);

if(length) {
SerialUSB.print("Length: ");
SerialUSB.println(length);
SerialUSB.print("RSSI: ");
SerialUSB.println(rssi);
SerialUSB.print("Data: ");
for(unsigned char i = 0; i < length; i ++) {
SerialUSB.print("0x");
SerialUSB.print(buffer[i], HEX);
SerialUSB.print(" ");
}
SerialUSB.println();
}
}
}


void setup(void) {
lora.init();

SerialUSB.begin(9600);
while(!SerialUSB);

lora.setDeviceReset();
lora.getVersion();
lora.setActivation(LWABP);
lora.setKeysABP(DEV_ADDR, NWK_S_KEY, APP_S_KEY);
lora.setEU433();
lora.setClassType(CLASS_A);
lora.setPort(1);
}

void loop(void) {

sendAndReceiveData();

delay(TX_INTERVAL*1000);
}
Loading

0 comments on commit c1de19e

Please sign in to comment.