initial upload

master
Harald Pichler 2017-03-24 11:08:45 +01:00
parent 0e861e76a3
commit afde635051
23 changed files with 1379 additions and 0 deletions

View File

@ -0,0 +1,45 @@
os: linux
language: c
before_install:
- "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16"
- sleep 3
- export DISPLAY=:1.0
- wget http://downloads.arduino.cc/arduino-${ARDUINO_IDE_VERSION}-linux64.tar.xz
- tar xf arduino-${ARDUINO_IDE_VERSION}-linux64.tar.xz
- sudo mv arduino-${ARDUINO_IDE_VERSION} /usr/local/share/arduino
- sudo ln -s /usr/local/share/arduino/arduino /usr/local/bin/arduino
install:
- ln -s $PWD /usr/local/share/arduino/libraries/I2CSoilMoistureSensor
script:
- arduino --verify --board ${BOARD} $PWD/examples/ChangeSensorI2CAddress/ChangeSensorI2CAddress.ino
- arduino --verify --board ${BOARD} $PWD/examples/ReadSensorData/ReadSensorData.ino
env:
- ARDUINO_IDE_VERSION=1.6.5 BOARD=arduino:avr:uno
- ARDUINO_IDE_VERSION=1.6.10 BOARD=arduino:avr:uno
- ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:uno
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:yun
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:diecimila:cpu=atmega168
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:diecimila:cpu=atmega328
- ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:nano:cpu=atmega168
- ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:nano:cpu=atmega328
- ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:mega:cpu=atmega1280
- ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:mega:cpu=atmega2560
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:megaADK
- ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:leonardo
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:micro
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:esplora
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:mini:cpu=atmega168
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:mini:cpu=atmega328
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:ethernet
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:bt:cpu=atmega168
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:bt:cpu=atmega328
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:lilypad:cpu=atmega168
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:lilypad:cpu=atmega328
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:pro:cpu=atmega168
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:avr:pro:cpu=atmega328
# - ARDUINO_IDE_VERSION=1.6.13 BOARD=arduino:sam:arduino_due_x

View File

@ -0,0 +1 @@
,harald,20150300048NB.evva.com,24.03.2017 09:55,file:///home/harald/.config/libreoffice/4;

View File

@ -0,0 +1,231 @@
/*----------------------------------------------------------------------*
* I2CSoilMoistureSensor.cpp - Arduino library for the Sensor version of*
* I2C Soil Moisture Sensor version from Chrirp *
* (https://github.com/Miceuz/i2c-moisture-sensor). *
* *
* Ingo Fischer 11Nov2015 *
* https://github.com/Apollon77/I2CSoilMoistureSensor *
* *
* MIT license *
*----------------------------------------------------------------------*/
#include "I2CSoilMoistureSensor.h"
//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
/*----------------------------------------------------------------------*
* Constructor. *
* Optionally set sensor I2C address if different from default *
*----------------------------------------------------------------------*/
I2CSoilMoistureSensor::I2CSoilMoistureSensor(uint8_t addr) : sensorAddress(addr) {
// nothing to do ... Wire.begin needs to be put outside of class
}
/*----------------------------------------------------------------------*
* Initializes anything ... it does a reset. *
* When used without parameter or parameter value is false then a *
* waiting time of at least 1 second is expected to give the sensor *
* some time to boot up. *
* Alternatively use true as parameter and the method waits for a *
* second and returns after that. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::begin(bool wait) {
resetSensor();
if (wait) {
delay(1000);
}
}
/*----------------------------------------------------------------------*
* Return measured Soil Moisture Capacitance *
* Moisture is somewhat linear. More moisture will give you higher *
* reading. Normally all sensors give about 290 - 310 as value in free *
* air at 5V supply. *
*----------------------------------------------------------------------*/
unsigned int I2CSoilMoistureSensor::getCapacitance() {
return readI2CRegister16bitUnsigned(sensorAddress, SOILMOISTURESENSOR_GET_CAPACITANCE);
}
/*----------------------------------------------------------------------*
* Change I2C address of the sensor to the provided address (1..127) *
* and do a reset after it in order for the new address to become *
* effective if second parameter is true. *
* Method returns true if the new address is set successfully on sensor.*
*----------------------------------------------------------------------*/
bool I2CSoilMoistureSensor::setAddress(int addr, bool reset) {
writeI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_SET_ADDRESS, addr);
if (reset) {
resetSensor();
delay(1000);
}
sensorAddress=addr;
return (readI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_GET_ADDRESS) == addr);
}
/*----------------------------------------------------------------------*
* Change the address (1..127) this instance is trying to read from *
* and do a reset after to initialize. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::changeSensor(int addr, bool wait) {
sensorAddress=addr;
begin(wait);
}
/*----------------------------------------------------------------------*
* Return current Address of the Sensor *
*----------------------------------------------------------------------*/
uint8_t I2CSoilMoistureSensor::getAddress() {
return sensorAddress;
}
/*----------------------------------------------------------------------*
* Starts the measurement for the Light sensor. Wait at least 3 seconds *
* till you call method getLight to get the Light value. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::startMeasureLight() {
writeI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_MEASURE_LIGHT);
}
/*----------------------------------------------------------------------*
* Read the Light Measurement from the sensor. When used without *
* parameter or parameter value is false then a former call to *
* method startMeasureLight and a waiting time of at least 3 seconds is *
* expected. *
* Alternatively use true as parameter and the method does the call to *
* startMeasureLight and a 3 seconds delay automatically and no former *
* call is needed. *
* The measurement gives 65535 in a dark room away form desk lamp - so *
* more light, lower reading. When it's dark, it takes longer to *
* measure light, reading the light register while measurement is in *
* progress (e.g. wait time too short) will return the previous reading.*
* Be aware, light sensor is pretty noisy. *
*----------------------------------------------------------------------*/
unsigned int I2CSoilMoistureSensor::getLight(bool wait) {
if (wait) {
startMeasureLight();
delay(3000);
}
return readI2CRegister16bitUnsigned(sensorAddress, SOILMOISTURESENSOR_GET_LIGHT);
}
/*----------------------------------------------------------------------*
* Read the Temperature Measurement. Temperature is measured by the *
* thermistor on the tip of the sensor. Calculated absolute measurement *
* accuracy is better than 2%. The returned value is in degrees Celsius *
* with factor 10, so need to divide by 10 to get real value *
*----------------------------------------------------------------------*/
int I2CSoilMoistureSensor::getTemperature() {
return readI2CRegister16bitSigned(sensorAddress, SOILMOISTURESENSOR_GET_TEMPERATURE);
}
/*----------------------------------------------------------------------*
* Resets sensor. Give the sensor 0.5-1 second time to boot up after *
* reset. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::resetSensor() {
writeI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_RESET);
}
/*----------------------------------------------------------------------*
* Get Firmware Version. 0x22 means 2.2 *
*----------------------------------------------------------------------*/
uint8_t I2CSoilMoistureSensor::getVersion() {
return readI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_GET_VERSION);
}
/*----------------------------------------------------------------------*
* Sleep sensor. Initiates SLEEP_MODE_PWR_DOWN in the sensor's MCU. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::sleep() {
writeI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_SLEEP);
}
/*----------------------------------------------------------------------*
* Check if sensor is busy. Returns true if a measurement is running. *
*----------------------------------------------------------------------*/
bool I2CSoilMoistureSensor::isBusy() {
return (readI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_GET_BUSY) == 1);
}
/*----------------------------------------------------------------------*
* Helper method to write an 8 bit value to the sensor via I2C *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::writeI2CRegister8bit(int addr, int value) {
i2cBeginTransmission(addr);
i2cWrite(value);
i2cEndTransmission();
}
/*----------------------------------------------------------------------*
* Helper method to write an 8 bit value to the sensor via I2C to the *
* given register *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::writeI2CRegister8bit(int addr, int reg, int value) {
i2cBeginTransmission(addr);
i2cWrite(reg);
i2cWrite(value);
i2cEndTransmission();
}
/*----------------------------------------------------------------------*
* Helper method to read a 16 bit unsigned value from the given register*
*----------------------------------------------------------------------*/
uint16_t I2CSoilMoistureSensor::readI2CRegister16bitUnsigned(int addr, byte reg)
{
uint16_t value;
i2cBeginTransmission((uint8_t)addr);
i2cWrite((uint8_t)reg);
i2cEndTransmission();
delay(20);
i2cRequestFrom((uint8_t)addr, (byte)2);
value = (i2cRead() << 8) | i2cRead();
i2cEndTransmission();
return value;
}
/*----------------------------------------------------------------------*
* Helper method to read a 16 bit signed value from the given register*
*----------------------------------------------------------------------*/
int16_t I2CSoilMoistureSensor::readI2CRegister16bitSigned(int addr, byte reg)
{
return (int16_t)readI2CRegister16bitUnsigned(addr, reg);
}
/*----------------------------------------------------------------------*
* Helper method to read a 8 bit value from the given register *
*----------------------------------------------------------------------*/
uint8_t I2CSoilMoistureSensor::readI2CRegister8bit(int addr, int reg) {
i2cBeginTransmission(addr);
i2cWrite(reg);
i2cEndTransmission();
delay(20);
i2cRequestFrom(addr, 1);
return i2cRead();
}

View File

@ -0,0 +1,60 @@
/*----------------------------------------------------------------------*
* I2CSoilMoistureSensor.h - Arduino library for the Sensor version of *
* I2C Soil Moisture Sensor version from Chrirp *
* (https://github.com/Miceuz/i2c-moisture-sensor). *
* *
* Ingo Fischer 11Nov2015 *
* https://github.com/Apollon77/I2CSoilMoistureSensor *
* *
* MIT license *
*----------------------------------------------------------------------*/
#ifndef I2CSOILMOISTURESENSOR_H
#define I2CSOILMOISTURESENSOR_H
#include <Arduino.h>
//Default I2C Address of the sensor
#define SOILMOISTURESENSOR_DEFAULT_ADDR 0x20
//Soil Moisture Sensor Register Addresses
#define SOILMOISTURESENSOR_GET_CAPACITANCE 0x00 // (r) 2 bytes
#define SOILMOISTURESENSOR_SET_ADDRESS 0x01 // (w) 1 byte
#define SOILMOISTURESENSOR_GET_ADDRESS 0x02 // (r) 1 byte
#define SOILMOISTURESENSOR_MEASURE_LIGHT 0x03 // (w) n/a
#define SOILMOISTURESENSOR_GET_LIGHT 0x04 // (r) 2 bytes
#define SOILMOISTURESENSOR_GET_TEMPERATURE 0x05 // (r) 2 bytes
#define SOILMOISTURESENSOR_RESET 0x06 // (w) n/a
#define SOILMOISTURESENSOR_GET_VERSION 0x07 // (r) 1 bytes
#define SOILMOISTURESENSOR_SLEEP 0x08 // (w) n/a
#define SOILMOISTURESENSOR_GET_BUSY 0x09 // (r) 1 bytes
class I2CSoilMoistureSensor {
public:
I2CSoilMoistureSensor(uint8_t addr = SOILMOISTURESENSOR_DEFAULT_ADDR);
void begin(bool wait = false);
unsigned int getCapacitance();
bool setAddress(int addr, bool reset);
void changeSensor(int addr, bool wait = false);
uint8_t getAddress();
void startMeasureLight();
unsigned int getLight(bool wait = false);
int getTemperature();
void resetSensor();
uint8_t getVersion();
void sleep();
bool isBusy();
private:
int sensorAddress;
void writeI2CRegister8bit(int addr, int value);
void writeI2CRegister8bit(int addr, int reg, int value);
uint16_t readI2CRegister16bitUnsigned(int addr, byte reg);
int16_t readI2CRegister16bitSigned(int addr, byte reg);
uint8_t readI2CRegister8bit(int addr, int reg);
};
#endif

View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 Ingo Fischer
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.

View File

@ -0,0 +1,93 @@
# I2CSoilMoistureSensor
Simple Arduino Library for the I2C Soil Moisture Sensor version from Chirp
(https://github.com/Miceuz/i2c-moisture-sensor) which works really great and is ready to
use with I2C.
## Informations
More informations at: https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/
## Version History
### v1.1.2
- changed/fixed handling of negative temperature values (thanks to @krikk)
### v1.1.0
- Added PlatformIO registration files (thanks to @DigitalGrowControl)
- Added added method changeSensor() to allow to talk to multiple sensors with one instance of the class (thanks to @elzoido)
- Added methods sleep() and isBusy() which are available beginning with FW 2.3 (thanks to @sekdiy)
### v1.0.0
- Initial Release
## Methods
### Constructor I2CSoilMoistureSensor
Optionally set sensor I2C address if different from default
### begin(bool wait)
Initializes anything ... it does a reset.
When used without parameter or parameter value is false then a
waiting time of at least 1 second is expected to give the sensor
some time to boot up.
Alternatively use true as parameter and the method waits for a
second and returns after that.
### getCapacitance()
Return measured Soil Moisture Capacitance Moisture is somewhat linear. More moisture will
give you higher reading. Normally all sensors give about 200 - 300 as value in free air at
5V supply.
### setAddress(int addr, bool reset)
Change I2C address of the sensor to the provided address (1..127) and do a reset after it
in order for the new address to become effective if second parameter is true. Method
returns true if the new address is set successfully on sensor.
### getAddress()
Return current Address of the Sensor
### changeSensor(int addr, bool wait)
Changes the address (1..127) of the sensor, this instance is trying to read from
and do a reset after to initialize.
The second parameter is optional and tells the method to wait for a second to allow
the sensor to boot up.
### startMeasureLight()
Starts the measurement for the Light sensor. Wait at least 3 seconds till you call method
getLight to get the Light value. *
### getLight(bool wait)
Read the Light Measurement from the sensor. When used without parameter or parameter value
is false then a former call to method startMeasureLight and a waiting time of at least 3
seconds is expected.
Alternatively use true as parameter and the method does the call to startMeasureLight and
a 3 seconds delay automatically and no former call is needed.
The measurement gives 65535 in a dark room away form desk lamp - so more light, lower
reading. When it's dark, it takes longer to measure light, reading the light register
while measurement is in progress (e.g. wait time too short) will return the previous
reading. Be aware, light sensor is pretty noisy.
### getTemperature()
Read the Temperature Measurement. Temperature is measured by the thermistor on the tip of
the sensor. Calculated absolute measurement accuracy is better than 2%. The returned value
is in degrees Celsius with factor 10, so need to divide by 10 to get real value
### sleep()
Powers down the sensor. Use this function in order to save power inbetween measurements.
You need to have FW 2.3 from the Sensor to use this method.
### isBusy()
Checks if sensor is busy. Returns true if a measurement is running.
You need to have FW 2.3 from the Sensor to use this method.
### resetSensor()
Resets sensor. Give the sensor 0.5-1 second time to boot up after reset.
### getVersion()
Get Firmware Version. 0x22 means 2.2
## Examples
You can find examples in the examples folder of this library

View File

@ -0,0 +1,63 @@
#include <I2CSoilMoistureSensor.h>
#include <Wire.h>
I2CSoilMoistureSensor sensor(0x20);
void setup() {
Wire.begin();
Serial.begin(9600);
sensor.begin(); // reset sensor
delay(1000); // give some time to boot up
Serial.print("I2C Soil Moisture Sensor Address: ");
Serial.println(sensor.getAddress(),HEX);
Serial.print("Sensor Firmware version: ");
Serial.println(sensor.getVersion(),HEX);
Serial.println();
Serial.print("Change address to 0x21 ...");
if (sensor.setAddress(0x21,true)) // set Sensor Address to 0x21 and reset
Serial.println("... DONE");
else
Serial.println("... ERROR");
Serial.println();
}
/*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 found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}

View File

@ -0,0 +1,28 @@
#include <I2CSoilMoistureSensor.h>
#include <Wire.h>
I2CSoilMoistureSensor sensor;
void setup() {
Wire.begin();
Serial.begin(9600);
sensor.begin(); // reset sensor
delay(1000); // give some time to boot up
Serial.print("I2C Soil Moisture Sensor Address: ");
Serial.println(sensor.getAddress(),HEX);
Serial.print("Sensor Firmware version: ");
Serial.println(sensor.getVersion(),HEX);
Serial.println();
}
void loop() {
while (sensor.isBusy()) delay(50); // available since FW 2.3
Serial.print("Soil Moisture Capacitance: ");
Serial.print(sensor.getCapacitance()); //read capacitance register
Serial.print(", Temperature: ");
Serial.print(sensor.getTemperature()/(float)10); //temperature register
Serial.print(", Light: ");
Serial.println(sensor.getLight(true)); //request light measurement, wait and read light register
sensor.sleep(); // available since FW 2.3
}

View File

@ -0,0 +1,13 @@
I2CSoilMoistureSensor KEYWORD1
begin KEYWORD2
getCapacitance KEYWORD2
setAddress KEYWORD2
changeSensor KEYWORD2
getAddress KEYWORD2
startMeasureLight KEYWORD2
getLight KEYWORD2
getTemperature KEYWORD2
resetSensor KEYWORD2
getVersion KEYWORD2
sleep KEYWORD2
isBusy KEYWORD2

View File

@ -0,0 +1,20 @@
{
"name": "I2CSoilMoistureSensor",
"keywords": "Soil, Capacitance, Light, Temperature",
"authors":
{
"name": "Ingo Fischer",
"email": "ingo@fischer-ka.de",
"url": "https://github.com/Apollon77/I2CSoilMoistureSensor"
},
"description": "Provide access to all functions of the I2C Soil Moisture Sensor from Catnip Electronics.",
"repository":
{
"type": "git",
"url": "https://github.com/Apollon77/I2CSoilMoistureSensor.git"
},
"frameworks": "arduino",
"platforms": "atmelavr"
}

View File

@ -0,0 +1,9 @@
name=I2CSoilMoistureSensor
version=1.1.2
author=Ingo Fischer <ingo@fischer-ka.de>
maintainer=Ingo Fischer <ingo@fischer-ka.de>
sentence=Provide access to all functions of the I2C Soil Moisture Sensor from Catnip Electronics.
paragraph=
category=Sensors
url=https://github.com/Apollon77/I2CSoilMoistureSensor
architectures=*

View File

@ -0,0 +1,213 @@
/*----------------------------------------------------------------------*
* I2CSoilMoistureSensor.cpp - Arduino library for the Sensor version of*
* I2C Soil Moisture Sensor version from Chrirp *
* (https://github.com/Miceuz/i2c-moisture-sensor). *
* *
* Ingo Fischer 11Nov2015 *
* https://github.com/Apollon77/I2CSoilMoistureSensor *
* *
* MIT license *
*----------------------------------------------------------------------*/
#include "I2CSoilMoistureSensor.h"
//define release-independent I2C functions
#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
/*----------------------------------------------------------------------*
* Constructor. *
* Optionally set sensor I2C address if different from default *
*----------------------------------------------------------------------*/
I2CSoilMoistureSensor::I2CSoilMoistureSensor(uint8_t addr) : sensorAddress(addr) {
// nothing to do ... Wire.begin needs to be put outside of class
}
/*----------------------------------------------------------------------*
* Initializes anything ... it does a reset. *
* When used without parameter or parameter value is false then a *
* waiting time of at least 1 second is expected to give the sensor *
* some time to boot up. *
* Alternatively use true as parameter and the method waits for a *
* second and returns after that. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::begin(bool wait) {
resetSensor();
if (wait) {
delay(1000);
}
}
/*----------------------------------------------------------------------*
* Return measured Soil Moisture Capacitance *
* Moisture is somewhat linear. More moisture will give you higher *
* reading. Normally all sensors give about 290 - 310 as value in free *
* air at 5V supply. *
*----------------------------------------------------------------------*/
unsigned int I2CSoilMoistureSensor::getCapacitance() {
return readI2CRegister16bitUnsigned(sensorAddress, SOILMOISTURESENSOR_GET_CAPACITANCE);
}
/*----------------------------------------------------------------------*
* Change I2C address of the sensor to the provided address (1..127) *
* and do a reset after it in order for the new address to become *
* effective if second parameter is true. *
* Method returns true if the new address is set successfully on sensor.*
*----------------------------------------------------------------------*/
bool I2CSoilMoistureSensor::setAddress(int addr, bool reset) {
writeI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_SET_ADDRESS, addr);
if (reset) {
resetSensor();
delay(1000);
}
sensorAddress=addr;
return (readI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_GET_ADDRESS) == addr);
}
/*----------------------------------------------------------------------*
* Change the address (1..127) this instance is trying to read from *
* and do a reset after to initialize. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::changeSensor(int addr, bool wait) {
sensorAddress=addr;
begin(wait);
}
/*----------------------------------------------------------------------*
* Return current Address of the Sensor *
*----------------------------------------------------------------------*/
uint8_t I2CSoilMoistureSensor::getAddress() {
return sensorAddress;
}
/*----------------------------------------------------------------------*
* Starts the measurement for the Light sensor. Wait at least 3 seconds *
* till you call method getLight to get the Light value. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::startMeasureLight() {
writeI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_MEASURE_LIGHT);
}
/*----------------------------------------------------------------------*
* Read the Light Measurement from the sensor. When used without *
* parameter or parameter value is false then a former call to *
* method startMeasureLight and a waiting time of at least 3 seconds is *
* expected. *
* Alternatively use true as parameter and the method does the call to *
* startMeasureLight and a 3 seconds delay automatically and no former *
* call is needed. *
* The measurement gives 65535 in a dark room away form desk lamp - so *
* more light, lower reading. When it's dark, it takes longer to *
* measure light, reading the light register while measurement is in *
* progress (e.g. wait time too short) will return the previous reading.*
* Be aware, light sensor is pretty noisy. *
*----------------------------------------------------------------------*/
unsigned int I2CSoilMoistureSensor::getLight(bool wait) {
if (wait) {
startMeasureLight();
delay(3000);
}
return readI2CRegister16bitUnsigned(sensorAddress, SOILMOISTURESENSOR_GET_LIGHT);
}
/*----------------------------------------------------------------------*
* Read the Temperature Measurement. Temperature is measured by the *
* thermistor on the tip of the sensor. Calculated absolute measurement *
* accuracy is better than 2%. The returned value is in degrees Celsius *
* with factor 10, so need to divide by 10 to get real value *
*----------------------------------------------------------------------*/
int I2CSoilMoistureSensor::getTemperature() {
return readI2CRegister16bitSigned(sensorAddress, SOILMOISTURESENSOR_GET_TEMPERATURE);
}
/*----------------------------------------------------------------------*
* Resets sensor. Give the sensor 0.5-1 second time to boot up after *
* reset. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::resetSensor() {
writeI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_RESET);
}
/*----------------------------------------------------------------------*
* Get Firmware Version. 0x22 means 2.2 *
*----------------------------------------------------------------------*/
uint8_t I2CSoilMoistureSensor::getVersion() {
return readI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_GET_VERSION);
}
/*----------------------------------------------------------------------*
* Sleep sensor. Initiates SLEEP_MODE_PWR_DOWN in the sensor's MCU. *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::sleep() {
writeI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_SLEEP);
}
/*----------------------------------------------------------------------*
* Check if sensor is busy. Returns true if a measurement is running. *
*----------------------------------------------------------------------*/
bool I2CSoilMoistureSensor::isBusy() {
return (readI2CRegister8bit(sensorAddress, SOILMOISTURESENSOR_GET_BUSY) == 1);
}
/*----------------------------------------------------------------------*
* Helper method to write an 8 bit value to the sensor via I2C *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::writeI2CRegister8bit(int addr, int value) {
i2cBeginTransmission(addr);
i2cWrite(value);
i2cEndTransmission();
}
/*----------------------------------------------------------------------*
* Helper method to write an 8 bit value to the sensor via I2C to the *
* given register *
*----------------------------------------------------------------------*/
void I2CSoilMoistureSensor::writeI2CRegister8bit(int addr, int reg, int value) {
i2cBeginTransmission(addr);
i2cWrite(reg);
i2cWrite(value);
i2cEndTransmission();
}
/*----------------------------------------------------------------------*
* Helper method to read a 16 bit unsigned value from the given register*
*----------------------------------------------------------------------*/
uint16_t I2CSoilMoistureSensor::readI2CRegister16bitUnsigned(int addr, byte reg)
{
uint16_t value;
i2cBeginTransmission((uint8_t)addr);
i2cWrite((uint8_t)reg);
i2cEndTransmission();
delay(20);
i2cRequestFrom((uint8_t)addr, (byte)2);
value = (i2cRead() << 8) | i2cRead();
i2cEndTransmission();
return value;
}
/*----------------------------------------------------------------------*
* Helper method to read a 16 bit signed value from the given register*
*----------------------------------------------------------------------*/
int16_t I2CSoilMoistureSensor::readI2CRegister16bitSigned(int addr, byte reg)
{
return (int16_t)readI2CRegister16bitUnsigned(addr, reg);
}
/*----------------------------------------------------------------------*
* Helper method to read a 8 bit value from the given register *
*----------------------------------------------------------------------*/
uint8_t I2CSoilMoistureSensor::readI2CRegister8bit(int addr, int reg) {
i2cBeginTransmission(addr);
i2cWrite(reg);
i2cEndTransmission();
delay(20);
i2cRequestFrom(addr, 1);
return i2cRead();
}

View File

@ -0,0 +1,60 @@
/*----------------------------------------------------------------------*
* I2CSoilMoistureSensor.h - Arduino library for the Sensor version of *
* I2C Soil Moisture Sensor version from Chrirp *
* (https://github.com/Miceuz/i2c-moisture-sensor). *
* *
* Ingo Fischer 11Nov2015 *
* https://github.com/Apollon77/I2CSoilMoistureSensor *
* *
* MIT license *
*----------------------------------------------------------------------*/
#ifndef I2CSOILMOISTURESENSOR_H
#define I2CSOILMOISTURESENSOR_H
#include <Arduino.h>
//Default I2C Address of the sensor
#define SOILMOISTURESENSOR_DEFAULT_ADDR 0x20
//Soil Moisture Sensor Register Addresses
#define SOILMOISTURESENSOR_GET_CAPACITANCE 0x00 // (r) 2 bytes
#define SOILMOISTURESENSOR_SET_ADDRESS 0x01 // (w) 1 byte
#define SOILMOISTURESENSOR_GET_ADDRESS 0x02 // (r) 1 byte
#define SOILMOISTURESENSOR_MEASURE_LIGHT 0x03 // (w) n/a
#define SOILMOISTURESENSOR_GET_LIGHT 0x04 // (r) 2 bytes
#define SOILMOISTURESENSOR_GET_TEMPERATURE 0x05 // (r) 2 bytes
#define SOILMOISTURESENSOR_RESET 0x06 // (w) n/a
#define SOILMOISTURESENSOR_GET_VERSION 0x07 // (r) 1 bytes
#define SOILMOISTURESENSOR_SLEEP 0x08 // (w) n/a
#define SOILMOISTURESENSOR_GET_BUSY 0x09 // (r) 1 bytes
class I2CSoilMoistureSensor {
public:
I2CSoilMoistureSensor(uint8_t addr = SOILMOISTURESENSOR_DEFAULT_ADDR);
void begin(bool wait = false);
unsigned int getCapacitance();
bool setAddress(int addr, bool reset);
void changeSensor(int addr, bool wait = false);
uint8_t getAddress();
void startMeasureLight();
unsigned int getLight(bool wait = false);
int getTemperature();
void resetSensor();
uint8_t getVersion();
void sleep();
bool isBusy();
private:
int sensorAddress;
void writeI2CRegister8bit(int addr, int value);
void writeI2CRegister8bit(int addr, int reg, int value);
uint16_t readI2CRegister16bitUnsigned(int addr, byte reg);
int16_t readI2CRegister16bitSigned(int addr, byte reg);
uint8_t readI2CRegister8bit(int addr, int reg);
};
#endif

View File

@ -0,0 +1,71 @@
# Set this to the name of your sketch (without extension .pde)
SKETCH=sketch
EXE=arduino-example
all: $(EXE)
CONTIKI=../../..
# Contiki IPv6 configuration
CONTIKI_WITH_IPV6 = 1
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
LFLAGS += -lm
PROJECT_SOURCEFILES += ${SKETCH}.cpp I2CSoilMoistureSensor.cpp
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES= $(notdir \
$(shell find $(REST_RESOURCES_DIR) -name '*.c') \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# variable for Makefile.include
ifneq ($(TARGET), minimal-net)
CFLAGS += -DUIP_CONF_IPV6_RPL=1
else
# minimal-net does not support RPL under Linux and is mostly used to test CoAP only
${info INFO: compiling without RPL}
CFLAGS += -DUIP_CONF_IPV6_RPL=0
CFLAGS += -DHARD_CODED_ADDRESS=\"fdfd::10\"
${info INFO: compiling with large buffers}
CFLAGS += -DUIP_CONF_BUFFER_SIZE=2048
CFLAGS += -DREST_MAX_CHUNK_SIZE=1024
CFLAGS += -DCOAP_MAX_HEADER_SIZE=640
endif
# linker optimizations
SMALL=1
# REST Engine shall use Erbium CoAP implementation
APPS += er-coap
APPS += rest-engine
APPS += arduino
include $(CONTIKI)/Makefile.include
include $(CONTIKI)/apps/arduino/Makefile.include
$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c
(cd $(CONTIKI)/tools && $(MAKE) tunslip6)
connect-router: $(CONTIKI)/tools/tunslip6
sudo $(CONTIKI)/tools/tunslip6 aaaa::1/64
connect-router-cooja: $(CONTIKI)/tools/tunslip6
sudo $(CONTIKI)/tools/tunslip6 -a 127.0.0.1 aaaa::1/64
connect-minimal:
sudo ip address add fdfd::1/64 dev tap0
avr-size: $(EXE).$(TARGET).sz
flash: $(EXE).$(TARGET).u $(EXE).$(TARGET).eu
.PHONY: flash avr-size
.PRECIOUS: $(EXE).$(TARGET).hex $(EXE).$(TARGET).eep

View File

@ -0,0 +1,11 @@
Arduino compatibility example
=============================
This example shows that it is now possible to re-use arduino sketches in
Contiki. This example documents the necessary magic. Arduino specifies
two routines, `setup` and `loop`. Before `setup` is called, the
framework initializes hardware. In original Arduino, all this is done in
a `main` function (in C). For contiki we define a process that does the
same.
See the documentation file in apps/contiki-compat/README.md

View File

@ -0,0 +1,2 @@
#include <arduino-process.h>
AUTOSTART_PROCESSES(&arduino_sketch);

View File

@ -0,0 +1,2 @@
#!/bin/bash
make TARGET=osd-merkur-128 flash

View File

@ -0,0 +1,106 @@
/*
* Copyright (c) 2010, Swedish Institute of Computer Science.
* 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 Institute 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 INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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 PROJECT_RPL_WEB_CONF_H_
#define PROJECT_RPL_WEB_CONF_H_
#define PLATFORM_HAS_LEDS 1
//#define PLATFORM_HAS_BUTTON 1
#define PLATFORM_HAS_BATTERY 1
#define SICSLOWPAN_CONF_FRAG 1
#define LOOP_INTERVAL (3 * CLOCK_SECOND)
/* Save energy */
//#define RDC_CONF_PT_YIELD_OFF
/* For Debug: Dont allow MCU sleeping between channel checks */
//#undef RDC_CONF_MCU_SLEEP
//#define RDC_CONF_MCU_SLEEP 0
/* Disabling RDC for demo purposes. Core updates often require more memory. */
/* For projects, optimize memory and enable RDC again. */
// #undef NETSTACK_CONF_RDC
//#define NETSTACK_CONF_RDC nullrdc_driver
/* Increase rpl-border-router IP-buffer when using more than 64. */
#undef REST_MAX_CHUNK_SIZE
#define REST_MAX_CHUNK_SIZE 64
/* Estimate your header size, especially when using Proxy-Uri. */
/*
#undef COAP_MAX_HEADER_SIZE
#define COAP_MAX_HEADER_SIZE 70
*/
/* The IP buffer size must fit all other hops, in particular the border router. */
#undef UIP_CONF_BUFFER_SIZE
#define UIP_CONF_BUFFER_SIZE 256
/* Multiplies with chunk size, be aware of memory constraints. */
#undef COAP_MAX_OPEN_TRANSACTIONS
#define COAP_MAX_OPEN_TRANSACTIONS 4
/* Must be <= open transaction number, default is COAP_MAX_OPEN_TRANSACTIONS-1. */
/*
#undef COAP_MAX_OBSERVERS
#define COAP_MAX_OBSERVERS 2
*/
/* Filtering .well-known/core per query can be disabled to save space. */
/*
#undef COAP_LINK_FORMAT_FILTERING
#define COAP_LINK_FORMAT_FILTERING 0
*/
/* Save some memory for the sky platform. */
/*
#undef NBR_TABLE_CONF_MAX_NEIGHBORS
#define NBR_TABLE_CONF_MAX_NEIGHBORS 10
#undef UIP_CONF_MAX_ROUTES
#define UIP_CONF_MAX_ROUTES 10
*/
/* Reduce 802.15.4 frame queue to save RAM. */
/*
#undef QUEUEBUF_CONF_NUM
#define QUEUEBUF_CONF_NUM 4
*/
/*
#undef SICSLOWPAN_CONF_FRAG
#define SICSLOWPAN_CONF_FRAG 1
*/
#endif /* PROJECT_RPL_WEB_CONF_H_ */

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* 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 Institute 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 INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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.
*
* This file is part of the Contiki operating system.
*/
/**
* \file
* Barometer resource
* \author
* Harald Pichler <harald@the-develop.net>
*/
#include "contiki.h"
#include <string.h>
#include "rest-engine.h"
#include "Arduino.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from the sensor with a simple etag */
RESOURCE(res_soilcap,
"title=\"soil status\";rt=\"atm\"",
res_get_handler,
NULL,
NULL,
NULL);
extern char soilcap_s[8];
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", soilcap_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'temperature':%s}", soilcap_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* 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 Institute 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 INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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.
*
* This file is part of the Contiki operating system.
*/
/**
* \file
* Barometer resource
* \author
* Harald Pichler <harald@the-develop.net>
*/
#include "contiki.h"
#include <string.h>
#include "rest-engine.h"
#include "Arduino.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from the sensor with a simple etag */
RESOURCE(res_soillight,
"title=\"pressure status\";rt=\"pressure\"",
res_get_handler,
NULL,
NULL,
NULL);
extern char soillight_s[8];
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", soillight_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'pressure':%s}", soillight_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* 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 Institute 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 INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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.
*
* This file is part of the Contiki operating system.
*/
/**
* \file
* Barometer resource
* \author
* Harald Pichler <harald@the-develop.net>
*/
#include "contiki.h"
#include <string.h>
#include "rest-engine.h"
#include "Arduino.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from the sensor with a simple etag */
RESOURCE(res_soiltemp,
"title=\"Temperature status\";rt=\"Temperatur\"",
res_get_handler,
NULL,
NULL,
NULL);
extern char soiltemp_s[8];
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", soiltemp_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'temperature':%s}", soiltemp_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}

View File

@ -0,0 +1,5 @@
#!/bin/bash
# For the ages-old bootloader (before 2014) you want to use
# BOOTLOADER_GET_MAC=0x0001f3a0 as parameter to make below.
make clean TARGET=osd-merkur-128
make TARGET=osd-merkur-128

View File

@ -0,0 +1,87 @@
/*
* Sample arduino sketch using contiki features.
* We turn the LED off
* We allow read the moisture sensor
* Unfortunately sleeping for long times in loop() isn't currently
* possible, something turns off the CPU (including PWM outputs) if a
* Proto-Thread is taking too long. We need to find out how to sleep in
* a Contiki-compatible way.
* Note that for a normal arduino sketch you won't have to include any
* of the contiki-specific files here, the sketch should just work.
*/
#include <Wire.h>
#include "I2CSoilMoistureSensor.h"
extern "C" {
#include "arduino-process.h"
#include "rest-engine.h"
extern resource_t res_soiltemp,res_soilcap,res_soillight, res_battery;
float soilcap;
float soiltemp;
float soillight;
uint8_t soiladdr;
uint8_t soilversion;
char soilcap_s[8];
char soiltemp_s[8];
char soillight_s[8];
I2CSoilMoistureSensor sensor;
#define LED_PIN 4
}
void setup (void)
{
// switch off the led
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
// Soil sensor
Wire.begin();
sensor.begin(); // reset sensor
delay(1000); // give some time to boot up
printf("I2C Soil Moisture Sensor Address: ");
soiladdr = sensor.getAddress();
printf("%X\n", soiladdr);
printf("Sensor Firmware version: ");
soilversion = sensor.getVersion();
printf("%X\n", soilversion);
sensor.startMeasureLight();
// init coap resourcen
rest_init_engine ();
#pragma GCC diagnostic ignored "-Wwrite-strings"
rest_activate_resource (&res_soiltemp, "s/temp");
rest_activate_resource (&res_soilcap, "s/soil");
rest_activate_resource (&res_soillight, "s/light");
rest_activate_resource (&res_battery, "s/battery");
#pragma GCC diagnostic pop
}
// at project-conf.h
// LOOP_INTERVAL (10 * CLOCK_SECOND)
void loop (void)
{
if(!sensor.isBusy()){ // available since FW 2.3
soilcap = sensor.getCapacitance(); //read capacitance register
soiltemp = sensor.getTemperature()/(float)10; //temperature register
soillight = sensor.getLight(0); //request light measurement, read light register
sensor.startMeasureLight();
dtostrf(soilcap , 0, 2, soilcap_s );
dtostrf(soiltemp , 0, 2, soiltemp_s );
dtostrf(soillight , 0, 2, soillight_s );
// sensor.sleep(); // available since FW 2.3
// Debug Print
printf("Temp: %s",soiltemp_s);
printf("\t\tSoil: %s",soilcap_s);
printf("\t\tLight: %s\n",soillight_s);
}
}