พ ้ ื นฐานการสร ้ างฟาร ์ มอ ั จฉริยะอย ่ างงา ่ ยด ้ วยเคร ่ ื องม ื อ Telegram fundamentals of constructing a rudimentary smart farm using Telegram tools นายสรวิทย ์สุจจติ 6304335001016 นายอุดมศั กด ์ิรอนนงค ์ 6304335001022 นายเอกวิทย ์ มะลิว ั ลย ์ 6304335001023 นายธ ัชธร วรรณายุว ั ฒน ์ 6304335001007 สาขาเทคโนโลย ีไฟฟ้ าอุตสาหกรรม คณะวิทยาศาสตร ์ และเทคโนโลย ี มหาวิทยาล ั ย ราชภ ั ฏสุราษฎร ์ ธาน ี
#include <WiFi.h> #include <WiFiClientSecure.h> #include <UniversalTelegramBot.h> // Wifi network station credentials #define WIFI_SSID "TP-Link_4064" #define WIFI_PASSWORD "11848066" // Telegram BOT Token (Get from Botfather) #define BOT_TOKEN "6578621321:AAEMXDLOl_1pWSk1URugifB1M7w_sNwQgqc" const unsigned long BOT_MTBS = 1000; // mean time between scan messages WiFiClientSecure secured_client; UniversalTelegramBot bot(BOT_TOKEN, secured_client); unsigned long bot_lasttime; // last time messages' scan has been done int soilSensorPin = 34; int waterPumpPin = 26; void handleNewMessages(int numNewMessages) { for (int i = 0; i < numNewMessages; i++) { String chat_id = bot.messages[i].chat_id; String text = bot.messages[i].text;
if (text == "/moisture") { float moistureValue = analogRead(soilSensorPin); bot.sendMessage(chat_id, "Soil Moisture: " + String(moistureValue)); } else if (text == "/wateron") { digitalWrite(waterPumpPin, HIGH); bot.sendMessage(chat_id, "Water Pump turned ON"); } else if (text == "/wateroff") { digitalWrite(waterPumpPin, LOW); bot.sendMessage(chat_id, "Water Pump turned OFF"); } } } void setup() { Serial.begin(115200); Serial.println(); // attempt to connect to Wifi network: Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT ); // Add root certificate for api.telegram.org while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.print("\nWiFi connected. IP address: "); Serial.println(WiFi.localIP()); pinMode(waterPumpPin, OUTPUT); } void loop() { if (millis() - bot_lasttime > BOT_MTBS) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while (numNewMessages) { handleNewMessages(numNewMessages); numNewMessages = bot.getUpdates(bot.last_message_received + 1); }
bot_lasttime = millis(); } }
ผลการทดสอบโปรแกรม
Coding เพื่อเปลี่ยน Icon แสดงผล #include <WiFi.h> #include <WiFiClientSecure.h> #include <UniversalTelegramBot.h> // Wifi network station credentials #define WIFI_SSID "TP-Link_4064" #define WIFI_PASSWORD "11848066" // Telegram BOT Token (Get from Botfather) #define BOT_TOKEN "6578621321:AAEMXDLOl_1pWSk1URugifB1M7w_sNwQgqc" const unsigned long BOT_MTBS = 1000; // mean time between scan messages WiFiClientSecure secured_client; UniversalTelegramBot bot(BOT_TOKEN, secured_client); unsigned long bot_lasttime; // last time messages' scan has been done int soilSensorPin = 34; int waterPumpPin = 26; void handleNewMessages(int numNewMessages) { for (int i = 0; i < numNewMessages; i++) {
String chat_id = bot.messages[i].chat_id; String text = bot.messages[i].text; if (text == "/moisture") { float moistureValue = map(analogRead(soilSensorPin), 0, 4096,100,0); bot.sendMessage(chat_id, "Soil Moisture: " + String(moistureValue)); } else if (text == "/wateron") { digitalWrite(waterPumpPin, HIGH); bot.sendMessage(chat_id, " Water Pump turned ON"); } else if (text == "/wateroff") { digitalWrite(waterPumpPin, LOW); bot.sendMessage(chat_id, " Water Pump turned OFF"); } else if (text == "/start") { // เมนูคำสั่งเมื่อเริ่มต้นการสนทนา String menu = "เลือกคำสั่ง:\n"; menu += "/moisture - ดูค่าความชื้นในดิน\n"; menu += "/wateron - เปิดปั๊มน้ำ\n"; menu += "/wateroff - ปิดปั๊มน้ำ\n"; bot.sendMessage(chat_id, menu);
} } } void setup() { Serial.begin(115200); Serial.println(); // attempt to connect to Wifi network: Serial.print("Connecting to Wifi SSID "); Serial.print(WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT ); // Add root certificate for api.telegram.org while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.print("\nWiFi connected. IP address: "); Serial.println(WiFi.localIP()); pinMode(waterPumpPin, OUTPUT); } void loop() { if (millis() - bot_lasttime > BOT_MTBS)
{ int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while (numNewMessages) { handleNewMessages(numNewMessages); numNewMessages = bot.getUpdates(bot.last_message_received + 1); } bot_lasttime = millis(); } }
ผลการทดสอบโปรแกรม
ตัวอย่างออกแบบ icon manu เพื่อกดสั่งการทำงาน #include <WiFi.h>
#include <WiFiClientSecure.h> #include <UniversalTelegramBot.h> // Wifi network station credentials #define WIFI_SSID "TP-Link_4064" #define WIFI_PASSWORD "11848066" // Telegram BOT Token (Get from Botfather) #define BOT_TOKEN "6578621321:AAEMXDLOl_1pWSk1URugifB1M7w_sNwQgqc" const unsigned long BOT_MTBS = 1000; // mean time between scan messages WiFiClientSecure secured_client; UniversalTelegramBot bot(BOT_TOKEN, secured_client); unsigned long bot_lasttime; // last time messages' scan has been done int soilSensorPin = 34; int waterPumpPin = 26; void handleNewMessages(int numNewMessages) { for (int i = 0; i < numNewMessages; i++) { String chat_id = bot.messages[i].chat_id; String text = bot.messages[i].text; if (text == "/moisture")
{ float moistureValue = map(analogRead(soilSensorPin), 0, 4096,100, 0); bot.sendMessage(chat_id, "Soil Moisture: " + String(moistureValue)); } else if (text == "/wateron") { digitalWrite(waterPumpPin, HIGH); bot.sendMessage(chat_id, "WaterPump turned ON"); } else if (text == "/wateroff") { digitalWrite(waterPumpPin, LOW); bot.sendMessage(chat_id, "Water Pump turned OFF"); } else if (text == "/start") { String menu = "เลือกคำสั่ง:\n"; menu += "/moisture - \n"; menu += "/wateron - \n"; menu += "/wateroff - \n"; bot.sendMessage(chat_id,menu); } } } void setup()
{ Serial.begin(115200); Serial.println(); // attempt to connect to Wifi network: Serial.print("Connecting to Wifi SSID "); Serial.print(WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT ); // Add root certificate for api.telegram.org while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.print("\nWiFi connected. IP address: "); Serial.println(WiFi.localIP()); pinMode(waterPumpPin, OUTPUT); } void loop() { if (millis() - bot_lasttime > BOT_MTBS) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while (numNewMessages)
{ handleNewMessages(numNewMessages); numNewMessages = bot.getUpdates(bot.last_message_received + 1); } bot_lasttime = millis(); } } ผลการทดสอบโปรแกรม