My goal is to make an automation of a swimming pool (light, pump,...). There are two hardwares : An IPX800v3 from gce electronics and a yun equiped with and lcd shield from sparkfun and a DS1621 (temperature sensor via I2C).
A functionnal diagram is given as attachment...
and here is the code of the sketch (not finished yet but the temperature is sent to the bridge...):
/*---------------------------------------------------------------------------------------------------------------------
BelPoolConsole - Arduino Side
Author : Olivier Boesch
date : nov 2013
---------------------------------------------------------------------------------------------------------------------*/
//-- Mode debug (uncomment to debug)
//#define DEBUG_BELPOOL
//-- Librairies
#include <Wire.h>
#include <LiquidCrystal.h>
#include <TimedEvent.h>
#include <ButtonEvent.h>
#include<MenuBackend.h>
#include <Bridge.h>
#ifdef DEBUG_BELPOOL
#include <Console.h>
#endif
//-- Led d'activité
#define ACT_LED 13
//-- Boutons
#define NUM_KEYS 5
int adc_key_val[5] ={50, 200, 400, 600, 800 };
//-- Sonde de temperature (adresse et mise a jour)
#define DS1621 (0x90 >> 1)
#define TAIR_UPDATE_TIME 10000 //mise a jour de Tair toutes les 10 secondes
//-- Lcd
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define BACKLIGHT_PIN 10
//-- Menu
MenuBackend menu = MenuBackend(menuUse, menuChange);
MenuItem miVolet = MenuItem(menu,"Volet",1);
MenuItem miDeverVolet = MenuItem(menu, "Deverr. Volet",2);
MenuItem miVerVolet = MenuItem(menu, "Verr. Volet",2);
MenuItem miLumiere = MenuItem(menu, "Lumiere",1);
MenuItem miLumiereOn = MenuItem(menu, "Lumiere Mar For",2);
MenuItem miLumiereOff = MenuItem(menu, "Lumiere Arr For",2);
MenuItem miLumiereAuto = MenuItem(menu, "Lumiere Auto",2);
MenuItem miFiltration = MenuItem(menu, "Filtration",1);
MenuItem miFiltrationDebor = MenuItem(menu, "Filt. Debord...",2);
MenuItem miFiltrationFond = MenuItem(menu, "Filt. Fond...",2);
MenuItem miPompe = MenuItem(menu, "Pompe",1);
MenuItem miPompeOn = MenuItem(menu, "Pompe Mar For",2);
MenuItem miPompeOff = MenuItem(menu, "Pompe Arr For",2);
MenuItem miPompeAuto = MenuItem(menu, "Pompe Auto",2);
MenuItem miSysteme = MenuItem(menu, "Systeme",1);
MenuItem miSystemeRedemCent = MenuItem(menu, "Redem. Centrale",2);
MenuItem miSystemeRedemConsole = MenuItem(menu, "Redem. Console",2);
MenuItem miSystemeRedemControlleur = MenuItem(menu, "Redem Controll.",2);
//---- menu utilisé
void menuUse(MenuUseEvent used){
}
void menuChange(MenuChangeEvent changed){
}
//---- Setup de l'arduino
void setup(){
pinMode(BACKLIGHT_PIN,OUTPUT);
pinMode(ACT_LED,OUTPUT);
pinMode(A5,INPUT);
digitalWrite(ACT_LED,HIGH);
Bridge.begin();
#ifdef DEBUG_BELPOOL
Console.begin();
while (!Console); // Attente de connection de la console.
#endif
#ifdef DEBUG_BELPOOL
Console.println("Demarrage de BelPoolConsole...");
#endif
buttons_setup();
lcd_setup();
menu_setup();
ds1621_setup();
TimedEvent.addTimer(TAIR_UPDATE_TIME,Tair_event);
Tair_event(NULL); //force mise a jour de Tair des le debut
digitalWrite(BACKLIGHT_PIN,HIGH);
digitalWrite(ACT_LED,LOW);
}
//---- Configuration des boutons
void buttons_setup(){
//buffer initial pour 5 boutons
ButtonEvent.initialCapacity = sizeof(ButtonInformation)*5;
//Bouton droit
ButtonEvent.addButton(0, //analog button pin
0, //analog value
50, //deviation
onDownBtnRight, //onDown event function
NULL,
NULL,
1000,
NULL,
400);
//Bouton haut
ButtonEvent.addButton(0, //analog button pin
145, //analog value
50, //deviation
onDownBtnUp, //onDown event function
NULL,
NULL,
1000,
NULL,
400);
//Bouton bas
ButtonEvent.addButton(0, //analog button pin
329, //analog value
50, //deviation
onDownBtnDown, //onDown event function
NULL,
NULL,
1000,
NULL,
400);
//Bouton gauche
ButtonEvent.addButton(0, //analog button pin
505, //analog value
50, //deviation
onDownBtnLeft, //onDown event function
NULL, //onUp event function
onHoldBtnLeft, //onHold event function
1000, //hold time in milliseconds
NULL,
400);
//Bouton select
ButtonEvent.addButton(0, //analog button pin
741, //analog value
50, //deviation
onDownBtnSelect, //onDown event function
NULL,
NULL,
1000, //hold time in milliseconds
NULL,
400);
}
void onDownBtnRight(ButtonInformation* Sender) {
#ifdef DEBUG_BELPOOL
Console.println("Bouton Droit");
#endif
}
void onDownBtnLeft(ButtonInformation* Sender) {
#ifdef DEBUG_BELPOOL
Console.println("Bouton Gauche");
#endif
}
void onDownBtnUp(ButtonInformation* Sender) {
#ifdef DEBUG_BELPOOL
Console.println("Bouton Haut");
#endif
}
void onDownBtnDown(ButtonInformation* Sender) {
#ifdef DEBUG_BELPOOL
Console.println("Bouton Bas");
#endif
}
void onDownBtnSelect(ButtonInformation* Sender) {
#ifdef DEBUG_BELPOOL
Console.println("Bouton Select");
#endif
}
void onHoldBtnLeft(ButtonInformation* Sender) {
#ifdef DEBUG_BELPOOL
Console.println("Bouton Gauche Long");
#endif
}
//---- Configuration des menus de l'ecran LCD
void menu_setup(){
#ifdef DEBUG_BELPOOL
Console.print("Setup Menu...");
#endif
//menu.getRoot()
//.onChangeTo(display_idle)
//.add(miVolet);
#ifdef DEBUG_BELPOOL
Console.println("Ok");
#endif
}
//---- Configuration de l'ecran LCD
void lcd_setup(){
#ifdef DEBUG_BELPOOL
Console.print("Setup Lcd...");
#endif
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BelPool v1.0");
lcd.setCursor(0,1);
lcd.print(" Demarrage");
delay(1000);
#ifdef DEBUG_BELPOOL
Console.println("Ok");
#endif
}
//---- Configuration du DS1621
void ds1621_setup(){
#ifdef DEBUG_BELPOOL
Console.print("Setup DS1621...");
#endif
Wire.begin();
Wire.beginTransmission(DS1621); // connect to DS1621 (#0)
Wire.write(0xAC); // Access Config
Wire.write(0x02); // set for continuous conversion
Wire.endTransmission();
delay(50);
Wire.beginTransmission(DS1621); // restart
Wire.write(0xEE); // start conversions
Wire.endTransmission();
#ifdef DEBUG_BELPOOL
Console.println("Ok");
#endif
}
//---- Lecture de la temperature sur le DS1621
float ds1621_read(){
#ifdef DEBUG_BELPOOL
Console.print("Lecture DS1621...");
#endif
float temp = 0.;
Wire.beginTransmission(DS1621);
//lecture de temperature
Wire.write(0xAA);
Wire.endTransmission();
//demande de 2 octets
Wire.requestFrom(DS1621, 2);
//partie entiere de la temperature
temp = (float)((int)(Wire.read()));
//ajout de 0.5 deg au besoin
if(Wire.read()) temp+=0.5;
#ifdef DEBUG_BELPOOL
Console.print("Tair: ");
Console.print(temp);
Console.println(" deg C");
#endif
return temp;
}
//---- Evenement de lecture du DS1621
void Tair_event(TimerInformation* Sender){
//lecture du DS1621 et ecriture sur le bridge REST
digitalWrite(ACT_LED,HIGH);
Bridge.put("Tair",String(ds1621_read()));
digitalWrite(ACT_LED,LOW);
}
//---- Fonction de Boucle
void loop(){
TimedEvent.loop(); //timers
ButtonEvent.loop(); //boutons
//messages_loop(); //messages de la mailbox
}