กลับไปหน้ารวมไฟล์
make-your-iot-cloud-kit-send-you-updates-on-telegram-6e1ad3.md

Make Your IoT Cloud Kit Send You Updates on Telegram

หมายเหตุ: บทช่วยสอนนี้อาจล้าสมัย โปรดไป ที่นี่ เพื่อดูเวอร์ชันล่าสุด

ในบทช่วยสอนนี้ เราจะมาลองเล่นกับ IoT Cloud Kit ใหม่จาก Arduino ซึ่งประกอบด้วย (นอกจาก LED, breadboards, LED และสายไฟ) บอร์ด MKR WiFi 1010, Environmental Shield และ MKR Relay Proto Shield.

เราจะทำให้ข้อมูลที่บอร์ดรวบรวมสามารถเข้าถึงได้ผ่าน conversational UI เราจะใช้ Telegram ดังนั้นคุณจะต้องตั้งค่าบัญชีในบริการแชทนั้น (คุณจะต้องใช้หมายเลขโทรศัพท์มือถือด้วย)

  • เราจะใช้ไลบรารี Universal Telegram Bot โดย Brian Lough ร่วมกับไลบรารี ArduinoJson ที่เขียนโดย Benoît Blanchon
  • เราจะได้รับการแจ้งเตือนหากเซ็นเซอร์เฉพาะ (ในตัวอย่างนี้คือ t สำหรับ temperature) เกิน temp_limit ที่ 35.5
  • เราจะสามารถควบคุม LED ในตัวของ Arduino และ RELAY สองตัวบนบอร์ดได้อย่างเต็มที่

เยี่ยม! มาเริ่มเล่นกับบอร์ดกันเลย

สร้าง TelegramBot ใหม่โดยใช้ BotFather

ตรวจสอบให้แน่ใจว่าคุณได้ติดตั้ง Telegram บนโทรศัพท์หรือแล็ปท็อปของคุณแล้ว จากนั้นในช่องค้นหา ให้ค้นหา @botfather

พูดคุยกับเขาและใช้คำสั่ง /newbot เพื่อสร้างบอทใหม่ BotFather จะขอชื่อและ username ของคุณ จากนั้นจะสร้าง authorization token สำหรับบอทใหม่ของคุณ Name ของบอทของคุณจะแสดงในรายละเอียดผู้ติดต่อและที่อื่นๆ Username เป็นชื่อย่อที่ใช้ในการอ้างถึงและลิงก์ telegram.me Username ของบอทของคุณ ต้อง ลงท้ายด้วย ‘bot’ เช่น ‘tetris_bot’ หรือ ‘TetrisBot’

เปิด Universal TelegramBot > 101 > Echobot.ino.

เนื่องจากโค้ดนี้ต้องพึ่งพาไลบรารี Wifi อื่น เราจึงต้องเปลี่ยน

#include <WiFi101.h>

เป็น

#include <WiFiNINA.h>

หรือแม้แต่เพิ่มโค้ดวิเศษนี้ที่จะทำให้ Arduino IDE เรียกใช้ไลบรารีที่เหมาะสมจากสองไลบรารีตามบอร์ดเป้าหมายที่ระบุ

// This header is used to tell the Arduino IDE
// to compile with the proper WIFI Library
// between MKR1000 and MKR1010
#ifdef ARDUINO_SAMD_MKRWIFI1010
#include <WiFiNINA.h>
#elif ARDUINO_SAMD_MKR1000
#include <WiFi101.h>
#else
#error unsupported board#endif

ทีนี้ หากคุณอัปเดต WiFi SSID และ password พร้อมกับ bot token ที่ถูกต้อง คุณควรจะได้รับการตอบกลับด้วยข้อความล่าสุดของคุณ มาเพิ่มข้อมูลกัน

สำคัญ! เพื่อให้ BOT คุยกับคุณ (และแค่คุณเท่านั้น!) คุณต้องค้นหา chat_id ของคุณ (ตัวเลขที่ Telegram กำหนดให้กับการสนทนาระหว่างคุณกับ BOT ของคุณ)

คุณสามารถค้นหาสิ่งนี้ได้โดยการเพิ่มสตริงนี้เข้าไปในฟังก์ชัน HandleNewMessages.

   Serial.println(chat_id);

เก็บโค้ดนี้ไว้สำหรับคุณ และเพิ่มลงในโค้ดสุดท้ายเป็น Mychat_id String ที่กำหนดไว้ที่จุดเริ่มต้นของ sketch.

ข้อมูลสิ่งแวดล้อมในการตอบกลับ

เราจะสร้างสตริงที่จะส่งเป็นคำตอบ เราจะจัดเก็บข้อมูลสำคัญทั้งหมดไว้ในสตริงข้อมูล

/*
MKR ENV Shield - Read Sensors
This example reads the sensors on-board the MKR ENV shield
and prints them to the Serial Monitor once a second.
The circuit:
- Arduino MKR board
- Arduino MKR ENV Shield attached
This example code is in the public domain.
*/

#include <Arduino_MKRENV.h>
#include <WiFiSSLClient.h>

#include <UniversalTelegramBot.h>
#ifdef ARDUINO_SAMD_MKRWIFI1010
#include <WiFiNINA.h>
#elif ARDUINO_SAMD_MKR1000
#include <WiFi101.h>
#else
#error unsupported board
#endif

// Initialize Wifi connection to the router
char ssid[] = "SSIDName"; // your network SSID (name)
char password[] = "SSIDKey"; // your network key
#define BOTtoken "BotToken" // your Bot Token (Get from Botfather)


WiFiSSLClient client;

UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done

void setup() {
Serial.begin(115200);
while (!Serial);
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while (1);
}
delay(100);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
while (WiFi.begin(ssid, password) != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
}
void loop() {
Serial.print("Temperature = ");
Serial.print(ENV.readTemperature());
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(ENV.readHumidity());
Serial.println(" %");
Serial.print("Pressure = ");
Serial.print(ENV.readPressure());
Serial.println(" kPa");
Serial.print("Lux . = ");
Serial.println(ENV.readLux());
Serial.print("UVA = ");
Serial.println(ENV.readUVA());
Serial.print("UVB = ");
Serial.println(ENV.readUVB());
Serial.print("UV Index = ");
Serial.println(ENV.readUVIndex());
Serial.println();
delay(1000);
String data = "Hey! Temperature is " + String(ENV.readTemperature()) + " C, " + " Humidity is " + String(ENV.readHumidity()) +

ข้อมูล Frontmatter ดั้งเดิม

title: "Make Your IoT Cloud Kit Send You Updates on Telegram"
description: "Read, monitor and get notified about environmental data using Arduino MKR(s), the Environmental Shield, and MKR Relay Proto Shield."
author: "Arduino_Genuino"
category: "Internet of Things, BT & Wireless"
tags:
  - "environmental sensing"
  - "telegram"
views: 7580
likes: 6
price: 2450
difficulty: "Easy"
components:
  - "1x Arduino MKR IoT Prime Bundle"
tools: []
apps:
  - "1x Telegram"
downloadableFiles:
  - "https://create.arduino.cc/editor/officine-innesto/777a9743-2a8f-47ac-8d51-a924ffec6773/preview"
documentationLinks: []
passwordHash: "2b2e5790b1d6a92448ec95bdec478390600e25d7776860b3ea908b921e8f0f5b"
encryptedPayload: "U2FsdGVkX1+1CwZa1F4Pa8f0Y/WLuhWsUOMaJJeFfXX3mKBufvtJ1rhi040TeHF9gXFGEF//ZJpzv0IGh55hYQ=="
seoDescription: "Learn to send environmental data updates to Telegram using Arduino MKR, IoT Cloud Kit, and Environmental Shield for real-time monitoring."
videoLinks: []
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/make-your-iot-cloud-kit-send-you-updates-on-telegram-6e1ad3_cover.jpg"
lang: "th"