Raspi-Nextion Weather Clock

Introduction: Raspi-Nextion Weather Clock

Seeing so many different clocks built with arduino and raspberry Pi's inspired me. I wanted to make one too.

I started by using an arduino mega with a ESP8266 but found the 8266 wifi to have too many hiccups. I decided to use a spare Pi I had (Raspberry Pi3 Model B) even though I had never used Python. I found some tutorials for using the arduino code on the Pi, but this all seemed to be bandaids.

The Raspi-Nextion Weather Clock uses the Pi running Raspian connected to a Nextion 5" enhanced screen.thru the serial port with the program written in Python.

I also 3D printed (with the help of some friends) the case.

As soon as the program boots - it gets the date & time from Google

It will also get the weather using thingspeak.com

You will need to signup at https://thingspeak.com/apps and use a ThingHTTP

The screen will change as the weather changes - ie: if its sunny out the screen will have a sunny background, if its snowing, the screen will have a snow filled background

The screen will dim at night so you can sleep - this is adjustable in the program as to what time it dims and undims

When its a holiday, you will see a holiday background and the screen will undim later so you can sleep in...

All backgrounds are editable using the nextion software

make custom screen for birthdays, anniversaries, important dates...

Step 1: Parts List

Here is the list of parts and software I used:

Hardware

1. Raspberry Pi 3 Model B ($50 on Amazon) https://www.amazon.com/CanaKit-Raspberry-Clear-Pow...

1a. You will need a microSD - I had a spare with a usb to microSD adapter

2. Nextion 5" Enhanced Screen ($72 from ITead) https://www.itead.cc/nextion-nx8048k050.html

3. 2 small speakers - optional - I wanted to add 2 bluetooth speakers

4. Bluetooth audio amp - optional - https://www.amazon.com/INSMA-Amplifier-TDA7492P-Re...

5. power supply - optional - if you add the speakers its nice to add a power supply that will supply the Raspberry and the audio amp - https://www.coolerguys.com/products/100-240v-ac-to...

6. I also added the 3D printer files for anyone interested

Software:

1 Raspian - https://www.raspberrypi.org/downloads/raspbian/

2. Win32 Disk Imager - used to write raspian to microSD for Raspberry Pi - https://sourceforge.net/projects/win32diskimager/

3. Nextion Editor - for creating custom screen layouts - https://nextion.itead.cc/resources/download/nextio...

Extras:

1. computer screen to connect to the PI - temporarily

2 usb keyboard and mouse to connect to the pi - temporarily

Step 2: Install Raspbian & Load the Nextion Screen

Once you have downloaded the raspian image, unzip the iso to your pc.

Place the microSD card into a USB adapter and plug in your pc

Perform a quick format on the sd card (fs=fat32)

Load the screen:

1. plug the SD card into your pc and place the attached hmi file on the card

2. install the microSD into the Nextion screen and power up

3. wait for the image to load. When done remove power from the screen and remove the card

4. Perform a quick format on the sd card (fs=fat32)

Load Rapian to microSD:

Start Win32 disk imager and write the image to the microSD

1. choose the letter of your microSD

2. select the raspian image iso file you unzipped

3. Click write

4. Once done you can close Win32 disk imager and unplug the microSD from your pc.

5. Plug the microSD into the raspberry Pi

Step 3: Boot the Raspberry Pi & Connect the Nextion

There are a few configuration changes that need to be made on the Raspberry Pi.

For instance, to use the serial output on the Pi3 Model B, you must disable the bluetooth.

Assemble the PI and plug in your keyboard, mouse, SD card and pc screen using the hdmi output of the PI

1. Once you plug the power into the Pi, it will start to boot. Once booted, you will find you have a nice desktop with everything you need already installed.

1A. Click the wifi icon in the top right corner of the screen and attach the Pi to your wifi and save the settings

2.The serial on GPIO 14 & 15 is disabled because it is used by the bluetooth. To enable the serial interface, you must add the lines below to /boot/config.txt you must do this thru the terminal using sudo nano /boot/config.txt

  • click the terminal icon (black screen icon) on the top of the screen
  • type sudo nano /boot/config.txt
  • using you arrows, scroll to the bottom of the screen and add the lines below to the bottom of the files

#disable bluetooth so nextion can work on serial 0

dtoverlay=pi3-disable-bt enable_uart=1

enable_uart=1

  • Once these lines are added, next we save and exit
  • press control x to exit
  • press Y to save
  • press [enter] to save file

shutdown & remove pwr from the pi

restart.

open the terminal and type pinout

we will be using GPIO 14 & 15 for serial comms to the nextion

You will see which pins on the Raspberrys GPIO are GPIO 14 & 15 on the screen

3. Shutdown the Pi and connect the screen as shown in the attached pic

4. Reboot the Pi

5. Open the terminal and type sudo raspi-config

6. go to Interfacing options, then serial and enable the serial port and disable the shell

7. Tab to finish

8. Reboot one more time

Step 4: Writing Your Python Program

Choose your favorite python IDE and get to coding...

I chose Geany as it was already installed on the Raspian image. Its also very easy to use and testing the program couldnt be easier. All you have to do is click the paper plane icon at the top of the Geany screen and it starts the program

I have attached my program as an example. This will help show you how to access the serial port and the different functions of the Nextion screen. Feel free to edit as you see fit

Step 5: Start Program When Raspberry Boots...

By this time, you should be able to run the program on your screen

If you want to be able to run your raspberry pi clock program without the keyboard, mouse and pc screen follow the steps below.

1.add this line to top of your program --> #!/usr/bin/python

2. make your program executable by using the chmod command in the terminal.

  • click the folder icon at the top of the Raspian screen and navigate to the folder where your program is. Mine is on the desktop in a folder named py
  • Right click on the folder (i will right click on the py folder and choose open terminal)
  • Type the command sudo chmod +x followed by the path to the program and click [enter]
  • sudo chmod +x /home/pi/Desktop/py/NameOfProgram.py
  • close the terminal when done

3. Now navigate to the /etc folder

4. Right click on the etc folder and choose open ternal

5. In terminal type--> sudo nano rc.local

6. Using your arrows, scroll to the bottom of the page, to the line above exit 0

7. Add a path to your executable program --> /home/pi/Desktop/py/NameOfProgram.py

8. Click control + x to exit, Y to save and click [enter]

8a. [EDIT] - You must also make the rc.local file executable by now typing sudo chmod a+X rc.local

9. Reboot the pi by typing reboot in the terminal and when it reboots the program should auto-start :)

Step 6: Optional Bluetooth Speakers

The bluetooth audio amp was needed as we disabled bluetooth to use the serial port

If using the suggested power supply, you will need to break out the wires and use the 5V for the Raspberry and the 12V for the bluetooth audio amp.

Connect the power and speakers according to the attached pic.

The speakers I used were taken from an old failed bluetooth bathroom fan/speaker setup

Step 7: Using ThingSpeak

You will need to signup at https://thingspeak.com/apps and use a ThingHTTP

I started one for the temp and another for the weather condition ie: sunny. foggy, snow...

When setting up the ThingHttp, you will need a URL to send a get request to

I use - https://www.wunderground.com/weather/us/in/Indianapolis

The method is Get

HTTP ver - 1.0

The parse string is a little tricky

1. Go to the weather underground site for your city

2. In google chrome, press F12 (go to step 3 or 3a)

3. On the right side of the screen, look for and click the icon for "select an element in the page to inspect"

3a. Or highlight and right click on the text you want to get and select "Inspect" (skip to step 5)

4. Now highlight the temp

5. On the right you will see a line get highlighted - Right click on this line, go to copy and select Copy XPath

6. Save this as your parse string in your thingHTTP setup

7. Save the thingHTTP and you will be given a link to use for the program with your API Key

8. Setup your link just as I have in the program.

9. You can test requests easily with a program called Insomnia, but thats a whole other tutorial..

First Time Author

Participated in the
First Time Author

2 People Made This Project!

Recommendations

  • Pocket-Sized Speed Challenge

    Pocket-Sized Speed Challenge
  • Audio Challenge 2020

    Audio Challenge 2020
  • Maps Challenge

    Maps Challenge

26 Discussions

0
david.molevelt
david.molevelt

Question 5 weeks ago on Introduction

HiTeeMick,
Can you please instruct me why my Pi does not accept Unicode in the serial instructions?
You suggest port.write "page0" . My serial port definition is ttys0 and using pi 3

Best regards,
David

0
DannyM62
DannyM62

Question 11 months ago

Thanks again for a great project. I have the project working but would like to make a couple of changes. I want to add buttons to call functions in my python program, I use them to turn on and off a Wemo switch. I can put the buttons on the display, but not sure how to send the data to the program that would call the function. I can see how you used the port.write command to send data to the display. I am just trying to understand how to do the reverse. Thanks again in advance for your help.
Danny

0
TeeMick
TeeMick

Reply 11 months ago

How did you fix the serial problem?
For creating on-screen buttons, look at the section of code below. There is a clear button in the bottom right of the screen.
Each pass of the program the code is looking for a button press. When It reads the button was pressed, the screen is dimmed or undimmed depending on the original state of the screen.
Create a button, then use print(ord(line1[0])) in your program to see the result of a button press.
See the attached pic.

<code>
line1 = port.read(1)
#print(ord(line1[0]))
if (line1 != ""):
if ((ord(line1[0])) == 101) and (IsDim == 0):
IsDim = 1
port.write(dimCmd + eof)
overRide = 1
pic = 0
Opic = 0
elif ((ord(line1[0])) == 101) and (IsDim == 1):
IsDim = 0
port.write(undimCmd + eof)
overRide = 0
pic = 0
Opic = 0
DimCntr = 0
</code>

Untitled.png
0
DannyM62
DannyM62

Reply 11 months ago

Do you have a different piece of code in python to read the data sent by the pressing of a command button? I have searched and searched and have not come up with any examples. I have found a similar example using Arduino IDE, but not sure how to convert it. On the Button I am using I have set up "Touch Press Event" and even tried to enter a line "print 1", but I cannot get the piece of code to appear trying to use the print command . I tried using your code but never could get anything to work properly. What I would like to happen is when I press the Command Button my python code would be looking for the button press and through an if statement check the output from the Nextion Button and execute another piece of code. Sounds easy but I have yet get it to work.
Thanks for your help.
Danny

0
DannyM62
DannyM62

Reply 11 months ago

Is the code part of the python program or set up on the Nextion?

0
TeeMick
TeeMick

Reply 11 months ago

The code is part of the python program. You might find that snippet in the program ur running. The only thing to do in nextion is create the button and check the box for the button to send its Id. Then your python program must have code looking for that particular Id

0
DannyM62
DannyM62

Reply 11 months ago

Thanks

0
DannyM62
DannyM62

Reply 11 months ago

Not sure what I did different but here is the line of code I am using to set up the serial port:"
port=serial.Serial(port='/dev/serial0',baudrate=9600, timeout=1.0)
eof = "\xff\xff\xff" "
I have changed the code quit a bit to fit the project I am working on, I am not looking up anything on Thinkspeak. I have a DH22 running and am working on replacement for an app I created using Tkinter and am turning on a wemo switch and pulling the outside temp from the Adafruit IO. I waned to change the display to the Nextion and get away from the Tkinter app. The block I have run into was how to turn on the wemo switch by using a command button on the Nextion display. I am working on the sample code you sent me and will respond when I get it up and running. Thanks again,.
Danny

0
DannyM62
DannyM62

Question 11 months ago

Hey TeeMick nice project. I am having a problem getting the python program to work. I have a smaller Nextion display and I change all of the sizes of the pages and graphics but not the names of the pages or fields. I have downloaded the tft file to the display and it all looks good and I have got a clean compile. However when I start the Pi (and run WeatherClockFinal.py) I can see only the display on the screen and not any data. Can you give me some direction as to what to do next?
Thanks,
Danny

0
TeeMick
TeeMick

Answer 11 months ago

Maybe there is no communication between the pi and the display. If this is the case, did you disable the Bluetooth and enable the serial? Also double check your wires? If you want to send some pics, I'd be happy to try and help.

0
DannyM62
DannyM62

Reply 11 months ago

Thanks for the quick response. I obtained a new display, the same as you show in your project. I ran into the same problem, but discovered that if I changed one line of code in WeatherClockFinal.py "port=serial.Serial(port='/dev/serial0',baudrate=9600, timeout=1.0)" from "port=serial.Serial(port='/dev/ttyAMA0',baudrate=9600, timeout=1.0)" it made a connection to the display. However, when it loads the file on the display it momentarily displays page0, then displays page1. I am not sure but the Nextion editor updated to a newer version. Any suggestions on this problem?
Thanks,
Danny

0
DannyM62
DannyM62

Reply 11 months ago

I am using a raspberry 3B+ and my config.txt file is so:
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
gpu_mem=128
enable_uart=1
I am using serial0 instead of ttyAMA0

0
DannyM62
DannyM62

Reply 11 months ago

I am also using a Pi 3B with Stretch.

0
TeeMick
TeeMick

Reply 11 months ago

Does the PI have the correct time? Maybe the program is running and it thinks the time is between 5am and 6am which would use Page1

0
TeeMick
TeeMick

Reply 11 months ago

The port that it refers to is on the PI. Are you using a diff version of PI?

0
DannyM62
DannyM62

Question 11 months ago

Do you have correct 3D stl files? I was trying to print the top at my local public library and the object is very tiny. Maybe you have adjusted the files for the inch Nexiton display. Thanks

0
TeeMick
TeeMick

Reply 11 months ago

The files I attached to the original project are the ones I used. Be sure to watch the inch/mm setting in the software you are using.

0
DannyM62
DannyM62

Reply 11 months ago

So when I print the files I should tell the slicer to set to inches or MM?