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/
Chirp.cpp@0:6164b94deb27, 2017-01-12 (annotated)
- Committer:
- Vlad
- Date:
- Thu Jan 12 22:01:43 2017 +0000
- Revision:
- 0:6164b94deb27
- Child:
- 1:faff11456df9
Chirp Library. ; https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/; https://github.com/Miceuz/i2c-moisture-sensor; Supports reading soil moisture and temperature.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 0:6164b94deb27 | 9 | |
| Vlad | 0:6164b94deb27 | 10 | } |
| Vlad | 0:6164b94deb27 | 11 | |
| Vlad | 0:6164b94deb27 | 12 | float Chirp::getCapacitance(){ |
| Vlad | 0:6164b94deb27 | 13 | short capacitance = 0; |
| Vlad | 0:6164b94deb27 | 14 | char i2cData[2]= {0}; |
| Vlad | 0:6164b94deb27 | 15 | |
| Vlad | 0:6164b94deb27 | 16 | char dataReg = 0; //Capacitance is stored in 2 bytes at address 0 |
| Vlad | 0:6164b94deb27 | 17 | i2c.write(address, &dataReg, 1); |
| Vlad | 0:6164b94deb27 | 18 | int success = i2c.read(address, i2cData, 2); |
| Vlad | 0:6164b94deb27 | 19 | |
| Vlad | 0:6164b94deb27 | 20 | if(success != 0) |
| Vlad | 0:6164b94deb27 | 21 | return -1000000; |
| Vlad | 0:6164b94deb27 | 22 | else |
| Vlad | 0:6164b94deb27 | 23 | { |
| Vlad | 0:6164b94deb27 | 24 | //I2C bytes are in different byte order, write into short bytes 0 and 1 in reverse order! |
| Vlad | 0:6164b94deb27 | 25 | *((char*)&capacitance) = i2cData[1]; |
| Vlad | 0:6164b94deb27 | 26 | *(((char*)(&capacitance)) + 1) = i2cData[0]; |
| Vlad | 0:6164b94deb27 | 27 | return capacitance; |
| Vlad | 0:6164b94deb27 | 28 | } |
| Vlad | 0:6164b94deb27 | 29 | } |
| Vlad | 0:6164b94deb27 | 30 | |
| Vlad | 0:6164b94deb27 | 31 | float Chirp::getTemperature(){ |
| Vlad | 0:6164b94deb27 | 32 | short temperature = 0; |
| Vlad | 0:6164b94deb27 | 33 | char i2cData[2]= {0}; |
| Vlad | 0:6164b94deb27 | 34 | |
| Vlad | 0:6164b94deb27 | 35 | char dataReg = 5; //Temperature is stored in 2 bytes at address 5 |
| Vlad | 0:6164b94deb27 | 36 | i2c.write(address, &dataReg, 1); |
| Vlad | 0:6164b94deb27 | 37 | int success = i2c.read(address, i2cData, 2); |
| Vlad | 0:6164b94deb27 | 38 | |
| Vlad | 0:6164b94deb27 | 39 | if(success != 0) |
| Vlad | 0:6164b94deb27 | 40 | return -1000000; |
| Vlad | 0:6164b94deb27 | 41 | else |
| Vlad | 0:6164b94deb27 | 42 | { |
| Vlad | 0:6164b94deb27 | 43 | *((char*)&temperature) = i2cData[1]; |
| Vlad | 0:6164b94deb27 | 44 | *(((char*)(&temperature)) + 1) = i2cData[0]; |
| Vlad | 0:6164b94deb27 | 45 | return (float) temperature / 10.0; |
| Vlad | 0:6164b94deb27 | 46 | } |
| Vlad | 0:6164b94deb27 | 47 | } |
| Vlad | 0:6164b94deb27 | 48 |