Help with I2C soil moisture sensor libraries

I’m trying to use this I2C soil moisture sensor with the Electron

Their github refers to this tutorial for working with particle devices.

The libraries and code example are here

I have tried to port Apollo77’s examples using the above-mentioned tutorial

Basically, this is their instructions for porting the code are
" 3) INCLUDES In any library or example files you added to your project, strip out “define if/else” statements and their contents, then replace them with the following line (which will add Wire library and device functionality): #include “Particle.h”
"

I tried this with no success because of the wire library.

I was wondering if someone has already got this sensor working on a particle device and could share their code.

Thanks

What is this supposed to mean?
Do you get any error messages? Posting them would help.

But without knowing your error messages, I'd guess you get a file not found message, which is exactly why they said "wire library is included via Particle.h"

And looking at the library, I'd also say

//define release-independent I2C functions
#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#include <TinyWireM.h>
#define i2cBegin TinyWireM.begin
#define i2cBeginTransmission TinyWireM.beginTransmission
#define i2cEndTransmission TinyWireM.endTransmission
#define i2cRequestFrom TinyWireM.requestFrom
#define i2cRead TinyWireM.receive
#define i2cWrite TinyWireM.send
#elif ARDUINO >= 100
#include <Wire.h>
#define i2cBegin Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission Wire.endTransmission
#define i2cRequestFrom Wire.requestFrom
#define i2cRead Wire.read
#define i2cWrite Wire.write
#else
#include <Wire.h>
#define i2cBegin Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission Wire.endTransmission
#define i2cRequestFrom Wire.requestFrom
#define i2cRead Wire.receive
#define i2cWrite Wire.send
#endif

should be replaced with

#include "Particle.h"
#define i2cBegin             Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission   Wire.endTransmission
#define i2cRequestFrom       Wire.requestFrom
#define i2cRead              Wire.read
#define i2cWrite             Wire.write

Much Thanks ScruffR !

The changes you suggested to the .cpp file fixed my issue - everything working fine now.

For other newbies that might be working with with this sensor, here are a few notes:

Use the library and examples provided by Apollo77

  1. modify I2CSoilMoistureSensor.cpp as ScruffR has shown above
  2. modify I2CSoilMoistureSensor.h by replacing

#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

With

#include “Particle.h”

  1. in the example programs like ReadSensorData.ino
    delete #include Wire.h

  2. remember to include 10K pull up resistors on D0 (SDA) and D1 (SCK) - these ports are wired to pins SDA and SCK on the sensor header. Wire VCC to 3.3V and GND to GND - four wires total. The MISO and Reset pins on the sensor are not used for these examples

2 Likes