Arduino_SourceCode_SmartCamera/gateway/gateway.ino
2022-01-20 14:21:14 +01:00

143 lines
3.7 KiB
C++

#include "led.h"
#include "sim5215_umts_shield.h"
#include <SPI.h>
#include <LoRa.h>
Shield shield(5,3,4);
const int pinNSS = 10;
const int pinRST = 9;
const int pinDIO0 = 2;
struct message {
int vers;
int type;
int cameraId;
float data;
};
struct message msg = {-1, -1, -1, 0.0};
void setup() {
// Initialisation du moniteur série
Serial.begin(115200);
while (!Serial);
// Initialisation des leds
Serial.println("Initialisation LEDs");
initLed(Green, 8);
initLed(Blue, 7);
initLed(Red, 6);
// Début de l'initialisation
Serial.println("Start initialisation");
blinkLed(Green);
// Initialisation de shield
Serial.println("Initialisation shield UMTS");
shield.power_on();
shield.init("0000", "mmsbouygtel.com", "", "");
// Initialisation LoRa
Serial.println("Initialisation LoRa transceiver");
LoRa.setPins(pinNSS, pinRST, pinDIO0);
while (!LoRa.begin(868E6)) {
Serial.print(".");
delay(500);
}
LoRa.setSyncWord(0x12);
// Fin de l'initialisation
Serial.println("Initialisation completed");
turnOnLed(Green);
}
void receiveLoRa() {
msg.vers = -1;
int packetSize = LoRa.parsePacket();
if (packetSize) {
turnOnLed(Blue);
Serial.print("Received packet '");
while (LoRa.available()) {
String stringData = LoRa.readString();
const char* LoRaData = stringData.c_str();
int partie_entiere;
int partie_decimale;
sscanf(LoRaData, "%d-%d-%d-%d.%d", &(msg.vers), &(msg.type), &(msg.cameraId), &partie_entiere, &partie_decimale);
msg.data = float(partie_entiere) + (float(partie_decimale))/100;
Serial.print(LoRaData);
Serial.print("");
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
turnOffLed(Blue);
}
}
void printMessage() {
if (msg.vers > 0) {
Serial.println("Message : ");
Serial.print("\tVersion : ");
Serial.println(msg.vers);
Serial.print("\tType : ");
Serial.println(msg.type);
Serial.print("\tCamera ID : ");
Serial.println(msg.cameraId);
Serial.print("\tData : ");
Serial.println(msg.data);
Serial.println("");
}
}
void processMessage() {
if (msg.vers > 0) {
delay(50);
const char url[] = "rasp.pikouri.fr";
int port = 80;
//Event
if (msg.type == 1) {
char request[150];
sprintf(request, "POST /api/cameras/%d/counter HTTP/1.1\r\nHost: rasp.pikouri.fr\r\n\r\n", msg.cameraId);
Serial.println(request);
turnOnLed(Red);
shield.sendTextMessage("0672654516", "[Smart Camera 1] Funny bird detected!");
if (shield.sendPOSTrequest(url, port, request, 20000) == 3) {
shield.power_on();
delay(1000);
shield.power_on();
shield.init("0000", "mmsbouygtel.com", "", "");
} else {
Serial.println("Request sent\n\n\n");
}
turnOffLed(Red);
}
//Temperature
else if (msg.type == 2) {
char request[200];
String tempData = String(msg.data);
sprintf(request, "POST /api/cameras/%d/temperature HTTP/1.1\r\nHost: rasp.pikouri.fr\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", msg.cameraId, tempData.length(),tempData.c_str());
Serial.println(request);
turnOnLed(Red);
if (shield.sendPOSTrequest(url, port, request, 20000) == 3) {
shield.power_on();
delay(1000);
shield.power_on();
shield.init("0000", "mmsbouygtel.com", "", "");
} else {
Serial.println("Request sent\n\n\n");
}
turnOffLed(Red);
} else {
Serial.print("Type not indentified: ");
Serial.println(msg.type);
}
}
}
void loop() {
receiveLoRa();
processMessage();
}