logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Raspberry Pi

Using Node-RED and Raspberry Pi to Tweet the Weather With a Touch of a Button

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at August 27, 2020
Categories
  • Raspberry Pi
Tags

Node-RED is a visual programming tool that can interface components on your Raspberry Pi’s GPIO pins with various websites and online services. It’s produced by IBM, released on an open-source license, and requires very little written code.

To show you how this works, let’s use Node-RED to tell you the temperature in Timbuktu by pressing a button.

A Raspberry Pi with power supply raspberry pi
A breadboard breadboard
A push-button push button

You’ll also need some patch cables or wires, and a phone with either Android or Apple OS.

How Node-RED Works

If you click on Node-RED in the menu without knowing what it’s about, all you’ll see is a terminal window open and display some status messages. Not very exciting, hey?

What it’s doing is starting a Node-RED server. You can access this with a web browser on your local machine or another machine on your local network.

Node-RED is built on a server technology called Node.js. This is a low code programming environment for the Internet of Things.

It provides a graphical environment that lets you connect different nodes corresponding to various elements of your project. Those elements can include the components on your GPIO pins, or services hosted on the internet.

Installing Node-RED on Your Raspberry Pi

Node-RED is pre-installed on some installations of Raspberry Pi OS. Have a look in the menu on your desktop and see if it’s there.

If it isn’t, you will need to install it. Your Raspberry Pi will need internet connectivity.

Your operating system will need to be no older than Raspbian Jesse. This will only matter if the operating system on your Raspberry Pi was installed before 2016.

These commands are long, so it will be easier to copy and paste them. You can do that over an SSH connection or by opening a terminal window in the Raspberry Pi desktop environment.

If you need to, you can launch the desktop by typing:

startx

Before, let’s make sure our operating system is up to date. Open a terminal window and type:

sudo apt update && sudo apt upgrade -y

Now we can install Node-RED using the handy script they provide for us. To run it, type:

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
When you run this script, it might ask you if you want to upgrade your old version of node.js, and whether you want to install the Pi-specific nodes. Answer ‘y’ to both.

Node-RED is now installed and ready to launch.

Launching Node-RED and Accessing it in Your Browser

You can launch Node-RED from the desktop menu; it’s located under “programming”.
If you prefer, you can run the same thing from the command line, by typing:
node-red-pi –max-old-space-size=256

Now that it’s running, you can access it on port 1880 using a web browser on your local network. If you are using the browser on your Raspberry Pi, you can do that by typing http://127.0.0.1:1880 into the address bar.
If you are accessing your Raspberry Pi remotely, replace it with the IP address that you use to log in to your SSH session. For example, on my local network, my Raspberry Pi is at 192.168.0.8, so I can direct my web browser to Node-RED by typing http://192.168.0.8:1880 into the address bar.
Finally, you’ll start to see what this is all about.

The column on the left contains a selection of nodes, which we can move into the workspace to construct flows that connect our device to online services.

If This, Then That

Ok, so how do we use Node-RED to display a notification on our phone?
We can send an http request to a tool called If This Then That, or IFTTT. This is useful for connecting apps and devices together, including DIY projects that we build with Node-RED and Raspberry Pi.
If you don’t already have IFTTT, install it from the app store or play store and create an account. It’s free.
Now switch back to your Raspberry Pi or to the computer you’ve been using to access Node-RED. Leave the Node-RED tab open, but open a new one to browse to https://ifttt.com/ and log in to your account. Click Create. You will see this screen:

The “this” field requires a trigger, the event that it responds to. The “that” field requires an action, the thing it will do.
The trigger we’ll use is an http request sent by Node-RED. IFTTT calls these “webhooks”, so type that into the search bar and select it. When it asks you to name the event, type in weather_button and click “Create trigger”.

Now let’s do the action. We want it to be a notification, so search for “notifications” and select it. An ordinary notification is fine. When it asks for the message text, type:
The temperature in Timbuktu is {{Value1}} degrees

Now let’s test that this notification appears on your phone.
Click on the profile picture in the top right corner to open the menu, and then click “My Services”. Browse to the bottom and select “Webhooks”, then click the link that says “Documentation” in the top right.
You’ll see a handy little form we can use to test what we’ve built. Remember, we called this event weather_button, so type that into the web address field, then type the number 42 into the “value1” field and click “test it”.
Within a few seconds, you should get a notification on your phone telling you that it’s 42 degrees in Timbuktu. If that doesn’t work, check that the IFTTT app on your phone is properly installed and has permission to send notifications, and then try again.
Don’t close this browser tab; we’ll want something from it soon.

Connecting Node-RED to IFTTT

On this documentation page, you will see instructions explaining that you can trigger this event with a POST or GET request to a particular URL. POST and GET are both kinds of http requests which we can send to IFTTT from Node-RED.
So switch your Node drag to an http request node onto the workspace. We need to configure this node, so double click it and a window will open up. For the method, select a POST request; for the URL, copy over the URL from the IFTTT documentation tab.
To test that this works, we’ll need another node to send a message containing a temperature value. Drag an inject node on to the workspace to the left of the http request and then double click on it to configure it. Where it says timestamp, change it to JSON and enter in this code:
{“value1”:”16”}

Click Done then connect the right done of the inject node to the left dot of the http request node. Click Deploy and then click Confirm deploy.

Then press the left button on the inject node to test that this works. Have you got a notification on your phone telling you that it’s 16 degrees in Timbuktu?
If that’s all working, you can close the IFTTT tabs. That bit’s done now.

Fetching Real Weather Information

Ok, so we’ve got the notifications working, let’s read in the real temperature.
First, we need to disconnect the inject node from the http request node, so click on the line connecting these nodes and press the delete key.
Scroll down through the list of nodes until you reach the weather section. Now drag a weather node on to the workspace and double click…
Hang on a second! What weather node? What weather section? There isn’t one, is there?
That’s okay. Node-RED makes it quick and easy to install this node and a zillion more.
Click the menu bar in the top right corner and then select Manage Palette. Click on install, and then type openweathermap into the search bar. Click the install button on node-red-node-openweathermap and then click install on the dialog that appears.
Now we have weather nodes. So drag one over into your workspace and double click it to configure it. Type Timbuktu in the city field, and ML for Mali as the country.

We need to get an API key now. Browse to https://openweathermap.org/ and sign up for a free account. After you’ve confirmed your email address, click the link that says API Keys and copy yours. Switch back to your Node-RED tab and paste it into the API Key field.
When this weather node is activated, it will fetch as a variable called tempc, but IFTTT is looking for a variable called value1. We can use a function node to assign that value.
So drag a function node in between the weather and http request nodes, and then double click on it to enter this line of code:
msg.payload.value1 = msg.payload.tempc

Now connect the inject node to the weather node, the weather node to the function node, and the function node to the http request node and deploy your flow again.

Press the button on the inject node to test that this works. It may take a few seconds, but you should be getting an IFTTT notification with the actual temperature in Timbuktu.

Bringing in the GPIO pins

This works with the click of a mouse, let’s make it work with the touch of a button. We won’t need that inject node anymore, so double click on it and then press delete.
Now let’s wire up the breadboard. First, connect the 3.3 volt power pin to the positive power rail running along the bottom of the board.

Next, place the button in the middle of the board. Connect one pin to GPIO pin 13, and the diagonally opposite corner to the positive power rail.

That’s everything connected! Time to configure it in Node-RED. Scroll down the list of nodes until you see rpio gpio in. Drag that on to your workspace. Now double-click the node to configure it and select GPIO13.
We’ve wired our button so that when it’s pressed, it closes a circuit to the 3.3v power rail, so we want a pulldown resistor on the pin to ensure that it reads 0 when the circuit is open. To do that, select pulldown from the resistor menu.

Now connect the gpio node to the weather node and deploy again.

Now press the button on the breadboard. If you’ve wired everything, you should receive a notification on your phone.

Why Is It Sending Duplicates?

You might notice that pressing the button once actually results in two notifications arriving almost simultaneously.
With the gpio node connected directly to the weather node, it triggers every time the logic level changes. That’s one change when the button is pressed, and another when it’s released.
We need another node in between these nodes to send only one of these messages through. We can route messages to different nodes by using a switch node.
First, delete the connection between the gpio node and the weather node, then place a switch node in between them. Double click it to configure it, and write the number 1 in the text field next to where it says ‘==’.

Click Done, then connect it to the gpio node and the weather node, then deploy again.

Now when you press the button once, you should get one notification.

Clearly, This is a Very Artificial Exercise

It would be much easier to get the weather for Timbuktu – or any other city – by installing a weather app on your phone.
Still, the principles we’ve explored here for all sorts of useful things. For example, you could configure a device to provide real time updates to a website or an application.
You could also connect things the other way and use information from online services to control your device, perhaps by using weather reports and forecasts to automate a home or garden.

Node-RED is A Fast Way to Glue Things Together

Node-RED is often discussed as a tool that introduces the IoT to people with no programming background. But even though it requires very little written code, you still need to grasp the same web and embedded systems concepts as you would using a written language.
Just in this introductory project, we touched on http requests, APIs, JSON data, logic levels, pullup and pulldown resistors. We also had to write a very short function.
You hardly need a degree to be across all of that. But if you’ve never done any of it before, it would really be much better and easier to get started in a more traditional environment like Python.
If you’re already over that initial hurdle, Node-RED is great. You can put things together very quickly and see how they’re connected at a glance. It’s a brilliant tool for fast prototyping and to coordinate different devices.

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • How to Make a VNC Server on Raspberry PiHow to Make a VNC Server on Raspberry Pi
  • Accessing Raspberry Pi From a Smartphone Part 1 – Implementing VNCAccessing Raspberry Pi From a Smartphone Part 1 – Implementing VNC
  • What You Need to Know About Wireless PowerWhat You Need to Know About Wireless Power
  • Beginner’s Guide to Raspbian Jessie with PIXELBeginner’s Guide to Raspbian Jessie with PIXEL
  • How to Use a Raspberry Pi as a DNS ServerHow to Use a Raspberry Pi as a DNS Server
  • How to Use a Raspberry Pi as a Web ServerHow to Use a Raspberry Pi as a Web Server
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort