Skip to content

Instantly share code, notes, and snippets.

@youngsoul
Created October 13, 2017 13:42
Show Gist options
  • Save youngsoul/55a820ca9ed250b04bf894d75ffb3904 to your computer and use it in GitHub Desktop.
Save youngsoul/55a820ca9ed250b04bf894d75ffb3904 to your computer and use it in GitHub Desktop.
Photon Temp/Humidity example
// This #include statement was automatically added by the Particle IDE.
#include <SHT1x.h>
// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// HCPA-5V-U3
// This code is designed to work with the HCPA-5V-U3_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Temperature?sku=HCPA-5V-U3_I2CS#tabs-0-product_tabset-2
#include <application.h>
#include <spark_wiring_i2c.h>
// HCPA-5V-U3 I2C address is 0x28(40)
#define TempHumidity_Addr 0x28
// TSL2561 I2C address is 0x39(57)
#define Light_Addr 0x39
// https://www.adafruit.com/product/1298
#define SHT1_DATAPIN D6
#define SHT1_CLOCKPIN D5
// ControlEverything boards
double cTemp = 0.0, fTemp = 0.0, humidity = 0.0;
double full_spectrum = 0.0;
double infrared = 0.0;
double visible = 0.0;
// External Moisture/Temperature
double sht1_humidty = 0.0;
double sht1_temperature = 0.0;
long last_data_sent_timestamp = 0;
long last_blynk_led_timestamp = 0;
// Blynk LED value
bool led_value = false;
// Create a variable for uptime robot to check to make sure
// there is connectivity.
// Uptime Robot Monitor URL
int uptime = 0;
// External Moisture/Temperature sensor
SHT1x sht10(SHT1_DATAPIN, SHT1_CLOCKPIN);
// Blynk LED
WidgetLED ledV1(V1);
BLYNK_READ(V2)
{
//Blynk.virtualWrite(V0, String(adcValue));
Blynk.virtualWrite(V2, fTemp);
}
BLYNK_READ(V3)
{
//Blynk.virtualWrite(V0, String(adcValue));
Blynk.virtualWrite(V3, humidity);
}
BLYNK_READ(V4)
{
//Blynk.virtualWrite(V0, String(adcValue));
Blynk.virtualWrite(V4, visible);
}
BLYNK_READ(V5)
{
//Blynk.virtualWrite(V0, String(adcValue));
Blynk.virtualWrite(V5, sht1_temperature);
}
BLYNK_READ(V6)
{
//Blynk.virtualWrite(V0, String(adcValue));
Blynk.virtualWrite(V6, sht1_humidty);
}
void setup_temphumidity()
{
// Start I2C transmission
Wire.beginTransmission(TempHumidity_Addr);
// Send start command
Wire.write(0x80);
// Stop I2C transmission
Wire.endTransmission();
delay(300);
}
void setup_light()
{
// Starts I2C communication
Wire.beginTransmission(Light_Addr);
// Select control register
Wire.write(0x00 | 0x80);
// Power ON mode
Wire.write(0x03);
// Stop I2C Transmission
Wire.endTransmission();
// Starts I2C communication
Wire.beginTransmission(Light_Addr);
// Select timing register
Wire.write(0x01 | 0x80);
// Nominal integration time = 402ms
Wire.write(0x02);
// Stop I2C Transmission
Wire.endTransmission();
delay(300);
}
void blink_led()
{
if(led_value==false) {
led_value = true;
digitalWrite(D7,1);
} else {
led_value = false;
digitalWrite(D7,0);
}
}
Timer timer1(1000, blink_led);
void setup()
{
Blynk.begin("");
pinMode(D7, OUTPUT);
// Set variable
Particle.variable("i2cdevice", "HCPA-5V-U3");
Particle.variable("humidity", humidity);
Particle.variable("cTemp", cTemp);
Particle.variable("uptime", uptime);
// Initialise I2C communication as Master
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
setup_temphumidity();
setup_light();
timer1.start();
}
void read_light()
{
unsigned int data[4];
for(int i = 0; i < 4; i++)
{
// Starts I2C communication
Wire.beginTransmission(Light_Addr);
// Select data register
Wire.write((140 + i));
// Stop I2C Transmission
Wire.endTransmission();
// Request 1 byte of data
Wire.requestFrom(Light_Addr, 1);
// Read 1 bytes of data
if(Wire.available() == 1)
{
data[i] = Wire.read();
}
delay(200);
}
// Convert the data
double full_spectrum = ((data[1] & 0xFF) * 256) + (data[0] & 0xFF);
double infrared = ((data[3] & 0xFF) * 256) + (data[2] & 0xFF);
// Output data to dashboard
Particle.publish("Full_Spectrum", String(full_spectrum));
//delay(1000);
Particle.publish("Infrared", String(infrared));
//delay(1000);
visible = full_spectrum-infrared;
if(visible >= 0.0)
{
Particle.publish("Visible", String(visible));
//delay(1000);
}
}
void read_temp_humidity()
{
unsigned int data[4];
// Start I2C transmission
Wire.beginTransmission(TempHumidity_Addr);
// Stop I2C transmission
Wire.endTransmission();
// Request 4 byte of data
Wire.requestFrom(TempHumidity_Addr, 4);
// Read 4 bytes of data
// humidity msb, humidity lsb, cTemp msb, cTemp lsb
if (Wire.available() == 4)
{
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
data[3] = Wire.read();
// Convert the data to 14-bits
humidity = (((data[0] & 0x3F) * 256) + data[1]) / 16384.0 * 100.0;
cTemp = (((data[2] * 256) + (data[3] & 0xFC)) / 4) / 16384.0 * 165.0 - 40.0;
fTemp = (cTemp * 1.8) + 32;
// Output data to dashboard
Particle.publish("Relative humidity : ", String(humidity));
//delay(1000);
Particle.publish("Temperature in Celsius : ", String(cTemp));
//delay(1000);
Particle.publish("Temperature in Fahrenheit : ", String(fTemp));
//delay(1000);
}
}
void read_sht1_temp_humidity()
{
sht1_humidty = sht10.readHumidity();
delay(500);
sht1_temperature = sht10.readTemperatureF();
Particle.publish("SHT1 Temperature", String(sht1_temperature));
delay(500);
Particle.publish("SHT1 humidity:", String(sht1_humidty));
//delay(1000);
//delay(1000);
}
void loop()
{
Blynk.run();
long now = millis();
// do this every 5 minutes. this stuff does not change often
if( (now - last_data_sent_timestamp) > 30000 ) {
// then its time to send more data
read_temp_humidity();
read_light();
read_sht1_temp_humidity();
last_data_sent_timestamp = now;
}
if(now - last_blynk_led_timestamp > 1000) {
if(ledV1.getValue() > 0) {
ledV1.off();
} else if(ledV1.getValue()==0) {
ledV1.on();
}
last_blynk_led_timestamp = now;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment