All Collections
IoT Projects Tutorials
IoT Interfaces Made Simple With Nextion, Raspberry Pi and Ubidots
IoT Interfaces Made Simple With Nextion, Raspberry Pi and Ubidots

Learn in 5 minutes or less how to add an Ubidots buttons in your Nextion Display Interface. It's very simple and yet very powerful

David Sepúlveda avatar
Written by David Sepúlveda
Updated over a week ago

Have you heard about Nextion Display? Nextion allows you to craft awesome interfaces, even if your coding level is "hello world!". And, Nextion works with Arduino and all versions of Raspberry Pi. 

In this tutorial you will learn how to add a Ubidots' buttons to your Nextion Display Interface. 

Components you need

Description

The logic behind activating a Ubidots button in your Nextion Display is quite simple. When the button is pressed, a variable in Ubidots changes. If the button is on, the variable will have the value of 1, else 0. 

We recommend following the guide How to setup the WiFi connection of your Raspberry Pi, and also learn how to connect to it through the SSH terminal. 

Setup

  1. Download the Ubidots Nextion file.

  2. Copy the file named Nextion.HMI to the micro SD card. (The memory must not contain any other files and must be formatted to FAT32).

  3. Good. Put the micro SD card inside the Nextion micro SD port.

  4. 4.Power the Nextion display and wait  until the file is update then extract the micro SD

  5. Connect the Nextion display to the Raspberry Pi.

  6. Now, wire up your Raspberry Pi according to this table:

    7. Turn on your Raspberry Pi and wait a few minutes until it boots up. 

Voila! Now you should get something like this:

Because the Raspberry uses TX and RX also as a way to access the shell, we need to disable this option so the Nextion can use these pins to communicate with the Raspberry. To do so, just follow these steps:

1. Connect to the Raspberry through SSH.

2. Copy and paste this in the Raspberry terminal:

  sudo systemctl stop serial-getty@ttyAMA0.service

Well done. You shouldn't see any line about ttyAMA0 when entering this command:

  ps aux | grep tty

Coding

Now we are done with the configuration needed to connect the display to the Ubidots cloud.

  • Create a python file named "display.py". 

  nano display.py
  • Copy and paste the code (we are almost finished, don't worry =D). 

import requests
import binascii
import serial

TOKEN = ""
DEVICE_LABEL = ""
VARIABLE_LABEL = ""

with serial.Serial('/dev/ttyAMA0', 9600, timeout=10) as ser:
while True:
ser.flush()
respuesta = binascii.hexlify(ser.read(4))
if respuesta == "01000000":
url = f"http://industrial.api.ubidots.com/api/v1.6/devices/{DEVICE_LABEL}/{VARIABLE_LABEL}/values?token={TOKEN}"
r = requests.post(url, data = {'value': 1})
  • Change the TOKEN, the variable label and the device label to match your device and the variable you want to control using the button. See here to find your variable and token

  • Save and exit.

  • Run the python script and you are done.

  sudo python display.py

Great job! You should now be able to control a Ubidots variable remotely from your Nextion display: 

Share your results with us in our IoT developers community. Here you can also find helpful hits and other great IoT projects to deploy today. 

This content was originally published in Ubidots' Blog on November 11, 2014.

Did this answer your question?