Skip to content

Instantly share code, notes, and snippets.

@kinasmith kinasmith/SHT2x.cpp
Created Sep 9, 2016

Embed
What would you like to do?
//Webhook setup for Particle Console
/*
"event": "sensor_1",
"url": "https://api.thingspeak.com/update",
"requestType": "POST",
"form": {
"api_key": "{{k}}",
"field1": "{{1}}",
"field2": "{{2}}",
"field3": "{{3}}",
"field4": "{{4}}",
"field5": "{{5}}",
"field6": "{{6}}",
"field7": "{{7}}",
"field8": "{{8}}",
"lat": "{{a}}",
"long": "{{o}}",
"elevation": "{{e}}",
"status": "{{s}}"
},
"mydevices": true,
"noDefaults": true
}
*/
#include "SHT2x.h"
SHT2x sht;
FuelGauge fuel;
int led = 7;
String Temp;
String Humidity;
String bat_v;
String SoC;
String Now;
String POST;
const String key = "<API_KEY>";
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(D7, OUTPUT);
}
void loop() {
Now = String(Time.now());
Temp = String(sht.GetTemperature());
Humidity = String(sht.GetHumidity());
bat_v = String(fuel.getVCell());
SoC = String(fuel.getSoC());
String item_1 = "{ \"1\": \"" + Now + "\",";
String item_2 = "\"2\": \"" + Temp + "\",";
String item_3 = "\"3\": \"" + Humidity + "\",";
String item_4 = "\"4\": \"" + bat_v + "\",";
String item_5 = "\"5\": \"" + SoC + "\",";
String item_k = "\"k\": \"" + key + "\" }";
POST = item_1 + item_2 + item_3 + item_4 + item_5 + item_k;
Serial.println(POST);
Particle.publish("sensor_1", POST, 60, PRIVATE); // publish to cloud
digitalWrite(D7, HIGH);
delay(250);
digitalWrite(D7, LOW);
delay(750);
for(int i = 0; i < 1; i++) {
delay(60000);
}
}
/*
SHT2x - A Humidity Library for Arduino.
Supported Sensor modules:
SHT21-Breakout Module - http://www.moderndevice.com/products/sht21-humidity-sensor
SHT2x-Breakout Module - http://www.misenso.com/products/001
Created by Christopher Ladden at Modern Device on December 2009.
Modified by Paul Badger March 2010
Modified by www.misenso.com on October 2011:
- code optimisation
- compatibility with Arduino 1.0
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SHT2x.h"
/******************************************************************************
* Global Functions
******************************************************************************/
/**********************************************************
* GetHumidity
* Gets the current humidity from the sensor.
*
* @return float - The relative humidity in %RH
**********************************************************/
float SHT2x::GetHumidity(void)
{
return (-6.0 + 125.0 / 65536.0 * (float)(readSensor(eRHumidityHoldCmd)));
}
/**********************************************************
* GetTemperature
* Gets the current temperature from the sensor.
*
* @return float - The temperature in Deg C
**********************************************************/
float SHT2x::GetTemperature(void)
{
return (-46.85 + 175.72 / 65536.0 * (float)(readSensor(eTempHoldCmd)));
}
/******************************************************************************
* Private Functions
******************************************************************************/
uint16_t SHT2x::readSensor(uint8_t command)
{
uint16_t result;
Wire.beginTransmission(eSHT2xAddress); //begin
Wire.write(command); //send the pointer location
delay(100);
Wire.endTransmission(); //end
Wire.requestFrom(eSHT2xAddress, 3);
while(Wire.available() < 3) {
; //wait
}
//Store the result
result = ((Wire.read()) << 8);
result += Wire.read();
result &= ~0x0003; // clear two low bits (status bits)
return result;
}
/*
SHT2x - A Humidity Library for Arduino.
Supported Sensor modules:
SHT21-Breakout Module - http://www.moderndevice.com/products/sht21-humidity-sensor
SHT2x-Breakout Module - http://www.misenso.com/products/001
Created by Christopher Ladden at Modern Device on December 2009.
Modified by www.misenso.com on October 2011:
- code optimisation
- compatibility with Arduino 1.0
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SHT2X_H
#define SHT2X_H
#include <inttypes.h>
#include "application.h"
#include "math.h"
typedef enum {
eSHT2xAddress = 0x40,
} HUM_SENSOR_T;
typedef enum {
eTempHoldCmd = 0xE3,
eRHumidityHoldCmd = 0xE5,
eTempNoHoldCmd = 0xF3,
eRHumidityNoHoldCmd = 0xF5,
} HUM_MEASUREMENT_CMD_T;
class SHT2x
{
private:
uint16_t readSensor(uint8_t command);
public:
float GetHumidity(void);
float GetTemperature(void);
};
// extern SHT2xClass SHT2x;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.