Created
July 24, 2015 19:30
-
-
Save gyengus/0b23d7abcef7fc3c89b7 to your computer and use it in GitHub Desktop.
Particle Photon thermometer demo
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
#include "OneWire.h" | |
OneWire ds(D3); | |
void btn_handler(void); | |
int readTemp(String command); | |
void pushbulletTemp(void); | |
uint8_t gateway_ip[] = {192, 168, 0, 11}; | |
int gateway_port = 80; | |
String gateway_path = "/pushbullet_gw.php"; | |
volatile bool getTemp = true; | |
double temperature; | |
byte addr[8]; | |
void setup() { | |
Spark.variable("temperature", &temperature, DOUBLE); | |
Spark.function("readTemp", readTemp); | |
pinMode(D7, OUTPUT); | |
digitalWrite(D7, false); | |
ds.reset_search(); | |
if (!ds.search(addr)) Spark.publish("temperature", "No sensor detected"); | |
attachInterrupt(D4, btn_handler, RISING); | |
} | |
void loop() { | |
if (getTemp) { | |
readTemp(""); | |
getTemp = false; | |
} | |
} | |
int readTemp(String command) { | |
int i; | |
byte data[12]; | |
digitalWrite(D7, true); | |
ds.reset(); | |
ds.select(addr); | |
ds.write(0x44, 0); // Start Convert T | |
delay(1000); | |
ds.reset(); | |
ds.select(addr); | |
ds.write(0xBE); // Read Scratchpad | |
for (i = 0; i < 9; i++) { | |
data[i] = ds.read(); | |
} | |
int16_t raw = (data[1] << 8) | data[0]; | |
raw = raw << 3; // 9 bit resolution | |
if (data[7] == 0x10) { | |
raw = (raw & 0xFFF0) + 12 - data[6]; | |
} | |
temperature = (double)raw / 16.0; | |
Spark.publish("temperature", String(temperature)); | |
Spark.process(); | |
pushbulletTemp(); | |
digitalWrite(D7, false); | |
return 1; | |
} | |
void btn_handler() { | |
getTemp = true; | |
} | |
void pushbulletTemp() { | |
TCPClient client; | |
if (client.connect(gateway_ip, gateway_port)) { | |
String body = "title=Temperature&text=" + String(temperature) + "+%C2%B0C"; | |
client.println("POST " + gateway_path + " HTTP/1.0"); | |
client.println("Content-Type: application/x-www-form-urlencoded"); | |
client.println("Content-Length: " + String(body.length())); | |
client.println(); | |
client.println(body); | |
client.println(); | |
client.flush(); | |
client.stop(); | |
Spark.publish("pushbullet gateway", "Connected, data sent"); | |
Spark.publish("pushbullet gateway", String(body.length()) + " " + body); | |
} else { | |
Spark.publish("pushbullet gateway", "Not connected"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment