/* Fill-in information from Blynk Device Info here */
/* Template ID、Template Name、Auth Token 一定要放在程式的最前面 */
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME "ESP32LED"
#define BLYNK_AUTH_TOKEN ""
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define Disp
#if defined(ESP32)
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#else
#warning "Board not support"
#endif
#if defined(Disp)
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> // 匯入OLED程式庫
#endif
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
#define LED1_PIN 4
#define LED2_PIN 2
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#if defined(Disp)
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#endif
float humidity=0; // 宣告一個資料型態為float的全域變數humidity
float temperature=0; // 宣告一個資料型態為float的全域變數temperature
BlynkTimer timer;
WidgetLED led1(V1);
void sendSensor();
BLYNK_WRITE(V0);
BLYNK_WRITE(V1);
void blinkLedWidget();
void setup(void) {
pinMode(LED1_PIN,OUTPUT);
pinMode(LED2_PIN,OUTPUT);
// Debug console
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#if defined(Disp)
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
#endif
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
// timer.setInterval(1000L, blinkLedWidget);
Serial.println("Start BLYNK");
for(int i=0;i<3;i++)
{
digitalWrite(LED1_PIN,HIGH);
delay(1000);
digitalWrite(LED1_PIN,LOW);
delay(1000);
}
}
void loop(void) {
Blynk.run(); // 執行Blynk
timer.run(); // 執行BlynkTimer計時器
String StringT = "T:"+String(temperature)+"*C";
String StringH = "H:"+String(humidity)+"%";
#if defined(Disp)
oled.clearDisplay(); // clear display
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 0); // x=0,y=0位置顯示溫度的值
oled.println(StringT);
oled.setCursor(0, 16); // x=0,y=16位置顯示濕度的值
oled.println(StringH);
oled.display(); //螢幕顯示畫面
#endif
}
BLYNK_WRITE(V0)
{
int pinValue = param.asInt(); // Get value as integer
if (pinValue == 1) {
digitalWrite(LED1_PIN,HIGH);
Serial.println("LED1 on V1: off");
} else {
digitalWrite(LED1_PIN,LOW);
Serial.println("LED1 on V1: on");
}
}
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // Get value as integer
if (pinValue == 1) {
digitalWrite(LED2_PIN,HIGH);
Serial.println("LED2 on V1: off");
} else {
digitalWrite(LED2_PIN,LOW);
Serial.println("LED2 on V1: on");
}
}
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor() {
// float humidity = dht.readHumidity();
// float temperature = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
humidity = random(100);
temperature = random(50);
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, humidity);
Blynk.virtualWrite(V6, temperature);
}
// V1 LED Widget is blinking
void blinkLedWidget()
{
if (led1.getValue()) {
led1.off();
Serial.println("LED on V1: off");
} else {
led1.on();
Serial.println("LED on V1: on");
}
}