/*#include <WiFi.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"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED 寬度像素
#define SCREEN_HEIGHT 64 // OLED 高度像素
const int DHT_PIN = 17;
DHTesp dhtSensor;
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
int number = 0;
//pir-motion-sensor
int pirdata = 34;
int pirstate =LOW ;
int value =0;
int r = 23;
int g = 18;
int b = 5;
// 設定OLED
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
static const uint8_t degreeIcon[] PROGMEM = {
B0000000,
B0001100,
B0010010,
B0010010,
B0010010,
B0001100,
B0000000
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);//Initialize serial
pinMode(pirdata, INPUT);
pinMode(r , OUTPUT);
pinMode(g , OUTPUT);
pinMode(b , OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
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() {
int value = digitalRead(pirdata);
if (value == HIGH) {
analogWrite(r , 100);
analogWrite(g , 0);
analogWrite(b , 0);
if (pirstate == LOW) {
Serial.print(value);
Serial.print(" ");
Serial.println("Motion detected!");
pirstate = HIGH;
}
} else {
analogWrite(g , 100);
analogWrite(r , 0);
analogWrite(b , 0);
if (pirstate == HIGH) {
Serial.print(value);
Serial.print(" ");
Serial.println("Motion ended!");
pirstate = LOW;
}
}
TempAndHumidity data = dhtSensor.getTempAndHumidity();
//Serial.println("Temp: " + String(data.temperature, 2) + "°C");
//Serial.println("Humidity: " + String(data.humidity, 1) + "%");
//Serial.println("---");
display.clearDisplay();
display.setTextSize(1.5); // 設定文字大小
display.setTextColor(WHITE); // 1:OLED預設的顏色(這個會依該OLED的顏色來決定)
display.setCursor(0,0); // 設定起始座標
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,10); // 設定起始座標
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());
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
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(2000); // Wait 20 seconds to update the channel again
}*/
#include <WiFi.h>
#include "ThingSpeak.h"
#include "DHTesp.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// WiFi配置
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
// ThingSpeak 配置
#define SECRET_CH_ID 2350557
#define SECRET_WRITE_APIKEY "7RCP9BJ1ODDPQPD4"
unsigned long myChannelNumber = SECRET_CH_ID;
const char *myWriteAPIKey = SECRET_WRITE_APIKEY;
// 人體紅外線感測器
const int DHT_PIN = 17;
const int PIR_PIN = 34;
// LED
const int LED_R = 18;
int p = LOW;
// OLED 配置
#define OLED_RESET -1
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
DHTesp dhtSensor;
WiFiClient client;
int number = 0;
int buzzer = 19;
//度的符號圖案
static const unsigned char PROGMEM O[]
{
B0000000,
B0001100,
B0010010,
B0010010,
B0010010,
B0001100,
B0000000
};
void WF() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
delay(1500);
Serial.print("."); //連線中顯示
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //連線成功會顯示wifi IP
}
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(LED_R, OUTPUT);
ThingSpeak.begin(client);//上傳到thingspeak
WF();//連接wifi
// 初始化 OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED N."));//失敗顯示
} else {
Serial.println(F("OLED Y."));//成功顯示
}
display.clearDisplay();
display.setTextColor(WHITE);
//display.clearDisplay();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);//初始化DHT,設定腳位
delay(1000);
}
void TH(float temperature, float humidity) { //在OLED顯示溫度濕度
display.clearDisplay();
display.setTextSize(1.5);
display.setCursor(0, 0);
display.print("Temperature="); //在OLED顯示溫度
display.print(String(temperature, 2));
display.drawBitmap(display.getCursorX(), display.getCursorY() - 1.5, O, 7, 7, SSD1306_WHITE);
display.setCursor(display.getCursorX() + 9, display.getCursorY());
display.print("C");
display.setCursor(0, 10);
display.print("Humidity="); //在OLED顯示濕度
display.print(String(humidity, 2));
display.print("%");
display.display();
}
void d() { //人體紅外線感測器
int value = digitalRead(PIR_PIN);
if (value == HIGH) { //如果感測器檢測到運動 那就顯示LED燈
digitalWrite(LED_R, HIGH);
if (p == LOW) { //如果之前狀態是低電平(表示之前未檢測到運動)
p = HIGH; //pirstate設置高電平
display.setTextSize(1.5);
display.setTextColor(WHITE);
display.setCursor(0, 30);
display.print("S!");
display.display();
delay(2000);
// Write to ThingSpeak
int x = ThingSpeak.writeField(myChannelNumber, 1, number, myWriteAPIKey);
if (x == 200) {
Serial.println("Channel update successful."); //傳送到thingspeak成功顯示
} else {
Serial.println("Problem updating channel. HTTP error code " + String(x));//傳送到thingspeak失敗顯示
}
// change the value
number++;
if (number > 99) {
number = 0;
}
}
} else {
digitalWrite(LED_R, LOW);
if (p == HIGH) {
p = LOW;
// Display on OLED
display.setTextSize(1.5);
display.setTextColor(WHITE);
display.setCursor(0, 40);
display.print("N!");
display.display();
delay(2000);
}
}
}
void loop() {
d();
TempAndHumidity data = dhtSensor.getTempAndHumidity();
TH(data.temperature, data.humidity);
}