Chirp is a great little soil moisture sensor. This is a library for the Chirp soil moisture sensor. It supports reading Capacitance (soil moisture) and soil temperature. You can get Chirp on Tindie https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/ And read documentation on github https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/

Dependents:   Test_Chirp

Committer:
Vlad
Date:
Fri Jan 20 00:36:11 2017 +0000
Revision:
1:faff11456df9
Parent:
0:6164b94deb27
10khz i2c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vlad 0:6164b94deb27 1 #include "mbed.h"
Vlad 0:6164b94deb27 2 #include "Chirp.h"
Vlad 0:6164b94deb27 3
Vlad 0:6164b94deb27 4 //Chirp Docs are at https://github.com/Miceuz/i2c-moisture-sensor and https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/
Vlad 0:6164b94deb27 5
Vlad 0:6164b94deb27 6 Chirp::Chirp(PinName sda, PinName scl, char slave_adr)
Vlad 0:6164b94deb27 7 :i2c_p(new I2C(sda, scl)), i2c(*i2c_p), address(slave_adr)
Vlad 0:6164b94deb27 8 {
Vlad 1:faff11456df9 9 i2c.frequency(10000);
Vlad 0:6164b94deb27 10 }
Vlad 0:6164b94deb27 11
Vlad 0:6164b94deb27 12 float Chirp::getCapacitance(){
Vlad 1:faff11456df9 13 i2c.frequency(10000);
Vlad 0:6164b94deb27 14 short capacitance = 0;
Vlad 0:6164b94deb27 15 char i2cData[2]= {0};
Vlad 0:6164b94deb27 16
Vlad 0:6164b94deb27 17 char dataReg = 0; //Capacitance is stored in 2 bytes at address 0
Vlad 0:6164b94deb27 18 i2c.write(address, &dataReg, 1);
Vlad 0:6164b94deb27 19 int success = i2c.read(address, i2cData, 2);
Vlad 0:6164b94deb27 20
Vlad 0:6164b94deb27 21 if(success != 0)
Vlad 0:6164b94deb27 22 return -1000000;
Vlad 0:6164b94deb27 23 else
Vlad 0:6164b94deb27 24 {
Vlad 0:6164b94deb27 25 //I2C bytes are in different byte order, write into short bytes 0 and 1 in reverse order!
Vlad 0:6164b94deb27 26 *((char*)&capacitance) = i2cData[1];
Vlad 0:6164b94deb27 27 *(((char*)(&capacitance)) + 1) = i2cData[0];
Vlad 0:6164b94deb27 28 return capacitance;
Vlad 0:6164b94deb27 29 }
Vlad 0:6164b94deb27 30 }
Vlad 0:6164b94deb27 31
Vlad 0:6164b94deb27 32 float Chirp::getTemperature(){
Vlad 1:faff11456df9 33 i2c.frequency(10000);
Vlad 0:6164b94deb27 34 short temperature = 0;
Vlad 0:6164b94deb27 35 char i2cData[2]= {0};
Vlad 0:6164b94deb27 36
Vlad 1:faff11456df9 37
Vlad 0:6164b94deb27 38 char dataReg = 5; //Temperature is stored in 2 bytes at address 5
Vlad 0:6164b94deb27 39 i2c.write(address, &dataReg, 1);
Vlad 0:6164b94deb27 40 int success = i2c.read(address, i2cData, 2);
Vlad 0:6164b94deb27 41
Vlad 0:6164b94deb27 42 if(success != 0)
Vlad 0:6164b94deb27 43 return -1000000;
Vlad 0:6164b94deb27 44 else
Vlad 0:6164b94deb27 45 {
Vlad 0:6164b94deb27 46 *((char*)&temperature) = i2cData[1];
Vlad 0:6164b94deb27 47 *(((char*)(&temperature)) + 1) = i2cData[0];
Vlad 0:6164b94deb27 48 return (float) temperature / 10.0;
Vlad 0:6164b94deb27 49 }
Vlad 0:6164b94deb27 50 }
Vlad 0:6164b94deb27 51