-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 21d9c4c
Showing
3 changed files
with
808 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
|
||
/** | ||
* BasicHTTPClient.ino | ||
* | ||
* Created on: 24.05.2015 | ||
* | ||
*/ | ||
|
||
#include <Arduino.h> | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <ESP8266HTTPClient.h> | ||
|
||
#include <DHT_U.h> | ||
#include <Adafruit_Sensor.h> | ||
|
||
const char* ssid = "YOUR-WIFI-SSID"; | ||
const char* password = "YOUR-WIFI-PASSWD"; | ||
|
||
// Define Pin for Sensor - I use GPIO2 | ||
#define DHTPIN 2 | ||
|
||
// Uncomment whatever type you're using! | ||
#define DHTTYPE DHT11 // DHT 11 | ||
//#define DHTTYPE DHT22 // DHT 22 (AM2302) | ||
//#define DHTTYPE DHT21 // DHT 21 (AM2301) | ||
|
||
// init DHT; 3rd parameter = 16 works for ESP8266@80MHz | ||
// FixMe: What is the third parameter?? | ||
DHT dht(DHTPIN, DHTTYPE, 16); | ||
|
||
|
||
// Generally, you should use "unsigned long" for variables that hold time | ||
const long interval = 2000; // interval at which to read sensor | ||
const String url="http://YOUR-SERVER-NAME/get_temp.php"; | ||
|
||
unsigned long previousMillis = 0; // will store last temp was read | ||
float hum, temp; // Values read from sensor | ||
unsigned int counter; | ||
|
||
void setup() { | ||
|
||
counter = 0; | ||
|
||
for(uint8_t t = 4; t > 0; t--) { | ||
delay(1000); | ||
} | ||
|
||
Serial.begin(115200); Serial.println(""); | ||
WiFi.begin(ssid, password); | ||
|
||
|
||
// Wait for connection | ||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
Serial.print("."); | ||
} | ||
Serial.println(""); | ||
Serial.print("Connected to "); Serial.println(ssid); | ||
Serial.print("IP address: "); Serial.println(WiFi.localIP()); | ||
} | ||
|
||
void gettemperature() { | ||
// Wait at least 2 seconds seconds between measurements. | ||
// if the difference between the current time and last time you read | ||
// the sensor is bigger than the interval you set, read the sensor | ||
// Works better than delay for things happening elsewhere also | ||
unsigned long currentMillis = millis(); | ||
|
||
if(currentMillis - previousMillis >= interval) { | ||
// save the last time you read the sensor | ||
previousMillis = currentMillis; | ||
|
||
// Reading temperature for humidity takes about 250 milliseconds! | ||
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor) | ||
hum = dht.readHumidity(); // Read humidity (percent) | ||
temp = dht.readTemperature(); // Read temperature as Celsius ...(true)=Fahrenheit | ||
// Check if any reads failed and exit early (to try again). | ||
if (isnan(hum) || isnan(temp)) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
void loop() { | ||
|
||
String url_act; | ||
|
||
// wait for WiFi connection | ||
if((WiFi.status() == WL_CONNECTED)) { | ||
|
||
gettemperature(); | ||
|
||
HTTPClient http; | ||
Serial.println("HTTP Start"); | ||
|
||
url_act = url; | ||
url_act += "?temp="; url_act += temp; | ||
url_act += "&hum="; url_act += hum; | ||
url_act += "&counter="; url_act += counter; | ||
|
||
Serial.print("URL="); Serial.println(url_act); | ||
http.begin(url_act); //HTTP-Client | ||
|
||
// start connection and send HTTP header | ||
int httpCode = http.GET(); | ||
|
||
// httpCode will be negative on error | ||
if(httpCode > 0) { | ||
// HTTP header has been send and Server response header has been handled | ||
// file found at server | ||
Serial.println("HTTP Code OK"); | ||
if(httpCode == HTTP_CODE_OK) { | ||
String payload = http.getString(); | ||
} | ||
} else { | ||
// We do nothing if something did not work... | ||
Serial.println("HTTP Not OK"); | ||
} | ||
http.end(); | ||
Serial.println("HTTP End"); | ||
} | ||
counter++; | ||
delay(10000); | ||
Serial.println("----- Next Loop -----"); Serial.println(""); | ||
} | ||
|
||
|
Oops, something went wrong.