#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#define WIFI_SSID "Wokwi-GUEST" // replace MySSID with your WiFi network name
#define WIFI_PASSWORD "" // replace MyPassword with your WiFi password
#define WIFI_CHANNEL 6
#define SECRET_CH_ID 2350557 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "7RCP9BJ1ODDPQPD4" // replace XYZ with your channel write API Key
#include "DHTesp.h"
#define SCREEN_WIDTH 128 // OLED 寬度像素
#define SCREEN_HEIGHT 64 // OLED 高度像素
#include "DHTesp.h"
DHTesp dhtSensor;
const int DHT_PIN = 17;
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
LiquidCrystal_I2C lcd1(0x27,16,2);
int number = 0;
static const uint8_t degreeIcon[] PROGMEM = {
B0000000,
B0001100,
B0010010,
B0010010,
B0010010,
B0001100,
B0000000
};
void setup() {
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
Serial.begin(115200);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(1000);
Serial.begin(9600);
display.clearDisplay();
// 偵測是否安裝好OLED了
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 一般1306 OLED的位址都是0x3C
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.setTextColor(WHITE);
display.clearDisplay(); // 清除畫面
// testdrawstyles(); // 測試文字
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
delay(1000);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
display.clearDisplay();
display.setTextSize(1.5); // 設定文字大小
display.setTextColor(WHITE); // 1:OLED預設的顏色(這個會依該OLED的顏色來決定)
display.setCursor(0,30); // 設定起始座標
display.print("Temperature="); // 要顯示的字串
display.print(String(data.temperature, 2));
//display.drawCircle(108,1,1,WHITE);
display.drawBitmap(display.getCursorX(), display.getCursorY() - 1.5, degreeIcon, 7, 7, SSD1306_WHITE);
display.setCursor(display.getCursorX() + 9, display.getCursorY());
display.print("C");
display.setCursor(0,40); // 設定起始座標
display.print("Humidity="); // 要顯示的字串
display.print(String(data.humidity, 2));
display.print("%");
display.display(); // 要有這行才會把文字顯示出來
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
int x = ThingSpeak.writeField(myChannelNumber, 1, number, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
// change the value
number++;
if(number > 99){
number = 0;
}
delay(1000);
}