Last active
August 29, 2015 14:14
-
-
Save ianjosephwilson/3ea6a52a357099c71708 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************\ | |
* Pinoccio Library * | |
* https://github.com/Pinoccio/library-pinoccio * | |
* Copyright (c) 2014, Pinoccio Inc. All rights reserved. * | |
* ------------------------------------------------------------------------ * | |
* This program is free software; you can redistribute it and/or modify it * | |
* under the terms of the MIT License as described in license.txt. * | |
\**************************************************************************/ | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <Scout.h> | |
#include <GS.h> | |
#include <bitlash.h> | |
#include <lwm.h> | |
#include <js0n.h> | |
#include "version.h" | |
double externalTemperature; | |
double externalHumidity; | |
void setup() { | |
Scout.setup(SKETCH_NAME, SKETCH_REVISION, SKETCH_BUILD); | |
// Add custom setup code here | |
externalTemperature = readExternalTemperature(); | |
externalHumidity = readExternalHumidity(); | |
addBitlashFunction("sht.temperature", (bitlash_function)getExternalTemperature); | |
addBitlashFunction("sht.humidity", (bitlash_function)getExternalHumidity); | |
addBitlashFunction("sht.temperature.report", (bitlash_function)reportExternalTemperature); | |
} | |
void loop() { | |
Scout.loop(); | |
// Add custom loop code here | |
externalTemperature = readExternalTemperature(); | |
externalHumidity = readExternalHumidity(); | |
} | |
// Code adapted from https://github.com/krvarma/DHT_Pinoccio and | |
// http://bildr.org/2012/11/sht15-arduino/ | |
int SHT_clockPin = 4; // D7: pin used for clock | |
int SHT_dataPin = 8; // D8: pin used for data | |
void reportExternalTemperature() { | |
char buf[200]; | |
int t; | |
int tf; | |
t = abs((int)externalTemperature); | |
tf = (abs(externalTemperature) * 100) - (t * 100); | |
if (externalTemperature < 0) { | |
t = -t; | |
} | |
// We are ignoring the float part. | |
sprintf(buf, "%i", t); | |
Shell.eval("hq.report", "exttemp", buf); | |
} | |
numvar getExternalTemperature(){ | |
return (numvar)(int)externalTemperature; | |
} | |
numvar getExternalHumidity() { | |
return (numvar)externalHumidity; | |
} | |
numvar readExternalTemperature(){ | |
// Coeffecients for degrees in F for 3.5V. | |
float d1 = -39.5; | |
// Coeffecients for degrees in F for 14bit. | |
float d2 = 0.018; | |
//Return Temperature in Celsius | |
SHT_sendCommand(B00000011, SHT_dataPin, SHT_clockPin); | |
SHT_waitForResult(SHT_dataPin); | |
int val = SHT_getData(SHT_dataPin, SHT_clockPin); | |
SHT_skipCrc(SHT_dataPin, SHT_clockPin); | |
return ((double)val * d2 + d1); | |
} | |
numvar readExternalHumidity(){ | |
//Return Relative Humidity | |
SHT_sendCommand(B00000101, SHT_dataPin, SHT_clockPin); | |
SHT_waitForResult(SHT_dataPin); | |
int val = SHT_getData(SHT_dataPin, SHT_clockPin); | |
SHT_skipCrc(SHT_dataPin, SHT_clockPin); | |
return (double)(-4.0 + 0.0405 * val + -0.0000028 * val * val); | |
} | |
void SHT_sendCommand(int command, int dataPin, int clockPin){ | |
// send a command to the SHTx sensor | |
// 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); | |
// shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command) | |
shiftOut(dataPin, clockPin, MSBFIRST, command); | |
// verify we get the right ACK | |
digitalWrite(clockPin, HIGH); | |
pinMode(dataPin, INPUT); | |
if (digitalRead(dataPin)) Serial.println("ACK error 0"); | |
digitalWrite(clockPin, LOW); | |
if (!digitalRead(dataPin)) Serial.println("ACK error 1"); | |
} | |
void SHT_waitForResult(int dataPin){ | |
// wait for the SHTx answer | |
pinMode(dataPin, INPUT); | |
int ack; //acknowledgement | |
//need to wait up to 2 seconds for the value | |
for (int i = 0; i < 1000; ++i){ | |
delay(2); | |
ack = digitalRead(dataPin); | |
if (ack == LOW) break; | |
} | |
if (ack == HIGH) Serial.println("ACK error 2"); | |
} | |
int SHT_getData(int dataPin, int clockPin){ | |
// get data from the SHTx sensor | |
// get the MSB (most significant bits) | |
pinMode(dataPin, INPUT); | |
pinMode(clockPin, OUTPUT); | |
byte MSB = shiftIn(dataPin, clockPin, MSBFIRST); | |
// send the required ACK | |
pinMode(dataPin, OUTPUT); | |
digitalWrite(dataPin, HIGH); | |
digitalWrite(dataPin, LOW); | |
digitalWrite(clockPin, HIGH); | |
digitalWrite(clockPin, LOW); | |
// get the LSB (less significant bits) | |
pinMode(dataPin, INPUT); | |
byte LSB = shiftIn(dataPin, clockPin, MSBFIRST); | |
return ((MSB << 8) | LSB); //combine bits | |
} | |
void SHT_skipCrc(int dataPin, int clockPin){ | |
// skip CRC data from the SHTx sensor | |
pinMode(dataPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
digitalWrite(dataPin, HIGH); | |
digitalWrite(clockPin, HIGH); | |
digitalWrite(clockPin, LOW); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onwake { led.green(2000); sht.temperature.report; scout.delay(3000,"sleep"); } | |
function sleep { power.sleep( 60 * 1000, "onwake"); }; | |
run onwake |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment