Created
October 3, 2016 23:14
-
-
Save Robotonics/1f202973e0db9091685ef8207e553cef to your computer and use it in GitHub Desktop.
Upload DHT22 data to LinkSprite IO
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 <ESP8266WiFi.h> | |
#include <WString.h> | |
#include <DNSServer.h> | |
#include <ESP8266WebServer.h> | |
#include <WiFiManager.h> | |
#include "DHT.h" | |
#define DHTPIN D3 // what pin we're connected to | |
#define DHTTYPE DHT22 // DHT 22 (AM2302) | |
char *tem=""; | |
String apikey = "cc5005ab-e1a3-4c9a-a5cf-9c5a513d7062"; | |
const char* deviceID="00000000cd"; | |
const char* server = "www.linksprite.io"; | |
WiFiClient client; | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(9600); | |
WiFiManager wifiManager; | |
wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); | |
wifiManager.autoConnect("LinkNodeAP"); | |
Serial.print("WiFi Connected ...\n"); | |
Serial.println("WiFi connected"); | |
Serial.println("DHTxx test!"); | |
dht.begin(); | |
} | |
void loop() { | |
delay(2000); | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); // Read temperature as Celsius (the default) | |
float f = dht.readTemperature(true); // Read temperature as Fahrenheit (isFahrenheit = true) | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
float hif = dht.computeHeatIndex(f, h); // Compute heat index in Fahrenheit (the default) | |
float hic = dht.computeHeatIndex(t, h, false);// Compute heat index in Celsius (isFahreheit = false) | |
Serial.print("Humidity: "); | |
Serial.print(h); | |
Serial.print(" %\t"); | |
Serial.print("Temperature: "); | |
Serial.print(t); | |
Serial.print(" *C "); | |
Serial.print(f); | |
Serial.print(" *F\t"); | |
Serial.print("Heat index: "); | |
Serial.print(hic); | |
Serial.print(" *C "); | |
Serial.print(hif); | |
Serial.println(" *F"); | |
if (client.connect(server,80)) | |
{ | |
String postStr ="{"; | |
postStr +="\"action\":\"update\","; | |
postStr +="\"apikey\":\""; | |
postStr += apikey; | |
postStr +="\","; | |
postStr +="\"deviceid\":\""; | |
postStr += deviceID; | |
postStr +="\","; | |
postStr +="\"params\":"; | |
postStr +="{"; | |
postStr +="\"temperature\":\""; | |
itoa(t,tem,10); | |
postStr +=tem; | |
postStr +="\","; | |
postStr +="\"humidity\":\""; | |
itoa(h,tem,10); | |
postStr +=tem; | |
postStr +="\"\r\n"; | |
postStr +="}"; | |
postStr +="}"; | |
client.print("POST /api/http HTTP/1.1\n"); | |
client.print("Host: "); | |
client.print(server); | |
client.print("\nContent-Type: application/json\n"); | |
client.print("Content-Length: "); | |
client.print(postStr.length()); | |
client.print("\n\n"); | |
client.print(postStr); | |
String request = ""; | |
delay(1000); | |
while (client.available()) | |
{ | |
char c = client.read(); | |
request +=c; | |
} | |
if (request!= NULL) | |
{ | |
int index1 = request.indexOf(":{"); | |
int index2 = request.indexOf("},"); | |
String param = request.substring(index1, index2 + 1); | |
String data="\0" ; | |
Serial.print("The param is "); | |
Serial.println(param); | |
client.stop(); | |
Serial.println("waiting ..."); | |
delay(1000); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment