###### tags: `TA Stuff 2021` `Pycom` `ESP32`
# Adafruit IO
This tutorial covers how to work with Adafruit IO, the IoT platform made by Adafruit. You will learn how to setup so you can start sending data over Wi-Fi, and then work with triggers and send messages to Discord.
## Introduction
Follow this tutorial to get set up with Adafruit IO: [**Internet of Things with Pycom and Adafruit IO - Zero to Hero**](https://core-electronics.com.au/tutorials/pycom/internet-of-things-with-pycom-and-adafruit-io.html). In short, you will find the following steps in the Core Electronic basic tutorial:
* The requirements: A PyCom Board (WiPy 3.0, SiPy, LoPy, GPy or FiPy), a PyCom Expansion board (Expansion board 3.0, 3.1, PySense or PyTrack), a Micro-B USB cable, and home WiFi to use for communication between your board and Adafruit server.
* The basic PyCom board setup including; Driver setup, firmware upgrade, Atom IDE installation, and board communication with Atom.
* Then it shows how to sign up for [**Adafruit IO**](https://io.adafruit.com/) to send and receive messages with MQTT protocol.
Please read the two following sections [**Adafruit IO terms**](#Adafruit-IO-terms) & [**Core-Electronic project notes**](#Core-Electronic-project-notes) first then follow Core-Electronics step by step to set up and communicate to Adafruit IO and finally continue with [**Triggers in Adafruit and sending messages to the Discord server**](#Triggers-in-Adafruit-and-sending-messages-to-the-Discord-server).
<br/>
## Adafruit IO terms
Each IoT cloud platform uses its terminology for different actions and It would be valuable to know them before starting working with them. So before signing up and using Adafruit IO read the following terms, we gathered from Adafruit documentation.
**Feed:** Feeds are the core of the Adafruit IO system. The feed contains the sensor data values that get pushed to Adafruit IO from your device, like temperature’s value. It also could be the value you need to send from your control panel to the device to control it, like turning on a lamp.
**Blocks:** Blocks are widgets that you can use to visualize feeds. It could be a gauge, a table, or a line chart to visualize data pushed from your device to the feed. Or it could be a toggle switch or a slider to send your commands from the Adafruit control panel to your device.
**Dashboard:** Dashboards allow you to visualize data and control Adafruit IO-connected projects from any modern web browser. Before being able to add any block to visualize your data you need a dashboard, and basically, it is a place to hold several blocks for each project.
**Trigger:** Triggers will initiate an action based on a condition. There are two types of triggers in Adagruit IO:
* **Reactive Trigger:** *It will be activated by a condition met from a feed value, like if the temperature reached a certain threshold send and alarm.* **(This is what we learn in this tutorial)**
* **Schedule Trigger:** *It will periodically trigger a command and work as a scheduler, like send an open/close valve command to our board.*
Adafruit IO supports the following two trigger’s reactions:
* **Send a Webhook message to:** *It can send a webhook message to a platform like sending a message to your Discord.* **(This is what we learn in this tutorial)**
* **Publish a message to:** *It could also change the state of a feed value so your PyCom board can subscribe to the MQTT and read it.*
<br/>
## Core-Electronic project notes
Core-Electronic uses two feeds:
* `randoms`: A random value generated from the PyCom board (between 0 - 100) and pushed to Adafuit to show it in a line chart. It is a temperature simulation and you can use the same technique to show temperature value.
* `lights`: this feed controls the PyCom board LED and you can turn on/off it from the Adafruit control panel. It is useful when you need to control something remotely.
Core-Electronic uses two blocks:
* **Line Chart:** As mentioned above they use a line chart to connect it to the “randoms” feed to show pushed value from your device.
* **Toggle Switch:** This switch is used to send “ON/OFF” commands to the “lights” feed then your PyCom board can subscribe to the feed and base of the state of “lights” feed turn on/off your board LED.
**NOTE:** `umqtt.py` link does not work in Core-Electronics’s tutorial but you can access it [here](https://github.com/pycom/pycom-libraries/blob/master/GoogleIOT/flash/umqtt.py) from the PyCom library.
<br/><br/>
## Triggers in Adafruit and sending messages to the Discord server
In this simple tutorial, we will activate the webhook link in our Discord channel, and based on the `randoms` values pushed from the PyCom board if the value is above `80` we forward the value to the Discord. Following we first create a webhook in the Discord and then send triggered value from Adafruit IO to it.
### Creating a server and activating webhook on a channel in Discord
**Step 1:** Open Discord and from the left pane click on `+` to **Add a Server**.
**Step 2:** From the pop-up window choose **Create My Own**.
**Step 3:** Next window choose **For me and my friends**.
**Step 4:** Give it a name like `LNU Project` and click on **Create**.
**Step 5:** By default you have a `general` channel click on the `gear` sign close to it.

**Step 6:** Click on `Integrations` then on `Create Webhook`.

**Step 7:** Click on `Copy Webhook URL` and save the link somewhere that you need later.

**Step 8:** Finally click on `ESC` on the top right corner to close the window. We are done in Discord.
### Adding a trigger in Adafruit and connect it to your webhook
**Step 1:** Login to your Adafruit IO control panel >> click on `Triggers` >> click on `View all` >> click on `+ New Trigger`

**Step 2:** Do the following steps:
1. Click on `Reactive Trigger`
2. Choose `randoms`
3. Choose `greater than`
4. Enter `80`
5. Choose `send a webhook message`
6. Paste the webhook link you got from Discord
7. choose `randoms`
8. choose `Discord Template`
9. click on `Create`

**Step 3:** Everything is set and you can see a new trigger in the trigger’s list.

### Results
Now you can run your code from Core-Electronics and if your board uploaded a value greater than `80` it will forward it to your Discord.

<br/>
**⚠️Attention⚠️ Don’t bug Discord by sending too many Webhooks, Adafruit will disable it if recognize too many requests.**
<br/>
:::info
**Credit**
Written by Hamed Talebi
Proofread by Erik Karlsson
:::