Riva:
Maybe the attached code for an Adafruit Feather Lora (AKA Lora32u4) with a BME280 & LDR sensor will help.
I use a different Lora library and it works well for me.
The project is battery powered so sleeps most of the time but every 30 minutes sends data over Lora. I pack the data into a JSON style string so the receiver just forwards the payload onto Node-Red where it gets extracted and used. I opted for this format so I don't need to also reprogram my Lora gateway every time I alter the Feather code/sensors.
jremington:
The following code uses the itoa() function to convert the variable "packetnum" into a human-readable ASCII character array (C-string) and sends it to the receiver.char radiopacket[10] = {0};
itoa(packetnum, radiopacket, 10);
rf95.send((uint8_t *)radiopacket, strlen(radiopacket));
You can do the same for several integer variables, separated by commas, using [the snprintf() function](http://www.cplusplus.com/reference/cstdio/snprintf/). For example:char radiopacket[30] = {0}; //make sure this is large enough
snprintf(radiopacket,sizeof(radiopacket),"%d,%d,%d",pressure, humidity, temperature); //assuming p,h,t are integers
rf95.send((uint8_t *)radiopacket, strlen(radiopacket));
Hey i have been trying out this code and this is what i came up with
#include <LoRa.h>
#include <Adafruit_SleepyDog.h>
#include <Adafruit_BME280.h>
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <RH_RF95.h>
#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 2
#define RF95_FREQ 433.0
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME680 bme;
RH_RF95 rf95(RFM95_CS, RFM95_INT);
uint8_t pktNumber = 0;
float temperature;
float pressure;
float altitude;
float humidity;
// OCCAL Tuning value. Calculated from expected 30 minutes actually taking about 33.5 minutes
#define ocCAL 900
const uint32_t delayRead = 10UL * 1 * ocCAL; // Delay time between reads (10 min)
const uint32_t delaySend = 10UL * 1 * ocCAL; // Delay time between sends (30 Mins)
uint32_t lastReading = delayRead; // Time last reading taken (Set to delayRead so reading happens straight away)
uint32_t lastSending = delaySend; // Time last send happened (Set to delaySend so sending happens straight away)
uint32_t sudoMillis = 0;
bool BME680Setup()
{
bool status = bme.begin();
if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
}
void readBME680() // Get BME280 readings
{
BME680Setup();
// Start with temperature, as that data is needed for accurate compensation.
// Reading the temperature updates the compensat ors of the other functions in the background.
temperature = bme.temperature;
delay(10);
pressure = bme.pressure / 100.0;
delay(10);
altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
delay(10);
humidity = bme.humidity;
delay(10);
}
void sendResults2(Stream& XXX, bool lora = false) // Send readings to Stream
{
if(lora)
{
LoRa.beginPacket();
}
XXX.print("{");
XXX.print("@N@:");
XXX.print(pktNumber);
XXX.print(",");
delay(1);
XXX.print("@T@:");
XXX.print(temperature, 2);
XXX.print(",");
delay(1);
XXX.print("@P@:");
XXX.print(pressure, 2);
XXX.print(",");
delay(1);
XXX.print("@A@:");
XXX.print(altitude, 2);
XXX.print(",");
delay(1);
XXX.print("@H@:");
XXX.print(humidity, 2);
XXX.print(",");
delay(1);
XXX.print("}");
delay(1);
if(lora)
{
LoRa.endPacket();
delay(1000);
pktNumber++;
}
}
/*
void errLoop()
{
while(1)
{
Watchdog.sleep();
}
}
*/
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
Serial.begin(115200);
//#ifdef DEBUG
//while (!Serial); // Wait for Serial Port Connection (Remove in final code)
// #endif
delay(10);
Serial.println("Arduino LoRa TX Test!");
// LoRa.setPins(SS,RST,DI0);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
while (1);
}
Serial.println("LoRa radio init OK!");
LoRa.setPreambleLength(20);
LoRa.enableCrc();
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
BME680Setup();
// Take initial readings to prime the rolling averages
temperature = bme.readTemperature();
delay(10);
pressure = bme.readPressure() / 100.0F;
delay(10);
altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
delay(10);
humidity = bme.readHumidity();
rf95.setTxPower(23, false);
// #ifdef DEBUG
// sendResults2(Serial);
// #endif
Serial.println(F("\nInitial OK!"));
sudoMillis = millis();
}
void loop()
{
// #ifdef DEBUG
uint32_t now = sudoMillis;
while(sudoMillis - now < 10)
{
{
bme.performReading(); // Do the readings
#ifdef DEBUG
Serial.print(F("\nForce Sending Values: "));
sendResults2(Serial);
Serial.println();
#endif
sendResults2(LoRa, true);
}
delay(10);
sudoMillis += 10;
}
if(sudoMillis - lastReading > delayRead)
{
#ifdef DEBUG
Serial.print(F("\nReading Values: "));
#endif
lastReading = sudoMillis;
bme.performReading(); // Do the readings
#ifdef DEBUG
sendResults2(Serial);
#endif
}
if(sudoMillis - lastSending > delaySend) // Time to send readings via LoRa
{
#ifdef DEBUG
Serial.print(F("\nSending Values: "));
sendResults2(Serial);
Serial.println();
#endif
lastSending = sudoMillis;
sendResults2(LoRa, true);
sendResults2(LoRa, true);
//sendResults2(LoRa, true);
// LoRa.sleep(); // Power down radio
}
}
But this is not working at all, not even getting the setup done so what is wrong/how should i fix it. And is it possible to send data via LoRas that are not integers, lets say for more accurate sensor readings,/gps cordinates.?