#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
 
#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);
   Serial.println("hello");
   pinMode(11, OUTPUT);
   pinMode(10, OUTPUT);
   pinMode(9, OUTPUT);
       analogWrite(11,255);//r
       analogWrite(10,255);//g
       analogWrite(9,255);//b  
  dht.begin();
  OLED.begin(SSD1306_SWITCHCAPVCC,0x3c);//Wokwi
  OLED.clearDisplay();
  OLED.setTextColor(WHITE,BLACK);
  OLED.setCursor(5,5);
  OLED.setTextSize(1);
  OLED.print("cmd : ");
  OLED.setCursor(5,18);
  OLED.print("dat1 : ");
  OLED.setCursor(5,29);
  OLED.print("dat2 : ");
  OLED.setCursor(5,40);
  OLED.print("dat3 : ");
}
 
String mes1,cmd,dat;
int dat1,dat2,dat3;
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 = mes1.indexOf(' ');  //RGB 10,20,40
     cmd = mes1.substring(0,index1);
     Serial.print("cmd=");    
     Serial.println(cmd);  //RGB
     OLED.setCursor(5,5);
     OLED.print("           ");
     OLED.setCursor(5,5);
     OLED.setTextSize(1);
     OLED.print("cmd : ");
     OLED.print(cmd);
 
 
     index1 = index1+1; //ตำแหน่งตัว 1 ได้เลขตำแหน่งที่5        
     index2 =  mes1.indexOf(',');//ตำแหน่งตัว , ได้เลขตำแหน่งที่6
     dat = mes1.substring(index1,index2); // (5,6)
     dat1 = dat.toInt();
     dat = "";
     Serial.print("dat1=");
     Serial.println(dat1);  //10
     OLED.setCursor(45,18);
     OLED.print("              ");
     
 
     index1 = index2+1; //ตำแหน่งตัว2
     index2 =  mes1.indexOf(',',index1);//ตำแหน่งตัว ,
     dat = mes1.substring(index1,index2);
     dat2 = dat.toInt();
      dat = "";
     Serial.print("dat2=");
     Serial.println(dat2);//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(cmd =="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;
    }
  }
}
Loading
ssd1306