Skip to content

Instantly share code, notes, and snippets.

@lifez
Created March 10, 2016 06:38
Show Gist options
  • Select an option

  • Save lifez/871710b14a63385696db to your computer and use it in GitHub Desktop.

Select an option

Save lifez/871710b14a63385696db to your computer and use it in GitHub Desktop.
#include "DHT.h"
#define DHTPIN 14 // what pin we're connected to
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE,15);
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <ESP_SSD1306.h>
#define SDAPIN 12
#define SCLPIN 05
#define ROTATION_0 0
#define ROTATION_90 1
ESP_SSD1306 display(false); //reset disable
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "AZ7DG5H95TFK4V1E";
const int updateThingSpeakInterval = 16 * 1000;
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
#include <ESP8266WiFi.h>
const char* ssid = "RLIS";
const char* password = "boonchanawifi";
// Initialize Arduino Ethernet Client
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println("DHTxx test!");
Wire.begin(SDAPIN, SCLPIN); //sda, scl
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false);
display.clearDisplay();
WiFi.begin(ssid, password); //เชื่อมต่อ WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void loop() {
display.clearDisplay();
// Wait a few seconds between measurements.
delay(2000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(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;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, 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");
display.setTextColor(WHITE);
display.setTextWrap(false);
//display.setRotation(ROTATION_90);
display.setCursor(0, 0);
display.print("humidity: ");
display.print(h);
display.print(" %\n");
display.print("Temperature: ");
display.print(t);
display.print(" *C ");
display.print(" \n");
display.print(f);
display.print(" *F\n");
display.print("Heat index: ");
display.print(hic);
display.print(" *C ");
display.print(" \n");
display.print(hif);
display.println(" *F");
display.display();
if(millis() - lastConnectionTime > updateThingSpeakInterval)
{
updateThingSpeak("field1="+String(h));
updateThingSpeak("field2="+String(t));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment