#include <SPI.h>
#include <LoRa.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define ss 5
#define rst 14
#define dio0 2
#define ObjectID 13
//Put your WiFi Credentials here
const char* ssid = "Galaxy";
const char* password = "012345677";
//URL Endpoint for the API
String URL = "http://api.openweathermap.org/data/2.5/weather?";
String ApiKey = "01ec8d99466d6339ae4fd689ff64fc92";
// Replace with your location Credentials
String lat = "36.74929766915863";
String lon = "3.0838425906615305";
typedef struct __attribute__((__packed__)) {
uint8_t id;
float temp1;
float hum;
} Donnee;
void setup() {
//initialize Serial Monitor
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Receiver");
//setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0);
while (!LoRa.begin(866E6)) {
Serial.println(".");
delay(500);
}
LoRa.setSyncWord(0xF3);
Serial.println("LoRa Initializing OK!");
// We start by connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// try to parse packett
int packetSize = LoRa.parsePacket ();
Donnee donnee;
if (packetSize) {
uint8_t* x_tab = new uint8_t [sizeof (Donnee)];
int i = 0;
while (LoRa.available()) {
uint8_t incoming = LoRa.read();
x_tab[i] = incoming;
i++;
}
Donnee* x = (Donnee *) x_tab;
Serial.print("L'identicatif de l'émetteur: ");
Serial.println(x->id);
Serial.print("Temperature1: ");
Serial.print(x->temp1);
Serial.print( "°C");
Serial.print(" | Humidité1: ");
Serial.print(x->hum);
Serial.println("%");
Serial.println("-----------------------------------");
// wait for WiFi connection
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
//Set HTTP Request Final URL with Location and API key information
http.begin(URL + "lat=" + lat + "&lon=" + lon + "&units=metric&appid=" + ApiKey);
// start connection and send HTTP Request
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
//Read Data as a JSON string
String JSON_Data = http.getString();
//Serial.println(JSON_Data);
//Retrieve some information about the weather from the JSON format
DynamicJsonDocument doc(2048);
deserializeJson(doc, JSON_Data);
JsonObject obj = doc.as<JsonObject>();
//Display the Current Weather Info
const char* description = obj["weather"][0]["description"].as<const char*>();
const float temp = obj["main"]["temp"].as<float>();
const float humidity = obj["main"]["humidity"].as<float>();
const float speed = obj["wind"]["speed"].as<float>();
// Serial.println(description);
Serial.print("Temperature : ");
Serial.print(temp);
Serial.print(" °C | ");
Serial.print(" Humidité: ");
Serial.print(humidity);
Serial.print(" % | ");
Serial.print("Wind speed : ");
Serial.print(speed);
Serial.println("m/s");
}
http.end();
}
//delay(50);
delete x_tab;
}
}