Created
February 28, 2016 08:25
-
-
Save adamrunner/7200f6fa58c7ad3d4633 to your computer and use it in GitHub Desktop.
Read a DS18x20 temperature sensor and upload the data to data.sparkfun.com
This file contains hidden or 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> | |
#include "ESP8266WiFi.h" | |
#include <Phant.h> | |
//NOTE: Do your configuration here: | |
const char* ssid = "Your-wifi-ssid"; | |
const char* password = "your-wifi-password"; | |
//NOTE: interval is in milliseconds | |
const long sendInterval = 600000; | |
const char PhantHost[] = "data.sparkfun.com"; | |
const char PublicKey[] = "public-key-from-sparkfun"; | |
const char PrivateKey[] = "private-key-from-sparkfun"; | |
unsigned long previousMillis; | |
OneWire ds(2); // on pin 2 (a 4.7K resistor is necessary) | |
float getTemp() { | |
byte i; | |
byte present = 0; | |
byte type_s; | |
byte data[12]; | |
byte addr[8]; | |
float celsius; | |
//NOTE: Search for sensors here | |
if ( !ds.search(addr)) { | |
ds.reset_search(); | |
delay(250); | |
} | |
// Discover which chip we're running - this is from the Arduino library for the DS18x20 series of sensors. We'll return 0.0 degrees celsius if we can't find a suitable sensor. | |
// the first ROM byte indicates which chip | |
switch (addr[0]) { | |
case 0x10: | |
// Serial.println(" Chip = DS18S20"); // or old DS1820 | |
type_s = 1; | |
break; | |
case 0x28: | |
// Serial.println(" Chip = DS18B20"); | |
type_s = 0; | |
break; | |
case 0x22: | |
// Serial.println(" Chip = DS1822"); | |
type_s = 0; | |
break; | |
default: | |
// Serial.println("Device is not a DS18x20 family device."); | |
return 0.0; | |
} | |
ds.reset(); | |
ds.select(addr); | |
ds.write(0x44, 1); // start conversion, with parasite power on at the end | |
delay(1000); // Delay for however long the sensor will need to collect the data - this depends on your resolution. 1000ms should cover the highest resolution of data from the sensor (12-bit) | |
present = ds.reset(); | |
ds.select(addr); | |
ds.write(0xBE); // Tell the sensor to read scratchpad | |
for ( i = 0; i < 9; i++) { // read the data from the sensor and store the bytes | |
data[i] = ds.read(); | |
} | |
// Verify the information is valid. | |
OneWire::crc8(data, 8); | |
// Convert the data to actual temperature | |
// because the result is a 16 bit signed integer, it should | |
// be stored to an "int16_t" type, which is always 16 bits | |
// even when compiled on a 32 bit processor. | |
// | |
// Those lines were in the original implementation, | |
// this part is somewhat foreign to me. | |
int16_t raw = (data[1] << 8) | data[0]; | |
if (type_s) { | |
raw = raw << 3; // 9 bit resolution default | |
if (data[7] == 0x10) { | |
// "count remain" gives full 12 bit resolution | |
raw = (raw & 0xFFF0) + 12 - data[6]; | |
} | |
} else { | |
byte cfg = (data[4] & 0x60); | |
// at lower res, the low bits are undefined, so let's zero them | |
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms | |
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms | |
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms | |
// default is 12 bit resolution, 750 ms conversion time | |
} | |
celsius = (float)raw / 16.0; | |
return celsius; | |
} | |
void setupWiFi(){ | |
Serial.println("Connecting to Wifi"); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println(WiFi.localIP()); | |
} | |
float convertToF(float celsius){ | |
float fahrenheit; | |
fahrenheit = celsius * 1.8 + 32.0; | |
return fahrenheit; | |
} | |
bool checkInterval(){ | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= sendInterval) { | |
previousMillis = currentMillis; | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
int postToPhant(float degrees_f) { | |
Phant phant(PhantHost, PublicKey, PrivateKey); | |
phant.add("degrees_f", degrees_f); | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(PhantHost, httpPort)) | |
{ | |
// If we fail to connect, return 0. | |
return 0; | |
} | |
// If we successfully connected, print our Phant post: | |
client.print(phant.post()); | |
// Read all the lines of the reply from server and print them to Serial | |
while(client.available()){ | |
// String line = client.readStringUntil('\r'); | |
// Serial.print(line); | |
} | |
return 1; // Return success | |
} | |
void setup(void) { | |
Serial.begin(115200); | |
setupWiFi(); | |
} | |
void loop(void) { | |
float celsius; | |
float fahrenheit; | |
celsius = getTemp(); | |
fahrenheit = convertToF(celsius); | |
//NOTE: Using checkInterval here to avoid ever using delay | |
//NOTE: Also only uploading data every 10 minutes. | |
if(checkInterval()) { | |
int result = postToPhant(fahrenheit); | |
//Use Serial.print(result); here to see if your data is successfully being sent or not. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment