-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ino
90 lines (74 loc) · 2.13 KB
/
client.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <SPI.h>
#include <LoRa.h>
#include "DHT.h"
#include <ArduinoJson.h>
#define DHTPIN 13 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Define the pins used by the LoRa transceiver module
#define NSS 4
#define RST 5
#define DI0 2
// Create a JSON document to hold the incoming data
DynamicJsonDocument doc(2048);
// Function prototypes
void initializeLora();
void sendPacket(String data);
void pushJsonDoc(String name, float data);
// =========================================================
void setup() {
Serial.begin(9600);
dht.begin();
initializeLora();
}
// =========================================================
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.println(F("°F"));
pushJsonDoc("Humidity", h);
pushJsonDoc("Temperature_C", t);
pushJsonDoc("Temperature_F", f);
String jsonPacket;
serializeJson(doc, jsonPacket);
Serial.print("Send Lora JSON : ");
Serial.print(jsonPacket);
sendPacket(jsonPacket);
delay(5000);
}
// =========================================================
// Initialize the LoRa module
void initializeLora(){
Serial.println("LoRa Sender");
// Setup the LoRa sender
LoRa.setPins(NSS, RST, DI0);
while (!LoRa.begin(433E6)) {
Serial.print(".");
delay(500);
}
LoRa.setSyncWord(0xF1);
Serial.println("LoRa Initializing Successful!");
}
// =========================================================
// Send a packet via LoRa
void sendPacket(String data){
LoRa.beginPacket();
LoRa.print(data);
LoRa.endPacket();
}
// =========================================================
// Add data to the JSON Document
void pushJsonDoc(String name, float data){
doc[name] = data;
}