adafruit / TinyLoRa

LoRaWAN Library
68 stars 37 forks source link

Add txpower and frame port support to TinyLoRa. Modify only one example. #26

Closed clavisound closed 4 years ago

clavisound commented 4 years ago

Hello there,

Added support for frame port and txpower. I tested only frame port and it works.

I modified only one arduino (.ino) example accordingly.

If you like my changes, I will try to correct also the other examples. It's my first time in C and OO, so please review my changes if you agree to include them.

Binary size is exactly the same for 32u4.

(I also added a missing spacebar character)

clavisound commented 4 years ago

Hmmm, pass failed because of dht example? I paste the modified version.

Here's the diff

diff -u ../libraries/TinyLoRa/examples/tinylora_dht22/tinylora_dht22.ino tinylora_dht22-clv.ino 
--- ../libraries/TinyLoRa/examples/tinylora_dht22/tinylora_dht22.ino    2019-08-07 16:29:45.000000000 +0300
+++ tinylora_dht22-clv.ino  2019-08-29 15:53:07.919917934 +0300
@@ -26,6 +26,10 @@
 uint8_t DevAddr[4] = { 0x00, 0x00, 0x00, 0x00 };

 /************************** Example Begins Here ***********************************/
+// setup TX settings
+uint8_t const TxPower = 0xff; // Default 0xff.
+uint8_t FramePort = 1;
+
 // Data Packet to Send to TTN
 unsigned char loraData[4];

@@ -57,7 +61,7 @@
   lora.setChannel(MULTI);
   // set datarate
   lora.setDatarate(SF7BW125);
-  if(!lora.begin())
+  if(!lora.begin(TxPower))
   {
     Serial.println("Failed");
     Serial.println("Check your radio");
@@ -103,7 +107,7 @@
   loraData[3] = lowByte(humidInt);

   Serial.println("Sending LoRa Data...");
-  lora.sendData(loraData, sizeof(loraData), lora.frameCounter);
+  lora.sendData(loraData, sizeof(loraData), lora.frameCounter, FramePort);
   Serial.print("Frame Counter: ");Serial.println(lora.frameCounter);
   lora.frameCounter++;

Full score :-)

// TinyLoRa DHT22 - ABP TTN Packet Sender (Multi-Channel)
// Tutorial Link: https://learn.adafruit.com/the-things-network-for-feather/using-a-feather-32u4
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Copyright 2015, 2016 Ideetron B.V.
//
// Modified by Brent Rubell for Adafruit Industries, 2018
/************************** Configuration ***********************************/
#include <TinyLoRa.h>
#include <SPI.h>
#include "DHT.h"

// Visit your thethingsnetwork.org device console
// to create an account, or if you need your session keys.

// Network Session Key (MSB)
uint8_t NwkSkey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

// Application Session Key (MSB)
uint8_t AppSkey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

// Device Address (MSB)
uint8_t DevAddr[4] = { 0x00, 0x00, 0x00, 0x00 };

/************************** Example Begins Here ***********************************/
// setup TX settings
uint8_t const TxPower = 0xff; // Default 0xff.
uint8_t FramePort = 1;

// Data Packet to Send to TTN
unsigned char loraData[4];

// How many times data transfer should occur, in seconds
const unsigned int sendInterval = 30;

// Pinout for Adafruit Feather 32u4 LoRa
TinyLoRa lora = TinyLoRa(7, 8, 4);

// Pinout for Adafruit Feather M0 LoRa
//TinyLoRa lora = TinyLoRa(3, 8, 4);

// pin the DHT22 is connected to
#define DHTPIN 10
DHT dht(DHTPIN, DHT22);

void setup()
{
  delay(2000);
  Serial.begin(9600);
  while (! Serial);

  // Initialize pin LED_BUILTIN as an output
  pinMode(LED_BUILTIN, OUTPUT);

  // Initialize LoRa
  Serial.print("Starting LoRa...");
  // define multi-channel sending
  lora.setChannel(MULTI);
  // set datarate
  lora.setDatarate(SF7BW125);
  if(!lora.begin(TxPower))
  {
    Serial.println("Failed");
    Serial.println("Check your radio");
    while(true);
  }
  Serial.println("OK");

  // Initialize DHT
  dht.begin();
}

void loop()
{
   // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println("");

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // encode float as int
  int16_t tempInt = round(t * 100);
  int16_t humidInt = round(h * 100);

  // encode int as bytes
  //byte payload[2];
  loraData[0] = highByte(tempInt);
  loraData[1] = lowByte(tempInt);

  loraData[2] = highByte(humidInt);
  loraData[3] = lowByte(humidInt);

  Serial.println("Sending LoRa Data...");
  lora.sendData(loraData, sizeof(loraData), lora.frameCounter, FramePort);
  Serial.print("Frame Counter: ");Serial.println(lora.frameCounter);
  lora.frameCounter++;

  // blink LED to indicate packet sent
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println("delaying...");
  delay(sendInterval * 1000);
}