Last active
March 7, 2017 05:37
-
-
Save kuc-arc-f/756937fa6060f99654b0ed206a2bab3c 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
/* | |
esp32 WIFI-Client. | |
dht11 sensor Use | |
ver 0.9.2 | |
*/ | |
#include <WiFi.h> | |
#include <DHT.h> | |
#define DHTPIN 14 // what digital pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
DHT dht(DHTPIN, DHTTYPE); | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* host = "api.thingspeak.com"; | |
String mAPI_KEY="your-KEY"; | |
uint32_t mTimer= 0; | |
uint32_t mTimerTmp=0; | |
//unsigned long mTimer= 0; | |
// | |
void setup() | |
{ | |
Serial.begin(115200); | |
dht.begin(); | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
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()); | |
} | |
int value = 0; | |
void proc_http(String sTemp , String sHum){ | |
// void proc_http(String sTemp ){ | |
//Serial.print("connecting to "); | |
//Serial.println(host); | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
String url = "/update?key="+ mAPI_KEY + "&field1="+ sTemp ; | |
url +="&field2="+ sHum; | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
unsigned long timeout = millis(); | |
while (client.available() == 0) { | |
if (millis() - timeout > 5000) { | |
Serial.println(">>> Client Timeout !"); | |
client.stop(); | |
return; | |
} | |
} | |
//int iSt=0; | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
//Serial.print(line); | |
} | |
Serial.println(); | |
Serial.println("closing connection"); | |
} | |
// | |
void loop() | |
{ | |
float fHum =0; | |
float fTmp =0; | |
if( millis() > mTimerTmp ){ | |
mTimerTmp= millis() +5000; | |
float fHum = dht.readHumidity(); | |
float fTmp = dht.readTemperature(); | |
if (isnan(fHum ) || isnan(fTmp ) ) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
String sBuff="sTmp=" +String(fTmp )+" ,sHum=" +String(fHum ) ; | |
//Serial.println("sTmp=" +String(fTmp ) ); | |
Serial.println(sBuff ); | |
} | |
if(millis() >mTimer ){ | |
mTimer =millis() +( 30 * 1000 ); | |
// mTimerTmp = millis()+ 1000; | |
proc_http(String(fTmp ) , String(fHum ) ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment