Last active
January 18, 2021 22:06
-
-
Save fxprime/c1b6130cc04c006096653fa1fe520b3d to your computer and use it in GitHub Desktop.
จากบทความ ควบคุมแอร์ด้วย infrared ผ่านมือถือด้วย blynk บน ESP8266 (หรือESP32ก็ได้นะ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* www.arduinona.com | |
* บทความ ควบคุมแอร์ด้วย infrared ผ่านมือถือด้วย blynk บน ESP8266 (หรือESP32ก็ได้นะ) | |
*/ | |
#include <Arduino.h> | |
#include <IRremoteESP8266.h> | |
#include <IRsend.h> | |
#include <ir_Panasonic.h> | |
#include <BlynkSimpleEsp8266.h> | |
const char *ssid = "WIFI_NAME"; // ใส่ชื่อ WiFi ของเราตรงนี้ | |
const char *password = "WIFI_PASSWORD"; // ใส่ Password WiFi ของเราตรงนี้ | |
char auth[] = "xxxxxxxxxxxxxxxxxxxxxx"; // ใส่ Token ที่ได้จาก blynk ตรงนี้ | |
/** | |
* IRRemote | |
*/ | |
const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D2). | |
IRPanasonicAc ac(kIrLed); // สร้าง Object ที่ใช้คุมแอร์แบบ Panasonic (หากเป็นยี่ห้ออื่นให้ดูตัวอย่างใน Library IRRemoteESP8266) | |
int temp_ac = 25; //อุณหภูมิตั้งต้น | |
typedef enum{ | |
REQ_NONE =0, //ไม่มีคำสั่ง | |
REQ_ON_AC, //คำสั่งเปิดแอร์ | |
REQ_OFF_AC, //คำสั่งปิดแอร์ | |
REQ_TEMP_AC //คำสั่งปรับอุณหภูมิ | |
}req_en; | |
req_en req; | |
int retry = 0; | |
unsigned long last_retry=0; | |
/** | |
* ตั้งค่า BLYNK | |
*/ | |
#define BLYNK_PRINT Serial | |
//เมื่อมีการกดปุ่มเปิดแอร์ (virtual pin 1) ฟังก์ชันนี้จะทำงาน | |
BLYNK_WRITE(V1) | |
{ | |
if(param.asInt()) { | |
Serial.println("Req on recv"); | |
req = REQ_ON_AC; | |
} | |
} | |
//เมื่อมีการกดปุ่มปิดแอร์ (virtual pin 2) ฟังก์ชันนี้จะทำงาน | |
BLYNK_WRITE(V2) | |
{ | |
if(param.asInt()) { | |
Serial.println("Req off recv"); | |
req = REQ_OFF_AC; | |
} | |
} | |
//เมื่อมีการปรับอุณหภูมิ (virtual pin 0) ฟังก์ชันนี้จะทำงาน | |
BLYNK_WRITE(V0) | |
{ | |
temp_ac = param.asInt(); | |
Serial.println("Req temp = " + String(temp_ac)); | |
req = REQ_TEMP_AC; | |
} | |
BlynkTimer timer; //เรียกใช้การตั้งเวลาของ Blynk | |
/** | |
* แสดงสถานะของข้อมูลที่เราจะส่งออกไปยังแอร์ | |
*/ | |
void printState() { | |
// Display the settings. | |
Serial.println("Panasonic A/C remote is in the following state:"); | |
Serial.printf(" %s\n", ac.toString().c_str()); | |
// Display the encoded IR sequence. | |
unsigned char* ir_code = ac.getRaw(); | |
Serial.print("IR Code: 0x"); | |
for (uint8_t i = 0; i < kPanasonicAcStateLength; i++) | |
Serial.printf("%02X", ir_code[i]); | |
Serial.println(); | |
} | |
void setup() { | |
/** | |
* เริ่มต้นการสื่อสารแบบ Serial กับคอมพิวเตอร์ | |
*/ | |
Serial.begin(115200); | |
delay(200); | |
/** | |
* เชื่อมต่อ WiFi | |
*/ | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print(F(".")); | |
} | |
/** | |
* แสดงค่า IP ปัจจุบัน | |
*/ | |
IPAddress ip = WiFi.localIP(); | |
Serial.println(F("WiFi connected")); | |
Serial.println(""); | |
Serial.println(ip); | |
Serial.println("\n\nTrying to connect to blynk server\n\n"); | |
/** | |
* ตั้งค่าเซิฟเวอร์ blynk โดยใช้ token ยืนยันตัวตน | |
*/ | |
Blynk.config(auth); | |
/** | |
* ตั้งเวลาเชื่อมต่อกับ Blynk Server ทุกๆ 30 วินาที และเริ่มเชื่อมต่อกับเซิฟเวอร์ | |
*/ | |
timer.setInterval(30000L, reconnecting); | |
reconnecting(); | |
Serial.println("\n\nBlynk server connected\n\n"); | |
/** | |
* เซ็ตค่าต่างๆที่ใช้ในการสั่งแอร์ ดูที่ไฟล์ ir_Panasonic.cpp ในไลบรารี่ IRRemoteESP8266 สำหรับคำสั่งอื่นๆ | |
*/ | |
ac.begin(); | |
Serial.println("Default state of the remote."); | |
printState(); | |
Serial.println("Setting desired state for A/C."); | |
ac.setModel(kPanasonicRkr); | |
ac.on(); | |
ac.setFan(kPanasonicAcFanAuto); | |
ac.setMode(kPanasonicAcCool); | |
ac.setTemp(temp_ac); | |
ac.setSwingVertical(kPanasonicAcSwingVAuto); | |
ac.setSwingHorizontal(kPanasonicAcSwingHAuto); | |
} | |
void loop() { | |
/** | |
* เช็คว่ามีการรีเควสสถานะใดๆจากการกดปุ่มใน blynk ไหม ถ้ามีให้กำหนดสถานะแอร์ตามสถานะที่รีเควสไว้ | |
*/ | |
switch(req) { | |
case REQ_NONE: break; | |
case REQ_ON_AC: ac.on(); break; | |
case REQ_OFF_AC: ac.off(); break; | |
case REQ_TEMP_AC: ac.on(); break; | |
} | |
/** | |
* ถ้ามีการรีเควสเกิดขึ้น ให้ส่งซ้ำ 2 ครั้ง | |
*/ | |
if(millis() - last_retry > 500 && req!=REQ_NONE ) { | |
last_retry = millis(); | |
Serial.println("Sending IR command to A/C ..."); | |
ac.send(); | |
printState(); | |
retry ++; | |
if(retry >= 2) { | |
retry = 0; | |
req = REQ_NONE; | |
} | |
} | |
/** | |
* ให้เวลาของ Blynk ทำงาน | |
*/ | |
timer.run(); | |
/** | |
* ถ้ามีการเชื่อมต่อ blynk อยู่ให้อัพเดทข้อมูลกับทางเซิฟเวอร์ ถ้าไม่ ให้แสดงสถานะ offline | |
*/ | |
if (Blynk.connected()) | |
{ | |
Blynk.run(); | |
} else { | |
delay(200); | |
Serial.println("Offline"); | |
} | |
} | |
/** | |
* ฟังก์ชันเชื่อมต่อ blynk อัตโนมัติเมื่อมีการขาดการเชื่อมต่อนานเกินไป | |
*/ | |
void reconnecting() | |
{ | |
static int blynkIsDownCount = 0; | |
if (!Blynk.connected()) | |
{ | |
blynkIsDownCount++; | |
BLYNK_LOG("blynk server is down! %d times", blynkIsDownCount); | |
Blynk.connect(5000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment