Something went wrong on our end
-
Puissegur Alexis authoredPuissegur Alexis authored
thingz.ino.txt 3.84 KiB
#include "Bouton.h"
#include "Led.h"
#include "Meteo.h"
#include "Screen.h"
#include "Luminosite.h"
#include "Bluetooth.h"
#include "Son.h"
#include "Potentiometer.h"
#define SPAN 3000
#define HEADER_LIGHT_ON "LIGHT"
#define HEADER_SOUND_ON "SOUND"
#define REMAINING_TIME 300000 // 5MIN
#define DATASPAN 300000 // 5MIN
#define ALARMSPAN 10000 // 10SEC
#define FAb4 740
#define LAb4 932
#define BT_NAME "Thingz-INSA"
#define BT_PASSWORD "1234"
/*
Thingz prend ses mesures et envoie les donnes l'appli java (toutes les 5mins par ex)
l'appli java est une IHM configurable qui :
affiche les donnes en temps rel sur des graphiques
sait s'il faut arroser la plante ou non
envoie un mail au prof si besoin pour arroser
envoie via bluetooth si arrosage possible ou urgent et si en semaine ou weekend
son plus ou moins long
Messages bluetooth :
LIGHT;
SOUND;WEEKEND
SOUND;SEMAINE
*/
Bouton monBouton1;
Led maLed1;
Meteo meteo;
Screen monEcran1;
Luminosite luminosite;
Potentiometer pot;
Son buzzer;
Bluetooth bluetooth;
int humi;
int lumi;
int temp;
int humiTerre;
int dataTimeStamp;
unsigned long timeStamp;
boolean ledHasBeenTurnedOn;
boolean alarmHasBeenTurnedOn;
unsigned long timePushed;
unsigned long alarmTimeStamp;
unsigned long lightTimeStamp;
void updateSensors(){
temp = meteo.temperature();
lumi = luminosite.etat();
humi = meteo.getHumidity();
humiTerre = pot.etat();
}
void display(){
monEcran1.setContrast(70, false);
updateSensors();
monEcran1.printMsg("CRYING PLANT !",0);
monEcran1.printMsg(((String)"TEMP : ") + temp,2);
monEcran1.printMsg(((String)"HUMI : ") + humi,3);
monEcran1.printMsg(((String)"LUMI : ") + lumi,4);
monEcran1.printMsg(((String)"TERRE : ") + humiTerre,5);
}
void sendBluetoothDataToServer(int humi, int lumi, int temp, int humiTerre){
String msg = String(humi) + ";" + String(lumi) + ";" + String(temp) + ";" + String(humiTerre);
bluetooth.send(msg);
}
void dealWithSound(){
alarmTimeStamp = millis();
alarmHasBeenTurnedOn = true;
}
void dealWithLight(){
lightTimeStamp = millis();
ledHasBeenTurnedOn = true;
maLed1.allume();
}
void receiveFromBluetooth() {
String buffer;
if (bluetooth.dataAvailable()){
buffer = bluetooth.receive();
}
if (buffer.startsWith(HEADER_LIGHT_ON)){
dealWithLight();
}
else if (buffer.startsWith(HEADER_SOUND_ON)){
dealWithSound();
}
}
void fireAlarm(){
buzzer.tone(FAb4,600);
buzzer.tone(LAb4,200);
}
void displayConnect() {
monEcran1.clear();
//monEcran1.printMsg(" CONNECT TO", 2);
Serial.println(" CONNECT TO");
//monEcran1.printMsg(" TABLET...", 3);
Serial.println(" PC...");
}
void connect() {
displayConnect();
bluetooth.acceptConnection(BT_NAME);
}
void setup()
{
temp = 0;
lumi = 0;
humi = 0;
humiTerre = 0;
timeStamp = 0;
ledHasBeenTurnedOn = false;
alarmHasBeenTurnedOn = false;
timePushed = 0;
alarmTimeStamp = 0;
dataTimeStamp = 0;
lightTimeStamp = 0;
bluetooth.setPassword(BT_PASSWORD);
connect();
}
void loop()
{
receiveFromBluetooth();
// IF WE PUSHED THE BUTTON IN THE LAST 5 MIN
if(millis() - timePushed < REMAINING_TIME){
if(millis() - timeStamp > SPAN){ // REFRESH EVERY 3 SECONDS
timeStamp = millis();
display();
}
} else {
monEcran1.clear();
monEcran1.setContrast(0, false);
}
if (monBouton1.estTenuAppuye()) {
timePushed = millis();
}
if(millis() - dataTimeStamp > DATASPAN){
dataTimeStamp = millis();
sendBluetoothDataToServer(humi,lumi,temp,humiTerre);
}
if(alarmHasBeenTurnedOn){
if(millis() - alarmTimeStamp < ALARMSPAN){
fireAlarm();
} else {
alarmHasBeenTurnedOn = false;
}
}
if(ledHasBeenTurnedOn){
if(millis() - lightTimeStamp > ALARMSPAN){
ledHasBeenTurnedOn = false;
maLed1.eteint();
}
}
}