Created
October 28, 2016 13:57
-
-
Save Miceuz/a23a29ba16ecf27525bd66dd8f17ba25 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
#include <ESP8266WiFi.h> | |
#include <Base64.h> | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#include <Phant.h> | |
//AP definitions | |
#define AP_SSID "TECHNARIUM" | |
#define AP_PASSWORD "change me" | |
const char PhantHost[] = "data.sparkfun.com"; | |
const char PublicKey[] = "change me"; | |
const char PrivateKey[] = "change me"; | |
#define REPORT_INTERVAL 60 // in sec | |
#define ONE_WIRE_BUS D3 // DS18B20 pin | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature DS18B20(&oneWire); | |
#define USER_PWD_LEN 40 | |
char unameenc[USER_PWD_LEN]; | |
float oldTemp; | |
void wifiConnect(); | |
int postToPhant(float temp); | |
void setup() { | |
Serial.begin(115200); | |
wifiConnect(); | |
pinMode(D2, OUTPUT); | |
digitalWrite(D2, LOW); | |
pinMode(D4, OUTPUT); | |
digitalWrite(D4, HIGH); | |
oldTemp = -1; | |
} | |
void loop() { | |
float temp; | |
do { | |
DS18B20.requestTemperatures(); | |
temp = DS18B20.getTempCByIndex(0); | |
Serial.print("Temperature: "); | |
Serial.println(temp); | |
} while (temp == 85.0 || temp == (-127.0)); | |
postToPhant(temp); | |
oldTemp = temp; | |
int cnt = REPORT_INTERVAL; | |
while(cnt--) | |
delay(1000); | |
} | |
void wifiConnect() | |
{ | |
Serial.print("Connecting to AP"); | |
WiFi.begin(AP_SSID, AP_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
} | |
int postToPhant(float temp) | |
{ | |
// LED turns on when we enter, it'll go off when we | |
// successfully post. | |
digitalWrite(LED_BUILTIN, HIGH); | |
Serial.println("posting to data sparkfun"); | |
// Declare an object from the Phant library - phant | |
Phant phant(PhantHost, PublicKey, PrivateKey); | |
// String postedID = "Technarium-Outside-Air"; | |
// Add the four field/value pairs defined by our stream: | |
phant.add("temperature", temp); | |
phant.add("timestamp", millis()); | |
// Now connect to data.sparkfun.com, and post our data: | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(PhantHost, httpPort)) | |
{ | |
Serial.println("Failed to connect to sparkfun"); | |
// 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); // Trying to avoid using serial | |
} | |
// Before we exit, turn the LED off. | |
digitalWrite(LED_BUILTIN, LOW); | |
return 1; // Return success | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment