#include <WiFi.h> //the order will try to be written on the order of include
#include "WiFiUdp.h"
#include <NTPClient.h>
#include "DHT.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SSID "Wokwi-GUEST"
#define PASS ""
//#define SSID "Nero"//"Wokwi-GUEST"
//#define PASS "NeoNeo2142"//""
IPAddress ip(192,168,0,30);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
#define TZ "PHT-8"
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
unsigned long timeNow=0; //set to 0
#define dhtPin 4
#define dhtPine 2
#define dhtType DHT22
//DHT dht(dhtPin, dhtType);
DHT dht[] = {
{dhtPin, dhtType},
{dhtPine, dhtType}
};
//DHT dhtE(dhtPin, dhtType);
float dhtS[5];
float dhtSE[5];
float dhtSX[2][5];
//using millis() instead of delay() for efficiency and timing
unsigned long milPrv = 0; //previous millis()
const long milInt = 100; //interval 100ms
int milDec=0; //decisecond 100ms=1ds
int milSec=0; //second elapsed
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int listSize=5;
String listString[listSize];
int barInt=0;
int barMax=4;
int barDly=500;
const unsigned char PROGMEM barLogo [] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
0x35, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x54, 0x2a, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xac, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0xcf, 0xe4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x01, 0xe7, 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x03, 0xf1, 0xf7, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x07, 0xf8, 0x77, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x0f, 0xfe, 0x37, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x84, 0x20, 0x1f, 0x80, 0x13, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x38, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x03, 0xc0, 0x03, 0xce, 0x03, 0xf0, 0x7f, 0x1f, 0xc3, 0xfe, 0x61, 0xc7, 0xf0, 0x7f, 0x04,
0x20, 0x1f, 0x80, 0x03, 0x9f, 0x07, 0xf8, 0x7f, 0x1f, 0xe7, 0xfc, 0x61, 0x87, 0xf8, 0x7f, 0x04, 0x20, 0x3e, 0x00, 0x01, 0x1f, 0x07, 0x1c, 0xe0, 0x38, 0xe0, 0xe0, 0x61, 0x8e, 0x38, 0xe0, 0x04,
0x20, 0x3e, 0x00, 0x01, 0x37, 0x07, 0x1c, 0xe0, 0x38, 0xe0, 0xe0, 0xe1, 0x8e, 0x38, 0xe0, 0x04, 0x20, 0x7c, 0x00, 0x00, 0x23, 0x0f, 0x38, 0xfe, 0x3f, 0x80, 0xc0, 0xe1, 0x8f, 0xe0, 0xfe, 0x04,
0x20, 0x7c, 0x00, 0x00, 0x63, 0x0f, 0xf0, 0xfe, 0x3f, 0x81, 0xc0, 0xc1, 0x8f, 0xe0, 0xfe, 0x04, 0x20, 0x79, 0x00, 0x00, 0xff, 0x0e, 0x01, 0xc0, 0x31, 0xc1, 0xc0, 0xc3, 0x8c, 0x71, 0xc0, 0x04,
0x20, 0x73, 0x00, 0x00, 0xff, 0x0e, 0x01, 0xc0, 0x31, 0xc1, 0xc0, 0xc3, 0x8c, 0x71, 0xc0, 0x04, 0x20, 0x33, 0x00, 0x01, 0xc3, 0x8c, 0x01, 0xfe, 0x71, 0xc1, 0x80, 0xff, 0x1c, 0x71, 0xfe, 0x04,
0x20, 0x27, 0x00, 0x01, 0x83, 0x8c, 0x01, 0xfc, 0x71, 0x81, 0x80, 0x7c, 0x1c, 0x61, 0xfc, 0x04, 0x20, 0x0f, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x1f, 0xa0, 0x07, 0xf0, 0x01, 0x07, 0x07, 0x0e, 0x04, 0x3e, 0x38, 0x70, 0x8f, 0x0e, 0x04, 0x20, 0x1f, 0xb0, 0x3f, 0xe1, 0x03, 0x84, 0x88, 0x89, 0x0e, 0x08, 0x44, 0x48, 0x88, 0x10, 0x04,
0x20, 0x0f, 0xb8, 0xff, 0xc1, 0x02, 0x87, 0x08, 0x8e, 0x0a, 0x08, 0x44, 0x70, 0x8e, 0x0c, 0x04, 0x20, 0x07, 0xbe, 0x7f, 0x81, 0x07, 0xc4, 0x88, 0x89, 0x1f, 0x08, 0x44, 0x48, 0x88, 0x02, 0x04,
0x20, 0x03, 0xbf, 0x1f, 0x01, 0xc4, 0x47, 0x07, 0x09, 0x11, 0x08, 0x38, 0x48, 0x8f, 0x1c, 0x04, 0x20, 0x01, 0x9f, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x9f, 0xe4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x0f, 0xf0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x84,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x04,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x29, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x34,
0x2d, 0x44, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x92, 0x14, 0x2b, 0x64, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x92, 0x14,
0x29, 0x44, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x92, 0x14, 0x29, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x94,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
Serial.begin(115200);
while (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {Serial.println(F("SSD1306 allocation failed"));} // Address 0x3D for 128x64
display.clearDisplay(); display.setTextColor(WHITE); display.drawBitmap(0, 0, barLogo, 128, 64, WHITE);
WiFiStat();
NTPStat();
delay(1000); //arbitrary, might prevent unstable dht22
//dht.begin();
//for (auto& sensor : dht) {
// sensor.begin();
//}
dht[0].begin(); dht[1].begin();
bar(true);
}
void WiFiStat() {
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);
WiFi.begin(SSID,PASS,6); Serial.println("[WiFi] Connecting to..."+String(SSID));
while (WiFi.status() != WL_CONNECTED) { bar(false); Serial.print("."); delay(500);}
WiFi.waitForConnectResult(); //waiting for connection is crucial to config
WiFi.config(WiFi.localIP(), WiFi.gatewayIP(), WiFi.subnetMask(), IPAddress(1,1,1,1));
Serial.println("\n[WiFi] Connected to..."+String(SSID)); bar(false);
WiFi.setAutoReconnect(true);
}
void NTPStat(){
int ul_t = 0
timeClient.begin(); Serial.println("[Time] Syncing to..."+String(TZ)); timeClient.setTimeOffset(3600*8);
while (!timeClient.isTimeSet()) {
bar(false);
Serial.print(".");
ul_t++;
timeClient.update();
if (ul_t>5){timeClient.forceUpdate();}
if (ul_t>8){return;}
timeNow=timeClient.getEpochTime();
delay(1000);}
Serial.println("\n[Time] Synced..."+String(timeNow)); bar(false);
}
bool DHTStat(){
bool result=true;
bool res[2];
for (int i=0;i<2;i++){
res[i]=true;
dhtSX[i][0] = dht[i].readHumidity();
dhtSX[i][1] = dht[i].readTemperature(false);
dhtSX[i][2] = dht[i].readTemperature(true); //(isFahrenheit = true)
if (isnan(dhtSX[i][0]) || isnan(dhtSX[i][1]) || isnan(dhtSX[i][2]))
{Serial.println(F("[DHT] No readings")); res[i]=false;}
dhtSX[i][3] = dht[i].computeHeatIndex(dhtSX[i][1], dhtSX[i][0], false);
dhtSX[i][4] = dht[i].computeHeatIndex(dhtSX[i][2], dhtSX[i][0], true);
}
//temp fix
if (res[0]==true or res[1]==true)
{result=true;}
else {result=false;}
return result;
}
void bar(bool fin) {
if (barInt==0) {display.fillRect(36,45,56,7,BLACK);} else {display.fillRect(36+14*(barInt-1),45,13,7,WHITE);} //add bar
if (barInt==barMax) {barInt=0;} else {barInt+=1;} //reset
if (fin) {display.fillRect(36,45,56,7,WHITE);}
display.display();
delay(barDly);
}
void OLEDUpdate() {
int config[3]={5,6,10};
display.fillRect(4,5,120,50,BLACK);
display.setTextSize(1);
for (int i=0;i<listSize;i++){
display.setCursor(config[0],config[1]+config[2]*i);
display.println(listString[i]);
Serial.println(listString[i]);
}
display.display(); //update
}
//add wifi update
//check for devices
//add storagefor unsent files
void loop(){
unsigned long milCur = millis(); //get milimilSecond passed
if (milCur - milPrv >= milInt) { //if current-previous>=interval
milPrv = milCur; milDec+=1;} //assign //update 100ms passed
if ((milDec==10)) { //Basically if it's 1.0s 2.0s 3.0s
listString[0]=("Time: "+String(timeClient.getFormattedTime()));//+milSec));
if ((milSec%2)==0){
if (DHTStat()){
listString[1]=("Humd: "+String(dhtSX[0][0])+"/"+String(dhtSX[1][0])+"%");
listString[2]=("Temp: "+String(dhtSX[0][1])+"C/"+String(dhtSX[1][1])+"C");
listString[3]=("Heat: "+String(dhtSX[0][3])+"C/"+String(dhtSX[1][3])+"C");// lcd.setCursor(0, 0);}
listString[4]="";
}
else {listString[1]="DHT22 offline";}
}
OLEDUpdate();
}
if (milDec>=10){ milSec+=1; milDec=0;} //if it reaches 10 milDeci or 1 milSecond //reset
if (milSec>=60){ milSec=0; timeClient.update();} //update every 60 seconds
}