Created
June 21, 2017 19:34
-
-
Save moomdate/aa5f5ea122375e5a7a6ee7a808e12691 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 <WiFiClientSecure.h> | |
#include <Wire.h> | |
#include "ClosedCube_HDC1080.h" | |
ClosedCube_HDC1080 hdc1080; | |
int StepCount = 1; | |
String thinkKey = "HIMSN43MQO0YURAJ"; | |
const char* ssid = "kk"; | |
const char* password = "12345678"; | |
const char* host = "api.thingspeak.com"; | |
const int httpsPort = 443; | |
// Use web browser to view and copy | |
// SHA1 fingerprint of the certificate | |
const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C"; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.print("connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
// Use WiFiClientSecure class to create TLS connection | |
hdc1080.begin(0x40); | |
} | |
void loop() { | |
//https://api.thingspeak.com/update… | |
if (StepCount % 30 == 0) { | |
WiFiClientSecure client; | |
Serial.print("connecting to "); | |
Serial.println(host); | |
if (!client.connect(host, httpsPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
if (client.verify(fingerprint, host)) { | |
Serial.println("certificate matches"); | |
} else { | |
Serial.println("certificate doesn't match"); | |
} | |
String url = "/update?api_key=" + thinkKey + "&field1="; | |
Serial.print("requesting URL: "); | |
Serial.println(url); | |
client.print(String("GET ") + url + hdc1080.readTemperature() + "&field2=" + hdc1080.readHumidity() + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
Serial.println("request sent"); | |
while (client.connected()) { | |
String line = client.readStringUntil('\n'); | |
if (line == "\r") { | |
Serial.println("headers received"); | |
break; | |
} | |
} | |
String line = client.readStringUntil('\n'); | |
if (line.toInt()>0) { | |
Serial.println("Push Sucess !!"); | |
} else { | |
Serial.println("Fail!!!"); | |
} | |
Serial.println("reply was:"); | |
Serial.println("=========="); | |
Serial.println(line); | |
} | |
delay(1000); | |
Serial.print("Time: "); | |
Serial.println(StepCount++); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment