Last active
June 25, 2016 08:56
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 <Time.h> | |
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
extern "C" { | |
#include "user_interface.h" | |
} | |
// | |
// Wifi | |
// | |
const char *ssid = "***********"; | |
const char *password = "***********"; | |
WiFiClient client; | |
void setup() { | |
Serial.begin(115200); | |
// | |
// Wifi | |
// | |
WiFi.begin ( ssid, password ); | |
Serial.println("Started"); | |
// Wait for connection | |
while ( WiFi.status() != WL_CONNECTED ) { | |
delay ( 500 ); | |
Serial.print ( "." ); | |
} | |
Serial.println("Wifi Connected"); | |
sendTemperature(); | |
ESP.deepSleep(55*1000*1000, WAKE_RF_DISABLED); | |
delay(1000); | |
} | |
void loop() { | |
} | |
void sendTemperature() { | |
char buffer[20]; | |
float t = readTemperature(); | |
dtostrf(t, 2,1, buffer); | |
Serial.println(buffer); | |
String postStr = "&field1=" + String(buffer); | |
send(postStr); | |
Serial.println(postStr); | |
} | |
void send(String inPostStr) { | |
String apiKey = "**********************"; | |
Serial.print("Connecting..."); | |
if (client.connect("184.106.153.149", 80)) { // api.thingspeak.com | |
Serial.print("Connected...."); | |
String postStr = apiKey + inPostStr + "\r\n\r\n"; | |
client.print("POST /update HTTP/1.1\n"); | |
client.print("Host: api.thingspeak.com\n"); | |
client.print("Connection: close\n"); | |
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n"); | |
client.print("Content-Type: application/x-www-form-urlencoded\n"); | |
client.print("Content-Length: "); | |
client.print(postStr.length()); | |
client.print("\n\n"); | |
client.print(postStr); | |
Serial.println("posted."); | |
} | |
client.stop(); | |
} | |
float readTemperature() { | |
float v = system_adc_read() / 1.062000 * 2.00000; // mV | |
return 30.00000 - (v - 1474.000) / 8.2000000; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment