@ambroisedutilleul Here is the code that’s working for me and the SHT1 sensor.
I’m sending data to Ubidots.com with this code.
// This #include statement was automatically added by the Spark IDE.
#include "SHT1x.h"
// This #include statement was automatically added by the Spark IDE.
#include "HttpClient/HttpClient.h"
/**
* ReadSHT1xValues
*
* Read temperature and humidity values from an SHT1x-series (SHT10,
* SHT11, SHT15) sensor.
*
* Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
* www.practicalarduino.com
*/
// Specify data and clock connections and instantiate SHT1x object
#define dataPin D0
#define clockPin D1
SHT1x sht1x(dataPin, clockPin);
/**
* Declaring the variables.
*/
HttpClient http;
#define VARIABLE_ID "53f8c7d576254210781ba266"
#define VARIABLE_ID2 "542310217625425104c40af4"
#define TOKEN "vTZWSU269MDP2FlcTXcIqqHEKf1CHuvSkIYWKxin0FnfmqJGMKUKOeq9gvvc"
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
void setup() {
request.hostname = "things.ubidots.com";
request.port = 80;
//Serial.begin(9600);
}
void loop() {
float temp_f = 0;
float humidity = 0;
// Read values from the sensor
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();
delay(10001);
// Send to Ubidots
request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
request.body = "{\"value\":" + String(temp_f) + "}";
http.post(request, response, headers);
request.path = "/api/v1.6/variables/"VARIABLE_ID2"/values";
request.body = "{\"value\":" + String(humidity) + "}";
http.post(request, response, headers);
//Serial.println(response.status);
//Serial.println(response.body);
}
.cpp
/**
* SHT1x Library
*
* Copyright 2009 Jonathan Oxer <jon@oxer.com.au> / <www.practicalarduino.com>
* Based on previous work by:
* Maurice Ribble: <www.glacialwanderer.com/hobbyrobotics/?p=5>
* Wayne ?: <ragingreality.blogspot.com/2008/01/ardunio-and-sht15.html>
*
* Manages communication with SHT1x series (SHT10, SHT11, SHT15)
* temperature / humidity sensors from Sensirion (www.sensirion.com).
*/
#include "SHT1x.h"
SHT1x::SHT1x(int dataPin, int clockPin)
{
_dataPin = dataPin;
_clockPin = clockPin;
}
/* ================ Public methods ================ */
/**
* Reads the current temperature in degrees Celsius
*/
float SHT1x::readTemperatureC()
{
int _val; // Raw value returned from sensor
float _temperature; // Temperature derived from raw value
// Conversion coefficients from SHT15 datasheet
const float CC1 = -40.0; // for 14 Bit @ 5V
const float CC2 = 0.01; // for 14 Bit DEGC
// Fetch raw value
_val = readTemperatureRaw();
// Convert raw value to degrees Celsius
_temperature = (_val * CC2) + CC1;
return (_temperature);
}
/**
* Reads the current temperature in degrees Fahrenheit
*/
float SHT1x::readTemperatureF()
{
int _val; // Raw value returned from sensor
float _temperature; // Temperature derived from raw value
// Conversion coefficients from SHT15 datasheet
const float CC1 = -40.0; // for 14 Bit @ 5V
const float CC2 = 0.018; // for 14 Bit DEGF
// Fetch raw value
_val = readTemperatureRaw();
// Convert raw value to degrees Fahrenheit
_temperature = (_val * CC2) + CC1;
return (_temperature);
}
/**
* Reads current temperature-corrected relative humidity
*/
float SHT1x::readHumidity()
{
int _val; // Raw humidity value returned from sensor
float _linearHumidity; // Humidity with linear correction applied
float _correctedHumidity; // Temperature-corrected humidity
float _temperature; // Raw temperature value
// Conversion coefficients from SHT15 datasheet
const float CC1 = -4.0; // for 12 Bit
const float CC2 = 0.0405; // for 12 Bit
const float CC3 = -0.0000028; // for 12 Bit
const float CT1 = 0.01; // for 14 Bit @ 5V
const float CT2 = 0.00008; // for 14 Bit @ 5V
// Command to send to the SHT1x to request humidity
int _gHumidCmd = 0b00000101;
// Fetch the value from the sensor
sendCommandSHT(_gHumidCmd, _dataPin, _clockPin);
waitForResultSHT(_dataPin);
_val = getData16SHT(_dataPin, _clockPin);
skipCrcSHT(_dataPin, _clockPin);
// Apply linear conversion to raw value
_linearHumidity = CC1 + CC2 * _val + CC3 * _val * _val;
// Get current temperature for humidity correction
_temperature = readTemperatureC();
// Correct humidity value for current temperature
_correctedHumidity = (_temperature - 25.0 ) * (CT1 + CT2 * _val) + _linearHumidity;
return (_correctedHumidity);
}
/* ================ Private methods ================ */
/**
* Reads the current raw temperature value
*/
float SHT1x::readTemperatureRaw()
{
int _val;
// Command to send to the SHT1x to request Temperature
int _gTempCmd = 0b00000011;
sendCommandSHT(_gTempCmd, _dataPin, _clockPin);
waitForResultSHT(_dataPin);
_val = getData16SHT(_dataPin, _clockPin);
skipCrcSHT(_dataPin, _clockPin);
return (_val);
}
/**
*/
int SHT1x::shiftIn(int _dataPin, int _clockPin, int _numBits)
{
int ret = 0;
int i;
for (i=0; i<_numBits; ++i)
{
digitalWrite(_clockPin, HIGH);
delay(10); // I don't know why I need this, but without it I don't get my 8 lsb of temp
ret = ret*2 + digitalRead(_dataPin);
digitalWrite(_clockPin, LOW);
}
return(ret);
}
/**
*/
void SHT1x::sendCommandSHT(int _command, int _dataPin, int _clockPin)
{
int ack;
// Transmission Start
pinMode(_dataPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
digitalWrite(_dataPin, HIGH);
digitalWrite(_clockPin, HIGH);
digitalWrite(_dataPin, LOW);
digitalWrite(_clockPin, LOW);
digitalWrite(_clockPin, HIGH);
digitalWrite(_dataPin, HIGH);
digitalWrite(_clockPin, LOW);
// The command (3 msb are address and must be 000, and last 5 bits are command)
shiftOut(_dataPin, _clockPin, MSBFIRST, _command);
// Verify we get the correct ack
digitalWrite(_clockPin, HIGH);
pinMode(_dataPin, INPUT);
ack = digitalRead(_dataPin);
if (ack != LOW) {
//Serial.println("Ack Error 0");
}
digitalWrite(_clockPin, LOW);
ack = digitalRead(_dataPin);
if (ack != HIGH) {
//Serial.println("Ack Error 1");
}
}
/**
*/
void SHT1x::waitForResultSHT(int _dataPin)
{
int i;
int ack;
pinMode(_dataPin, INPUT);
for(i= 0; i < 100; ++i)
{
delay(10);
ack = digitalRead(_dataPin);
if (ack == LOW) {
break;
}
}
if (ack == HIGH) {
//Serial.println("Ack Error 2"); // Can't do serial stuff here, need another way of reporting errors
}
}
/**
*/
int SHT1x::getData16SHT(int _dataPin, int _clockPin)
{
int val;
// Get the most significant bits
pinMode(_dataPin, INPUT);
pinMode(_clockPin, OUTPUT);
val = shiftIn(_dataPin, _clockPin, 8);
val *= 256;
// Send the required ack
pinMode(_dataPin, OUTPUT);
digitalWrite(_dataPin, HIGH);
digitalWrite(_dataPin, LOW);
digitalWrite(_clockPin, HIGH);
digitalWrite(_clockPin, LOW);
// Get the least significant bits
pinMode(_dataPin, INPUT);
val |= shiftIn(_dataPin, _clockPin, 8);
return val;
}
/**
*/
void SHT1x::skipCrcSHT(int _dataPin, int _clockPin)
{
// Skip acknowledge to end trans (no CRC)
pinMode(_dataPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
digitalWrite(_dataPin, HIGH);
digitalWrite(_clockPin, HIGH);
digitalWrite(_clockPin, LOW);
}
.h
#include "application.h"
#ifndef SHT1x_h
#define SHT1x_h
class SHT1x
{
public:
SHT1x(int dataPin, int clockPin);
float readHumidity();
float readTemperatureC();
float readTemperatureF();
private:
int _dataPin;
int _clockPin;
int _numBits;
float readTemperatureRaw();
int shiftIn(int _dataPin, int _clockPin, int _numBits);
void sendCommandSHT(int _command, int _dataPin, int _clockPin);
void waitForResultSHT(int _dataPin);
int getData16SHT(int _dataPin, int _clockPin);
void skipCrcSHT(int _dataPin, int _clockPin);
};
#endif