Last active
January 6, 2020 02:26
-
-
Save StevenBCamp/091e8fdecc3587c481db3f4911508616 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 <DHT.h> | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* host = ""; | |
const int httpPort = 80; | |
const String device = "Bryn\'s Room"; | |
#define DHTPIN 2 // Digital pin 4 | |
// Uncomment whatever type you're using! | |
#define DHTTYPE DHT11 // DHT 11 | |
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321 | |
//#define DHTTYPE DHT21 // DHT 21, AM2301 | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(115200); | |
delay(100); | |
// 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()); | |
dht.begin(); | |
} | |
void loop() { | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit | |
if (isnan(h) || isnan(t)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
// You can send any value at any time. | |
// Please don't send more that 10 values per second. | |
//h //V5 is for Humidity | |
//t //V6 is for Temperature | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
String data = "{\"type\": \"temp\", \"device\": \" " + device + " \", \"val\": " + String(t) + "}"; | |
Serial.print(data); | |
Serial.print("Requesting POST: "); | |
// Send request to the server: | |
client.println("POST /api/posts HTTP/1.1"); | |
client.println("Host: server_name"); | |
client.println("Accept: */*"); | |
client.println("Content-Type: application/json"); | |
client.print("Content-Length: "); | |
client.println(data.length()); | |
client.println(); | |
client.print(data); | |
delay(500); // Can be changed | |
if (client.connected()) { | |
client.stop(); // DISCONNECT FROM THE SERVER | |
} | |
Serial.println(); | |
Serial.println("closing connection"); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment