NodeMCU ESP8266 TM1637 NTP DHT11

  • NODEMCU ESP8266
  • 4 Digit Seven Segment TM1637
  • DHT11
  • SevenSegmentTM1637.h

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include “SevenSegmentTM1637.h”
#include “SevenSegmentExtended.h”
#include <DHT.h>

#define CLK D2 // Define the connections pins:
#define DIO D3
#define DHTPIN D5 // Pin sensor is connected to

char cdmy[11];

// Set DHT type, uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
float temp_c ;
float humi ;
char tempf[4];
float tempr;
char humif[4];
//SevenSegmentTM1637 display(CLK, DIO); // Create display object of type TM1637Display:
SevenSegmentExtended display(CLK, DIO);
DHT dht = DHT(DHTPIN, DHTTYPE);

const char *ssid = “xxxxx”;
const char *password = “xxxxxxx”;

//const long utcOffsetInSeconds = 19802;
const long utcOffsetInSeconds = 25200;

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, “1.th.pool.ntp.org”, utcOffsetInSeconds);

void setup(){
display.begin(); // initializes the display
display.setBacklight(100); // set the brightness to 100 %
Serial.begin(9600);
Serial.println();
Serial.println(“DHTxx test!”);
dht.begin();
// Begin serial communication at a baud rate of 9600:

WiFi.begin(ssid, password);

while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print (“.”);
}

timeClient.begin();
}

void loop() {

// Read the temperature as Celsius and Fahrenheit:
temp_c = dht.readTemperature();
humi = dht.readHumidity();

// print both to serial console
Serial.println();
Serial.print(temp_c);
Serial.print(“<-T H->”);
Serial.println(humi);

timeClient.update();
unsigned long epochTime = timeClient.getEpochTime(); //get date

int currentHour = timeClient.getHours();
int currentMinute = timeClient.getMinutes();

struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
sprintf(cdmy,” %02d-%02d-%02d “,ptm->tm_mday,ptm->tm_mon+1,ptm->tm_year+2443);

for (int i = 0; i < 2; i++) {
display.clear(); // clear the display
display.print(cdmy); // print right->left day month year
delay(100);
}
// print time hour min
//for (int i = 0; i < 2; i++) {
display.clear(); // clear the display
// print hour min for
display.printTime(currentHour,currentMinute, true); //print hour.min
//display.blink(); // blink
delay(1000);

// }

display.setColonOn(1); // open dot . led version dot if use time led : show led
display.printNumber(static_cast<int>(temp_c * 100),false,false,false);
display.printRaw(88,3); // display c at location 3
display.blink();
delay(2000);
display.printRaw(128,1);
display.printNumber(static_cast<int>(humi * 100),false,false,false);
display.printRaw(116,3); // display h at location 3
display.blink();
delay (3000);
display.setColonOn(false); // close colon
display.clear();

}xxx