Arduino_SourceCode_SmartCamera/gateway/gateway.ino

144 lines
3.7 KiB
Arduino
Raw Normal View History

2022-01-14 20:48:28 +01:00
#include "led.h"
2022-01-19 13:31:11 +01:00
#include "sim5215_umts_shield.h"
2022-01-19 14:04:06 +01:00
#include <SPI.h>
#include <LoRa.h>
2022-01-19 13:31:11 +01:00
Shield shield(5,3,4);
2022-01-14 20:48:28 +01:00
2022-01-19 14:04:06 +01:00
const int pinNSS = 10;
const int pinRST = 9;
const int pinDIO0 = 2;
struct message {
int vers;
int type;
int cameraId;
float data;
};
2022-01-20 00:37:51 +01:00
struct message msg = {-1, -1, -1, 0.0};
2022-01-14 20:48:28 +01:00
void setup() {
2022-01-19 14:04:06 +01:00
// Initialisation du moniteur série
2022-01-20 00:37:51 +01:00
Serial.begin(115200);
2022-01-19 14:04:06 +01:00
while (!Serial);
// Initialisation des leds
Serial.println("Initialisation LEDs");
2022-01-14 20:48:28 +01:00
initLed(Green, 8);
initLed(Blue, 7);
initLed(Red, 6);
2022-01-19 13:31:11 +01:00
2022-01-19 14:04:06 +01:00
// Début de l'initialisation
Serial.println("Start initialisation");
2022-01-19 13:31:11 +01:00
blinkLed(Green);
2022-01-19 14:04:06 +01:00
// Initialisation de shield
Serial.println("Initialisation shield UMTS");
2022-01-19 13:31:11 +01:00
shield.power_on();
2022-01-20 00:37:51 +01:00
shield.init("0000", "mmsbouygtel.com", "", "");
2022-01-19 14:04:06 +01:00
// 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");
2022-01-19 13:31:11 +01:00
turnOnLed(Green);
2022-01-14 20:48:28 +01:00
}
2022-01-20 00:37:51 +01:00
void receiveLoRa() {
msg.vers = -1;
2022-01-19 14:04:06 +01:00
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();
2022-01-20 00:37:51 +01:00
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;
2022-01-19 14:04:06 +01:00
Serial.print(LoRaData);
Serial.print("");
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
turnOffLed(Blue);
}
}
2022-01-20 00:37:51 +01:00
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);
2022-01-20 14:21:14 +01:00
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);
2022-01-20 00:37:51 +01:00
}
//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);
2022-01-20 14:21:14 +01:00
turnOnLed(Red);
2022-01-20 00:37:51 +01:00
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");
}
2022-01-20 14:21:14 +01:00
turnOffLed(Red);
2022-01-20 00:37:51 +01:00
} else {
Serial.print("Type not indentified: ");
Serial.println(msg.type);
}
}
2022-01-19 14:04:06 +01:00
}
2022-01-14 20:48:28 +01:00
void loop() {
2022-01-20 00:37:51 +01:00
receiveLoRa();
processMessage();
2022-01-14 20:48:28 +01:00
}