Make Your IoT Cloud Kit Send You Updates on Telegram

Read, monitor and get notified about environmental data using Arduino MKR(s), the Environmental Shield, and MKR Relay Proto Shield.

Components and Supplies

Apps and Online Services

About This Project

In this tutorial, we'll toy around the new IoT Cloud Kit from Arduino, featuring (among LEDs, breadboards, LEDs & wires) a MKR WiFi 1010, the Environmental Shield and the MKR Relay Proto Shield.

We are going to make the data collected by the board accessible through a conversational UI. We'll be using Telegram, so you'll need to set up an account on that chat service (you are going to need a mobile phone number as well).

  • We are going to use Universal Telegram Bot library by Brian Lough together with ArduinoJson library written by Benoît Blanchon.
  • We are going to be notified if a specific sensor (in this example is
    t
    for temperature) goes over
    temp_limit
    , 35.5.
  • We are going to have full control on the built-in LED on the Arduino and on the two RELAYs on the board.

Wow! Let's start toying the boards.

Create a New TelegramBot Using BotFather

Be sure you have installed Telegram on your phone or your laptop, then, in the search bar, look for @botfather.

Search for @botfather.

Talk to him and use the /newbot command to create a new bot. The BotFather will ask you for a name and username, then generate an authorization token for your new bot. The Name of your bot will be displayed in contact details and elsewhere. The Username is a short name, to be used in mentions and telegram.me links. Your bot's username must end in ‘bot’, e.g. ‘tetris_bot’ or ‘TetrisBot’.

Open

Universal TelegramBot > 101 > Echobot.ino
.

Since this code relies on another Wifi Library, we'll have to change the

1#include <WiFi101.h>

to

1#include <WiFiNINA.h>

Or even add this magic piece of code that would make the Arduino IDE call which of the two libraries based on the target board specified.

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

Now, if you updated the WiFi SSID and password, together with a valid bot token you should be replied with your latest message. Let's add the data.

Important! In order to make the BOT talking to you (and just to you!) you need to sort out you

chat_id 
(the number that Telegram assigns to the conversation between you and your BOT)

You can discover this by adding this string to the

HandleNewMessages
function.

1Serial.println(chat_id);

Keep this code for you, and add it in the final code as

Mychat_id
String defined at the beginning of the sketch.

Environmental Data in the Replies

We are going to create a string that will be sent as an answer. We are going to store all the important information in the data string.

1/*
2 MKR ENV Shield - Read Sensors
3 This example reads the sensors on-board the MKR ENV shield
4 and prints them to the Serial Monitor once a second.
5 The circuit:
6 - Arduino MKR board
7 - Arduino MKR ENV Shield attached
8 This example code is in the public domain.
9*/
10
11#include <Arduino_MKRENV.h>
12#include <WiFiSSLClient.h>
13
14#include <UniversalTelegramBot.h>
15#ifdef ARDUINO_SAMD_MKRWIFI1010
16#include <WiFiNINA.h>
17#elif ARDUINO_SAMD_MKR1000
18#include <WiFi101.h>
19#else
20#error unsupported board
21#endif
22
23// Initialize Wifi connection to the router
24char ssid[] = "SSIDName"; // your network SSID (name)
25char password[] = "SSIDKey"; // your network key
26#define BOTtoken "BotToken" // your Bot Token (Get from Botfather)
27
28
29WiFiSSLClient client;
30
31UniversalTelegramBot bot(BOTtoken, client);
32int Bot_mtbs = 1000; //mean time between scan messages
33long Bot_lasttime; //last time messages' scan has been done
34
35void setup() {
36 Serial.begin(115200);
37 while (!Serial);
38 if (!ENV.begin()) {
39 Serial.println("Failed to initialize MKR ENV shield!");
40 while (1);
41 }
42 delay(100);
43 // Attempt to connect to Wifi network:
44 Serial.print("Connecting Wifi: ");
45 Serial.println(ssid);
46 while (WiFi.begin(ssid, password) != WL_CONNECTED) {
47 Serial.print(".");
48 delay(500);
49 }
50 Serial.println("");
51 Serial.println("WiFi connected");
52 Serial.println("IP address: ");
53 IPAddress ip = WiFi.localIP();
54 Serial.println(ip);
55}
56void loop() {
57 Serial.print("Temperature = ");
58 Serial.print(ENV.readTemperature());
59 Serial.println(" °C");
60 Serial.print("Humidity = ");
61 Serial.print(ENV.readHumidity());
62 Serial.println(" %");
63 Serial.print("Pressure = ");
64 Serial.print(ENV.readPressure());
65 Serial.println(" kPa");
66 Serial.print("Lux . = ");
67 Serial.println(ENV.readLux());
68 Serial.print("UVA = ");
69 Serial.println(ENV.readUVA());
70 Serial.print("UVB = ");
71 Serial.println(ENV.readUVB());
72 Serial.print("UV Index = ");
73 Serial.println(ENV.readUVIndex());
74 Serial.println();
75 delay(1000);
76 String data = "Hey! Temperature is " + String(ENV.readTemperature()) + " C, " + " Humidity is " + String(ENV.readHumidity()) + "%";
77 if (millis() > Bot_lasttime + Bot_mtbs) {
78 int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
79 while (numNewMessages) {
80 Serial.println("got response");
81 for (int i = 0; i < numNewMessages; i++) {
82 Serial.println(bot.messages[i].chat_id);
83 bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
84 bot.sendMessage(bot.messages[i].chat_id, data, "");
85 }
86 numNewMessages = bot.getUpdates(bot.last_message_received + 1);
87 }
88 Bot_lasttime = millis();
89 }
90}

Complete Sketch

Adding the Relays

The MKR Relay Proto Shield has two relays on pin 1 and 2. We are going to add them. I have adapted this needs to a very powerful conversational UI example made by Giancarlo Bacchio and Brian Lough for the Universal Telegram Bot Library

Feel free to change

bot_name
to match the name of your real bot! Have fun!

Turning the relays off and on.
Turning the relays off and on.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.