i2c-moisture-sensor, 基于I2C的土壤水分传感器

分享于 

9分钟阅读

GitHub

  繁體 雙語
I2C based soil moisture sensor
  • 源代码名称:i2c-moisture-sensor
  • 源代码网址:http://www.github.com/Miceuz/i2c-moisture-sensor
  • i2c-moisture-sensor源代码文档
  • i2c-moisture-sensor源代码下载
  • Git URL:
    git://www.github.com/Miceuz/i2c-moisture-sensor.git
    Git Clone代码到本地:
    git clone http://www.github.com/Miceuz/i2c-moisture-sensor
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/Miceuz/i2c-moisture-sensor
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    i2c-moisture-sensor

    基于I2C的土壤水分传感器。 调频植物水报警系统的延续。 还有一个 RS485 和一个模拟插件版本。

    I2C协议

    用于读写的可用寄存器。

    名称 register R/w 数据长度
    GET_CAPACITANCE0x00( R )2
    SET_ADDRESS0x01( w )1
    GET_ADDRESS0x02( R )1
    MEASURE_LIGHT0x03( w )0
    GET_LIGHT0x04( R )2
    GET_TEMPERATURE0x05( R )2
    重置0x06( w )0
    GET_VERSION0x07( R )1
    睡眠0x08( w )0
    GET_BUSY0x09( R )1

    如果任何度量正在进行,GET_BUSY返回 1,否则返回 0.

    用于 树莓派的 python-库

    注意:如果在 3上遇到问题,请将这一行添加到/boot/config. txt中,从而降低I2C总线速度: dtparam=i2c1_baudrate=30000

    它拥有非常全面的文档并且涵盖了许多功能。

    某些功能:

    • 使用触发器功能触发所有已经启用的传感器。 用户可选。
    • 以( 需要校准) 或者电容值获取土壤水分。
    • 有几种温度可以供选择。 摄氏度,Farenheit和开尔文。
    • 校准温度传感器的偏移。
    • 所有电路板传感器的测量时间戳。
    • 内置支持更改传感器的I2C地址。
    • 深度睡眠模式以节约功耗。
    • 土壤水分校准工具。

    树莓派 示例

    这是 Daniel Tamm和 Jasper Wallace提供的接口类

    #!/usr/bin/python# cannot use python3 because smbus not working there# Modified script from https://github.com/JasperWallace/chirp-graphite/blob/master/chirp.py# by DanielTammimport smbus, time, sysclassChirp:
     def__init__(self, bus=1, address=0x20):
     self.bus_num = bus
     self.bus = smbus.SMBus(bus)
     self.address = address
     defget_reg(self, reg):
     # read 2 bytes from register val =self.bus.read_word_data(self.address, reg)
     # return swapped bytes (they come in wrong order)return (val >>8) + ((val &0xFF) <<8)
     defreset(self):
     # To reset the sensor, write 6 to the device I2C addressself.bus.write_byte(self.address, 6)
     defset_addr(self, new_addr):
     # To change the I2C address of the sensor, write a new address# (one byte [1..127]) to register 1; the new address will take effect after resetself.bus.write_byte_data(self.address, 1, new_addr)
     self.reset()
     self.address = new_addr
     defmoist(self):
     # To read soil moisture, read 2 bytes from register 0returnself.get_reg(0)
     deftemp(self):
     # To read temperature, read 2 bytes from register 5returnself.get_reg(5)
     deflight(self):
     # To read light level, start measurement by writing 3 to the# device I2C address, wait for 3 seconds, read 2 bytes from register 4self.bus.write_byte(self.address, 3)
     time.sleep(1.5)
     returnself.get_reg(4)
     def__repr__(self):
     return"<Chirp sensor on bus %d, addr %d>"% (self.bus_num, self.address)if__name__=="__main__":
     addr =0x20iflen(sys.argv) ==2:
     if sys.argv[1].startswith("0x"):
     addr =int(sys.argv[1], 16)
     else:
     addr =int(sys.argv[1])
     chirp = Chirp(1, addr)
     print chirp
     print"MoisturetTemperaturetBrightness"whileTrue:
     print"%dt%dt%d"% (chirp.moist(), chirp.temp(), chirp.light())
     time.sleep(1)

    这是由用户 krikk 提供的另一个RasPi示例。

    #!/usr/bin/python#https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_I2Cfrom Adafruit_I2C import Adafruit_I2Cfrom time import sleep, strftimefrom datetime import datetime
    deviceAddr =0x20i2c = Adafruit_I2C( deviceAddr, -1, False )#to change adress#i2c.write8( 1, 0x22 )#reset sensor, we need this otherwise i get inconsistent light reading in the dark...i2c.write8( deviceAddr, 0x06 )
    sleep(5)
    i2c.write8(deviceAddr, 3)
    sleep(3)
    light = i2c.readU16(4, False)
    temp = i2c.readS16(5, False)/float(10)
    moisture = i2c.readU16(0, False)print"TemperaturetMoisturetBrightness"printstr(temp) +":"+str(moisture) +":"+str(light)

    Ingo Fischer为传感器编写了一个Arduino库,它有几个现成的示例: https://github.com/Apollon77/I2CSoilMoistureSensor

    下面是bare的旧示例,说明了基本的I2C使用。

    示例

    
    #include <Wire.h>
    
    
    
    void writeI2CRegister8bit(int addr, int value) {
    
    
     Wire.beginTransmission(addr);
    
    
     Wire.write(value);
    
    
     Wire.endTransmission();
    
    
    }
    
    
    
    unsigned int readI2CRegister16bit(int addr, int reg) {
    
    
     Wire.beginTransmission(addr);
    
    
     Wire.write(reg);
    
    
     Wire.endTransmission();
    
    
     delay(20);
    
    
     Wire.requestFrom(addr, 2);
    
    
     unsigned int t = Wire.read() <<8;
    
    
     t = t | Wire.read();
    
    
     return t;
    
    
    }
    
    
    
    void setup() {
    
    
     Wire.begin();
    
    
     Serial.begin(9600);
    
    
     writeI2CRegister8bit(0x20, 6);//reset
    
    
    }
    
    
    
    void loop() {
    
    
     Serial.print(readI2CRegister16bit(0x20, 0));//read capacitance register
    
    
     Serial.print(",");
    
    
     Serial.print(readI2CRegister16bit(0x20, 5));//temperature register
    
    
     Serial.print(",");
    
    
     writeI2CRegister8bit(0x20, 3);//request light measurement 
    
    
     Serial.println(readI2CRegister16bit(0x20, 4));//read light register
    
    
    }
    
    
    
    

    电子邮件地址更改示例

    默认情况下,传感器以 0设置作为地址,这是关于如何更改indivitual传感器地址的示例:

    
    #include <Wire.h>
    
    
    
    void writeI2CRegister8bit(int addr, int reg, int value) {
    
    
     Wire.beginTransmission(addr);
    
    
     Wire.write(reg);
    
    
     Wire.write(value);
    
    
     Wire.endTransmission();
    
    
    }
    
    
    
    void writeI2CRegister8bit(int addr, int value) {
    
    
     Wire.beginTransmission(addr);
    
    
     Wire.write(value);
    
    
     Wire.endTransmission();
    
    
    }
    
    
    
    void setup() {
    
    
     Wire.begin();
    
    
     Serial.begin(9600);
    
    
    //talking to the default address 0x20
    
    
     writeI2CRegister8bit(0x20, 1, 0x21);//change address to 0x21
    
    
     writeI2CRegister8bit(0x20, 6);//reset
    
    
     delay(1000);//give it some time to boot
    
    
    }
    
    
    
    /*loop scans I2C bus and displays foud addresses*/
    
    
    void loop()
    
    
    {
    
    
     byte error, address;
    
    
     int nDevices;
    
    
    
     Serial.println("Scanning...");
    
    
    
     nDevices = 0;
    
    
     for(address = 1; address <127; address++ )
    
    
     {
    
    
    //The i2c_scanner uses the return value of
    
    
    //the Write.endTransmisstion to see if
    
    
    //a device did acknowledge to the address.
    
    
     Wire.beginTransmission(address);
    
    
     error = Wire.endTransmission();
    
    
    
     if (error == 0)
    
    
     {
    
    
     Serial.print("I2C device found at address 0x");
    
    
     if (address<16)
    
    
     Serial.print("0");
    
    
     Serial.print(address,HEX);
    
    
     Serial.println("!");
    
    
    
     nDevices++;
    
    
     }
    
    
     else if (error==4)
    
    
     {
    
    
     Serial.print("Unknow error at address 0x");
    
    
     if (address<16)
    
    
     Serial.print("0");
    
    
     Serial.println(address,HEX);
    
    
     } 
    
    
     }
    
    
     if (nDevices == 0)
    
    
     Serial.println("No I2C devices foundn");
    
    
     else
    
    
     Serial.println("donen");
    
    
    
     delay(5000);//wait 5 seconds for next scan
    
    
    }
    
    
    
    

    粒子光子

    有一个伟大的教程,由Miriam的粒子光子板。 还有一个库可用,由Mike提供。


    BASE  sens  传感器  I2C  SOIL  
    相关文章