Skip to content

Instantly share code, notes, and snippets.

@devhammer
Created February 16, 2016 17:42
Show Gist options
  • Save devhammer/d0eb367d24a2ad7c8eea to your computer and use it in GitHub Desktop.
Save devhammer/d0eb367d24a2ad7c8eea to your computer and use it in GitHub Desktop.
Gadgeteer Driver for GHI TempHumidity module, ported to run on Raspberry Pi 2 under Windows 10 IoT Core
/*
* Driver for Seeed Studios Temperature and Humidity Gadgeteer module
* also should work for GHI TempHumidity module (discontinued).
* Driver code is based on the GHI TempHumid module driver found at:
* https://bitbucket.org/ghi_elect/gadgeteer/src/88bd3ab6f5ed02c3e1c524780182ea4f6901d091/Modules/GHIElectronics/TempHumidity/TempHumidity_43/TempHumidity_43.cs
*
* Module uses SHT10 sensor. Datasheet can be found at:
* https://www.sparkfun.com/datasheets/Sensors/SHT1x_datasheet.pdf
*
* Please note that to use this driver with Windows 10 IoT Core on
* Raspberry Pi 2, you need to install the GHI Gadgeteer package for UWP
* which can be found using the Nuget Package Manager.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GHIElectronics.UWP.GadgeteerCore;
using GSI = GHIElectronics.UWP.GadgeteerCore.SocketInterfaces;
namespace PiTempHumid
{
public class TempHumid : Module
{
private GSI.DigitalIO data;
private GSI.DigitalIO sck;
public override string Name => "TempHumid";
public override string Manufacturer => "GHI Electronics, LLC";
public TempHumid()
{
}
protected override async Task Initialize(ISocket parentSocket)
{
this.data = await parentSocket.CreateDigitalIOAsync(SocketPinNumber.Three, true);
this.sck = await parentSocket.CreateDigitalIOAsync(SocketPinNumber.Four, false);
await Task.Delay(TimeSpan.FromMilliseconds(11));
this.ResetCommuncation();
}
public Measurement TakeMeasurement()
{
return TakeMeasurement(1.0, 1.0);
}
public Measurement TakeMeasurement(double humidityCalibrationFactor, double tempCalibrationFactor)
{
this.ResetCommuncation();
this.TransmissionStart();
int tempTemp = this.MeasureTemperature();
double temperature = -39.6 + 0.01 * tempTemp;
temperature = temperature * tempCalibrationFactor;
this.TransmissionStart();
int rawHumidity = this.MeasureHumidity();
double humidity = -2.0468 + 0.0367 * rawHumidity - 1.5955E-6 * rawHumidity * rawHumidity;
humidity = (temperature - 25) * (0.01 + 0.00008 * rawHumidity) + humidity;
humidity = humidity * humidityCalibrationFactor;
temperature = Math.Round(100.0 * temperature) / 100.0;
humidity = Math.Round(100.0 * humidity) / 100.0;
return new Measurement() { Temperature = temperature, RelativeHumidity = humidity };
}
private void TransmissionStart()
{
this.data.Write(true);
this.sck.Write(true);
this.data.Write(false);
this.sck.Write(false);
this.sck.Write(true);
this.data.Write(true);
this.sck.Write(false);
}
private int MeasureTemperature()
{
this.data.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.data.Write(true);
this.sck.Write(true);
this.sck.Write(false);
this.data.Write(false);
this.data.Write(true);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.data.Read();
this.sck.Write(false);
while(this.data.Read())
Task.Delay(1).Wait();
int reading = 0;
for (int i = 0; i < 8; i++)
{
reading |= this.data.Read() ? (1 << (15 - i)) : 0;
this.sck.Write(true);
this.sck.Write(false);
}
this.data.Write(false);
this.sck.Write(true);
this.sck.Write(false);
for (int i = 8; i < 16; i++)
{
reading |= this.data.Read() ? (1 << (15 - i)) : 0;
this.sck.Write(true);
this.sck.Write(false);
}
this.data.Write(true);
return reading;
}
private int MeasureHumidity()
{
this.data.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.data.Write(true);
this.sck.Write(true);
this.sck.Write(false);
this.data.Write(false);
this.sck.Write(true);
this.sck.Write(false);
this.data.Write(true);
this.sck.Write(true);
this.sck.Write(false);
this.sck.Write(true);
this.data.Read();
this.sck.Write(false);
while (this.data.Read())
Task.Delay(1).Wait();
int reading = 0;
for (int i = 0; i < 8; i++)
{
reading |= this.data.Read() ? (1 << (15 - i)) : 0;
this.sck.Write(true);
this.sck.Write(false);
}
this.data.Write(false);
this.sck.Write(true);
this.sck.Write(false);
for (int i = 8; i < 16; i++)
{
reading |= this.data.Read() ? (1 << (15 - i)) : 0;
this.sck.Write(true);
this.sck.Write(false);
}
this.data.Write(true);
return reading;
}
private void ResetCommuncation()
{
this.data.Write(true);
for (int i = 0; i < 9; i++)
{
this.sck.Write(true);
this.sck.Write(false);
}
}
public struct Measurement
{
public double TemperatureFahrenheit { get { return this.Temperature * 1.8 + 32.0; } }
public double Temperature { get; set; }
public double RelativeHumidity { get; set; }
public override string ToString()
{
return this.Temperature.ToString("F1") + " degrees Celsius, " + this.RelativeHumidity.ToString("F1") + "% relative humidity.";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment