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

main.cpp

Committer:
GregC
Date:
2016-10-20
Revision:
0:4cf89612afab
Child:
1:7b9c19276087

File content as of revision 0:4cf89612afab:

/******************************************************************************
* Includes
*******************************************************************************/

#include "mbed.h"
#include "Hexi_OLED_SSD1351.h"
#include "images.h"
#include "string.h"

/******************************************************************************
* Input and Output Pinout
*******************************************************************************/

AnalogIn ain(PTB2);
SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)
DigitalOut led1(LED_GREEN);
Serial pc(USBTX, USBRX);

/******************************************************************************
* Module Variable Definitions
*******************************************************************************/
                
double ppm;                                  // ppm

int value = 0;
int value_old = 0;
uint16_t adc_rd;
    
const uint8_t *image1;  // Pointer for the image to be displayed
char text[20];          // Text Buffer for dynamic value displayed
    


/**************************************************************************************************
* Function InitModules()
* -------------------------------------------------------------------------------------------------
* Overview:
* Input: None
* Output: None
**************************************************************************************************/

void InitModules()
{
    image1  = AirQuality;         // Setting pointer location of the 96 by 96 pixel bitmap
    oled.DrawImage(image1,0,0);     // Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0  
    
    oled_text_properties_t textProperties = {0};    // Get OLED Class Default Text Properties
    oled.GetTextProperties(&textProperties); 
      
    textProperties.fontColor = COLOR_BLUE;          // Set text properties to white and right aligned for the dynamic text
    textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
    oled.SetTextProperties(&textProperties);  
      
    strcpy((char *) text,"ppm:");       // Display Legends
    oled.Label((uint8_t *)text,15,75);      
    
    textProperties.fontColor = COLOR_BLUE;          // Set text properties to white and right aligned for the dynamic text
    textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
    oled.SetTextProperties(&textProperties);  

    sprintf(text,"%i",value);                       // Format the value
    oled.TextBox((uint8_t *)text,55,75,20,15);   //Increase textbox for more digits
      
    /** initialize ADC */
//    ADC0_Init();

    /** pause for 100ms for ADC module stabilization */
    wait_ms(100);
}


/**************************************************************************************************
* Function ReadSensor()
* -------------------------------------------------------------------------------------------------
* Overview: Read sensor.
* Input: None
* Output: None
**************************************************************************************************/

void ReadSensor()
{
    adc_rd = ain.read_u16();
    wait_ms(10);
}


/**************************************************************************************************
* Function CalculatePPM()
* -------------------------------------------------------------------------------------------------
* Overview: Calculation of PPM.
* Input: None
* Output: None
**************************************************************************************************/
    
void CalculatePPM()
{
    const double Rl      = 5000.0;               // Rl (Ohm) - Load resistance
    const double Vadc_33 = 0.0032226562;         // ADC step 3,3V/1024 3,22mV (10bit ADC)
    double Vrl;                                  // Output voltage
    double Rs;                                   // Rs (Ohm) - Sensor resistance
    double ratio;                                // Rs/Rl ratio
    double lgPPM;

    Vrl = (double)adc_rd * Vadc_33;            // For 3.3V Vcc use Vadc_33
    Rs = Rl * (5 - 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
}



/**************************************************************************************************
* Function DisplayAirQValue()
* -------------------------------------------------------------------------------------------------
* Overview:
* Input: None
* Output: None
**************************************************************************************************/

void DisplayAirQValue( uint16_t value )
{
    if (value_old != value)
    {

        oled_text_properties_t textProperties = {0};    // Get OLED Class Default Text Properties
        oled.GetTextProperties(&textProperties); 

        // clear the previous value
        textProperties.fontColor = COLOR_WHITE;          // Set text properties to white and right aligned for the dynamic text
        textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
        oled.SetTextProperties(&textProperties);  

        sprintf(text,"%i",value_old);                       // Format the value
        oled.TextBox((uint8_t *)text,55,75,20,15);   //Increase textbox for more digits

        // display the new value
        textProperties.fontColor = COLOR_WHITE;          // Set text properties to white and right aligned for the dynamic text
        textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
        oled.SetTextProperties(&textProperties);  

        sprintf(text,"%i",value);                       // Format the value
        oled.TextBox((uint8_t *)text,55,75,20,15);   //Increase textbox for more digits
    }
    value_old = value;
}


/**************************************************************************************************
* Function Main()
* -------------------------------------------------------------------------------------------------
* Overview:
* Input: None
* Output: None
**************************************************************************************************/

int main() 
{

    InitModules();

    while (true) 
    {

        ReadSensor();
        pc.printf("Sensor Value 0x%04X\n\r",ain.read_u16());

        CalculatePPM();

        DisplayAirQValue(ppm);
        
        led1 = !led1;
        Thread::wait(500);

    }
}