#include <WiFi.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include "symbols.h"
// v 1
// Uncomment one of the lines below for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// DHT Sensor
uint8_t DHTPin = 4;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
float Temperature;
float Humidity;
// LCD
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
// Button
#define BUTTON_PIN 26 // GPIO26 pin connected to button
int buttonState = LOW; // Змінна для зберігання стану кнопки
int lastButtonState = LOW;
int currentState;
int buttonCounter = 0; // Лічильник кількості натискань кнопки
int maxSwitch = 3;
// Temperature sensor DS18B20
const int oneWireBus = 12;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
float temperatureC;
float temperatureF;
void setup() {
Serial.begin(115200);
// Button
pinMode(BUTTON_PIN, INPUT);
// Temperature sensor DS18B20
sensors.begin();
// LCD
LCD.begin(16, 2);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
}
unsigned long next_time; // время очередного переключения первого светодиода
int timeout = 10000; // половина периода мигания
void loop() {
int now_time = millis(); // текущее время
if( now_time >= next_time ){ // если текущее время превысило намеченное время, то
next_time = now_time + timeout; // вычисляем время следующего переключения
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
temperatureF = sensors.getTempFByIndex(0);
Serial.print(temperatureC);
Serial.println("ºC");
Serial.print(temperatureF);
Serial.println("ºF");
Temperature = dht.readTemperature(); // Gets the values of the temperature
Humidity = dht.readHumidity(); // Gets the values of the humidity
Serial.print(Temperature);
Serial.println("ºC");
Serial.print(Humidity);
Serial.println("%");
}
buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
if(buttonCounter == maxSwitch){
buttonCounter = 1;
}
else{
buttonCounter++;
}
displayPageOnLCD(buttonCounter);
Serial.print("Кількість натискань кнопки: ");
Serial.println(buttonCounter);
// Затримка для уникнення подвійних натискань
delay(500);
}
lastButtonState = buttonState;
}
}
void signalLevelIcon( int signalLevel, int indexVertical, int indexHorizontal ){
if(signalLevel == 0){
byte signalLevelIconRAW[8] = {
0b00000,
0b00100,
0b00100,
0b00100,
0b00100,
0b00000,
0b00100,
0b00000
};
LCD.createChar(0, signalLevelIconRAW);
}
else if(signalLevel == 1){
byte signalLevelIconRAW[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00100,
0b00000
};
LCD.createChar(0, signalLevelIconRAW);
}
else if(signalLevel == 2){
byte signalLevelIconRAW[8] = {
0b00000,
0b00000,
0b00000,
0b00100,
0b01110,
0b00000,
0b00100,
0b00000
};
LCD.createChar(0, signalLevelIconRAW);
}
else if(signalLevel == 3){
byte signalLevelIconRAW[8] = {
0b00100,
0b01110,
0b10001,
0b00100,
0b01110,
0b00000,
0b00100,
0b00000
};
LCD.createChar(0, signalLevelIconRAW);
}
//LCD.setCursor(0, 0);
LCD.setCursor(indexVertical, indexHorizontal);
//LCD.write((uint8_t) signalLevel);
LCD.write((byte) 0);
}
void batteryLevelIcon( int batteryLevel, int indexVertical, int indexHorizontal ){
if(batteryLevel >= 0 && batteryLevel <= 10){
byte batteryLevelIconRAW[8] = {
0b01110,
0b11011,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b11111
};
LCD.createChar(1, batteryLevelIconRAW);
}
else if(batteryLevel > 10 && batteryLevel <= 20){
byte batteryLevelIconRAW[8] = {
0b01110,
0b11011,
0b10001,
0b10001,
0b10001,
0b10001,
0b11111,
0b11111
};
LCD.createChar(1, batteryLevelIconRAW);
}
else if(batteryLevel > 20 && batteryLevel <= 40){
byte batteryLevelIconRAW[8] = {
0b01110,
0b11011,
0b10001,
0b10001,
0b10001,
0b11111,
0b11111,
0b11111
};
LCD.createChar(1, batteryLevelIconRAW);
}
else if(batteryLevel > 40 && batteryLevel <= 60){
byte batteryLevelIconRAW[8] = {
0b01110,
0b11011,
0b10001,
0b10001,
0b11111,
0b11111,
0b11111,
0b11111
};
LCD.createChar(1, batteryLevelIconRAW);
}
else if(batteryLevel > 60 && batteryLevel <= 80){
byte batteryLevelIconRAW[8] = {
0b01110,
0b11011,
0b10001,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
LCD.createChar(1, batteryLevelIconRAW);
}
else if(batteryLevel > 80 && batteryLevel <= 100){
byte batteryLevelIconRAW[8] = {
0b01110,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
LCD.createChar(1, batteryLevelIconRAW);
}
else if(batteryLevel < 0 || batteryLevel > 100){
byte batteryLevelIconRAW[8] = {
0b00000,
0b00100,
0b00100,
0b00100,
0b00100,
0b00000,
0b00100,
0b00000
};
LCD.createChar(1, batteryLevelIconRAW);
}
//LCD.setCursor(0, 1);
LCD.setCursor(indexVertical, indexHorizontal);
// LCD.write((uint8_t) batteryLevel);
LCD.write((byte) 1);
}
void displayPageOnLCD(int namberPage){
LCD.begin(16, 2);
LCD.clear();
signalLevelIcon(0, 0, 0);
batteryLevelIcon(195, 0, 1);
// Page 1 Signal, battery, Temperature, Humidity
if(namberPage == 1){
LCD.setCursor(7, 0);
LCD.println("|");
LCD.setCursor(7, 1);
LCD.println("|");
LCD.setCursor(9, 0);
LCD.println(Temperature);
LCD.setCursor(15, 0);
LCD.println("%");
//LCD.write((uint8_t)6); // show icon degreesCelsius
LCD.setCursor(9, 1);
LCD.println(Humidity);
LCD.setCursor(15, 1);
LCD.println("%");
}
else if(namberPage == 2){
LCD.setCursor(8, 0);
LCD.println(temperatureC);
LCD.setCursor(15, 0);
LCD.println("C");
LCD.setCursor(8, 1);
LCD.println(temperatureF);
LCD.setCursor(15, 1);
LCD.println("F");
}
else if(namberPage == 3){
LCD.setCursor(8, 0);
LCD.println("ERROR");
}
}