Created
September 11, 2019 00:43
-
-
Save paulocanedo/fafe6691d2346cd14972c89e80f3b461 to your computer and use it in GitHub Desktop.
Projeto Arduino: Temperatura, humidade (DHT22), horario NTP, LED 7SEG TM1637
This file contains hidden or 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
# O que fazer para melhorar a eficiência energética? | |
# projeto: https://ibb.co/D9sMqyf | |
#include <NTPClient.h> | |
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
#include <Arduino.h> | |
#include <TM1637Display.h> | |
#include <Adafruit_Sensor.h> | |
#include <DHT.h> | |
#include <DHT_U.h> | |
#define CLK D1 | |
#define DIO1 D2 | |
#define DIO2 D3 | |
#define DIO3 D4 | |
#define DHTPIN D5 // Digital pin connected to the DHT sensor | |
#define DHTTYPE DHT22 // DHT 22 (AM2302) | |
TM1637Display displayTime(CLK, DIO1); | |
TM1637Display displayTemp(CLK, DIO2); | |
TM1637Display displayHumi(CLK, DIO3); | |
DHT dht(DHTPIN, DHTTYPE); | |
WiFiUDP ntpUDP; | |
const char *ssid = "pc_gateway"; | |
const char *password = "q1w2e3r4"; | |
const unsigned short brightness = 0x99; | |
const short BR_TIME_ZONE = -3600 * 3; //-3 hours | |
const unsigned short UPDATE_INTERVAL = 6 * 60 * 60 * 1000; //6 hour | |
const unsigned short INVALID_VALUE = 8888; | |
const unsigned short CONNECTING_VALUE1 = 0x8888; | |
const unsigned short CONNECTING_VALUE2 = 0x0000; | |
const int thresholdRead = 30 * 1000; //30sec | |
long lastRead = 0; | |
NTPClient timeClient(ntpUDP); | |
void setupDisplays() { | |
displayTime.setBrightness(brightness); | |
displayTemp.setBrightness(brightness); | |
displayHumi.setBrightness(brightness); | |
} | |
void setupNtp() { | |
timeClient.begin(); | |
timeClient.setTimeOffset(BR_TIME_ZONE); | |
timeClient.setUpdateInterval(UPDATE_INTERVAL); | |
} | |
void setupDHT() { | |
dht.begin(); | |
} | |
void setupWireless() { | |
WiFi.begin(ssid, password); | |
bool flag = false; | |
while ( WiFi.status() != WL_CONNECTED ) { | |
delay ( 500 ); | |
Serial.print ( "." ); | |
displayTime.showNumberHexEx(flag ? CONNECTING_VALUE1 : CONNECTING_VALUE2); | |
displayTemp.showNumberHexEx(flag ? CONNECTING_VALUE1 : CONNECTING_VALUE2); | |
displayHumi.showNumberHexEx(flag ? CONNECTING_VALUE1 : CONNECTING_VALUE2); | |
flag = !flag; | |
} | |
} | |
void setup(){ | |
setupDisplays(); | |
setupNtp(); | |
setupDHT(); | |
setupWireless(); | |
Serial.println("pronto!"); | |
} | |
void loop() { | |
long currentRead = millis(); | |
if(currentRead - lastRead > thresholdRead) { | |
lastRead = currentRead; | |
float temperature = dht.readTemperature(); | |
float humidity = dht.readHumidity(); | |
float hic = dht.computeHeatIndex(temperature, humidity, false); | |
showTemperature(temperature, displayTemp); | |
showHumidity(humidity, displayHumi); | |
} | |
timeClient.update(); | |
int h = timeClient.getHours(); | |
int m = timeClient.getMinutes(); | |
showTime(h, m, displayTime); | |
delay(1000); | |
} | |
void showTemperature(float temperature, TM1637Display display) { | |
if (isnan(temperature)) { | |
Serial.println(F("Error reading temperature!")); | |
displayTemp.showNumberDec(INVALID_VALUE); | |
} else { | |
int itemp = round(temperature); | |
int d0 = itemp / 10; | |
int d1 = itemp % 10; | |
uint8_t data[] = { | |
display.encodeDigit(d0), | |
display.encodeDigit(d1), | |
SEG_A | SEG_B | SEG_G | SEG_F, | |
display.encodeDigit(0xc) | |
}; | |
display.setSegments(data); | |
} | |
} | |
void showHumidity(float humidity, TM1637Display display) { | |
if (isnan(humidity)) { | |
Serial.println(F("Error reading humidity!")); | |
display.showNumberDec(INVALID_VALUE); | |
} else { | |
int ihumi = round(humidity); | |
int d0 = ihumi / 10; | |
int d1 = ihumi % 10; | |
uint8_t data[] = { | |
display.encodeDigit(d0), | |
display.encodeDigit(d1), | |
SEG_A | SEG_B | SEG_G | SEG_F, | |
SEG_C | SEG_D | SEG_E | SEG_G | |
}; | |
display.setSegments(data); | |
} | |
} | |
void showTime(int hours, int minutes, TM1637Display display) { | |
int value = hours * 100 + minutes; | |
display.showNumberDecEx(value, 0b01000000, true, 4, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Projeto: