Roqyun KO / Mbed 2 deprecated MeringueCitron

Dependencies:   mbed DHT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #define GND_TMP_SENSOR A0
00003 #define HUM_MAX 24500.0
00004 #define HUM_MIN 20600.0
00005 
00006 #include "mbed.h"
00007 #include "ADA326.h"
00008 #include "DS1820.h"
00009 //#include "glibr.h"
00010 #include "LinkedList.h"
00011 #include "DHT.h"
00012 #include "FSM.h"
00013 #include "HMI.h"
00014 #include "Sensor.h"
00015 #include "Data.h"
00016 
00017 // Sleep
00018 bool modeSleep;
00019 // Time events
00020 Ticker ticker;
00021 Timer timer;
00022 Ticker chrono;
00023 unsigned int sec = 0;
00024 
00025 // Communication
00026 bool usingI2C;
00027 Serial pc(USBTX, USBRX);
00028 I2C i2c(I2C_SDA, I2C_SCL);
00029 #define _SIGFOX_
00030 #ifdef _SIGFOX_
00031 Serial sg(D1, D0); //Sigfox
00032 #endif
00033 
00034 
00035 /**** Declaration for ****/
00036 // LED & Interrupt
00037 DigitalOut led(D13);
00038 
00039 // Human Machine Inteface
00040 InterruptIn button(D10);
00041 FSM fsm_button(FSM_Button::NB_BUTTON_STATE);
00042 Ada326 lcd;
00043 FSM fsm_display(FSM_Display::NB_DISPLAY_STATE);
00044 
00045 // Sensors - see Sensor.h to activate sensors with macros.
00046 #ifdef _GND_SENSOR_
00047 DS1820 gndTemperatureSensor(GND_TMP_SENSOR); 
00048 AnalogIn gndHumSensor(A2);
00049 #endif
00050 
00051 #ifdef _COLOR_SENSOR_
00052 glibr rgbSensor; // liaison de type I2C avec le module de mesure de lumière
00053 #endif
00054 
00055 #ifdef _DHT_SENSOR_
00056 DHT dht(A1, DHT22);
00057 #endif
00058 ////////////////////////////////////////////
00059 
00060 // Interrupt events
00061 void onRisingEdge(void);
00062 void count_3600s(void);
00063 
00064 // Intialization
00065 void initialize_device(); // Initialize sensors
00066 void initialize_fsm(); // Add finite states / transitions.
00067 
00068 /* 
00069 Repeat the function pointed by the function pointer "fnc" every "interval" seconds.
00070 During the wait, the microcontroler will be on standby mode for low consumption of energy.
00071 */
00072 void repeat(Callback<void()> event, bool &wait, unsigned int interval);
00073 
00074 void measure(); // Activate sensors to measure surroundings and read data.
00075 void transmission_sigfox(); // send data read from sensors using sigfox
00076 
00077 
00078 int main()
00079 {
00080     bool wait_sensor = false, wait_sigfox = false;
00081     // Waiting time in seconds until ...
00082     // next measurement, using sensors:
00083     int SENSOR_SLEEP_TIME = 600;
00084     int SIGFOX_SLEEP_TIME = 600;
00085 
00086     if(SENSOR_SLEEP_TIME >= 3599 || SIGFOX_SLEEP_TIME >= 3599) {
00087         pc.printf("Waiting too long to activate sensors/sigfox");
00088         return -1;
00089     }
00090         
00091     // Initialize.
00092     usingI2C = false;
00093     modeSleep = true; 
00094     initialize_fsm();
00095     initialize_device();
00096     
00097     // Start timer (measurement in second)
00098     chrono.attach(&count_3600s,1);
00099     // Attach a function to be called at button interrupt event.
00100     button.rise(&onRisingEdge);
00101     
00102     while(1) {
00103         // Call "measure" to activate sensors every SENSOR_SLEEP_TIME
00104         repeat(&measure, wait_sensor, SENSOR_SLEEP_TIME);
00105         // Call "transmission_sigfox" to send data every SENSOR_SLEEP_TIME
00106         repeat(&transmission_sigfox, wait_sigfox, SIGFOX_SLEEP_TIME);
00107         
00108         /* 
00109         Execute the task of the current state of FSM.
00110         Transition will be made automatically if the transition is triggered by
00111         conditions imposed by each state.
00112         
00113         cf. the FSM explained at our website.
00114         
00115         FSM is implemented to debounce button; turn on/off and change the display.
00116         */
00117         
00118         // fsm_button <= implemented for debounce.
00119         fsm_button.execute();
00120         fsm_display.execute();
00121     }
00122     return 0;
00123 }
00124 
00125 void repeat(Callback<void()> event, bool &wait, unsigned int interval)
00126 {
00127     if(sec % interval == 0 && !wait) { // Time to call event. 
00128         //Prevent repeated calling within a second by setting the flag, "wait".
00129         event();
00130         wait = true;
00131     }
00132     else if(sec % interval) { // Time to be on standby
00133         if(modeSleep) sleep();
00134         wait = false;
00135     }
00136 }
00137 void measure() 
00138 {
00139     // Signal that sensors are activated.
00140     // It hints that button and display can't be used during this time.
00141     if(display_on) {
00142         lcd.clear(false);
00143         lcd.print("En cours...");  
00144         lcd.display();
00145     }
00146     // Deactivate button / screen.
00147     usingI2C = true;  
00148     
00149     // Start measurement of ...
00150     
00151     // 1. Ground sensors.
00152     #ifdef _GND_SENSOR_
00153     gndTemperature = getGndTemperature();
00154     gndHumidity = 100 - (((float)(getGndHumidity() - HUM_MIN)) / 30148.0) * 100;
00155     #endif
00156     
00157     // 2. Light sensors        
00158     #ifdef _COLOR_SENSOR_
00159     getLightValues(&ambientLight, &redLight, &greenLight, &blueLight); // récupère les données du capteur de lumière 
00160     #endif
00161     
00162     // 3. Air sensors
00163     #ifdef _DHT_SENSOR_
00164     dht.readData();
00165     airHumidity = dht.ReadHumidity();
00166     airTemperature = dht.ReadTemperature(CELCIUS);
00167     #endif
00168     
00169     // Done activate button / screen
00170     usingI2C = false;
00171     if(display_on) {
00172         renew_screen = true;
00173         next_screen = false; 
00174     }
00175 }
00176 void transmission_sigfox()
00177 {
00178     //pc.printf("Start sigfox.\r\n");     
00179 #ifdef _SIGFOX_
00180     // Encrypt and compress the measurement data in 3trame3.
00181     struct SensorData data = {gndHumidity, (double)gndTemperature, airHumidity, (double)airTemperature, ambientLight, redLight, greenLight, blueLight};
00182     char text[150];
00183     int trame[3];
00184     create_frame(trame, data); 
00185     sprintf(text, "AT$SF=%08x%08x%08x\n\r", trame[2] , trame[1] , trame[0]);
00186     
00187     sg.printf(text);        
00188 #endif
00189     //pc.printf(text);        
00190     //pc.printf("Finish sigfox.\r\n");     
00191 }
00192 
00193 
00194 void initialize_device()
00195 {
00196     //lcd.initialize();
00197     lcd.setFontSize(0.21f);
00198     //lcd.clear(true);
00199     
00200 //Initaliise the sensors.
00201 #ifdef _GND_SENSOR_
00202     init_gndTmp();
00203 #endif
00204 #ifdef _COLOR_SENSOR_
00205     init_rgbSensor();
00206 #endif
00207 }
00208 // Button interrupt event :
00209 void onRisingEdge(void) 
00210 {
00211     
00212     if (usingI2C) return;
00213     
00214     modeSleep = false;
00215     if(!RE_detected)
00216         RE_detected = true;
00217 }
00218 void count_3600s(void) 
00219 {
00220     // Reset counter at every 3600 (It never reaches 3600)
00221     sec = (sec + 1) % 3600;
00222 } 
00223 void initialize_fsm()
00224 {
00225     fsm_button.setInitialState(FSM_Button::State_Idle);
00226     fsm_button.addState(FSM_Button::State_Idle, FSM_Button::button_idle);
00227     fsm_button.addState(FSM_Button::State_Debounce, FSM_Button::button_debounce);
00228     fsm_button.addState(FSM_Button::State_Pressed, FSM_Button::button_pressed);
00229     fsm_button.addState(FSM_Button::State_Released, FSM_Button::button_released);
00230     fsm_button.addState(FSM_Button::State_Wait, FSM_Button::button_wait);
00231     
00232     fsm_display.setInitialState(FSM_Display::State_Idle);
00233     fsm_display.addState(FSM_Display::State_Idle, FSM_Display::display_idle);
00234     fsm_display.addState(FSM_Display::State_Activate, FSM_Display::display_activate);
00235     fsm_display.addState(FSM_Display::State_Write, FSM_Display::display_write);
00236     fsm_display.addState(FSM_Display::State_Standby, FSM_Display::display_standby);
00237 }