Arduino_SourceCode_SmartCamera/node/node.ino

120 lines
2.5 KiB
C++

#include <MsTimer2.h>
#include <SPI.h>
#include <LoRa.h>
#define MESURE_PERIOD 10
const int cameraID = 0x1;
const int pinCmdFlash = 4;
const int pinTemperature = A0;
const int pinPir = 2;
const int pinNSS = 10;
const int pinRST = 9;
const int pinDIO0 = 3;
volatile boolean movementState = false;
volatile boolean mesurementState = false;
void setup() {
// Initialisation du moniteur série
Serial.begin(9600);
while (!Serial);
// Initialisation de la commande du Flash
Serial.println("Initialising IR FLASH");
pinMode(pinCmdFlash, OUTPUT);
digitalWrite(pinCmdFlash, LOW);
// Initialisation du capteur PIR
Serial.println("Initialising PIR sensor");
attachInterrupt(digitalPinToInterrupt(pinPir), setMovementState, RISING);
// Initialisation du timer de mesure
Serial.println("Initialising Temperature sensor");
MsTimer2::set(MESURE_PERIOD*1000, setMesurementState);
Serial.println("Initialising LoRa emiter");
while (!LoRa.begin(868E6)) {
Serial.println(".");
delay(500);
}
LoRa.setSyncWord(0x12);
Serial.println("Starting periodic measurement");
MsTimer2::start();
Serial.println("Initialisation done");
}
void setMovementState() {
movementState = true;
}
void setMesurementState() {
mesurementState = true;
}
float readVoltage(int pin) {
int mesure = analogRead(pin);
return (float(mesure)*5.0)/1024.0;
}
float readVoltageNTimes(int pin, int N) {
int mesure = 0;
int i = 0;
for (i=0; i<N; i++) {
mesure += analogRead(pin);
}
return ((float(mesure)/float(N))*5.0)/1024.0;
}
float convertVoltageTemperature(float voltage) {
return (voltage - 0.50)*100.0;
}
float readTemp() {
return convertVoltageTemperature(readVoltageNTimes(pinTemperature, 50));
}
void turnOnFlash() {
digitalWrite(pinCmdFlash, HIGH);
}
void turnOffFlash() {
digitalWrite(pinCmdFlash, LOW);
}
int sendPresence() {
while (!LoRa.beginPacket()) {}
LoRa.print("1-1-");
LoRa.print(cameraID);
LoRa.print("-0");
return LoRa.endPacket();
}
int sendTemperature(float temperature) {
while (!LoRa.beginPacket()) {}
LoRa.print("1-2-");
LoRa.print(cameraID);
LoRa.print("-");
LoRa.print(temperature);
return LoRa.endPacket();
}
void loop() {
if (movementState) {
Serial.println("Mouvement detecté");
sendPresence();
movementState = false;
} else if (mesurementState) {
float temperature = readTemp();
Serial.print("Temperature : ");
Serial.print(temperature);
Serial.println("°C");
sendTemperature(temperature);
mesurementState = false;
}
}