close

Raspberry Pi

KnowledgeBaseRaspberry Pi

Raspberry Pi Live Weather Forecast With TFT LCD Display

20190523_134147

Overview

Accurate weather predictions are important for planning our day-to-day activities. For example, farmers need information to help them plan for the planting and harvesting of their crops. For us, it can help us prepare the things we bring when we go out, such as an umbrella if we know it’s going to rain. Weather forecasting helps us to make more informed daily decisions, and may even help keep us out of danger.

This project aims to grab information from an online weather forecast website and display the necessary information on a TFT LCD screen for users to view easily.

The application is not limited to just a weather forecast display. It can grab information from any website, such as stock market prices or even your YouTube video’s view count. Following this guide should give you a general idea on how to make it work.


Hardware Used

Software Used

Libraries Used


Application Description

Raspberry Pi Zero W

The Raspberry Pi (RPi) is a series of small single-board computers developed by the Raspberry Pi Foundation to promote teaching of basic computer science in schools and in developing countries. The RPi has been widely used to make things like cameras, gaming machines, robots, web servers and media centers.

Pin Mapping

How does it work?

The RPi works similar to an Arduino in terms of how it operates, but there are several differences between the two. All Raspberry Pi boards use Python as the main language, but also support Ruby and C, while the Arduino only supports C. Moreover, the RPi is more expensive and much more complex for beginners looking to get into electronics projects. However, it can handle multiple programs at a time due to its higher processing power.

2.2-inch TFT LCD Display ILI9341 (240×320)

TFT LCD is short for thin-film-transistor liquid-crystal display. It is a variant of a LCD that uses thin-film transistor technology to improve image qualities such as addressability and contrast. These are commonly used in appliances including computer monitors, mobile phones, handheld devices, video game systems, navigation systems, projectors, and television.

How does it work?

All the pixels on a TFT LCD screen are configured in a row-and-column format, and each pixel is attached to a very tiny silicon transistor that rests directly on the glass panel. This setup allows each pixel to be given a charge and for the charge to be kept even when the screen is refreshed to produce a new image.


Hardware Setup

  • SDO/MISO connected to RPi MISO board pin 21
  • LED connected to RPi 5V with a 47Ω resistor in series
  • SCK connected to RPi SCLK board pin 23
  • SDI/MOSI connected to RPi MOSI board pin 19
  • DC/RS connected to RPi board pin 12
  • RESET connected to RPi board pin 16
  • CS connected to RPi CE0_N board pin 24
  • VCC and GND connected to RPi 5V and Ground respectively

Raspberry Pi Setup

Before proceeding, you need to be able to connect to your Raspberry Pi using the VNC Viewer. Click here for the official documentation on how to do it made by the company themselves.

Enabling SPI

To allow the TFT LCD to work, we have to enable SPI through the Raspberry Pi Configurations. Assuming you’ve already connected to your Raspberry Pi, click on the raspberry icon on the bottom left, hover over Preferences, and click on “Raspberry Pi Configuration.”

When the window pops up, click on “Interfaces” and ensure that SPI is enabled. Once done, click “OK.”

Installing Required Libraries

Next, we have to install the requests and Adafruit_Python_ILI9341 library so make sure your Raspberry Pi is connected to the Internet. Open the Terminal by clicking its icon on the bottom left and simply enter the command pip install requests to install the requests library. Once that is done, enter the command sudo apt-get install build-essential python-dev python-smbus python-imaging python-numpy then the command sudo python setup.py install to install the Adafruit_Python_ILI9341 library.


ThingHTTP Setup

To fetch data, we must request an API. Assuming that you’ve already installed the requests library as instructed above, the next step is to make APIs for a particular data from a website. We’ll be using ThingSpeak’s ThingHTTP app so you have to Sign up on their website https://thingspeak.com. If you already have an account, login and click on “Apps” and then “ThingHTTP.”

In doing so, you should be directed to your ThingHTTP page. Click on “New ThingHTTP” to create one.

Once loaded, you will be presented with the its settings. Change the name for easier identification and put the URL of the website you’re trying to fetch data from. Keep the rest default.

The next step requires you to go to the website you want to get the data from, hover over the specific data you want to fetch, and select “Inspect.”

Right click the data from the window pop-up on the right side, hover over “Copy” and then select “Copy XPath.”

Once you’ve copied the XPath, go back to the ThingHTTP settings and paste it on the Parse String box and hit save.

Once saved, you can get the API link on the right side of the page. You will need that later in the code.

Repeat the process for the other data you need, such as the temperature, precipitation, humidity, etc.


Code

For the code, you can do it on your computer via Python IDLE, Notepad++, or any text editing software or IDE that supports Python then transfer it to the Raspberry Pi through the VNC Viewer. You can also code inside the RPi itself, but I find this to be easier.

To run the code, open the terminal and change the directory to where the file is located using the command cd <directory> then run the command python <file name>.py.


Code Breakdown

Library Import

Before starting, we must include the libraries to be used in the project. The Adafruit_ILI9341 library will allow the TFT LCD to work and it is dependent on the PIL (Python Imaging Library) to function. The RPi.GPIO library provides a class to control the GPIO on the Raspberry Pi and the GPIO.SPI library allows the SPI protocol that the TFT LCD requires. The requests library allows us to fetch data from any website. The datetime library is for showing the current time and the time.sleep library is for the delay.

Pre-Initialization

We then assign the DC and RESET pins to the RPi board pin 18 and 23 respectively. The SPI port and device should be 0 by default. After that, we create the TFT LCD display and initialize the display. Two font settings have been created to be used later to display our text on the TFT LCD. Since the default font of the library is only 8 pixels which is very small, I downloaded the Highest font style from https://www.dafont.com/ and placed the .tff file in the same directory as the code. You can use the default font if you like by removing the comment from the code, or download a different font from the same website aforementioned. The font sizes can be specified in the second parameter of ImageFont.truetype().

Draw Rotated Text Function

This function is from the example of the Adafruit_Python_ILI9341 library. Since PIL doesn’t have good native support for rotated fonts, this function was created to cover that. The specific details of how each line contributes is commented on the code.

Display Loop

This is where the data from the website is being fetched via API requests. Make sure the URL inside requests.get() is the data you want to fetch, and store that in a variable. To get the current date and time, we can use datetime.datetime.now().

Before drawing the words, first we have to clear the display to refresh its content. The draw_rotated_text() function is called to draw the words and data onto the TFT LCD screen. Just follow the format given and change the coordinates or position relative to the TFT LCD screen. I have set it to be 30-40 pixels apart. You can even change the rotation, font, and color of the text. For the time display format, we have to use time.strftime to get the string representing date and time. %b is for the month, %d is for the day, %H is for the hour, %M is for the minute, and %S is for the seconds. After the words are drawn, we then display it with disp.display() and set a delay so it does not spam.


Conclusion

This project is really a great way to learn about fetching data from a given website. This can be used in a wide range of applications. Since it can grab information from any website, such as stock market prices or even your YouTube video’s view count, people can use this to create live information boards without having to give public access to the whole website.


References

read more
KnowledgeBaseRaspberry Pi

LED Control With APDS-9960 Gesture Sensor Using Raspberry Pi Zero W

20190518_163227

Overview

This is a simple application of the APDS-9960 Gesture Sensor on the Raspberry Pi Zero. Throughout the tutorial, you’ll learn about the basics of installing libraries on the Raspberry Pi as well as using them to aid you in the code. In this tutorial, we will use Python programming language to program our Raspberry Pi. Hence, for people migrating from Arduino, learning LED control is the best way to get started on Raspberry Pi projects and become more familiar with Python.


Hardware Used

Software Used

Libraries Used

  • time

Application Description

Raspberry Pi Zero W

The Raspberry Pi (RPi) is a series of small single-board computers developed by Raspberry Pi Foundation to promote teaching of basic computer science in schools and in developing countries. The RPi has been widely used to make devices like cameras, gaming machines, robots, web servers and media centers.

Pin Mapping

How does it work?

The RPi works similar to an Arduino in terms of how it operates, but there are several differences between the two. All Raspberry Pi boards support Python as the main language, but also support Ruby, C/C++, Javascript, while the Arduino only supports C/C++. Moreover, the RPi is more expensive and much more complex for beginners looking to get into electronics projects. However, it can handle multiple programs at a time due to its higher processing power.

APDS-9960 Gesture Sensor

The APDS-9960 Gesture Sensor module is not only capable of touchless gesture sensing, but is also of advanced proximity detection, ambient light sensing, and RGB color sensing. Four sensors in such a slim modular package! For this project however, we’ll only be focusing on the gesture sensing. You can check out how to use its proximity sensing feature to turn LEDs on by clicking here.

How does it work?

It utilizes four directional photodiodes to sense reflected IR energy to convert physical motion information (i.e. velocity, direction and distance) to a digital information. Learn more from its datasheet.


Hardware Setup

APDS-9960 Gesture Sensor Connections

  • SDA connected to RPi board pin 3
  • SCL connected to RPi board pin 5
  • INT connected to RPi board pin 7
  • VCC and GND connected to 5V and Ground respectively

Fun Fact: SCL (Serial Clock) and SDA (Serial Data) pins are the dedicated pins for I2C communication. On the Raspberry Pi Zero they are found on board pins 3 and 5.

LED Connections

  • 1st LED connected to RPi board pin 8
  • 2nd LED connected to RPi board pin 10
  • 3rd LED connected to RPi board pin 12
  • 4th LED connected to RPi board pin 16

Raspberry Pi Setup

Before proceeding, you need to be able to connect to your Raspberry Pi using the VNC Viewer. Click here for the official documentation on how to do it made by the company themselves.

Enabling I2C

To allow the Gesture Sensor to work, we have to enable I2C through the Raspberry Pi Configurations. This also allows us to use the smbus library. Assuming you’ve already connected to your Raspberry Pi, click on the raspberry icon on the bottom left, hover over Preferences, and click on “Raspberry Pi Configuration.”

When the window pops up, click on “Interfaces” and ensure that I2C is enabled. Once done, click “OK.”

Installing Required Libraries

Next, we have to install the Python-APDS9960 library so make sure your Raspberry Pi is connected to the Internet. Open the Terminal by clicking its icon on the bottom left and simply enter the command pip install apds9960.


Code

For the code, you can do it on your computer via Python IDLE, Notepad++, or any text editing software or IDE that supports Python then transfer it to the Raspberry Pi through the VNC Viewer. You can also code inside the RPi itself, but I find this to be easier.

To run the code, open the terminal and change the directory to where the file is located using the command cd <directory> then run the command python <file name>.py.


Code Breakdown

Library Import

First things first, we must include the libraries to be used in the project. The APDS9960 library will allow the Gesture Sensor to work. The RPi.GPIO library provides a class to control the GPIO on the Raspberry Pi and the smbus library allows the I2C protocol that the Gesture Sensor requires.

Channel & Pin Setup

Next, we set the SMBus port to 1, which is the default, and then set the APDS9960 Gesture Sensor to that bus. After that, we set the GPIO pin mode to follow the board numbers for easier identification, set the pin 7 as the input coming from the Gesture Sensor, and set pins 8, 10, 12, and 16 as the output for the 4 LEDs. states 1-4 are initially set to false to indicate that the LEDs will be initially in an off state.

Dictionary for Print

This part is a dictionary where each gesture detected by the Gesture Sensor, defined by APDS9960_DIR_<gesture> in the Python-APDS9960 library is equated to shorter, easier to understand words to be printed later on.

Gesture Detection Loop

GPIO.add_event_detect(7, GPIO.FALLING) adds an event that will trigger when a falling edge is detected; in other words, when a gesture is detected on the Gesture Sensor it will sent that to pin 7. We then set its proximity threshold and enable it. An infinite while loop is then created to simulate a loop that detects for a gesture continuously and once it does, it enters the if statement. Inside the if statement, motion = apds.readGesture() reads the detected gesture and stores it in a variable. Next, it will print what the motion was using the dictionary format made above and turn LEDs on or off depending on the motion.


Conclusion

LED controlling is often the first thing people try to become more familiar with the microcontroller they are using. For this specific case, a simple sensor was integrated along with the LED controlling which kicks it up a notch. Initially, migrating from C to Python will be confusing, despite the fact that Python is much easier to use according to many people. It just requires time and experience to get used to.


References

read more

Warning: Cannot assign an empty string to a string offset in /home/createl1/public_html/community/wp-includes/class.wp-scripts.php on line 454

Warning: Cannot assign an empty string to a string offset in /home/createl1/public_html/community/wp-includes/class.wp-scripts.php on line 454