📘Learn Digital Making weekly on Telegram. Join Channel Now📱
Send Meal Selection and Locations through Telegram Bot Using ESP8266 Grove WiFi Module on micro:bit

Send Meal Selection and Locations through Telegram Bot Using ESP8266 Grove WiFi Module on micro:bit

Having a hard time deciding what to eat during lunch or dinner time? Having difficulties searching for the right place to get your meals done?

This guide will help you on setting up a micro:bit device paired with Grove WiFi Module 8266 that helps you to decide on what to eat and where to eat by sending the choice of food and locations through your Telegram Bot automatically during lunch and dinner time.

 

Hardware and Software Preparation

Hardware:

  1. REKA:BIT (incl. micro:bit) x1 or EDU:BIT (incl. micro:bit) x1
  2. Grove WiFi 8266 x1

Software:

  1. MakeCode editor for micro:bit
  2. Telegram

 

Before You Start

  1. Set up your micro:bit device with the Grove WiFi module and add the MakeCode extension for Grove WiFi 8266 in your MakeCode Editor.
    • Follow the steps here to get started with the Grove WiFi 8266 module.
  2. Create a new Telegram Bot if you don’t have one.
    • Follow the guide here to create a new Telegram Bot and get the API key and chat ID of the chat.

 

What Does the Code Do?

  1. Able to connect to WiFi.
  2. Able to get the information on internet time according to timezone.
  3. Have an array that stores a list of food.
  4. Have an array that stores a list of locations.
  5. Able to select one food choice randomly.
  6. Able to select the location according to the choice of food.
  7. Able to send the choice of food to the user through Telegram Bot during lunch and dinner time.

 

Let’s Start Coding

Step 1: WiFi Module Initialization and Connection

First, initialize the WiFi Module and connect your micro:bit device to the WiFi. 

Follow the steps here to initialize the WiFi Module and connect to your WiFi.

 

Step 2: Internet Time Initialization and Update

1. Select [initialize internet time at timezone _ ] block under “Internet Time” category and snap it to the [on start] block after the WiFi initialization and connection.

2. Insert the timezone, for example “8” for UTC+8.

3. You may use the [internet time initialized] and [if-else] blocks to verify whether the internet time is successfully initialized – show “Tick” if successful, “Cross” if not.

4. Select [update internet time] block and snap it to the [forever] block.

5. You may use the [internet time updated] and [if-else] blocks to verify whether the internet time is successfully updated – show “smiley face” if successful, “sad face” if not.

 

Step 3: Creates Food and Locations’ Arrays

An array is a list or collection of related variables used to store information. We will be creating multiple arrays to store information about food and location of the food.

1. Make three new variables from the “Variables” category and name it just as listed:

  • Choice
  • Food
  • Location

2. Select [set “text list” to array of _ _ _ (+) (-) ] block from “Arrays” category and snap it to the [forever] block.

3. Change the variable of “test list” to “Food” and insert the name of food into the array. For example, “Nasi Lemak”, “Economic Rice”, “Chicken Rice”, “Hokkien Mee”, “Maggi Goreng” and “Wan Tan Mee”. 

Tips: Click the (+) icon to add the number of elements in the array or (-) icon to reduce the number of elements in the array.

4. Select another [set “text list” to array of _ _ _ (+) (-) ] block and change the variable from  “text list” to “Location”.

5. Go to your web browser search engine, search and select “Google Maps”. At the Google Maps site, search for the location of the food, for example “nasi lemak near me”.

6. Copy the URL on top of the web browser (including https://www.) and paste it into the array named “Location”.

Note: If the web browser didn’t show “https://www.” double-click the URL and you will see the full link of the web page.

Note: Remember to put the locations’ URL in the array with the same index number to “Food”.

 

Step 4: Sets the Conditions for Lunch and Dinner Time

1. Select [if-else] block under “Logic” category and snap it to the [forever] block.

2. Select the Boolean, [ _ and _ ] block under “Logic” category and snap to the [if-else] block to replace the “true”.

3. Select the Comparison, [ _ = _ ] block under “Logic” category and snap to the [ _ and _ ] block.

4. Select the [hour] block under the “Internet Time” category and snap it to the [ _ = _ ] block.

5. Fill in the lunch time in terms of “hour” on the other side of the [ _ = _ ] block. For example “12” if your lunch time is 12 p.m., to get [ “hour” = “12”].

6. Repeat 3 to 5 and select the [minute] block and fill in the lunch time in terms of “minute”. For example “0”, if your lunch time is 12:00 p.m., to get [ “minute” = “0” ].

7. Click the (+) icon of the [if-else] block to get “else if _ then”. Then, click the (-) icon to remove the “else”.

8. Repeat 2 to 6 and change the “hour” and “minute” to your dinner time. For example, 7.00p.m., then you should insert [ “hour” = “19”] and [ “minute” = “0”].

 

Step 5: Picks a Food Randomly from the Array

1. Select [set _ to _ ] block from “Variable” category and snap it to the [if-else] block, under condition of [ “hour” = “12”] and [ “minute” = “0” ].

2. Change the variable to “Choice”.

3. Select [pick random _ to _ ] block from “Math” category and snap it to the [set “Choice” to _ ] block.

4. Fill in “0” for the initial value and select [length of array _ ] block from “Arrays” category and snap it to the [pick random “0” to _ ] block. This is to let the micro:bit pick a random index number within the array based on the amount of food or location that you have filled in.

5. Change the variable in the [length of array _ ] block to “Food”.

6. Repeat 4 and 5 and snap the [set _ to _ ] under the condition of [ “hour” = “19”] and [ “minute” = “0” ] or u may duplicate the [set “Choice” to “pick random 0 to length of array ‘Food’” ] and snap to the [if_else] block.

 

Step 6: Telegram Bot Sends Choice of Food and the Location

1. Select [send message to Telegram: API Key _ Chat ID _ Message _ ] block from “Telegram” category and snap it to the [if-else] block, under condition of [ “hour” = “12”] and [ “minute” = “0” ].

2. Fill in the API key of your Telegram Bot and the chat ID.

You may follow the steps here to get the API key of your Telegram Bot and the chat ID.

3. Select [ _ get value at _ ] block from “Arrays” category and snap it to the “Message” of [send message to Telegram: API Key _ Chat ID _ Message _ ] block.

4. Change the variable to “Food”.

5. Select [Choice] from the “Variables” category and snap it to  [ “Food” get value at _ ] block.

6. Repeat 1 to 5 and change the variable “Food” to “Location”.

7. You may select multiple [send message to Telegram: API Key _ Chat ID _ Message _ ] blocks to send multiple messages of :

  • “It’s lunch time!”
  • “Today’s meal is…”
  • “Choice of Food”
  • “Location of the Choice of Food”

8. Select [pause (ms) _ ] block from “Basic ” category and fill in “60000”. This will prevent the micro:bit from sending you those messages continuously from 12:00p.m. to 12:01p.m.

Tips: 60000 millisecond = 60 second = 1 minute

9. Repeat 1 to 8 and snap those [send message to Telegram: API Key _ Chat ID _ Message _ ] blocks under the condition of [ “hour” = “19”] and [ “minute” = “0” ]. Remember to change the first message to “It’s dinner time!”.

10. Download the code to your micro:bit and if the WiFi connection is successful, you should receive the message during your lunch time and dinner time.

Tips: Turn on the Location feature in your smartphone to find out about the food around your place.

 

Full Code

picture 25

Hope you learned how to use Grove WiFi 8266 module with the micro:bit and MakeCode. Thank you.

You are encouraged to further improve the code for higher efficiency and more advanced applications.

Related Products


Related Posts

Get started with Grove WiFi 8266 on the micro:bit

Get started with Grove WiFi 8266 on the micro:bit

This post shows you the tutorial on how to get started with your Grove WiFi 8266 module on the BBC micro:bit board.  ....