Very advanced Click Air Quality example for Hexiwear featuring OLED Display, Bluetooth, Cloud and Touch

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

This project has been developed by mbed user daveyclk

This project demonstrates the use of the Mikroelektronika Click Air Quality module with hexiwear featuring the OLED display, the Bluetooth for Cloud connectivity and Touch buttons

Plug Hexiwear into the Docking Station and the Air Quality Click to the Click Socket 1
Connect the USB cable to your computer and to the micro-USB port of the Docking Station

Compile the project and copy the binary "Hexi_Click_AirQuality_Example_HEXIWEAR.bin" in the DAP-LINK drive from your computer file explorer
Press the K64F-RESET button on the docking station to start the program on your board

The OLED screen will display some graphics and the Air Quality measurement in ppm below
Blow gently on the sensor and see the value changing
Graphic displayed will move the Arrow from Green to Purple depending from the ppm value measured by the Air Quality sensor
Download the cell phone App Hexiwear from iOS or Android stores to connect your board to your phone
Type the pin displayed on the screen and give a name to your board to pair it via the App
Congratulation your data are now streamed directly to Wolkabout Cloud...
To visualize the data remotely (over cloud not bluetooth), you can go to Wolksense.com or download the Wolksense iOS/Android App and login with same account

Revision:
3:97bc87323258
Parent:
2:8e7d89c2cbde
--- a/main.cpp	Mon Oct 24 15:14:39 2016 +0000
+++ b/main.cpp	Fri Nov 04 23:11:29 2016 +0000
@@ -6,6 +6,7 @@
 
 #include "mbed.h"
 #include "Hexi_OLED_SSD1351.h"
+#include "Hexi_KW40Z.h"
 #include "images.h"
 #include "new_images.h"
 #include "string.h"
@@ -17,17 +18,29 @@
 
 AnalogIn ain(PTB2);
 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)
-DigitalOut led1(LED_GREEN);
+KW40Z kw40z_device(PTE24, PTE25); // Instantiate the Hexi KW40Z Driver (UART TX, UART RX)
+DigitalOut blueLed(LED3,1);
 Serial pc(USBTX, USBRX);
 
+Thread txThread;
+
+/******************************************************************************
+* prototypes
+*******************************************************************************/
+void InitModules();
+void ReadSensor();
+void CalculatePPM();
+void DisplayAirQValue(uint16_t value);
+
+
 /******************************************************************************
 * Module Variable Definitions
 *******************************************************************************/
                 
-double ppm;                                  // ppm
+uint16_t ppm;
 
-int value = 0;
-int value_old = 0;
+uint16_t value = 0;
+uint16_t value_old = 0;
 uint16_t adc_rd;
     
 //const uint8_t *image1;          // old cover image
@@ -41,6 +54,76 @@
 
 bool test = false; // used to put the program in text mode to check images    
 
+/**************************************************************************************************
+* Call Back Functions
+**************************************************************************************************/
+
+void ButtonRight(void)
+{
+    kw40z_device.ToggleAdvertisementMode();
+}
+
+void ButtonLeft(void)
+{
+    kw40z_device.ToggleAdvertisementMode();
+}
+
+void PassKey(void)
+{
+    /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
+    sprintf(text,"%d", kw40z_device.GetPassKey());
+    oled.TextBox((uint8_t *)text,0,72,95,18);
+}
+
+
+
+/**************************************************************************************************
+* Function Main()
+* -------------------------------------------------------------------------------------------------
+* Overview:
+* Input: None
+* Output: None
+**************************************************************************************************/
+
+int main() 
+{
+
+    InitModules();
+   
+    while (true) 
+    {
+       
+        ReadSensor();
+        pc.printf("Sensor Value 0x%04X\n\r",ain.read_u16());
+
+        blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
+       
+  
+        CalculatePPM();
+        DisplayAirQValue(ppm);
+        
+        Thread::wait(500); 
+
+    }
+}
+
+/**************************************************************************************************
+* Function TxTask()
+**************************************************************************************************/
+
+void txTask(void){
+   
+   while (true) 
+   {
+        
+        /*Notify Hexiwear App that it is running Sensor Tag mode*/
+        kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
+      
+        kw40z_device.SendPressure(10*ppm); //send ppm Air Quality Click value
+      
+        Thread::wait(1000);                 
+    }
+}
 
 /**************************************************************************************************
 * Function InitModules()
@@ -65,6 +148,11 @@
     
     oled_text_properties_t textProperties = {0};    // Get OLED Class Default Text Properties
     oled.GetTextProperties(&textProperties); 
+    
+    /* Register callbacks to application functions */
+    kw40z_device.attach_buttonLeft(&ButtonLeft);
+    kw40z_device.attach_buttonRight(&ButtonRight);
+    kw40z_device.attach_passkey(&PassKey);
       
     //PPM text now in the Bitmap
    
@@ -81,7 +169,8 @@
 
     sprintf(text,"%i",value);                       // Format the value
     oled.TextBox((uint8_t *)text,55,76,20,15);   //Increase textbox for more digits
-      
+
+    txThread.start(txTask); /*Start transmitting Sensor Tag Data */      
 
 }
 
@@ -122,7 +211,7 @@
     Rs = Rl * (3.3 - Vrl)/Vrl;                 // Calculate sensor resistance
     ratio = Rs/Rl;                             // Calculate ratio
     lgPPM = (log10(ratio) * -0.8) + 0.9;       // Calculate ppm
-    ppm = pow(10,lgPPM);                       // Calculate ppm
+    ppm = 6* pow(10,lgPPM);                       // Calculate ppm
 }
 
 
@@ -135,7 +224,7 @@
 * Output: None
 **************************************************************************************************/
 
-void DisplayAirQValue( uint16_t value )
+void DisplayAirQValue(uint16_t value )
 {
     if (value_old != value)
     {
@@ -171,51 +260,4 @@
         oled.TextBox((uint8_t *)text,55,76,20,15);   //Increase textbox for more digits
     }
     value_old = value;
-}
-
-
-/**************************************************************************************************
-* Function Main()
-* -------------------------------------------------------------------------------------------------
-* Overview:
-* Input: None
-* Output: None
-**************************************************************************************************/
-
-int main() 
-{
-
-    InitModules();
-    test = false; // use this to test the BMPS 
-         
-    
-    while (true) 
-    {
-
-        ReadSensor();
-        pc.printf("Sensor Value 0x%04X\n\r",ain.read_u16());
-       
-        // added a test to toggle through the images
-        if (test == true)
-        {  
-            int test_ppm = 10;
-            for( int i = 0; i < 5; i++)
-            {
-               DisplayAirQValue(test_ppm);
-               test_ppm += 50;
-               led1 = !led1;
-               Thread::wait(1000);
-            }
-        }        
-        else
-        {
-        
-            CalculatePPM();
-            DisplayAirQValue(ppm);
-        
-            led1 = !led1;
-            Thread::wait(500);
-        }   
-
-    }
 }
\ No newline at end of file