Clarisse Berthe
Published

Plant Monitoring Using Sigfox

We have created an autonomous plant monitoring system using Sigfox.

IntermediateFull instructions providedOver 1 day2,403
Plant Monitoring Using Sigfox

Things used in this project

Hardware components

STMicroelectronics STM32 Nucleo-32 L432KC
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
DFRobot Humidity and temperature sensor SEN0227
×1
Adafruit LUX Sensor - TSL2561
×1
Gravity: TCS34725 RGB Color Sensor For Arduino
DFRobot Gravity: TCS34725 RGB Color Sensor For Arduino
×1
Adafruit Monochrome 1.3" 128x64 OLED graphic display
×1
Sigfox Sigfox Breakout Card Kit BRKWS01 + Antenna
×1
SOL2W Solar Cell
×1
Li-Ion battery 3.7 V 1050 mAh
×1
Seeed Studio LiPo Rider Pro Card
×1

Software apps and online services

Mbed Studio
Arm Mbed Studio
Sigfox
Sigfox
Ubidots for Education
EasyEDA

Story

Read more

Custom parts and enclosures

Gerber PCB

Gerber files

[ALTIUM] Schematic

Schematic to import in Altium

[EASY EDA] Schematic

Schematic to import in Easy EDA

[EASY EDA] PCB

PCB to import in Easy EDA

Code

RGB sensor

C/C++
Adafruit_TCS34725.cpp
#include "Adafruit_TCS34725.h"

/*example*****************************************************************

#include "mbed.h"
#include "Adafruit_TCS34725.h"

#define commonAnode true

I2C i2c(p28, p27);
Adafruit_TCS34725 tcs = Adafruit_TCS34725(&i2c, TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

int main()
{
    char gammatable[256];
    if (tcs.begin())
    {
        printf("Found sensor");
    }
    else
    {
        printf("No TCS34725 found ... check your connections");
        while (1); // halt!
    }
    for (int i=0; i<256; i++)
    {
        float x = i;
        x /= 255;
        x = pow((double)x, 2.5);
        x *= 255;
        if (commonAnode)
        {
            gammatable[i] = 255 - x;
        }
        else
        {
            gammatable[i] = x;
        }
        printf("%d\r\n", gammatable[i]);
    }
    while(1)
    {
        uint16_t clear, red, green, blue;
        tcs.setInterrupt(false);      // turn on LED
        tcs.getRawData(&red, &green, &blue, &clear);
        tcs.setInterrupt(true);  // turn off LED
        printf("%d, %d, %d, %d\r\n", clear, red, green, blue);
        // Figure out some basic hex code for visualization
        uint32_t sum = clear;
        float r, g, b;
        r = red; r /= sum;
        g = green; g /= sum;
        b = blue; b /= sum;
        r *= 256; g *= 256; b *= 256;
    }
}


*************************************************************************/

void Adafruit_TCS34725::write8 (uint8_t reg, uint32_t value)
{
    char data[2] = {TCS34725_COMMAND_BIT | reg, value & 0xFF};
    _i2c->write(TCS34725_ADDRESS, data, 2);
}

uint8_t Adafruit_TCS34725::read8(uint8_t reg)
{
    char data[2] = {TCS34725_COMMAND_BIT | reg, 0};
    char r_data = 0;
    _i2c->write(TCS34725_ADDRESS, data, 1);
    _i2c->read(TCS34725_ADDRESS, &r_data, 1);

    return r_data;
}

uint16_t Adafruit_TCS34725::read16(uint8_t reg)
{
    uint16_t x; uint16_t t;
    char data[2] = {TCS34725_COMMAND_BIT | reg, 0};
    char r_data[2] = {};
    _i2c->write(TCS34725_ADDRESS, data, 1);
    _i2c->read(TCS34725_ADDRESS, r_data, 2);
    t = r_data[0];
    x = r_data[1];
    x <<= 8;
    x |= t;
    return x;
}

void Adafruit_TCS34725::enable(void)
{
    write8(TCS34725_ENABLE, TCS34725_ENABLE_PON);
    wait_ms(3);
    write8(TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
}

void Adafruit_TCS34725::disable(void)
{
    /* Turn the device off to save power */
    uint8_t reg = 0;
    reg = read8(TCS34725_ENABLE);
    write8(TCS34725_ENABLE, reg & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
}

Adafruit_TCS34725::Adafruit_TCS34725(I2C *i2c, tcs34725IntegrationTime_t it, tcs34725Gain_t gain)
{
    _i2c = i2c;
    _tcs34725Initialised = false;
    _tcs34725IntegrationTime = it;
    _tcs34725Gain = gain;
}

bool Adafruit_TCS34725::begin(void)
{
    /* Make sure we're actually connected */
    uint8_t x = read8(TCS34725_ID);
    //Serial.println(x, HEX);
    if (x != 0x44)
    {
        return false;
    }
    _tcs34725Initialised = true;

    /* Set default integration time and gain */
    setIntegrationTime(_tcs34725IntegrationTime);
    setGain(_tcs34725Gain);

    /* Note: by default, the device is in power down mode on bootup */
    enable();

    return true;
}

void Adafruit_TCS34725::setIntegrationTime(tcs34725IntegrationTime_t it)
{
    if (!_tcs34725Initialised) begin();

    /* Update the timing register */
    write8(TCS34725_ATIME, it);

    /* Update value placeholders */
    _tcs34725IntegrationTime = it;
}

void Adafruit_TCS34725::setGain(tcs34725Gain_t gain)
{
    if (!_tcs34725Initialised) begin();

    /* Update the timing register */
    write8(TCS34725_CONTROL, gain);

    /* Update value placeholders */
    _tcs34725Gain = gain;
}

void Adafruit_TCS34725::getRawData (uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c)
{
    if (!_tcs34725Initialised) begin();

    *c = read16(TCS34725_CDATAL);
    *r = read16(TCS34725_RDATAL);
    *g = read16(TCS34725_GDATAL);
    *b = read16(TCS34725_BDATAL);

    /* Set a delay for the integration time */
    switch (_tcs34725IntegrationTime)
    {
        case TCS34725_INTEGRATIONTIME_2_4MS:
            wait_ms(3);
            break;
        case TCS34725_INTEGRATIONTIME_24MS:
            wait_ms(24);
            break;
        case TCS34725_INTEGRATIONTIME_50MS:
            wait_ms(50);
            break;
        case TCS34725_INTEGRATIONTIME_101MS:
            wait_ms(101);
            break;
        case TCS34725_INTEGRATIONTIME_154MS:
            wait_ms(154);
            break;
        case TCS34725_INTEGRATIONTIME_700MS:
            wait_ms(700);
            break;
    }
}

uint16_t Adafruit_TCS34725::calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b)
{
    float X, Y, Z;      /* RGB to XYZ correlation      */
    float xc, yc;       /* Chromaticity co-ordinates   */
    float n;            /* McCamy's formula            */
    float cct;

    /* 1. Map RGB values to their XYZ counterparts.    */
    /* Based on 6500K fluorescent, 3000K fluorescent   */
    /* and 60W incandescent values for a wide range.   */
    /* Note: Y = Illuminance or lux                    */
    X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
    Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
    Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b);

    /* 2. Calculate the chromaticity co-ordinates      */
    xc = (X) / (X + Y + Z);
    yc = (Y) / (X + Y + Z);

    /* 3. Use McCamy's formula to determine the CCT    */
    n = (xc - 0.3320F) / (0.1858F - yc);

    /* Calculate the final CCT */
    cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;

    /* Return the results in degrees Kelvin */
    return (uint16_t)cct;
}

uint16_t Adafruit_TCS34725::calculateLux(uint16_t r, uint16_t g, uint16_t b)
{
    float illuminance;

    /* This only uses RGB ... how can we integrate clear or calculate lux */
    /* based exclusively on clear since this might be more reliable?      */
    illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);

    return (uint16_t)illuminance;
}


void Adafruit_TCS34725::setInterrupt(bool i)
{
    uint8_t r = read8(TCS34725_ENABLE);
    if (i)
    {
        r |= TCS34725_ENABLE_AIEN;
    }
    else
    {
        r &= ~TCS34725_ENABLE_AIEN;
    }
    write8(TCS34725_ENABLE, r);
}

void Adafruit_TCS34725::clearInterrupt(void)
{
    char data[2] = {0x66, 0};
    _i2c->write(TCS34725_ADDRESS, data, 1);
}


void Adafruit_TCS34725::setIntLimits(uint16_t low, uint16_t high)
{
    write8(0x04, low & 0xFF);
    write8(0x05, low >> 8);
    write8(0x06, high & 0xFF);
    write8(0x07, high >> 8);
}

RGB sensor

C/C++
Adafruit_TCS34725.h
/**************************************************************************/
/*!
    @file     Adafruit_TCS34725.h
    @author   KTOWN (Adafruit Industries)

    @section LICENSE

    Software License Agreement (BSD License)

    Copyright (c) 2013, Adafruit Industries
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holders nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#ifndef Adafruit_TCS34725_H_
#define Adafruit_TCS34725_H_

#include "mbed.h"

#define TCS34725_ADDRESS          (0x29<<1)

#define TCS34725_COMMAND_BIT      (0x80)

#define TCS34725_ENABLE           (0x00)
#define TCS34725_ENABLE_AIEN      (0x10)    /* RGBC Interrupt Enable */
#define TCS34725_ENABLE_WEN       (0x08)    /* Wait enable - Writing 1 activates the wait timer */
#define TCS34725_ENABLE_AEN       (0x02)    /* RGBC Enable - Writing 1 actives the ADC, 0 disables it */
#define TCS34725_ENABLE_PON       (0x01)    /* Power on - Writing 1 activates the internal oscillator, 0 disables it */
#define TCS34725_ATIME            (0x01)    /* Integration time */
#define TCS34725_WTIME            (0x03)    /* Wait time (if TCS34725_ENABLE_WEN is asserted) */
#define TCS34725_WTIME_2_4MS      (0xFF)    /* WLONG0 = 2.4ms   WLONG1 = 0.029s */
#define TCS34725_WTIME_204MS      (0xAB)    /* WLONG0 = 204ms   WLONG1 = 2.45s  */
#define TCS34725_WTIME_614MS      (0x00)    /* WLONG0 = 614ms   WLONG1 = 7.4s   */
#define TCS34725_AILTL            (0x04)    /* Clear channel lower interrupt threshold */
#define TCS34725_AILTH            (0x05)
#define TCS34725_AIHTL            (0x06)    /* Clear channel upper interrupt threshold */
#define TCS34725_AIHTH            (0x07)
#define TCS34725_PERS             (0x0C)    /* Persistence register - basic SW filtering mechanism for interrupts */
#define TCS34725_PERS_NONE        (0b0000)  /* Every RGBC cycle generates an interrupt                                */
#define TCS34725_PERS_1_CYCLE     (0b0001)  /* 1 clean channel value outside threshold range generates an interrupt   */
#define TCS34725_PERS_2_CYCLE     (0b0010)  /* 2 clean channel values outside threshold range generates an interrupt  */
#define TCS34725_PERS_3_CYCLE     (0b0011)  /* 3 clean channel values outside threshold range generates an interrupt  */
#define TCS34725_PERS_5_CYCLE     (0b0100)  /* 5 clean channel values outside threshold range generates an interrupt  */
#define TCS34725_PERS_10_CYCLE    (0b0101)  /* 10 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_15_CYCLE    (0b0110)  /* 15 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_20_CYCLE    (0b0111)  /* 20 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_25_CYCLE    (0b1000)  /* 25 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_30_CYCLE    (0b1001)  /* 30 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_35_CYCLE    (0b1010)  /* 35 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_40_CYCLE    (0b1011)  /* 40 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_45_CYCLE    (0b1100)  /* 45 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_50_CYCLE    (0b1101)  /* 50 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_55_CYCLE    (0b1110)  /* 55 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_60_CYCLE    (0b1111)  /* 60 clean channel values outside threshold range generates an interrupt */
#define TCS34725_CONFIG           (0x0D)
#define TCS34725_CONFIG_WLONG     (0x02)    /* Choose between short and long (12x) wait times via TCS34725_WTIME */
#define TCS34725_CONTROL          (0x0F)    /* Set the gain level for the sensor */
#define TCS34725_ID               (0x12)    /* 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 */
#define TCS34725_STATUS           (0x13)
#define TCS34725_STATUS_AINT      (0x10)    /* RGBC Clean channel interrupt */
#define TCS34725_STATUS_AVALID    (0x01)    /* Indicates that the RGBC channels have completed an integration cycle */
#define TCS34725_CDATAL           (0x14)    /* Clear channel data */
#define TCS34725_CDATAH           (0x15)
#define TCS34725_RDATAL           (0x16)    /* Red channel data */
#define TCS34725_RDATAH           (0x17)
#define TCS34725_GDATAL           (0x18)    /* Green channel data */
#define TCS34725_GDATAH           (0x19)
#define TCS34725_BDATAL           (0x1A)    /* Blue channel data */
#define TCS34725_BDATAH           (0x1B)

typedef enum
{
    TCS34725_INTEGRATIONTIME_2_4MS  = 0xFF,   /**<  2.4ms - 1 cycle    - Max Count: 1024  */
    TCS34725_INTEGRATIONTIME_24MS   = 0xF6,   /**<  24ms  - 10 cycles  - Max Count: 10240 */
    TCS34725_INTEGRATIONTIME_50MS   = 0xEB,   /**<  50ms  - 20 cycles  - Max Count: 20480 */
    TCS34725_INTEGRATIONTIME_101MS  = 0xD5,   /**<  101ms - 42 cycles  - Max Count: 43008 */
    TCS34725_INTEGRATIONTIME_154MS  = 0xC0,   /**<  154ms - 64 cycles  - Max Count: 65535 */
    TCS34725_INTEGRATIONTIME_700MS  = 0x00    /**<  700ms - 256 cycles - Max Count: 65535 */
}
tcs34725IntegrationTime_t;

typedef enum
{
    TCS34725_GAIN_1X                = 0x00,   /**<  No gain  */
    TCS34725_GAIN_4X                = 0x01,   /**<  2x gain  */
    TCS34725_GAIN_16X               = 0x02,   /**<  16x gain */
    TCS34725_GAIN_60X               = 0x03    /**<  60x gain */
}
tcs34725Gain_t;

class Adafruit_TCS34725 {
public:
    Adafruit_TCS34725(I2C *i2c, tcs34725IntegrationTime_t = TCS34725_INTEGRATIONTIME_2_4MS, tcs34725Gain_t = TCS34725_GAIN_1X);

    bool     begin(void);
    void     setIntegrationTime(tcs34725IntegrationTime_t it);
    void     setGain(tcs34725Gain_t gain);
    void     getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
    uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b);
    uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b);
    void     write8 (uint8_t reg, uint32_t value);
    uint8_t  read8 (uint8_t reg);
    uint16_t read16 (uint8_t reg);
    void setInterrupt(bool flag);
    void clearInterrupt(void);
    void setIntLimits(uint16_t l, uint16_t h);
    void     enable(void);

private:
    bool _tcs34725Initialised;
    tcs34725Gain_t _tcs34725Gain;
    tcs34725IntegrationTime_t _tcs34725IntegrationTime;

    I2C *_i2c;

    void     disable(void);
};

#endif

Display

C/C++
ssd1306.cpp
//https://os.mbed.com/users/Byrn/code/SSD1306/
 
#include "mbed.h"
#include "ssd1306.h"
 
#include <stdarg.h>
 
SSD1306::SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data)
    : _spi(data, NC, clk), 
      _cs(cs), 
      _reset(rs), 
      _dc(dc),
      _cursor_x(0),
      _cursor_y(0)
{
}
 
void SSD1306::off()
{
    _send_command(0xAE);
}
 
void SSD1306::on()
{
    _send_command(0xAF);
}
 
void SSD1306::sleep()
{
    _send_command(0xAE);
}
 
void SSD1306::wake()
{
    _send_command(0xAF);
}
 
void SSD1306::set_inverse(unsigned char value)
{
    _send_command(value ? 0xA7 : 0xA6);
}
 
void SSD1306::set_display_offset(unsigned char value)
{
    _send_command(0xD3);
    _send_command(value & 0x3F); 
}
 
void SSD1306::set_contrast(unsigned char value) 
{
    _send_command(0x81);
    _send_command(value);
}
 
void SSD1306::set_display_start_line(unsigned char value)
{
    _send_command(0x40 | value);
}
 
void SSD1306::set_segment_remap(unsigned char value)
{
    _send_command(value ? 0xA1 : 0xA0);
}
 
void SSD1306::set_multiplex_ratio(unsigned char value)
{
    _send_command(0xA8);
    _send_command(value & 0x3F);
}
 
void SSD1306::set_com_output_scan_direction(unsigned char value)
{
    _send_command(value ? 0xC8 : 0xC0);
}
 
void SSD1306::set_com_pins_hardware_configuration(unsigned char sequential, unsigned char lr_remap)
{
    _send_command(0xDA);
    _send_command(0x02 | ((sequential & 1) << 4) | ((lr_remap & 1) << 5));
}
 
void SSD1306::start_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval) 
{
    _send_command(direction ? 0x27 : 0x26);
    _send_command(0x00);
    _send_command(start & 0x07);
    switch (interval) {
        case   2: _send_command(0x07); break; // 111b
        case   3: _send_command(0x04); break; // 100b
        case   4: _send_command(0x05); break; // 101b
        case   5: _send_command(0x00); break; // 000b
        case  25: _send_command(0x06); break; // 110b
        case  64: _send_command(0x01); break; // 001b
        case 128: _send_command(0x02); break; // 010b
        case 256: _send_command(0x03); break; // 011b
        default:
            // default to 2 frame interval
            _send_command(0x07); break;
    }
    _send_command(end & 0x07);
    _send_command(0x00);
    _send_command(0xFF);
    
    // activate scroll
    _send_command(0x2F);
}
 
void SSD1306::start_vertical_and_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval, unsigned char vertical_offset)
{
    _send_command(direction ? 0x2A : 0x29);
    _send_command(0x00);
    _send_command(start & 0x07);
    switch (interval) {
        case   2: _send_command(0x07); break; // 111b
        case   3: _send_command(0x04); break; // 100b
        case   4: _send_command(0x05); break; // 101b
        case   5: _send_command(0x00); break; // 000b
        case  25: _send_command(0x06); break; // 110b
        case  64: _send_command(0x01); break; // 001b
        case 128: _send_command(0x02); break; // 010b
        case 256: _send_command(0x03); break; // 011b
        default:
            // default to 2 frame interval
            _send_command(0x07); break;
    }
    _send_command(end & 0x07);
    _send_command(vertical_offset);    
    
    // activate scroll
    _send_command(0x2F);
}
 
void SSD1306::stop_scroll()
{
    // all scroll configurations are removed from the display when executing this command.
    _send_command(0x2E);
}
 
void SSD1306::pam_set_start_address(unsigned char address)
{
    // "Set Lower Column Start Address for Page Addressing Mode"
    _send_command(address & 0x0F);
    
    // "Set Higher Column Start Address for Page Addressing Mode"
    _send_command((address << 4) & 0x0F);
}
 
void SSD1306::set_memory_addressing_mode(unsigned char mode)
{
    _send_command(0x20);
    _send_command(mode & 0x3);
}
 
void SSD1306::hv_set_column_address(unsigned char start, unsigned char end)
{
    _send_command(0x21);
    _send_command(start & 0x7F);
    _send_command(end & 0x7F);
}
 
void SSD1306::hv_set_page_address(unsigned char start, unsigned char end)
{
    _send_command(0x22);
    _send_command(start & 0x07);
    _send_command(end & 0x07);
}
 
void SSD1306::pam_set_page_start(unsigned char address)
{
    _send_command(0xB0 | (address & 0x07));
}
 
void SSD1306::set_display_clock_ratio_and_frequency(unsigned char ratio, unsigned char frequency)
{
    _send_command(0xD5);
    _send_command((ratio & 0x0F) | ((frequency & 0x0F) << 4));
}
 
void SSD1306::set_precharge_period(unsigned char phase1, unsigned char phase2)
{
    _send_command(0xD9);
    _send_command((phase1 & 0x0F) | ((phase2 & 0x0F ) << 4));
}
 
void SSD1306::set_vcomh_deselect_level(unsigned char level)
{
    _send_command(0xDB);
    _send_command((level & 0x03) << 4);
}
 
void SSD1306::nop()
{
    _send_command(0xE3);
}
 
void SSD1306::set_charge_pump_enable(unsigned char enable)
{
    _send_command(0x8D);
    _send_command(enable ? 0x14 : 0x10);
}
 
void SSD1306::initialise()
{
    // Init
    _reset = 1;
    wait(0.01);
    _reset = 0;
    wait(0.10);
    _reset = 1;
    
    off();
 
    set_display_clock_ratio_and_frequency(0, 8);
    set_multiplex_ratio(0x3F); // 1/64 duty
    set_precharge_period(0xF, 0x01);
    set_display_offset(0);    
    set_display_start_line(0);  
    set_charge_pump_enable(1);    
    set_memory_addressing_mode(0); // horizontal addressing mode; across then down
    set_segment_remap(1);
    set_com_output_scan_direction(1);
    set_com_pins_hardware_configuration(1, 0);
    set_contrast(0xFF);
    set_vcomh_deselect_level(1);
    
    wake();
    set_inverse(0);
    
    hv_set_column_address(0, 127);
    hv_set_page_address(0, 7);
    
    pam_set_start_address(0);
    pam_set_page_start(0);
    
    // set_precharge_period(2, 2);
}
 
void SSD1306::update()
{
    hv_set_column_address(0, 127);
    hv_set_page_address(0, 7);
    
    for (int i = 0; i < 1024; i++)
        _send_data(_screen[i]);
}
 
void SSD1306::set_pixel(int x, int y)
{
    if (x >= SSD1306_LCDWIDTH || y >= SSD1306_LCDHEIGHT) return;
    
    _screen[x + (y / 8) * 128] |= 1 << (y % 8);
}
 
void SSD1306::clear_pixel(int x, int y)
{
    if (x >= SSD1306_LCDWIDTH || y >= SSD1306_LCDHEIGHT) return;
    
    _screen[x + (y / 8) * 128] &= ~(1 << (y % 8));
}
 
void SSD1306::line(int x0, int y0, int x1, int y1) {
  int steep = abs(y1 - y0) > abs(x1 - x0);
  int t;
  
  if (steep) {
    t = x0; x0 = y0; y0 = t;
    t = x1; x1 = y1; y1 = t;
  }
 
  if (x0 > x1) {
    t = x0; x0 = x1; x1 = t;
    t = y0; y0 = y1; y1 = t;
  }
 
  int dx, dy;
  
  dx = x1 - x0;
  dy = abs(y1 - y0);
 
  int err = dx / 2;
  int ystep;
 
  if (y0 < y1) {
    ystep = 1;
  } else {
    ystep = -1;}
 
  for (; x0<x1; x0++) {
    if (steep) {
      set_pixel(y0, x0);
    } else {
      set_pixel(x0, y0);
    }
    err -= dy;
    if (err < 0) {
      y0 += ystep;
      err += dx;
    }
  }
}
 
void SSD1306::set_font(unsigned char *font, unsigned int width)
{
    _console_font_data = font;
    _console_font_width = width;
}
 
void SSD1306::set_double_height_text(unsigned int double_height)
{
    _double_height_text = double_height;
}
 
void SSD1306::putc(unsigned char c)
{
    while (_cursor_x >= (128 / _console_font_width))
    {
        _cursor_x -= (128 / _console_font_width);
        _cursor_y++;            
    }
 
    while (_cursor_y > 7)
    {
        scroll_up();            
    }
 
    switch (c)
    {
    case '\n':
        _cursor_y++;
        break;
        
    case '\r':
        _cursor_x = 0;
        break;
        
    case '\t':
        _cursor_x = (_cursor_x + 4) % 4;
        break;
        
    default:
        for (int b = 0; b < _console_font_width; b++)
        {
            _screen[_cursor_x * _console_font_width + _cursor_y * 128 + b] = _console_font_data[(c - FONT_START) * _console_font_width + b];
        }
        
        _cursor_x++;
    }            
}
 
void SSD1306::scroll_up()
{
    for (int y = 1; y <= 7; y++)
    {
        for (int x = 0; x < 128; x++)
        {
            _screen[x + 128 * (y - 1)] = _screen[x + 128 * y];
        }
    }
    
    for (int x = 0; x < 128; x++)
    {
        _screen[x + 128 * 7] = 0;
    }    
 
    _cursor_y--;
}
 
void SSD1306::printf(const char *format, ...)
{
    static char buffer[128];
    
    va_list args;
    va_start(args, format);
    vsprintf(buffer, format, args);
    va_end(args);
    
    char *c = (char *)&buffer;
    while (*c != 0)
    {
        putc(*c++);
    }
}
 
void SSD1306::clear()
{
    for (int i = 0; i < 1024; i++)
        _screen[i] = 0;
        
    _cursor_x = 0;
    _cursor_y = 0;
}
 
void SSD1306::_send_command(unsigned char code)
{
    _cs = 1;
    _dc = 0;
    _cs = 0;
    _spi.write(code);
    _cs = 1;
}
 
void SSD1306::_send_data(unsigned char value)
{
    _cs = 1;
    _dc = 1;
    _cs = 0;
    _spi.write(value);
    _cs = 1;
}

STH20

C/C++
SHT21_nlceee.h
/* Copyright (c) 2012 Graeme Coapes, Newcastle University, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#include "mbed.h"


#ifndef SHT21_ncleee
#define SHT21_ncleee

    /*
     * Constants used in communication
     *
     * Refer to datasheet for full explanation
     */ 
     
    //Sensor I2C address
    #define SHT_I2C_ADDR        0x80
    
    //Commands...
    //Trigger Temp with hold master
    #define SHT_TRIG_TEMP_HOLD  0xE3
    //Trigger RH with hold master
    #define SHT_TRIG_RH_HOLD    0xE5
    //Trigger Temp with no hold master
    #define SHT_TRIG_TEMP       0xF3
    //Trigger RH with no hold master
    #define SHT_TRIG_RH         0xF5
    //Write to user register
    #define SHT_WRITE_REG       0xE6
    //Read from user register
    #define SHT_READ_REG        0xE7
    //Soft reset the sensor
    #define SHT_SOFT_RESET      0xFE
    
    //User Register information
    
    //Data precision settings
    //RH 12 T 14 - default
    #define SHT_PREC_1214       0x00
    //RH 8  T 10 
    #define SHT_PREC_0812       0x01
    //RH 10 T 13
    #define SHT_PREC_1013       0x80
    //RH 11 T 11
    #define SHT_PREC_1111       0x81

    //Battery status
    #define SHT_BATTERY_STAT    0x40
    //Enable on chip heater
    #define SHT_HEATER          0x04
    //Disable OTP reload
    #define SHT_DISABLE_OTP     0x02
    
    
    //Fail conditions on the I2C bus
    #define SHT_FAIL            1
    #define SHT_SUCCESS         0
    
    //Author fail conditions
    //1, 2, 3 can be used because these are status bits
    //in the received measurement value
    #define SHT_GOOD            0xFFFC
    #define SHT_TRIG_FAIL       1
    #define SHT_READ_FAIL       2
    
    /** SHT21 Connection class, utilizing a I2C interface
     *  
     * Example:
     * @code
     *   #include "mbed.h"
     *   #include "SHT21_ncleee.h"
     *   
     *   
     *   DigitalOut myled(LED1);
     *   Serial pc(USBTX, USBRX);
     *   I2C i2c(p28,p27);
     *   SHT21 sht(&i2c);
     *   
     *   int main() 
     *   {
     *   
     *       pc.printf("Hello World...\n\tTesting temperature Sensor\n");
     *       
     *       int temperature = sht.readTemp();
     *   
     *       pc.printf("Temperature is: %d \n", temperature);
     *       
     *       pc.printf("Experiment complete...\n");
     *   
     *   }
     *        
     * @endcode
     *
     *
     */
    class SHT21
    {
        private:
            I2C *_i2c;
//            Serial *_pc;
            int triggerTemp();  
            int requestTemp();
            unsigned short temperature;
            int triggerRH();
            int requestRH();
            unsigned short humidity;
            int wr(int cmd);
            
        public:
        
            /** Constructor - create a connection to a SHT21 temperature and humidity sensor
             * through an I2C interface
             *
             * @param *i2c a pointer to the i2c interface that is used for communication
             */             
            SHT21(I2C *i2c);
            
            /** Read the temperature value from the sensor \n
             *
             * Involves triggering the measuring unit then
             * waiting for 100ms for the measuring to complete 
             * before reading the temperature             
             * 
             * @param returns a value representing the temperature in degrees centigrade
             */ 
            float readTemp();
            
            /** Read the humidity value from the sensor \n
             *
             * Involves triggering the measuring unit then
             * waiting for 100ms for the measuring to complete 
             * before reading the humidity             
             * 
             * @param returns the percentage humidity
             */             
            float readHumidity();
            
            /**
             * Perform a soft-reset of the sensor unit.
             */
            int reset();
            
            /**
             * Set the precision of the measuring
             *
             *    //Data precision settings \n
             *    //RH 12 T 14 - default    \n
             *    #define SHT_PREC_1214       0x00 \n
             *    //RH 8  T 10 \n
             *    #define SHT_PREC_0812       0x01 \n
             *    //RH 10 T 13 \n
             *    #define SHT_PREC_1013       0x80 \n
             *    //RH 11 T 11 \n
             *    #define SHT_PREC_1111       0x81 \n
             *
             * @param precision - the precision, refer to above or datasheet.
             * 
             */ 
            int setPrecision(char precision);
    
    };
    
    
#endif

Lux Sensor TSL2561

C/C++
TSL2561.cpp
/*
 * mbed library program
 *  Luminosity sensor -- LIGHT-TO-DIGITAL CONVERTER (light intensity to a digital signal output)
 *  TSL2561 by Texas Advanced Optoelectronic Solutions Inc.
 *
 * Copyright (c) 2015,'17 Kenji Arai / JH1PJL
 *  http://www.page.sannet.ne.jp/kenjia/index.html
 *  http://mbed.org/users/kenjiArai/
 *      Created: Feburary   21st, 2015
 *      Revised: August     23rd, 2017
 */

#include "TSL2561.h"

TSL2561::TSL2561 (PinName p_sda, PinName p_scl)
 : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
{
    TSL2561_addr = TSL2561_ADDRESS_GND;
    init();
}

TSL2561::TSL2561 (PinName p_sda, PinName p_scl, uint8_t addr)
 : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
{
    TSL2561_addr = addr;
    init();
}

TSL2561::TSL2561 (I2C& p_i2c)
 : _i2c(p_i2c)
{
    TSL2561_addr = TSL2561_ADDRESS_GND;
    init();
}

TSL2561::TSL2561 (I2C& p_i2c, uint8_t addr)
 : _i2c(p_i2c)
{
    TSL2561_addr = addr;
    init();
}

/////////////// Read Lux from sensor //////////////////////
/*
For 0    < CH1/CH0 < 0.50 Lux = 0.0304  x CH0-0.062  x CH0 x ((CH1/CH0)1.4)
For 0.50 < CH1/CH0 < 0.61 Lux = 0.0224  x CH0-0.031  x CH1
For 0.61 < CH1/CH0 < 0.80 Lux = 0.0128  x CH0-0.0153 x CH1
For 0.80 < CH1/CH0 < 1.30 Lux = 0.00146 x CH0-0.00112x CH1
For        CH1/CH0 > 1.30 Lux = 0
 */
float TSL2561::lux()
{
    double lux0, lux1;
    double ratio;
    double dlux;

    dt[0] = CMD_MULTI + TSL2561_DATA0LOW;
    _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
    _i2c.read(TSL2561_addr, (char *)dt, 2, false);
    ch0 = dt[1] << 8 | dt[0];
    dt[0] = CMD_MULTI + TSL2561_DATA1LOW;
    _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
    _i2c.read(TSL2561_addr, (char *)dt, 2, false);
    ch1 = dt[1] << 8 | dt[0];
    if (ch0 == 0xFFFF) {
        return 2500.0;
    }
    lux0 = (double)ch0;
    lux1 = (double)ch1;
    ratio = lux1 / lux0;
    read_timing_reg();
    lux0 *= (402.0/integ_time);
    lux1 *= (402.0/integ_time);
    lux0 /= gain;
    lux1 /= gain;
    if (ratio <= 0.5) {
        dlux = 0.03040 * lux0 - 0.06200 * lux0 * pow(ratio,1.4);
    } else if (ratio <= 0.61) {
        dlux = 0.02240 * lux0 - 0.03100 * lux1;
    } else if (ratio <= 0.80) {
        dlux = 0.01280 * lux0 - 0.01530 * lux1;
    } else if (ratio <= 1.30) {
        dlux = 0.00146 * lux0 - 0.00112 * lux1;
    } else {
        dlux = 0;
    }
    return (float)dlux;
}

/////////////// Initialize ////////////////////////////////
void TSL2561::init()
{
    _i2c.frequency(100000);
    power_up();
    set_timing_reg(TIMING_DEFAULT);
}

/////////////// Timing Register ///////////////////////////
uint8_t TSL2561::set_timing_reg(uint8_t parameter)
{
    dt[0] = CMD_SINGLE + TSL2561_TIMING;
    dt[1] = parameter;
    _i2c.write((int)TSL2561_addr, (char *)dt, 2, false);
    dt[0] = CMD_SINGLE + TSL2561_TIMING;
    _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
    _i2c.read(TSL2561_addr, (char *)dt, 1, false);
    return dt[0];
}

uint8_t TSL2561::read_timing_reg(void)
{
    uint8_t i;

    dt[0] = CMD_SINGLE + TSL2561_TIMING;
    _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
    _i2c.read(TSL2561_addr, (char *)dt, 1, false);
    if (dt[0] & TIMING_GAIN_16){
        gain = 16;
    } else {
        gain = 1;
    }
    i = dt[0] & 0x3;
    switch (i) {
        case 0:
            integ_time = 13.7;
            break;
        case 1:
            integ_time = 101.0;
            break;
        case 2:
            integ_time = 402.0;
            break;
        default:
            integ_time = 0;
            break;
    }
    return dt[0];
}

/////////////// ID ////////////////////////////////////////
uint16_t TSL2561::read_ID()
{
    dt[0] = CMD_SINGLE + TSL2561_ID;
    _i2c.write((int)TSL2561_addr, (char *)dt, 1, true);
    _i2c.read(TSL2561_addr, (char *)dt, 2, false);
    id_number = dt[0] << 8 | dt[1];
    return id_number;
}

uint8_t TSL2561::who_am_i()
{
    read_ID();
    if ((id_number >> 4) == I_AM_TSL2561) {
        return 1;
    } else {
        return 0;
    }
}

/////////////// Power ON/OFF //////////////////////////////
void TSL2561::power_up()
{
    dt[0] = CMD_SINGLE + TSL2561_CONTROL;
    dt[1] = 3;
    _i2c.write((int)TSL2561_addr, (char *)dt, 2, false);
}

void TSL2561::power_down()
{
    dt[0] = CMD_SINGLE + TSL2561_CONTROL;
    dt[1] = 0;
    _i2c.write((int)TSL2561_addr, (char *)dt, 2, false);
}

/////////////// I2C Freq. /////////////////////////////////
void TSL2561::frequency(int hz)
{
    _i2c.frequency(hz);
}

Lux Sensor TSL2561

C/C++
TSL2561.h
/*
 * mbed library program
 *  Luminosity sensor -- LIGHT-TO-DIGITAL CONVERTER (light intensity to a digital signal output)
 *  TSL2561 by Texas Advanced Optoelectronic Solutions Inc.
 *
 * Copyright (c) 2015,'17 Kenji Arai / JH1PJL
 *  http://www.page.sannet.ne.jp/kenjia/index.html
 *  http://mbed.org/users/kenjiArai/
 *      Created: Feburary   21st, 2015
 *      Revised: August     23rd, 2017
 */
/*
 *---------------- REFERENCE ----------------------------------------------------------------------
 *  https://docs.google.com/viewer?url=http%3A%2F%2Fwww.adafruit.com%2Fdatasheets%2FTSL256x.pdf
 *  https://learn.adafruit.com/tsl2561?view=all
 *  http://www.adafruit.com/products/439
 *  http://akizukidenshi.com/catalog/g/gM-08219/
 */

#ifndef TSL2561_H
#define TSL2561_H

#include "mbed.h"

// Luminosity sensor, TSL2561
// Address b7=0,b6=1,b5=1,b4=1,b3=0,b2=0,b1=1, b0=R/W
#define TSL2561_ADDRESS_GND         (0x29 << 1)
#define TSL2561_ADDRESS_FLOAT       (0x39 << 1)
#define TSL2561_ADDRESS_VDD         (0x49 << 1)

////////////// Registers //////////////////////////////////
// Register definition
#define TSL2561_CONTROL             0x00
#define TSL2561_TIMING              0x01
#define TSL2561_THRESHLOWLOW        0x02
#define TSL2561_THRESHHIGHLOW       0x04
#define TSL2561_INTERRUPT           0x06
#define TSL2561_CRC                 0x08
#define TSL2561_ID                  0x0A
#define TSL2561_DATA0LOW            0x0C
#define TSL2561_DATA0HIGH           0x0D
#define TSL2561_DATA1LOW            0x0E
#define TSL2561_DATA1HIGH           0x0F

////////////// TIMING PARAMETER ///////////////////////////
#define TIMING_GAIN_1               (0UL << 4)
#define TIMING_GAIN_16              (1UL << 4)
#define TIMING_TIME_13R7            (0x0)
#define TIMING_TIME_101             (0x1)
#define TIMING_TIME_402             (0x2)
#define TIMING_TIME_MANU            (0x3)
#define TIMING_DEFAULT              (TIMING_GAIN_1 + TIMING_TIME_402)

////////////// ID /////////////////////////////////////////
#define I_AM_TSL2561                0x50
#define REG_NO_MASK                 0x0F

////////////// COMMAND ////////////////////////////////////
#define CMD_CMDMODE                 (1UL << 7)
#define CMD_CLEAR                   (1UL << 6)
#define CMD_WORD                    (1UL << 5)
#define CMD_BLOCK                   (1UL << 4)
#define CMD_SINGLE                  (CMD_CMDMODE)
#define CMD_MULTI                   (CMD_CMDMODE + CMD_WORD)

/** Interface for Luminosity sensor, TSL2561
 * @code
 * #include "mbed.h"
 * #include "TSL2561.h"
 *
 * // I2C Communication
 *  TSL2561      lum(dp5,dp27);    // TSL2561 SDA, SCL
 * // If you connected I2C line not only this device but also other devices,
 * //     you need to declare following method.
 *  I2C          i2c(dp5,dp27);    // SDA, SCL
 *  TSL2561      lum(i2c);         // TSL2561 SDA, SCL (Data available every 400mSec)
 *
 * int main() {;
 *   while(true){
 *      printf("Illuminance: %+7.2f [Lux]\r\n", lum.lux());
 *      wait(1.0);
 *   }
 * }
 * @endcode
 */

class TSL2561
{
public:
    /** Configure data pin
      * @param data SDA and SCL pins
      */
    TSL2561(PinName p_sda, PinName p_scl);
    TSL2561(PinName p_sda, PinName p_scl, uint8_t addr);

    /** Configure data pin (with other devices on I2C line)
      * @param I2C previous definition
      */
    TSL2561(I2C& p_i2c);
    TSL2561(I2C& p_i2c, uint8_t addr);

    /** Get approximates the human eye response
      *  in the commonly used Illuminance unit of Lux
      * @param none
      * @return Lux
      */
    float lux(void);

    /** Set timing register
      * @param timing parameter
      * @return timing read data
      */
    uint8_t set_timing_reg(uint8_t parameter);

    /** Read timing register
      * @param timing parameter
      * @return timing read data
      */
    uint8_t read_timing_reg(void);

    /** Set I2C clock frequency
      * @param freq.
      * @return none
      */
    void frequency(int hz);

    /** check Device ID number
      * @param none
      * @return TSL2561 = 1, others  0
      */
    uint8_t who_am_i(void);

    /** Read ID and Revision Number
      * @param none
      * @return ID + REVNO
      */
    uint16_t read_ID(void);

    /** Power Up/Down
      * @param none
      * @return none
      */
    void power_up(void);
    void power_down(void);

protected:
    I2C *_i2c_p;
    I2C &_i2c;

    void init(void);

private:
    bool TSL2561Initialised;
    uint8_t  TSL2561_addr;
    uint8_t  dt[4];
    uint32_t ch0;
    uint32_t ch1;
    int8_t   gain;
    uint8_t  id_number;
    double   integ_time;
};

#endif      // TSL2561_H

Main

C/C++
main.cpp
#include "mbed.h"
#include "mapping.h"
#include "tca9548a.h"

//Display
#include "ssd1306.h"
#include "standard_font.h"
#include "bold_font.h"

//Sensors
#include "DHT22.h"
#include "SHT21_ncleee.h"
#include "Adafruit_TCS34725.h"
#include "TSL2561.h"

//Initialisations

//I2C
//I2C i2c1(I2C1_SDA, I2C1_SCL); //SDA, SCL
I2C i2c2(I2C2_SDA, I2C2_SCL); //SDA, SCL

//Multiplexer
TCA9548A i2c_sw(I2C2_SDA, I2C2_SCL); //SDA, SCL

#define commonAnode true
Serial Sigfox(SigfoxTX, SigfoxRX);

//pour afficher sur terminal avec Putty : pc.printf("...");
//Serial pc(SERIAL_TX, SERIAL_RX);

//Display
SSD1306 oled(CS, RST, DC, CLK, DATA);
    
//DHT22
DHT airSensor(GPIO, DHT22);

//TCS34725
Adafruit_TCS34725 tcs = Adafruit_TCS34725(&i2c2, TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

//TSL2561
TSL2561 tsl(i2c2,TSL2561_ADDRESS_FLOAT);

//SHT20
SHT21 groundSensor(&i2c2);

DigitalOut myled(LED1);

/************************************
 * Declaration des variables globales
 ************************************/

int err, TemperatureAir, HumiditeAir;
int RGB;
float Lux;
float TemperatureSol;
float HumiditeSol;
float RedValue, GreenValue, BlueValue;
uint16_t Clear, Red, Green, Blue;


    void affichageOLED(){
   
        oled.clear();
        oled.set_font(bold_font, 8);
        oled.printf("Projet AGRAL\r\n");
        oled.printf("\r\n");
        oled.set_font(standard_font, 6);
        
        oled.printf("Tempe Air : %d\r\n", TemperatureAir);
       
        oled.printf("Humidite : %d %%\r\n", HumiditeAir);
    
        oled.printf("Lux : %.0f lux\r\n", Lux);
    
        oled.printf("RGB : %d %d %d\r\n", int(RedValue),int(GreenValue),int(BlueValue));
    
        oled.printf("Temp Sol : %3.0f\r\n", TemperatureSol);
    
        oled.printf("Hum Sol : %.0f %%\r\n", HumiditeSol);
        oled.update();
   
   }
    
    void affichageErreur(){
   
        oled.clear();
        oled.set_font(bold_font, 8);
        oled.printf("Erreur de mesure\r\n");
        oled.printf("\r\n");
        oled.set_font(standard_font, 6);

        oled.printf("Erreur : %d\r\n", err);
        oled.printf("Une nouvelle mesure va etre effectuee\r\n", HumiditeSol);
        oled.update();
   }
   
    void mesureDHT22(){
        err = airSensor.readData();
        TemperatureAir = airSensor.ReadTemperature(CELCIUS);
        HumiditeAir = airSensor.ReadHumidity();
    }


    void mesureSonde(){
            
        TemperatureSol = groundSensor.readTemp();
        HumiditeSol = groundSensor.readHumidity(); 
    }
        
void mesureRGB(){
       tcs.getRawData(&Red, &Green, &Blue, &Clear);
       uint32_t Sum = Clear;
       RedValue = Red; RedValue /= Sum;
       GreenValue = Green; GreenValue /= Sum;
       BlueValue = Blue; BlueValue /= Sum;
       RedValue *= 256; GreenValue *= 256; BlueValue *= 256;
    }
    
void mesureLux(){
   
    Lux = 0;
    for(int i = 0; i < 10; i++){
        Lux += COEF_TSL*tsl.lux();
        wait_ms(25);
    }
    Lux /= 10;
}

void ClignotementLED(int duree){
    myled = !myled;
    wait_ms(duree);
    myled = !myled;
    wait_ms(duree);
    myled = !myled;
    wait_ms(duree);
    myled = !myled;
    wait_ms(duree);
    }
    
int main()
{
    oled.initialise();
    oled.clear();
    oled.set_contrast(255); // max contrast
    mesureDHT22();
    
    while(1)
    {
        
        mesureDHT22();
        mesureLux();
        mesureRGB();
        mesureSonde();
        if(TemperatureAir == 0 && HumiditeAir == 0){
            affichageErreur();
            }
        
        else{
        affichageOLED();
        wait(600);
        Sigfox.printf("AT$SF=%02x%02x%02x%02x%02x%02x%02x%02x\r\n", TemperatureAir, HumiditeAir, (int)Lux, (int)TemperatureSol, (int)HumiditeSol, (int)RedValue, (int)GreenValue, (int)BlueValue);
        oled.printf("Trame envoyee\r\n");
        oled.update();
        wait(5);
        }
        
    }
}

Mapping

C/C++
mapping.h
#ifndef __MAPPING_H__
    #define __MAPPING_H__
    
    //Sigfox
    #define DelaiEnvoi 30 //Délai entre 2 envois en secondes
    #define SigfoxTX D1
    #define SigfoxRX D0
    
    //I2C
    #define I2C1_SDA D4
    #define I2C1_SCL D5
    
    #define I2C2_SDA D12
    #define I2C2_SCL A6
    
    //DATA
    #define GPIO PB_5 //D11
    
    //DISPLAY SCREEN
    #define CS PB_1 //D6
    #define RST PB_0 //D3
    #define DC PA_11 //D10
    #define CLK PA_5 //A4
    #define DATA PA_12 //D2
    
    #define COEF_TSL 10
    
#endif

DTH22

C/C++
DHT22.cpp
/*
 *  DHT Library for  Digital-output Humidity and Temperature sensors
 *
 *  Works with DHT11, DHT22
 *             SEN11301P,  Grove - Temperature&Humidity Sensor     (Seeed Studio)
 *             SEN51035P,  Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
 *             AM2302   ,  temperature-humidity sensor
 *             HM2303   ,  Digital-output humidity and temperature sensor
 *
 *  Copyright (C) Wim De Roeve
 *                based on DHT22 sensor library by HO WING KIT
 *                Arduino DHT11 library 
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documnetation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to  whom the Software is
 * furished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
 
#include "DHT22.h"
 
#define DHT_DATA_BIT_COUNT 41
 
DHT::DHT(PinName pin,int DHTtype) {
    _pin = pin;
    _DHTtype = DHTtype;
    _firsttime=true;
}
 
DHT::~DHT() {
}
 
int DHT::readData() {
    int i, j, retryCount,b;
    unsigned int bitTimes[DHT_DATA_BIT_COUNT];
 
    eError err = ERROR_NONE;
    time_t currentTime = time(NULL);
 
    DigitalInOut DHT_io(_pin);
 
    for (i = 0; i < DHT_DATA_BIT_COUNT; i++) {
        bitTimes[i] = 0;
    }
 
    if (!_firsttime) {
        if (int(currentTime - _lastReadTime) < 2) {
            err = ERROR_NO_PATIENCE;
            return err;
        }
    } else {
        _firsttime=false;
        _lastReadTime=currentTime;
    }
    retryCount = 0;
 
    do {
        if (retryCount > 125) {
            err = BUS_BUSY;
            return err;
        }
        retryCount ++;
        wait_us(2);
    } while ((DHT_io==0));
 
 
    DHT_io.output();
    DHT_io = 0;
    wait_ms(18);
    DHT_io = 1;
    wait_us(40);
    DHT_io.input();
 
    retryCount = 0;
    do {
        if (retryCount > 40)  {
            err = ERROR_NOT_PRESENT;
            return err;
        }
        retryCount++;
        wait_us(1);
    } while ((DHT_io==1));
 
    if (err != ERROR_NONE) {
        return err;
    }
 
    wait_us(80);
 
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 8; j++) {
 
            retryCount = 0;
            do {
                if (retryCount > 75)  {
                    err = ERROR_DATA_TIMEOUT;
                    return err;
                }
                retryCount++;
                wait_us(1);
            } while (DHT_io == 0);
            wait_us(40);
            bitTimes[i*8+j]=DHT_io;
 
            int count = 0;
            while (DHT_io == 1 && count < 100) {
                wait_us(1);
                count++;
            }
        }
    }
    DHT_io.output();
    DHT_io = 1;
    for (i = 0; i < 5; i++) {
        b=0;
        for (j=0; j<8; j++) {
            if (bitTimes[i*8+j+1] > 0) {
                b |= ( 1 << (7-j));
            }
        }
        DHT_data[i]=b;
    }
 
    if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) {
        _lastReadTime = currentTime;
        _lastTemperature=CalcTemperature();
        _lastHumidity=CalcHumidity();
 
    } else {
        err = ERROR_CHECKSUM;
    }
 
    return err;
 
}
 
float DHT::CalcTemperature() {
    int v;
 
    switch (_DHTtype) {
        case DHT11:
            v = DHT_data[2];
            return float(v);
        case DHT22:
            v = DHT_data[2] & 0x7F;
            v *= 256;
            v += DHT_data[3];
            v /= 10;
            if (DHT_data[2] & 0x80)
                v *= -1;
            return float(v);
    }
    return 0;
}
 
float DHT::ReadHumidity() {
    return _lastHumidity;
}
 
float DHT::ConvertCelciustoFarenheit(float celsius) {
    return celsius * 9 / 5 + 32;
}
 
float DHT::ConvertCelciustoKelvin(float celsius) {
    return celsius + 273.15;
}
 
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
float DHT::CalcdewPoint(float celsius, float humidity) {
    float A0= 373.15/(273.15 + celsius);
    float SUM = -7.90298 * (A0-1);
    SUM += 5.02808 * log10(A0);
    SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
    SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
    SUM += log10(1013.246);
    float VP = pow(10, SUM-3) * humidity;
    float T = log(VP/0.61078);   // temp var
    return (241.88 * T) / (17.558-T);
}
 
// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
float DHT::CalcdewPointFast(float celsius, float humidity)
{
        float a = 17.271;
        float b = 237.7;
        float temp = (a * celsius) / (b + celsius) + log(humidity/100);
        float Td = (b * temp) / (a - temp);
        return Td;
}
 
float DHT::ReadTemperature(eScale Scale) {
    if (Scale == FARENHEIT)
        return ConvertCelciustoFarenheit(_lastTemperature);
    else if (Scale == KELVIN)
        return ConvertCelciustoKelvin(_lastTemperature);
    else
        return _lastTemperature;
}
 
float DHT::CalcHumidity() {
    int v;
 
    switch (_DHTtype) {
        case DHT11:
            v = DHT_data[0];
            return float(v);
        case DHT22:
            v = DHT_data[0];
            v *= 256;
            v += DHT_data[1];
            v /= 10;
            return float(v);
    }
    return 0;
}

DTH22

C/C++
DTH22.h
/* 
 *  DHT Library for  Digital-output Humidity and Temperature sensors 
 *  
 *  Works with DHT11, DHT21, DHT22
 *             SEN11301P,  Grove - Temperature&Humidity Sensor     (Seeed Studio)
 *             SEN51035P,  Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
 *             AM2302   ,  temperature-humidity sensor    
 *             RHT01,RHT02, RHT03    ,  Humidity and Temperature Sensor         (Sparkfun)
 *
 *  Copyright (C) Wim De Roeve
 *                based on DHT22 sensor library by HO WING KIT
 *                Arduino DHT11 library
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documnetation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to  whom the Software is
 * furished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
 
#ifndef MBED_DHT_H
#define MBED_DHT_H
 
#include "mbed.h"
 
enum eType{
        DHT11     = 11,
        SEN11301P = 11,
        RHT01     = 11,
        DHT22     = 22,
        AM2302    = 22,
        SEN51035P = 22,
        RHT02     = 22,
        RHT03     = 22
    } ;
 
enum eError {
    ERROR_NONE = 0,
    BUS_BUSY =1,
    ERROR_NOT_PRESENT =2 ,
    ERROR_ACK_TOO_LONG =3 ,
    ERROR_SYNC_TIMEOUT = 4,
    ERROR_DATA_TIMEOUT =5 ,
    ERROR_CHECKSUM = 6,
    ERROR_NO_PATIENCE =7
} ;
 
typedef enum {
    CELCIUS =0 ,
    FARENHEIT =1,
    KELVIN=2
} eScale;
 
 
class DHT {
 
public:
 
    DHT(PinName pin,int DHTtype);
    ~DHT();
    int readData(void);
    float ReadHumidity(void);
    float ReadTemperature(eScale Scale);
    float CalcdewPoint(float celsius, float humidity);
    float CalcdewPointFast(float celsius, float humidity);
 
private:
    time_t  _lastReadTime;
    float _lastTemperature;
    float _lastHumidity;
    PinName _pin;
    bool _firsttime;
    int _DHTtype;
    int DHT_data[6];
    float CalcTemperature();
    float CalcHumidity();
    float ConvertCelciustoFarenheit(float);
    float ConvertCelciustoKelvin(float);
 
};
 
#endif

Display

C/C++
bold.cpp
/** Thick 8x8 font, good for headings etc. */
//https://os.mbed.com/users/Byrn/code/SSD1306/
static unsigned char bold_font[] = {
 
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x20] ' '
    0x0,  0x0,  0x0,  0xDF,  0xDF,  0x0,  0x0,  0x0,    // [0x21] '!'
    0x0,  0x3,  0x7,  0x0,  0x3,  0x7,  0x0,  0x0,    // [0x22] '"'
    0x0,  0x14,  0x3E,  0x14,  0x3E,  0x14,  0x0,  0x0,    // [0x23] '#'
    0x0,  0x24,  0x2A,  0x7F,  0x2A,  0x12,  0x0,  0x0,    // [0x24] '$'
    0x43,  0x23,  0x10,  0x8,  0x4,  0x62,  0x61,  0x0,    // [0x25] '%'
    0x38,  0x7C,  0x44,  0x7F,  0x3F,  0x4,  0x4,  0x0,    // [0x26] '&'
    0x0,  0x0,  0x0,  0x7,  0x7,  0x0,  0x0,  0x0,    // [0x27] '''
    0x0,  0x0,  0x7E,  0xFF,  0x81,  0x0,  0x0,  0x0,    // [0x28] '('
    0x0,  0x0,  0x81,  0xFF,  0x7E,  0x0,  0x0,  0x0,    // [0x29] ')'
    0x8,  0x2A,  0x1C,  0x7F,  0x1C,  0x2A,  0x8,  0x0,    // [0x2A] '*'
    0x0,  0x8,  0x8,  0x3E,  0x3E,  0x8,  0x8,  0x0,    // [0x2B] '+'
    0x0,  0x0,  0x80,  0xE0,  0x60,  0x0,  0x0,  0x0,    // [0x2C] ','
    0x0,  0x8,  0x8,  0x8,  0x8,  0x8,  0x8,  0x0,    // [0x2D] '-'
    0x0,  0x0,  0x0,  0xC0,  0xC0,  0x0,  0x0,  0x0,    // [0x2E] '.'
    0x0,  0xC0,  0xF0,  0x3C,  0xF,  0x3,  0x0,  0x0,    // [0x2F] '/'
    0x3E,  0x7F,  0x51,  0x49,  0x45,  0x7F,  0x3E,  0x0,    // [0x30] '0'
    0x0,  0x40,  0x42,  0x7F,  0x7F,  0x40,  0x40,  0x0,    // [0x31] '1'
    0x72,  0x7B,  0x49,  0x49,  0x49,  0x4F,  0x46,  0x0,    // [0x32] '2'
    0x22,  0x63,  0x41,  0x49,  0x49,  0x7F,  0x36,  0x0,    // [0x33] '3'
    0x7,  0xF,  0x8,  0x8,  0x8,  0x7E,  0x7E,  0x0,    // [0x34] '4'
    0x27,  0x6F,  0x49,  0x49,  0x49,  0x79,  0x31,  0x0,    // [0x35] '5'
    0x3E,  0x7F,  0x49,  0x49,  0x49,  0x79,  0x30,  0x0,    // [0x36] '6'
    0x1,  0x1,  0x1,  0x1,  0x1,  0x7F,  0x7E,  0x0,    // [0x37] '7'
    0x36,  0x7F,  0x49,  0x49,  0x49,  0x7F,  0x36,  0x0,    // [0x38] '8'
    0x6,  0xF,  0x9,  0x9,  0x9,  0x7F,  0x7F,  0x0,    // [0x39] '9'
    0x0,  0x0,  0x0,  0x63,  0x63,  0x0,  0x0,  0x0,    // [0x3A] ':'
    0x0,  0x0,  0x80,  0xE3,  0x63,  0x0,  0x0,  0x0,    // [0x3B] ';'
    0x0,  0x8,  0x1C,  0x36,  0x63,  0x41,  0x0,  0x0,    // [0x3C] '<'
    0x0,  0x14,  0x14,  0x14,  0x14,  0x14,  0x14,  0x0,    // [0x3D] '='
    0x0,  0x41,  0x63,  0x36,  0x1C,  0x8,  0x0,  0x0,    // [0x3E] '>'
    0x2,  0x3,  0xD1,  0xD9,  0x9,  0xF,  0x6,  0x0,    // [0x3F] '?'
    0x3E,  0x7F,  0x41,  0x5D,  0x55,  0x5F,  0xE,  0x0,    // [0x40] '@'
    0x7E,  0x7F,  0x9,  0x9,  0x9,  0x7F,  0x7E,  0x0,    // [0x41] 'A'
    0x7F,  0x7F,  0x49,  0x49,  0x49,  0x7F,  0x36,  0x0,    // [0x42] 'B'
    0x3E,  0x7F,  0x41,  0x41,  0x41,  0x63,  0x22,  0x0,    // [0x43] 'C'
    0x7F,  0x7F,  0x41,  0x41,  0x63,  0x3E,  0x1C,  0x0,    // [0x44] 'D'
    0x7F,  0x7F,  0x49,  0x49,  0x49,  0x41,  0x41,  0x0,    // [0x45] 'E'
    0x7F,  0x7F,  0x9,  0x9,  0x9,  0x1,  0x1,  0x0,    // [0x46] 'F'
    0x3E,  0x7F,  0x41,  0x49,  0x49,  0x7B,  0x7A,  0x0,    // [0x47] 'G'
    0x7F,  0x7F,  0x8,  0x8,  0x8,  0x7F,  0x7F,  0x0,    // [0x48] 'H'
    0x0,  0x41,  0x41,  0x7F,  0x7F,  0x41,  0x41,  0x0,    // [0x49] 'I'
    0x20,  0x61,  0x41,  0x7F,  0x3F,  0x1,  0x1,  0x0,    // [0x4A] 'J'
    0x7F,  0x7F,  0x8,  0x1C,  0x36,  0x63,  0x41,  0x0,    // [0x4B] 'K'
    0x7F,  0x7F,  0x40,  0x40,  0x40,  0x40,  0x40,  0x0,    // [0x4C] 'L'
    0x7F,  0x7F,  0x6,  0xC,  0x6,  0x7F,  0x7F,  0x0,    // [0x4D] 'M'
    0x7F,  0x7F,  0x6,  0xC,  0x18,  0x7F,  0x7F,  0x0,    // [0x4E] 'N'
    0x3E,  0x7F,  0x41,  0x41,  0x41,  0x7F,  0x3E,  0x0,    // [0x4F] 'O'
    0x7F,  0x7F,  0x9,  0x9,  0x9,  0xF,  0x6,  0x0,    // [0x50] 'P'
    0x3E,  0x7F,  0x41,  0x61,  0xC1,  0xFF,  0xBE,  0x0,    // [0x51] 'Q'
    0x7F,  0x7F,  0x9,  0x9,  0x9,  0x7F,  0x76,  0x0,    // [0x52] 'R'
    0x26,  0x6F,  0x49,  0x49,  0x49,  0x7B,  0x32,  0x0,    // [0x53] 'S'
    0x0,  0x1,  0x1,  0x7F,  0x7F,  0x1,  0x1,  0x0,    // [0x54] 'T'
    0x3F,  0x7F,  0x40,  0x40,  0x40,  0x7F,  0x7F,  0x0,    // [0x55] 'U'
    0x0,  0x7,  0x1F,  0x78,  0x78,  0x1F,  0x7,  0x0,    // [0x56] 'V'
    0x7F,  0x7F,  0x30,  0x18,  0x30,  0x7F,  0x7F,  0x0,    // [0x57] 'W'
    0x63,  0x77,  0x1C,  0x8,  0x1C,  0x77,  0x63,  0x0,    // [0x58] 'X'
    0x27,  0x6F,  0x48,  0x48,  0x48,  0x7F,  0x3F,  0x0,    // [0x59] 'Y'
    0x61,  0x71,  0x59,  0x4D,  0x47,  0x43,  0x41,  0x0,    // [0x5A] 'Z'
    0x0,  0x0,  0xFF,  0xFF,  0x81,  0x81,  0x0,  0x0,    // [0x5B] '['
    0x0,  0x3,  0xF,  0x3C,  0xF0,  0xC0,  0x0,  0x0,    // [0x5C] '\\'
    0x0,  0x0,  0x81,  0x81,  0xFF,  0xFF,  0x0,  0x0,    // [0x5D] ']'
    0x4,  0x6,  0x3,  0x1,  0x3,  0x6,  0x4,  0x0,    // [0x5E] '^'
    0x80,  0x80,  0x80,  0x80,  0x80,  0x80,  0x80,  0x0,    // [0x5F] '_'
    0x3,  0x7,  0x4,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x60] '`'
    0x38,  0x7C,  0x44,  0x44,  0x24,  0x7C,  0x78,  0x0,    // [0x61] 'a'
    0x7F,  0x7F,  0x24,  0x44,  0x44,  0x7C,  0x38,  0x0,    // [0x62] 'b'
    0x38,  0x7C,  0x44,  0x44,  0x44,  0x44,  0x44,  0x0,    // [0x63] 'c'
    0x38,  0x7C,  0x44,  0x44,  0x24,  0x7F,  0x7F,  0x0,    // [0x64] 'd'
    0x38,  0x7C,  0x54,  0x54,  0x54,  0x5C,  0x8,  0x0,    // [0x65] 'e'
    0x0,  0x4,  0x7E,  0x7F,  0x5,  0x1,  0x0,  0x0,    // [0x66] 'f'
    0x18,  0xBC,  0xA4,  0xA4,  0x94,  0xFC,  0x78,  0x0,    // [0x67] 'g'
    0x7F,  0x7F,  0x8,  0x4,  0x4,  0x7C,  0x78,  0x0,    // [0x68] 'h'
    0x0,  0x0,  0x0,  0x7D,  0x7D,  0x0,  0x0,  0x0,    // [0x69] 'i'
    0x0,  0x40,  0x40,  0x40,  0x7D,  0x3D,  0x0,  0x0,    // [0x6A] 'j'
    0x7F,  0x7F,  0x8,  0x8,  0x1C,  0x76,  0x62,  0x0,    // [0x6B] 'k'
    0x0,  0x0,  0x0,  0x7F,  0x7F,  0x0,  0x0,  0x0,    // [0x6C] 'l'
    0x78,  0x7C,  0x18,  0x30,  0x18,  0x7C,  0x78,  0x0,    // [0x6D] 'm'
    0x7C,  0x7C,  0x8,  0x4,  0x4,  0x7C,  0x78,  0x0,    // [0x6E] 'n'
    0x38,  0x7C,  0x44,  0x44,  0x44,  0x7C,  0x38,  0x0,    // [0x6F] 'o'
    0xFC,  0xFC,  0x28,  0x24,  0x24,  0x3C,  0x18,  0x0,    // [0x70] 'p'
    0x1C,  0x1E,  0x22,  0x22,  0x12,  0xFE,  0xFE,  0x0,    // [0x71] 'q'
    0x7C,  0x7C,  0x8,  0x4,  0x4,  0xC,  0x8,  0x0,    // [0x72] 'r'
    0x48,  0x5C,  0x54,  0x54,  0x54,  0x74,  0x20,  0x0,    // [0x73] 's'
    0x3F,  0x7F,  0x44,  0x44,  0x44,  0x60,  0x20,  0x0,    // [0x74] 't'
    0x3C,  0x7C,  0x40,  0x40,  0x20,  0x7C,  0x7C,  0x0,    // [0x75] 'u'
    0x0,  0xC,  0x3C,  0x70,  0x70,  0x3C,  0xC,  0x0,    // [0x76] 'v'
    0x3C,  0x7C,  0x30,  0x18,  0x30,  0x7C,  0x3C,  0x0,    // [0x77] 'w'
    0x44,  0x6C,  0x38,  0x10,  0x38,  0x6C,  0x44,  0x0,    // [0x78] 'x'
    0xC,  0x5C,  0x50,  0x50,  0x50,  0x7C,  0x3C,  0x0,    // [0x79] 'y'
    0x44,  0x64,  0x74,  0x54,  0x5C,  0x4C,  0x44,  0x0,    // [0x7A] 'z'
    0x0,  0x8,  0x3E,  0x77,  0x41,  0x0,  0x0,  0x0,    // [0x7B] '{'
    0x0,  0x0,  0x0,  0xFF,  0xFF,  0x0,  0x0,  0x0,    // [0x7C] '|'
    0x0,  0x41,  0x77,  0x3E,  0x8,  0x0,  0x0,  0x0,    // [0x7D] '}'
    0x2,  0x3,  0x1,  0x3,  0x2,  0x3,  0x1,  0x0,    // [0x7E] '~'
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x7F] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x80] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x81] ''
    0x0,  0x0,  0x80,  0xE0,  0x60,  0x0,  0x0,  0x0,    // [0x82] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x83] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x84] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x85] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x86] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x87] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x88] ''
};
 

Display

C/C++
ssd1306.h
//https://os.mbed.com/users/Byrn/code/SSD1306/
#ifndef __SSD1306_H__
#define __SSD1306_H__

#define FONT_START          ' '  /* First character value in the font table */

/** SSD1306 Controller Driver
  *
  * This class provides a buffered display for the SSD1306 OLED controller.
  *
  * TODO:
  *   - At the moment, the driver assumes a 128x64 pixel display.
  *   - Only fonts of 8 pixel height are supported (different widths can be used).
  *   - Pretty much no drawing functions are provided as yet.
  *   - Possible "auto-update", automatically calling update() after a printf etc.
  *
  * Information taken from the datasheet at:
  *   http://www.adafruit.com/datasheets/SSD1306.pdf
  *
  */
class SSD1306
{
public:
    /** Construct a new SSD1306 object.
     *  @param cs The connected C/S pin.
     *  @param rs The connected RS pin.
     *  @param dc The connected DC pin.
     *  @param clk The connected CLK pin.
     *  @param data The connected Data pin.
     */
    SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data);

    // ----- HARDWARE CONTROL -----

    /** Initialise the display with defaults.*/
    void initialise();

    /** Force a refresh of the display.  Copies the buffer to the controller. */
    void update();

    /** Turn the whole display off.  This will reset all configuration settings on the controller to their defaults. */
    void off();

    /** Turn the whole display on.  Used during initialisation. */
    void on();

    /** Sends the display to sleep, but leaves RAM intact. */
    void sleep();

    /** Wakes up this display following a sleep() call.
     *  @see sleep()
     */
    void wake();

    /** Set the display contrast.
     *  @param value The contrast, from 1 to 256.
     */
    void set_contrast(unsigned char value); // 1-256

    /** Set the display to normal or inverse.
     *  @param value 0 for normal mode, or 1 for inverse mode.
     */
    void set_inverse(unsigned char value); // 0 or 1

    /** Set the display start line.  This is the line at which the display will start rendering.
     *  @param value A value from 0 to 63 denoting the line to start at.
     */
    void set_display_start_line(unsigned char value); // 0-63

    /** Set the segment remap state.  This allows the module to be addressed as if flipped horizontally.
      * NOTE: Changing this setting has no effect on data already in the module's GDDRAM.
      * @param value 0 = column address 0 = segment 0 (the default), 1 = column address 127 = segment 0 (flipped).
      */
    void set_segment_remap(unsigned char value); // 0 or 1

    /** Set the vertical shift by COM.
      * @param value The number of rows to shift, from 0 - 63.
      */
    void set_display_offset(unsigned char value); // 0-63

    /** Set the multiplex ratio.
     *  @param value MUX will be set to (value+1). Valid values range from 15 to 63 - MUX 16 to 64.
     */
    void set_multiplex_ratio(unsigned char value); // 15-63 (value+1 mux)

    /** Set COM output scan direction.  If the display is active, this will immediately vertically
      * flip the display.
      * @param value 0 = Scan from COM0 (default), 1 = reversed (scan from COM[N-1]).
      */
    void set_com_output_scan_direction(unsigned char value); // 0 or 1

    /** Set COM pins hardware configuration.
      * @param sequential 0 = Sequental COM pin configuration, 1 = Alternative COM pin configuration (default).
      * @param lr_remap 0 = Disable COM left/right remap (default), 1 = enable COM left/right remap.
      */
    void set_com_pins_hardware_configuration(unsigned char sequential, unsigned char lr_remap); // 0 or 1 for both parametrs

    /** Set up and start a continuous horizontal scroll.
      * Once you have set up the scrolling, you can deactivate it with stop_scroll().
      * @param direction 0 for right, 1 for left.
      * @param start Start page address, 0 - 5.
      * @param end End page address, 0 - 5.
      * @param interval Interval in frame frequency.  Valid values are: 2, 3, 4, 5, 25, 64, 128, 256.
      * @see stop_scrol
      */
    void start_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval);

    /** Set up and start a continuous horizontal and vertical scroll.
      * NOTE: No continuous vertical scroll is available.
      * Once you have set up the scrolling, you can deactivate it with stop_scroll().
      * @param direction 0 for vertical and right horizontal scroll, 1 for vertical and left horizontal scroll.
      * @param start Start page address, 0 - 5.
      * @param end End page address, 0 - 5.
      * @param interval Interval in frame frequency.  Valid values are: 2, 3, 4, 5, 25, 64, 128, 256.
      * @param vertical_offset Offset of vertical scroll, 1 - 63.
      * @see stop_scroll
      */
    void start_vertical_and_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval, unsigned char vertical_offset);

    /** Deactivate the continuous scroll set up with start_horizontal_scroll() or
      * start_vertical_and_horizontal_scroll().
      * @see set_horizontal_scroll, set_vertical_and_horizontal_scroll
      */
    void stop_scroll();

    // ----- ADDRESSING -----

    /** Set memory addressing mode to the given value.
      * @param mode 0 for Horizontal addressing mode, 1 for Vertical addressing mode, or 2 for Page addressing mode (PAM).  2 is the default.
      */
    void set_memory_addressing_mode(unsigned char mode);

    /** Page Addressing Mode: Set the column start address register for
      * page addressing mode.
      * @param address The address (full byte).
      */
    void pam_set_start_address(unsigned char address);

    /** Set the GDDRAM page start address for page addressing mode.
      * @param address The start page, 0 - 7.
      */
    void pam_set_page_start(unsigned char address);

    /** Set page start and end address for horizontal/vertical addressing mode.
      * @param start The start page, 0 - 7.
      * @param end The end page, 0 - 7.
      */
    void hv_set_page_address(unsigned char start, unsigned char end);

    /** Set column address range for horizontal/vertical addressing mode.
      * @param start Column start address, 0 - 127.
      * @param end Column end address, 0 - 127.
      */
    void hv_set_column_address(unsigned char start, unsigned char end);

    // ----- TIMING & DRIVING -----
    /** Set the display clock divide ratio and the oscillator frequency.
      * @param ratio The divide ratio, default is 0.
      * @param frequency The oscillator frequency, 0 - 127. Default is 8.
      */
    void set_display_clock_ratio_and_frequency(unsigned char ratio, unsigned char frequency);

    /** Set the precharge period.
      * @param phase1 Phase 1 period in DCLK clocks.  1 - 15, default is 2.
      * @param phase2 Phase 2 period in DCLK clocks.  1 - 15, default is 2.
      */
    void set_precharge_period(unsigned char phase1, unsigned char phase2);

    /** Set the Vcomh deselect level.
      * @param level 0 = 0.65 x Vcc, 1 = 0.77 x Vcc (default), 2 = 0.83 x Vcc.
      */
    void set_vcomh_deselect_level(unsigned char level);

    /** Perform a "no operation".
      */
    void nop();

    /** Enable/disable charge pump.
      @param enable 0 to disable, 1 to enable the internal charge pump.
      */
    void set_charge_pump_enable(unsigned char enable);

    // ----- BUFFER EDITING -----

    void clear();
    void set_pixel(int x, int y);
    void clear_pixel(int x, int y);
    void line(int x0, int y0, int x1, int y1);

    /** Set the current console font.
      * @param font Font data, layed out vertically!
      * @param width Width of the font characters in pixels.
      * Fonts are always (at present) 8 pixels in height.
      */
    void set_font(unsigned char *font, unsigned int width);

    /** Set double height text output.
      * @param double_height If 1, calls to putc(), printf() etc will
      * result in text taking up 2 lines instead of 1.
      */
    void set_double_height_text(unsigned int double_height);

    /** Put a single character to the screen buffer.
      * Repeated calls to putc() will cause the cursor to move across and
      * then down as needed, with scrolling.
      * @param c The character to write.
      */
    void putc(unsigned char c);

    /** Print to the screen buffer.
      * printf() will wrap and scroll the screen as needed to display the text given.
      * @param format Format specifier, same as printf() in normal C.
      */
    void printf(const char *format, ...);

    /** Scroll the screen buffer up by one line. */
    void scroll_up();

private:
    SPI _spi;
    DigitalOut _cs, _reset, _dc;
    unsigned char _screen[1024];

    int _cursor_x, _cursor_y;

    void _send_command(unsigned char code);
    void _send_data(unsigned char value);

    unsigned char *_console_font_data;
    unsigned int _console_font_width;
    unsigned int _double_height_text;
};

#define SSD1306_LCDWIDTH 128
#define SSD1306_LCDHEIGHT 64

#endif

Display

C/C++
standard_font.h
/** Thin 5x8 font. */
//https://os.mbed.com/users/Byrn/code/SSD1306/
static unsigned char standard_font[] = {
 
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x20] ' '
    0x0,  0x0,  0x2F,  0x0,  0x0,  0x0,    // [0x21] '!'
    0x0,  0x3,  0x0,  0x3,  0x0,  0x0,    // [0x22] '"'
    0x14,  0x3E,  0x14,  0x3E,  0x14,  0x0,    // [0x23] '#'
    0x24,  0x2A,  0x7F,  0x2A,  0x12,  0x0,    // [0x24] '$'
    0x22,  0x10,  0x8,  0x4,  0x22,  0x0,    // [0x25] '%'
    0x18,  0x24,  0x24,  0x1E,  0x4,  0x0,    // [0x26] '&'
    0x0,  0x0,  0x3,  0x0,  0x0,  0x0,    // [0x27] '''
    0x0,  0x1C,  0x22,  0x41,  0x0,  0x0,    // [0x28] '('
    0x0,  0x41,  0x22,  0x1C,  0x0,  0x0,    // [0x29] ')'
    0x2A,  0x1C,  0x3E,  0x1C,  0x2A,  0x0,    // [0x2A] '*'
    0x8,  0x8,  0x3E,  0x8,  0x8,  0x0,    // [0x2B] '+'
    0x0,  0x40,  0x20,  0x0,  0x0,  0x0,    // [0x2C] ','
    0x8,  0x8,  0x8,  0x8,  0x8,  0x0,    // [0x2D] '-'
    0x0,  0x0,  0x20,  0x0,  0x0,  0x0,    // [0x2E] '.'
    0x0,  0xC0,  0x30,  0xC,  0x3,  0x0,    // [0x2F] '/'
    0x1E,  0x29,  0x2D,  0x25,  0x1E,  0x0,    // [0x30] '0'
    0x0,  0x22,  0x3F,  0x20,  0x0,  0x0,    // [0x31] '1'
    0x32,  0x29,  0x29,  0x29,  0x26,  0x0,    // [0x32] '2'
    0x12,  0x21,  0x29,  0x29,  0x16,  0x0,    // [0x33] '3'
    0x7,  0x8,  0x8,  0x8,  0x3E,  0x0,    // [0x34] '4'
    0x17,  0x25,  0x25,  0x25,  0x18,  0x0,    // [0x35] '5'
    0x1E,  0x25,  0x25,  0x25,  0x18,  0x0,    // [0x36] '6'
    0x1,  0x1,  0x9,  0x9,  0x3E,  0x0,    // [0x37] '7'
    0x1A,  0x25,  0x25,  0x25,  0x1A,  0x0,    // [0x38] '8'
    0x6,  0x9,  0x9,  0x9,  0x3E,  0x0,    // [0x39] '9'
    0x0,  0x0,  0x22,  0x0,  0x0,  0x0,    // [0x3A] ':'
    0x0,  0x40,  0x22,  0x0,  0x0,  0x0,    // [0x3B] ';'
    0x0,  0x8,  0x14,  0x22,  0x41,  0x0,    // [0x3C] '<'
    0x14,  0x14,  0x14,  0x14,  0x14,  0x0,    // [0x3D] '='
    0x0,  0x41,  0x22,  0x14,  0x8,  0x0,    // [0x3E] '>'
    0x2,  0x1,  0x29,  0x9,  0x6,  0x0,    // [0x3F] '?'
    0x1E,  0x21,  0x2D,  0x2D,  0x6,  0x0,    // [0x40] '@'
    0x3E,  0x11,  0x11,  0x11,  0x3E,  0x0,    // [0x41] 'A'
    0x3E,  0x25,  0x25,  0x25,  0x1A,  0x0,    // [0x42] 'B'
    0x1E,  0x21,  0x21,  0x21,  0x12,  0x0,    // [0x43] 'C'
    0x3E,  0x21,  0x21,  0x22,  0x1C,  0x0,    // [0x44] 'D'
    0x3F,  0x29,  0x29,  0x21,  0x21,  0x0,    // [0x45] 'E'
    0x3F,  0x9,  0x9,  0x1,  0x1,  0x0,    // [0x46] 'F'
    0x1E,  0x21,  0x29,  0x29,  0x1A,  0x0,    // [0x47] 'G'
    0x3F,  0x8,  0x8,  0x8,  0x3F,  0x0,    // [0x48] 'H'
    0x0,  0x21,  0x3F,  0x21,  0x0,  0x0,    // [0x49] 'I'
    0x10,  0x20,  0x21,  0x21,  0x1F,  0x0,    // [0x4A] 'J'
    0x3F,  0x8,  0xC,  0x12,  0x21,  0x0,    // [0x4B] 'K'
    0x1F,  0x20,  0x20,  0x20,  0x20,  0x0,    // [0x4C] 'L'
    0x3E,  0x1,  0x6,  0x1,  0x3E,  0x0,    // [0x4D] 'M'
    0x3E,  0x1,  0x1,  0x2,  0x3C,  0x0,    // [0x4E] 'N'
    0x1E,  0x21,  0x21,  0x21,  0x1E,  0x0,    // [0x4F] 'O'
    0x3E,  0x11,  0x11,  0x11,  0xE,  0x0,    // [0x50] 'P'
    0x1E,  0x21,  0x29,  0x71,  0x5E,  0x0,    // [0x51] 'Q'
    0x3E,  0x9,  0x9,  0x9,  0x36,  0x0,    // [0x52] 'R'
    0x12,  0x25,  0x25,  0x25,  0x18,  0x0,    // [0x53] 'S'
    0x1,  0x1,  0x3F,  0x1,  0x1,  0x0,    // [0x54] 'T'
    0x1F,  0x20,  0x20,  0x20,  0x1F,  0x0,    // [0x55] 'U'
    0xF,  0x10,  0x20,  0x10,  0xF,  0x0,    // [0x56] 'V'
    0x1F,  0x20,  0x18,  0x20,  0x1F,  0x0,    // [0x57] 'W'
    0x31,  0xA,  0x4,  0xA,  0x31,  0x0,    // [0x58] 'X'
    0x7,  0x28,  0x28,  0x28,  0x1F,  0x0,    // [0x59] 'Y'
    0x31,  0x29,  0x25,  0x23,  0x21,  0x0,    // [0x5A] 'Z'
    0x0,  0x7F,  0x41,  0x41,  0x0,  0x0,    // [0x5B] '['
    0x0,  0x3,  0xC,  0x30,  0xC0,  0x0,    // [0x5C] '\\'
    0x0,  0x41,  0x41,  0x7F,  0x0,  0x0,    // [0x5D] ']'
    0x0,  0x2,  0x1,  0x2,  0x0,  0x0,    // [0x5E] '^'
    0x40,  0x40,  0x40,  0x40,  0x40,  0x0,    // [0x5F] '_'
    0x1,  0x2,  0x0,  0x0,  0x0,  0x0,    // [0x60] '`'
    0x1C,  0x22,  0x22,  0x22,  0x3C,  0x0,    // [0x61] 'a'
    0x1F,  0x22,  0x22,  0x22,  0x1C,  0x0,    // [0x62] 'b'
    0x1C,  0x22,  0x22,  0x22,  0x20,  0x0,    // [0x63] 'c'
    0x1C,  0x22,  0x22,  0x22,  0x1F,  0x0,    // [0x64] 'd'
    0x1C,  0x2A,  0x2A,  0x2A,  0x4,  0x0,    // [0x65] 'e'
    0x8,  0x7E,  0x9,  0x1,  0x1,  0x0,    // [0x66] 'f'
    0xC,  0x52,  0x52,  0x52,  0x3C,  0x0,    // [0x67] 'g'
    0x3F,  0x2,  0x2,  0x2,  0x3C,  0x0,    // [0x68] 'h'
    0x0,  0x0,  0x3D,  0x0,  0x0,  0x0,    // [0x69] 'i'
    0x20,  0x40,  0x40,  0x40,  0x3D,  0x0,    // [0x6A] 'j'
    0x3F,  0x8,  0x8,  0x14,  0x22,  0x0,    // [0x6B] 'k'
    0x0,  0x1F,  0x20,  0x20,  0x0,  0x0,    // [0x6C] 'l'
    0x3C,  0x2,  0x4,  0x2,  0x3C,  0x0,    // [0x6D] 'm'
    0x3C,  0x2,  0x2,  0x2,  0x3C,  0x0,    // [0x6E] 'n'
    0x1C,  0x22,  0x22,  0x22,  0x1C,  0x0,    // [0x6F] 'o'
    0x7C,  0x12,  0x12,  0x12,  0xC,  0x0,    // [0x70] 'p'
    0xC,  0x12,  0x12,  0x12,  0x7E,  0x0,    // [0x71] 'q'
    0x3C,  0x2,  0x2,  0x2,  0x4,  0x0,    // [0x72] 'r'
    0x24,  0x2A,  0x2A,  0x2A,  0x10,  0x0,    // [0x73] 's'
    0x1F,  0x22,  0x22,  0x20,  0x10,  0x0,    // [0x74] 't'
    0x1E,  0x20,  0x20,  0x20,  0x1E,  0x0,    // [0x75] 'u'
    0xE,  0x10,  0x20,  0x10,  0xE,  0x0,    // [0x76] 'v'
    0x1E,  0x20,  0x10,  0x20,  0x1E,  0x0,    // [0x77] 'w'
    0x22,  0x14,  0x8,  0x14,  0x22,  0x0,    // [0x78] 'x'
    0xE,  0x50,  0x50,  0x50,  0x3E,  0x0,    // [0x79] 'y'
    0x22,  0x32,  0x2A,  0x26,  0x22,  0x0,    // [0x7A] 'z'
    0x0,  0x8,  0x36,  0x41,  0x0,  0x0,    // [0x7B] '{'
    0x0,  0x0,  0x7F,  0x0,  0x0,  0x0,    // [0x7C] '|'
    0x0,  0x41,  0x36,  0x8,  0x0,  0x0,    // [0x7D] '}'
    0x0,  0x2,  0x1,  0x2,  0x1,  0x0,    // [0x7E] '~'
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x7F] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x80] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x81] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x82] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x83] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x84] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x85] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x86] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x87] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x88] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x89] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x8A] ''
    0x0,  0x0,  0x0,  0x0,  0x0,  0x0,    // [0x8B] ''
};

SHT20

C/C++
SHT21_ncleee.cpp
/* Copyright (c) 2012 Graeme Coapes, Newcastle University, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


 #include "mbed.h"
 #include "SHT21_ncleee.h"

 
 // Class constructor

//SHT21::SHT21(I2C *i2c, Serial *pc) :
//_i2c(i2c), _pc(pc)
//{
//}

SHT21::SHT21(I2C *i2c) :
_i2c(i2c)
{
}

int SHT21::triggerTemp()
{
    return wr(SHT_TRIG_TEMP);
}
 
int SHT21::requestTemp()
{
    int res;
    
    int rx_bytes = 3;
    
    char rx[rx_bytes];
    
    res = _i2c->read(SHT_I2C_ADDR,rx,rx_bytes);
    
//    _pc->printf("%x %x %x \n", rx[0], rx[1], rx[2]);
    
    //should use checksum here
    
    //shift the MSByte to the left of the 16-bit temperature value
    //don't shift the LSByte
    //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor
    //temperature = ((rx[0] << 8) || (rx[1] << 0)) ; //&& (0x00FC);
    unsigned short msb = (rx[0] << 8);
    unsigned short lsb = (rx[1] << 0);
    
    temperature = msb + lsb;
    
    //_pc->printf("msb: 0x%x, lsb: 0x%x, r: 0x%x, temp: 0x%x \n",msb, lsb,r,  temperature);
    
    return res;
}
 
float SHT21::readTemp()
{
    //First of all trigger the temperature reading
    //process on the sensor
    int trig = triggerTemp();
    
    if(trig != SHT_SUCCESS)
    {
        //if this has failed, exit function with specific error condition
        return SHT_TRIG_FAIL;
    }
    
    //else pause whilst sensor is measuring
    //maximum measuring time is: 85ms
    wait_ms(100);
    
    //Now request the temperature
    if(requestTemp() != SHT_SUCCESS)
    {
        //if this has failed, exit function with specific error condition
        return SHT_READ_FAIL;
    }    
    
    //the received temperature value should now
    //be stored in the temperature field
    
    float realtemp;
    
    realtemp = -46.85 + 175.72 * ( ((float) temperature) / 65536 );
    
    return realtemp;
} 
 
int SHT21::triggerRH()
{
    return wr(SHT_TRIG_RH);
}

int SHT21::requestRH()
{
    int res;
    
    char rx[3];
    
    res = _i2c->read(SHT_I2C_ADDR,rx,3);
    
    //should use checksum here
    
    //shift the MSByte to the left of the 16-bit temperature value
    //don't shift the LSByte
    //Clear bit 1 and bit 0 of the result - these are status bits sent from the sensor
    //humidity = ((rx[0] << 8) || (rx[1] << 0)) && (0xFC);
    humidity = (rx[0]<<8) + rx[1];
    
    
    return res;
}
 
float SHT21::readHumidity()
{
    //First of all trigger the temperature reading
    //process on the sensor
    if(triggerRH() != SHT_SUCCESS)
    {
        //if this has failed, exit function with specific error condition
        return SHT_TRIG_FAIL;
    }
    
    //else pause whilst sensor is measuring
    //maximum measuring time is: 85ms
    wait_ms(100);
    
    //Now request the temperature
    if(requestRH() != SHT_SUCCESS)
    {
        //if this has failed, exit function with specific error condition
        return SHT_READ_FAIL;
    }
    
    float realhum;
    realhum = -6 + 125 * ( ((float) humidity) / 65536 );
    return realhum;
    
    //the received temperature value should now
    //be stored in the temperature field
//    return humidity;
    
    
} 
 
int SHT21::reset()
{
    return wr(SHT_SOFT_RESET);
}


int SHT21::setPrecision(char precision)
{
    int res;
    
    char command[2];
    command[0] = SHT_WRITE_REG;
    command[1] = precision;
    
    res = _i2c->write(SHT_I2C_ADDR,command,2);
    
    return res;
}

int SHT21::wr(int cmd)
{
   
    int res;
    
    char command[1];
    command[0] = cmd;
    
    
    res = _i2c->write(SHT_I2C_ADDR,command,1);    
    
    return res;
}

Credits

Clarisse Berthe

Clarisse Berthe

1 project • 1 follower

Comments