I’m looking for some help regarding sending sensor data as a string over a LoRa network. I’ve been working on adapting a sketch I found on line that will allow me to send multiple sensors with one node over a LoRa network.
https://www.electroniclinic.com/multiple-sensors-monitoring-with-arduino-lora-nodes-sx1278-lora/
It’s a nice project and works fine with the three sensors used in the sketch. I’m trying to adapt it for use on my cottage’s well and tanks. I had no trouble adding an ultrasonic sensor. I also wanted to monitor the voltage of the battery powering the system. Again no trouble building that with a couple of resistors. The voltage sensor works fine at the sensor end. For instance giving me a reading of 8.63 volts. The problem comes when I send it through the LoRa to the master. The result at that end is a single digit reading, for instance, the previous reading is upscaled to 9 volts. Even changing from an int to a float, all I get is 9.00 volts. I think the problem is sending the processed data as a string).
I’ve thought about sending just the raw data (analogInput) through to the master and processing it there. But there must be a simpler way.
Any ideas?
I've stripped the other two sensors out of the original program to make it easier(I think ) to troubleshoot.
/*
Lora Node 1
*/
#include <SPI.h> // include libraries
#include <LoRa.h>
#include <OneWire.h>
int analogInput = A1;
float vout = 0.0;
float vin =0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float PinFloat = A1 *5.0/1023.0;
int value = 0;
String outgoing; // outgoing message
byte msgCount = 0; // count of outgoing messages
byte MasterNode = 0xFF;
byte Node1 = 0xBB;
float Sensor2 = 0; // Voltage Divider
String Mymessage = "";
void setup() {
Serial.begin(9600); // initialize serial
pinMode(analogInput, INPUT);
if (!LoRa.begin(433E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing
}
Serial.println("LoRa init succeeded.");
}
void loop() {
{
value = analogRead(analogInput);
vout = (value * 5.0)/ 1024.0;
vin = vout / (R2/(R1+R2));
Sensor2 = (vin);
Serial.println (vin);
}
Mymessage = Mymessage + Sensor2 +"," + Sensor2 ;
sendMessage(Mymessage,MasterNode,Node1);
delay(100);
Mymessage = "";
Serial.println (Sensor2);
}
void sendMessage(String outgoing, byte MasterNode, byte otherNode) {
LoRa.beginPacket(); // start packet
LoRa.write(MasterNode); // add destination address
LoRa.write(Node1); // add sender address
LoRa.write(msgCount); // add message ID
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket(); // finish packet and send it
msgCount++; // increment message ID
}
I know someone is going to ask why I have "Sensor2" twice in the line Mymessage= Mymessage + Sensor2 +"," + Sensor2 ; The reason is it won't work if I take one of the duplicates out. It probably has to do with the original program that had multiple sensors.
Here's the master (receiver)
/*
Master Lora Node
*/
#include <SPI.h> // include libraries
#include <LoRa.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
byte MasterNode = 0xFF;
byte Node1 = 0xBB;
String SenderNode = "";
String outgoing; // outgoing message
byte msgCount = 0; // count of outgoing messages
String incoming = "";
float Sensor2 = 0; // voltage Sensor
void setup() {
Serial.begin(9600); // initialize serial
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(500);
display.clearDisplay();
display.setTextColor(WHITE);
if (!LoRa.begin(433E6)) { // initialize ratio at 915 MHz
Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing
}
Serial.println("LoRa init succeeded.");
}
void loop() {
// parse for a packet, and call onReceive with the result:
onReceive(LoRa.parsePacket());
}
void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return
// read packet header bytes:
int recipient = LoRa.read(); // recipient address
byte sender = LoRa.read(); // sender address
if( sender == 0XBB )
SenderNode = "Node1:";
byte incomingMsgId = LoRa.read(); // incoming msg ID
byte incomingLength = LoRa.read(); // incoming msg length
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
if (incomingLength != incoming.length()) { // check length for error
//Serial.println("error: message length does not match length");
;
return; // skip rest of function
}
// if the recipient isn't this device or broadcast,
if (recipient != Node1 && recipient != MasterNode) {
// Serial.println("This message is not for me.");
;
return; // skip rest of function
}
String r = getValue(incoming, ',', 1); // voltage Sensor
Sensor2 = r.toInt();
incoming = "";
//clear display
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 30);
display.print("Volts:"+String(Sensor2));
Serial.println ("Volts: " +String(Sensor2));
display.display();
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
So any ideas on how to solve this problem would be appreciated.
Jeff