Hello, I am writing a code that transmits data via LoRa (rfm95)
This code prints out and transmits the BME280 sensor data. After two seconds, it should print out the sensor value for the accelerometer and transmit the data via LoRa. However, this code does not print the accelerometer value. I've checked that all the sensors are working properly.
Here's the code:
#include <SPI.h>
#include <RH_RF95.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 2
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
int val_X;
int val_Y;
int val_Z;
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
while (!Serial);
Serial.begin(9600);
delay(100);
Serial.println("Arduino LoRa TX Test!");
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
while (1);
}
Serial.println("LoRa radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
rf95.setTxPower(23, false);
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
int16_t packetnum = 0; // packet counter, we increment per xmission
void loop()
{
float t = bme.readTemperature();
float p = bme.readPressure() / 100.0F;
float a = bme.readAltitude(SEALEVELPRESSURE_HPA);
float h = bme.readHumidity();
String data = String(t) + "-" + String(p) + "-" + String(a) + "-" + String(h);
int dataLength = data.length(); dataLength++;
uint8_t total[dataLength];
data.toCharArray(total, dataLength);
Serial.println(data);
rf95.send(total, dataLength);
rf95.waitPacketSent();
delay(2000);
float val_X = analogRead(A0);
float val_Y = analogRead(A1);
float val_Z = analogRead(A2);
String data1 = String(val_X) + "-" + String(val_Y) + "-" + String(val_Z);
int datalength = data1.length(); datalength++;
uint8_t total1[datalength];
data1.toCharArray(total1, datalength);
Serial.println(data1);
rf95.send(total1, datalength);
rf95.waitPacketSent();
delay(2000);
}
I'm an amateur myself, so I'm not certain here, but it may be worth checking if your analogRead FLOAT values are being cast in to strings correctly.
It looks like the difference between the data that it is printing and the data that it is not printing is that in one case you are getting the data from a separate sensor, and in the other case you are getting data directly from an analog value on the Arduino. I've had problems in the past casting a float to something that can print in Serial Monitor.
Is your external sensor (pressure, temp, etc) really providing float values, or are they INTs that you are casting as floats then casting as strings? I can guarantee that the analogRead values that you are getting from the Arduino will definitely be floats and will be hard to cast as either string or CHAR.
Again, I'm no expert, but I have definitely had problems starting with a sensor value that was analog, reading it as a float, then casting it as a string or char, then sending it over serial. Seems like a good place to start. Hope that helps!
I'm using Arduino Uno for this project.
It prints out bme280 data without a problem but does not print accelerometer data. I think something is wrong with the codes rf95.send() and rf95.waitPacketSent();. If I remove these codes, it prints the accelerometer data. But, I can't remove this line of code since the LoRa should confirm that the message was sent.
This appears in my serial monitor.
12:32:58.941 -> Arduino LoRa TX Test!
12:32:59.071 -> LoRa radio init OK!
12:32:59.103 -> Set Freq to: 915.00
12:32:59.201 -> 27.35-987.67-215.21-55.52
what is the accelerometer module? give a link to the datasheet?
can you upload a schematic of the circuit in particular showing how devices are connected and it is powered?
using the String class is not recommended on low power micros such as the UNO as it can fragment the memory
use char arrays and snprintf(), e.g.
void loop()
{
int t = bme.readTemperature();
int p = bme.readPressure() / 100.0F;
int a = bme.readAltitude(SEALEVELPRESSURE_HPA);
int h = bme.readHumidity();
char text[100]={0};
snprintf(text,100,"%d-%d-%d-%d",t, p, a, h );
int dataLength = strlen(text);
Serial.println(text);
rf95.send(text, dataLength);
rf95.waitPacketSent();
delay(2000);
transmit the data as int values convert to floats at the receiver