Hello everyone i have issue with my code i want to send dht11 data using arduino and lora Shield
here is my transmitter
/*---------------------
Send sensor data from dht11 to LORA server demo
Sketch made by miliohm.com
-----------------------*/
#include "DHT.h"
#define DHTPIN 5 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
#include <SPI.h>
#include <RH_RF95.h>
RH_RF95 rf95;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
if (!rf95.init())
Serial.println("init failed");
rf95.setFrequency(433.0);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//Serial.print("Humidity: ");
//Serial.print(h);
//Serial.print(" %\t");
//Serial.print("Temperature: ");
//Serial.print(t);
//Serial.println(" *C ");
String data = String(h) + "-" + String(t);
int dataLength = data.length(); dataLength++;
uint8_t total[dataLength]; //variable for data to send
data.toCharArray(total, dataLength); //change type data from string ke uint8_t
Serial.println(data);
rf95.send(total, dataLength); //send data
rf95.waitPacketSent();
delay(500);
}
And it work perfectly
here is my receiver
#include <SPI.h>
#include <RH_RF95.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMonoBold12pt7b.h>
RH_RF95 rf95;
int led = 13;
unsigned long int millisBefore;
int turn = 0;
int h, t;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // 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);
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600);
while (!Serial) ; // Wait for serial port to be available
if (!rf95.init())
Serial.println("init failed");
rf95.setFrequency(433.0);
}
void loop()
{
// Now wait for a reply
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
Serial.println("ok");
Serial.print("Received at Server: ");
Serial.println((char*)buf);
String dataTotal = (char*)buf;
Serial.println(dataTotal);
delay(1000);
}
when I open the serial monitor on my receiver I see this
thank you for your reply
I think I did not understand your question well but I can tell you that I have two Arduino boards two Lora shields the two boards are plugged into the same PC
// Now wait for a reply
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
Serial.println("ok");
Serial.print("Received at Server: ");
Serial.println((char*)buf);
String dataTotal = (char*)buf;
Serial.println(dataTotal);
delay(1000);
No. They just set up a buffer full of random garbage and print it. There are no calls to rf95.read() or whatever function would get received characters into the buffer. Look for an example from your LoRa library to see how they do it.
I forgot that part
if (rf95.recv(buf, &len))
{
Serial.print("Received at Server: ");
Serial.println((char*)buf);
String dataTotal = (char*)buf;
Serial.println(dataTotal);
}
Check out this project that sets up a 'uart' connection via LoRa with acks and resends to get reliable connection.
My free pfodDesigner app, mentioned in that project, generates the Arduino code that includes the LoRa Stream. Just remove the pfodApp stuff you don't need.
Hi. It's in the receiving part of the code. It looks like you've used the same example I based my code off. If this is the case, there's a function that you're missing. Here's all my code on my receiving Arduino (I've used the Nokia 5510 LCD screen so you'll have to pick through it). I've commented it for myself in detail because I'm learning too (so my comments might be a bit inaccurate).
#include <RH_RF95.h>
#include <SPI.h>
#include <LCD5110_Graph.h>
// Program for receiving signal via LoRa RF95 and displaying the value on a Nokia 5510 LCD display.
// Based on LC5110_Graph_Demo by RinkyDinkElectronics.com
// LCD module is connected to
// the following pins using a levelshifter to get the
// correct voltage to the module.
// E.G. A.K.A. PIN
// SCK - SCK Pin 5
// MOSI - DIN Pin 4
// DC - Pin 3
// RST - Pin 8
// CS - Pin 10
//
// Set up the RF shield
RH_RF95 rf95;
// Ints for the program
int turn = 0;
int h, t;
int LEDPIN = 11;
LCD5110 myGLCD(5,4,3,8,10);
// Initiate size of font for the LCD
extern uint8_t SmallFont[];
void setup()
{
Serial.begin(9600);
while (!Serial) ; // Wait for serial port to be available
if (!rf95.init())
Serial.println("init failed");
rf95.setFrequency(915.0); // set the frequency to 915.0 MHz
pinMode(LEDPIN, OUTPUT); // LEDPIN is pin 11 for the LED testing light.
myGLCD.InitLCD(); // Initiate the LCD Screen
}
void loop()
{
// Now wait for a reply
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.waitAvailableTimeout(500))
{
if (rf95.recv(buf, &len))
{
Serial.print("Received at Server: ");
Serial.println((char*)buf); // Prints the array of messages
String dataTotal = (char*)buf; // Converts the array into a string of data
Serial.println(dataTotal); // Prints the data on a new line
explode(dataTotal); // send the string "dataTotal" to the funtion "explode". This will return nothing
}
else
{
Serial.println("recv failed"); // If you didn're receive something, say so.
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myGLCD.print("No Reception", CENTER, 0);
}
}
}
void explode(String req) {
char str[20]; // makes an array of 20 characters
req.toCharArray(str, 20); // puts the string's characters in a buffer called str
char * pch; // declare a pointer variable where the first character in the string will bp
pch = strtok (str, "-"); // the first character for the above variable is the "-"
while (pch != NULL) // until you get to "-" the numbers
{
String ValuesFromLoRa = pch; // a string called 'ValuesFromLoRa' is what the pointer points to
turn++; // make this the first turn
if (turn == 1) {
Serial.print("Humidity : "); // print the value of what the pointer is pointing to
Serial.println(pch); // print to the serial the value what the pointer points to
h = ValuesFromLoRa.toFloat(); // make the first value of ValuesFromLoRa variable, the int, h
myGLCD.clrScr(); // clear the LCD
myGLCD.setFont(SmallFont);
myGLCD.print("Humidity (%):", CENTER, 0);
myGLCD.printNumI(h,CENTER,10);
myGLCD.update();
delay(500);
}
if (turn == 2) { // on the 2nd turn
Serial.print("Temperature : "); //
Serial.println(pch); // print the value what the pointer points to
t = ValuesFromLoRa.toFloat(); // put the value after the "-" into the int, t
myGLCD.setFont(SmallFont); // make it small font
myGLCD.print("Temp (degC):", CENTER, 22); // print to the LCD
myGLCD.printNumI(t, CENTER, 30); // print the value to the LCD
myGLCD.update(); // update the LCD with the above
delay (500);
}
if (turn == 2){
for (int i=0; i<3; i++) // do some flashing, but only when both values are printed
{
myGLCD.invert(true);
delay(500);
myGLCD.invert(false);
delay(500);
}
}
pch = strtok (NULL, "-"); // what the pointer points to is everything before or after the "-"
delay(500);
}
turn = 0;
}