Advanced temperature and humidity program example for Hexiwear featuring OLED Display

Dependencies:   Hexi_OLED_SSD1351

This project demonstrates the use of the HTU21D temperature and humidity sensor embedded in Hexiwear

Compile the project and copy the binary "Hexi_Humid_Temp-v2_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 temperature and the humidity values will be displayed in real-time on the OLED Display.

/media/uploads/GregC/hexi_temphumid.jpg

Committer:
GregC
Date:
Mon Oct 17 03:50:35 2016 +0000
Revision:
0:8bb615a783ed
Advanced temperature and humidity program example for Hexiwear featuring OLED Display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregC 0:8bb615a783ed 1 #include "mbed.h"
GregC 0:8bb615a783ed 2 #include "HTU21D.h"
GregC 0:8bb615a783ed 3 #include "Hexi_OLED_SSD1351.h"
GregC 0:8bb615a783ed 4 #include "images.h"
GregC 0:8bb615a783ed 5 #include "string.h"
GregC 0:8bb615a783ed 6
GregC 0:8bb615a783ed 7 // Pin connections
GregC 0:8bb615a783ed 8 DigitalOut led1(LED_GREEN); // RGB LED
GregC 0:8bb615a783ed 9 Serial pc(USBTX, USBRX); // Serial interface
GregC 0:8bb615a783ed 10 HTU21D temphumid(PTB1,PTB0); // HTU21D Sensor
GregC 0:8bb615a783ed 11 DigitalOut powerEN (PTB12); // Power Enable HTU21D Sensor
GregC 0:8bb615a783ed 12 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)
GregC 0:8bb615a783ed 13
GregC 0:8bb615a783ed 14 // Variables
GregC 0:8bb615a783ed 15 int sample_ftemp;
GregC 0:8bb615a783ed 16 int sample_ctemp;
GregC 0:8bb615a783ed 17 int sample_ktemp;
GregC 0:8bb615a783ed 18 int sample_humid;
GregC 0:8bb615a783ed 19 const uint8_t *image1; // Pointer for the image to be displayed
GregC 0:8bb615a783ed 20 char text[20]; // Text Buffer for dynamic value displayed
GregC 0:8bb615a783ed 21
GregC 0:8bb615a783ed 22 int main() {
GregC 0:8bb615a783ed 23 powerEN = 0;
GregC 0:8bb615a783ed 24
GregC 0:8bb615a783ed 25 /* Setting pointer location of the 96 by 96 pixel bitmap */
GregC 0:8bb615a783ed 26 image1 = TempHumid;
GregC 0:8bb615a783ed 27
GregC 0:8bb615a783ed 28 /* Turn on the backlight of the OLED Display */
GregC 0:8bb615a783ed 29 // oled.DimScreenON();
GregC 0:8bb615a783ed 30
GregC 0:8bb615a783ed 31 /* Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 */
GregC 0:8bb615a783ed 32 oled.DrawImage(image1,0,0);
GregC 0:8bb615a783ed 33
GregC 0:8bb615a783ed 34 while(true) {
GregC 0:8bb615a783ed 35
GregC 0:8bb615a783ed 36 sample_ftemp = temphumid.sample_ftemp();
GregC 0:8bb615a783ed 37 printf("Temperature: %d F\n\r", sample_ftemp);
GregC 0:8bb615a783ed 38
GregC 0:8bb615a783ed 39 sample_ctemp = temphumid.sample_ctemp();
GregC 0:8bb615a783ed 40 printf("Temperature: %d C\n\r", sample_ctemp);
GregC 0:8bb615a783ed 41
GregC 0:8bb615a783ed 42 sample_ktemp = temphumid.sample_ktemp();
GregC 0:8bb615a783ed 43 printf("Temperature: %d K\n\r", sample_ktemp);
GregC 0:8bb615a783ed 44
GregC 0:8bb615a783ed 45 sample_humid = temphumid.sample_humid();
GregC 0:8bb615a783ed 46 printf("Humidity: %d %%\n\r", sample_humid);
GregC 0:8bb615a783ed 47 printf("\n\r");
GregC 0:8bb615a783ed 48
GregC 0:8bb615a783ed 49 /* Get OLED Class Default Text Properties */
GregC 0:8bb615a783ed 50 oled_text_properties_t textProperties = {0};
GregC 0:8bb615a783ed 51 oled.GetTextProperties(&textProperties);
GregC 0:8bb615a783ed 52
GregC 0:8bb615a783ed 53 /* Set text properties to white and right aligned for the dynamic text */
GregC 0:8bb615a783ed 54 textProperties.fontColor = COLOR_RED;
GregC 0:8bb615a783ed 55 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
GregC 0:8bb615a783ed 56 oled.SetTextProperties(&textProperties);
GregC 0:8bb615a783ed 57
GregC 0:8bb615a783ed 58 /* Display Legends */
GregC 0:8bb615a783ed 59 strcpy((char *) text,"Temp.");
GregC 0:8bb615a783ed 60 oled.Label((uint8_t *)text,5,67);
GregC 0:8bb615a783ed 61
GregC 0:8bb615a783ed 62 /* Format the value */
GregC 0:8bb615a783ed 63 sprintf(text,"%i",sample_ctemp);
GregC 0:8bb615a783ed 64 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
GregC 0:8bb615a783ed 65 oled.TextBox((uint8_t *)text,57,67,20,15); //Increase textbox for more digits
GregC 0:8bb615a783ed 66
GregC 0:8bb615a783ed 67 /* Display Units */
GregC 0:8bb615a783ed 68 strcpy((char *) text,"dC");
GregC 0:8bb615a783ed 69 oled.Label((uint8_t *)text,82,67);
GregC 0:8bb615a783ed 70
GregC 0:8bb615a783ed 71 /* Set text properties to white and right aligned for the dynamic text */
GregC 0:8bb615a783ed 72 textProperties.fontColor = COLOR_BLUE;
GregC 0:8bb615a783ed 73 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
GregC 0:8bb615a783ed 74 oled.SetTextProperties(&textProperties);
GregC 0:8bb615a783ed 75
GregC 0:8bb615a783ed 76 /* Display Legends */
GregC 0:8bb615a783ed 77 strcpy((char *) text,"Humidity");
GregC 0:8bb615a783ed 78 oled.Label((uint8_t *)text,5,81);
GregC 0:8bb615a783ed 79
GregC 0:8bb615a783ed 80 /* Format the value */
GregC 0:8bb615a783ed 81 sprintf(text,"%i",sample_humid);
GregC 0:8bb615a783ed 82 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
GregC 0:8bb615a783ed 83 oled.TextBox((uint8_t *)text,57,81,20,15); //Increase textbox for more digits
GregC 0:8bb615a783ed 84
GregC 0:8bb615a783ed 85 /* Display Units */
GregC 0:8bb615a783ed 86 strcpy((char *) text,"%");
GregC 0:8bb615a783ed 87 oled.Label((uint8_t *)text,82,81);
GregC 0:8bb615a783ed 88
GregC 0:8bb615a783ed 89 led1 = !led1;
GregC 0:8bb615a783ed 90 Thread::wait(500);
GregC 0:8bb615a783ed 91 }
GregC 0:8bb615a783ed 92 }