Display a message on the M5StickC screen when tweet
2022-01-25 | By M5Stack
License: General Public License Arduino
* Thanks to the project info and source code provided by @obniz developer team
When a particular user tweets on Twitter, you will be notified on the M5StickC display.
Materials
- M5StickC / M5StickC PLUS
- smartPhone or PC
- power supply for M5SticKC
- USB-Type-C Cable
You can install obniz OS on your M5StickC. → obnizOS installation
※It doesn’t matter if you have no power supply and no cable because M5StickC has its own battery. However, you should use the power supply if you want to run the program for a long time.
How to make
Hardware connection
Prepare an M5StickC obnizOS installed. No parts are needed.
Software
The program detects tweets through IFTTT. IFTTT is a web service that makes it easy to connect various web services and devices.
Setting up an obniz Event
Set up the obniz Event so that the program will be run when IFTTT calls the Webhook URL. Learn more about obniz Events here.
First, go to the official obniz website and enter the “Developer’s Console“.
Next, click on “Create New” in the left menu, paste and save the program.
You can name the file as you like. In this case, I tried it as a Tweet notification.
Next, click on Create New in the “Serverless Event” field to create an event. Please refer to the following for details of the settings
Name (any) Webapp to run Select the name of the program saved in the repository. Trigger Webhook
Click “Create“.
Make a recipe with IFTTT
Make a recipe with IFTTT to call Webhook URL when a particular user tweets on Twitter.
First, you need to access IFTTTT and log in. If you are using IFTTTT for the first time, you will be asked to register as a member or log in through Google or Facebook.
If you can log in to IFTTT, click “create.” And click “This” part (we call it the trigger) of the “If This Then That.”
Type “twitter” in the search bar and select Twitter. Then select “New tweet by a specific user“.
Input the username you want to notify on “Username to watch.” (Please remove the @ mark.)
Next, click on the part of “That” (we call it the action).
Type “webhook” in the search bar and select Webhooks. Then, select “Make a web request“.
Set “URL” to the URL you have saved, “Method” to POST, “Content Type” to application/json, and “body” to {"tweet":true, "content":"{{Text}}"} and click “Create action“.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
<script
src="https://unpkg.com/[email protected]/obniz.js"
crossorigin="anonymous"
></script>
</head>
<body>
<script>
let obniz = new Obniz.M5StickC("OBNIZ_ID_HERE");
obniz.onconnect = async () => {
if (typeof req === "object") {
if (req.body.tweet) {
console.log("New tweet");
console.log(req.body);
obniz.display.clear();
obniz.display.print("New tweet!");
obniz.display.print("content:");
obniz.display.print(req.body.content);
await obniz.wait(5000);
}
} else {
console.log("failed");
}
if (typeof done === "function") {
done();
}
};
</script>
</body>
</html>
If a particular user tweets on Twitter, the notification of “New tweet!” and contents of the tweet will be displayed on M5StickC.
In this article, we used the body to get the content of the tweet (Text), you can also get other information such as the user name (UserName). Try changing the notification text and content to make the notification screen whatever you want it to be.
Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
<script
src="https://unpkg.com/[email protected]/obniz.js"
crossorigin="anonymous"
></script>
</head>
<body>
<script>
let obniz = new Obniz.M5StickC("OBNIZ_ID_HERE");
obniz.onconnect = async () => {
if (typeof req === "object") {
if (req.body.tweet) {
console.log("New tweet");
console.log(req.body);
obniz.display.clear();
obniz.display.print("New tweet!");
obniz.display.print("content:");
obniz.display.print(req.body.content);
await obniz.wait(5000);
}
} else {
console.log("failed");
}
if (typeof done === "function") {
done();
}
};
</script>
</body>
</html>