#include "DHT.h"
#define DHTPIN 12
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#include<Wire.h>
//#include<SPI.h>
//#include<Adafruit_SH1106.h>
//Adafruit_SH1106 OLED(-1);
#include<Adafruit_GFX.h>//Wokwi
#include<Adafruit_SSD1306.h>//Wokwi
Adafruit_SSD1306 OLED(128,64,&Wire,-1);//Wokwi
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
OLED.begin(SSD1306_SWITCHCAPVCC,0x3c);
OLED.clearDisplay();
OLED.setTextColor(WHITE,BLACK);
OLED.setCursor(0,0);
dht.begin();
OLED.begin(SSD1306_SWITCHCAPVCC,0x3c);//Wokwi
OLED.clearDisplay();
OLED.setTextColor(WHITE,BLACK);
OLED.setCursor(5,5);
OLED.setTextSize(1);
OLED.print("tanachot Num: 6");
OLED.setCursor(5,18);
OLED.print("Temp= 40 C Hum= 50 %");
OLED.setCursor(5,29);
OLED.print("Heat: 55 C");
OLED.setCursor(5,40);
OLED.print("Alarm: Danger");
}
String mes1,Temp,Hum,Heat,Alarm;
int Temp1,Hum1,Heat1,Alarm1;
bool f_serial = false;
int index1,index2;
float volt;
int l=0;
void loop() {
if(f_serial == 1){ //RGB 10,20,40/
f_serial = 0;
l=0;
Serial.println(mes1); //RGB 10,20,40
index1 = index1+1; //ตำแหน่งตัว 1 ได้เลขตำแหน่งที่5
index2 = mes1.indexOf(',');//ตำแหน่งตัว , ได้เลขตำแหน่งที่6
Temp = mes1.substring(index1,index2); // (5,6)
Temp1 = Temp.toInt();
dat = "";
Serial.print("Temp=");
Serial.println(Temp1); //10
OLED.setCursor(45,18);
OLED.print(" ");
index1 = index2+1; //ตำแหน่งตัว2
index2 = mes1.indexOf(',',index1);//ตำแหน่งตัว ,
Hum = mes1.substring(index1,index2);
Hum1 = Hum.toInt();
dat = "";
Serial.print("Hum=");
Serial.println(Hum1);//20
OLED.setCursor(45,29);
OLED.print(" ");
index1 = index2+1; //ตำแหน่งตัว4
dat = mes1.substring(index1); //”40”
dat3 = dat.toInt();
dat = "";
Serial.print("dat3=");
Serial.println(dat3); //40
OLED.setCursor(45,40);
OLED.print(" ");
mes1="";
if(Temp =="rgb"){
l=0;
OLED.setTextColor(WHITE,BLACK);
OLED.setCursor(5,5);
OLED.setTextSize(1);
OLED.setCursor(5,18);
OLED.print("dat1 : ");
OLED.print(dat1);
OLED.setCursor(5,29);
OLED.print("dat2 : ");
OLED.print(dat2);
OLED.setCursor(5,40);
OLED.print("dat3 : ");
OLED.print(dat3);
//your code
analogWrite(11,255-dat1);//r
analogWrite(10,255-dat2);//g
analogWrite(9,255-dat3);//b
}
else if(cmd == "dht"){
l=1;
}
else if(cmd == "read"){
//your code
OLED.clearDisplay();
}
}OLED.display();
if(l==1){
float h = dht.readHumidity();
float t = dht.readTemperature();
float hic = dht.computeHeatIndex(t, h, false);
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
OLED.setTextColor(WHITE,BLACK);
OLED.setTextSize(1);
OLED.setCursor(5,18);
OLED.print("Temp : ");
OLED.print(t);
OLED.setCursor(5,29);
OLED.print("hum : ");
OLED.print(h);
OLED.setCursor(5,40);
OLED.print("Heat index : ");
OLED.print(hic);
delay(500);
}
}
void serialEvent() {
while (Serial.available()) { //RGB 1,50,100/
char inChar = (char)Serial.read();
if (inChar != '\n') {
mes1 += inChar; //“RGB 1,50,100”
}else{
f_serial = true;
}
}
}