#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <time.h>
#include "secrets.h"
#include "DHT.h"
#define TIME_ZONE -5
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
float h ;
float t;
float f;
unsigned long lastMillis = 0;
unsigned long previousMillis = 0;
const long interval = 5000;
#define AWS_IOT_PUBLISH_TOPIC "esp8266/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp8266/sub"
WiFiClientSecure net;
BearSSL::X509List cert(cacert);
BearSSL::X509List client_crt(client_cert);
BearSSL::PrivateKey key(privkey);
PubSubClient client(net);
time_t now;
time_t localnow;
time_t nowish = 1510592825;
IPAddress ip(192, 168, 0, 200);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress pDns(8, 8, 8, 8);
IPAddress sDns(8, 8, 4, 4);
void NTPConnect(void)
{
Serial.print("Setting time using SNTP");
configTime(5 * 3600, 1800, "pool.ntp.org", "time.nist.gov");
now = time(nullptr);
while (now < nowish)
{
delay(500);
Serial.print(".");
Serial.print(now);
time(&now);
}
Serial.println("done!");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}
void messageReceived(char *topic, byte *payload, unsigned int length)
{
Serial.print("Received [");
Serial.print(topic);
Serial.print("]: ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
void connectAWS()
{
delay(3000);
WiFi.mode(WIFI_STA);
WiFi.config(ip,gateway,subnet,pDns,sDns);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println(String("Attempting to connect to SSID: ") + String(WIFI_SSID));
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
NTPConnect();
net.setTrustAnchors(&cert);
net.setClientRSACert(&client_crt, &key);
client.setServer(MQTT_HOST, 8883);
client.setCallback(messageReceived);
Serial.println("Connecting to AWS IOT");
while (!client.connect(THINGNAME))
{
Serial.print(".");
delay(1000);
}
if (!client.connected()) {
Serial.println("AWS IoT Timeout!");
return;
}
// Subscribe to a topic
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
Serial.println("AWS IoT Connected!");
}
void publishMessage()
{
StaticJsonDocument<200> doc;
time(&now);
doc["time"] = now;
doc["humidity"] = h;
doc["temperature"] = t;
doc["temperature_f"] = f;
char jsonBuffer[512];
serializeJson(doc, jsonBuffer); // print to client
client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
connectAWS();
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
dht.begin();
}
void loop()
{
h = dht.readHumidity();
t = dht.readTemperature();
f = dht.readTemperature(true); // Read temperature as Fahrenheit (isFahrenheit = true)
if (isnan(h) || isnan(t) ) // Check if any reads failed and exit early (to try again).
{
Serial.println(F("Failed to read from DHT sensor!"));
delay(2000);
//Read again
h = dht.readHumidity();
t = dht.readTemperature();
f = dht.readTemperature(true); // Read temperature as Fahrenheit (isFahrenheit = true)
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
delay(3000);
WiFiClient web_client = server.available();
web_client.println("HTTP/1.1 200 OK");
web_client.println("Content-Type: text/html");
web_client.println("Connection: close"); // the connection will be closed after completion of the response
web_client.println("Refresh: 5"); // refresh the page automatically every 5 sec
web_client.println();
web_client.println("<!DOCTYPE html>");
web_client.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
web_client.println("<head>\n<meta charset='UTF-8'>");
web_client.println("<title>ESP8266 Temperature & Humidity DHT11 Sensor</title>");
web_client.println("</head>\n<body>");
web_client.println("<H2>ESP8266 & DHT11 Sensor</H2>");
web_client.println("<H3>Humidity / Temperature</H3>");
web_client.println("<pre>");
web_client.print("Humidity (%) : ");
web_client.println((float)h, 2);
web_client.print("Temperature (°C) : ");
web_client.println((float)t, 2);
web_client.print("Temperature (°F) : ");
web_client.println((float)f, 2);
//client.print("Temperature (°K) : ");
//client.println(Kelvin(temp), 2);
web_client.println("</pre>");
web_client.print("</body>\n</html>");
now = time(nullptr);
if (!client.connected())
{
connectAWS();
}
else
{
client.loop();
if (millis() - lastMillis > 60000)
{
lastMillis = millis();
publishMessage();
}
}
}
Loading
esp-01
esp-01