This is a continuation from my prior WIoT weather station.  I struggled with the name as it does much more than just weather…  I suppose I should have called it IoT “thing”….  Anyway, what is WIoT-2?  Well, it’s lots of things…  I use it as a cloud-connected (IoT) info center of sorts mounted near our front door.  It is a culmination of information my wife or I often look for either on our way out of the house, or when we come home.  It displays current/forecast weather from WeatherUnderground, weather outside the house, date, time, our local waste collection schedule, currency rates, the status of our garage doors, our hot tub temperature, and even allows our kids to check-in when the get home.    I decided to upgrade my original WIoT project to a Nextion TFT LCD due to issues I was having with SPI comms with a different TFT/controller (RA8875).

Latest Version of WIoT-2

Pop-Up Window

Earlier Version

 

I came across the Nextion display over a year or so ago during their Indiegogo campaign.  I passed it by as there was not enough information at the time.   In hindsight I regret that decision.  Itead hit the nail on the head with this display as far as I am concerned. It uses simple serial commands to communicate between the MCU and the display – which is perfect for my needs (and I think most makers like myself).   It uses a HMI editor (below) that allows you to layout all the graphic elements/buttons on the TFT. You can also setup multiple pages.  Refresh and image load rates are fast as well.  After spending countless hours coding for the RA8875 controller, this is a walk in the park.  Anyway, enough on this. I’ve done a more fulsome write-up as to why I like this display so much.  There are many other advanced features of the Nextion that I won’t get into here as this is not the purpose of this post.

editor capture

 

The intent of WIoT-2 is the following:

  • Display current and forecast weather information.  This time around, I used the Weather Underground API to pull both current and forecast data in JSON format.
  • Display weather data generated outside the house via another ESP unit.  It polls temp, humidity and battery info (it’s solar powered) – Integrated with Blynk
  • Has option to change weather to other locations.
  • Displays temperature and humidity from inside the house. Watch out Nest!!!
  • Poll and display the temperature of the hot tub – Integrated with Blynk
  • Poll and display the status of garage doors (open/closed) – Also integrated with Blynk
  • Allow our kids to interact with the unit – sends notifications via Blynk
  • Polls waste collection calendar information to show us which waste items are being collected on a weekly basis.
  • Show the current date and time – uses Blynk RTC widget
  • Shows up-to-date currency conversions CAD>USD, CAD>GBP.

JanInterface2

Latest HMI – Jan 17

img_2905

img_2906

Main display at front door

MCU unit on backside of wall. Contains NodeMCU, power supply and connectors.

NodeMCU controller unit on backside of wall

Garage door status icons

Garage door status added.  Allows monitoring and control of doors.

Option to selection weather reporting location

Option to change weather reporting location

 

Pin-Outs & Connections:

I’m using the NodeMCU, so your connections may be different.   This project takes advantage of both Serial ports. Serial is used for comms between the PC and NodeMCU, Serial1 is used for comms between MCU and Nextion.  Well, not exactly.   The Node’s Serial RX line is shared on both sides.  More on that in a bit.    The connections are Node RX to Nextion TX, and Node D4 to Nextion RX.  The sketch uses hardware serial using Serial1 (TXD1) to transmit to the Nextion, while return data comes over Serial RX.  In the sketch, both Serial and Serial1 have been created.  The only trick is that you cant have the Nextion powered when uploading sketches to the Node.  This can be done by simply disconnecting the power to the display when uploading a sketch.

Important:  Don’t fall into this trap!  If you are powering your MCU and Nextion by different sources their GND connections must be common!   Also ensure you are supplying ample current to the Nextion.  Here are the connections between the MCU and Nextion:   NodeMCU RX >> Nextion TX.  NodMCU D4 (TX1) goes to Nextion RX.
Schematic

Blynk Integration:

The project also uses Blynk extensively to allow me to view the status and control the various devices I have throughout my house from my phone and tablets.  I also use it to debug the devices to ensure they are still sending data at their defined intervals.

Outside data is also logged to Blynk. Temp, Humidity and Battery are tracked.  Data also reported to WIoT. The Hot Tub controller monitors the temperature and allows the RGBW NeoPixels to be set.  Data also reported to WIoT. The Garage Door Monitor allows for monitoring and controller both garage doors.  Data also reported to WIoT.

Outside Data Logging Unit

Outside Data Logging Unit

Hot Tub Temp and LED Controller

Hot Tub Temp and LED Controller

Garage Door Controller / Monitor

Garage Door Controller / Monitor


Hardware:

Libraries & Resources:

  • Nextion Library – Note there is an official Nextion library as well, but this one was sufficient for my needs given it allowed for me to send and receive information from the Nextion.  Be sure to read the notes and links (link, link) provided as they give great samples.  Note: You can use software or hardware serial.  I used hardware serial in my code.  If doing this you have to comment out a line in Nextion.h   //#define USE_SOFTWARE_SERIAL //Comment this line for use HardwareSerial
  • Blynk Library –  I use the app to control and monitor the hot tub, garage and others components.
  • Time – you will also need Paul Stoffregen’s time library for Blynk:
  • Arduino IDE – I used 1.6.13, but others should work fine as well.
  • Node MCU firmware – comes as part of the ESP package.  Google is your friend if you need help. Just google setting up NodeMCU in Arduino.
  • Arduino JSON for parsing the JSON data.
  • Nextion forums –  (a big shoutout to Patrick and Steve for being so helpful in getting me up and running).
  • Arduino Source Code & Nextion HMI file – see bottom of page.

Challenges:

I had my first challenge with the NodeMCU when trying to both display debug messages using the standard Arduino Serial window and trying to communicate with the Nextion.  After some digging and help from Steve and Patrick on the Nextion forums, I was able to get both Serial debugging and comms with the Nextion.  It uses a simple trick in that the Nextion uses the NodeMCU’s D4 pin (Serial 1 TX) to send comms and Serial RX to receive.  For those with a keen eye, you can see that this is a problem when uploading sketches – which is easily solved by either disconnecting the pin, or powering down the Nextion display during sketch uploads.

Tips n Tricks:

Changing IDs in the HMI Editor:  I discovered that the below approach messes up each time I add or shuffle picture objects in the editor.  Instead of using the ID of an object, I have the HMI send a print call when any button click sends content back to the MCU.  This way, I can check for more realistic values in the call-backs.  i.e. I can send “print b1” when button 1 is pressed.  You can use whatever text you want, and you can have separate calls when buttons are pressed or released.

Part of the app watches for incoming messages from the Nextion that look like this “65 0 2e 1 ff ff ff”“. Where 2e represents the component ID being clicked/or sending a message from the display.  You can see the message output in the HMI.  One of the issues is if you are making changes and removing images/components, the HMI re-numbers the ID’s.  This is a problem if you write code to interpret them.  Solution – wait until you are done your project design and have the HMI correct before writing statements to interpret messaging.   If you have to edit it later, that is fine. Just make sure you check all output values and ensure they updated in the matching receive statements in the sketch. Code sample:

 String message = listen2Nextion(); //check for message
  if (message != "") { // if a message is received...
    //Serial.println(message); //...print it out
    //if (debug) {Serial.print(F("*****Nextion Msg: "));}
    //if (debug) {Serial.println(message);}
    Serial.print(F("*****Nextion Msg: ")); Serial.println(message);

    //Refresh the data.
    if (message == "b1") 
    { 
      //ESP.restart();
      refreshData();
      getWasteCollection();
    }
    if (message == "dYes")  //photo icon clicked - run photo show
    {
      dontDim=true;
      myNextion.sendCommand("dim=60");  //brighten the screen
      myNextion.sendCommand("page photo");
    }

Nextion Baud Rate:   You can view the default baud by referring to the ‘baud’ command in the Nextion Instruction Set.  Good news is you can also change it by issuing the bauds command with the baud rate you want to use.  Don’t forget to update the baud rate in your sketch for Serial1 (i.e. Serial1.begin(9600);).    Default from factory is 9600.  I changed mine to 115200.

 

img_2801

Inside view of MCU case and unit

template

Nextion HMI Template View

 

Source Code Arduino Sketch / Nextion HMI:

I’ve decided to take a different approach on sharing for this project.  I have spent well over 100 hours and nearly a year developing this project.  A lot of hard work has gone into the Arduino source (56+ versions) as well as the Nextion HMI.  For a small PayPal donation of $5 or more (c’mon, it’s a Starbucks coffee… :-), I will share the Arduino sketch and it’s HMI file.

What you can expect:

  •  Arduino sketch (libraries are your responsibility – links above).
    • This code contains various functions and tricks working with strings and char arrays.  It’s a result of many hours researching how to do specific tasks.
    • Connecting to Weather Underground and parsing JSON data
    • Blynk connectivity including interfacing with 2 other IoT devices.
    • Communicating with the Nextion display.
    • I now use this code as reference for my projects due to the variety of working with string/char array data and manipulation of them.
  • Nextion HMI file (developed in v0.42)
    • You get the HMI file that interfaces with the sketch.  This version is for the 5″ regular Nextion.  I’ve provided links above to where you can buy one at a good price and reliable seller. I use them all the time.
    • This does not include source graphics as many are copyright and some are custom.  You will only get the HMI with graphics/fonts embedded.  It’s up to you to create your own graphics if you want to change what is in there.  Also, if you don’t get the 5″, expect to have to modify the layout to fit.
  • Setting Expectations
    • Due to the popularity of this project and those interested in this source code, I’ve create a How-To below.
    • If you use this in a project, please give credit where due to those who wrote the libraries as well as Plastibots.com.  You must retain the information at the top of the sketch, and you may not sell this code to others.
    • You will have to make changes to the Nextion library for your Serial connection. Here: (scroll up).
    • You will also need to ensure the Serial1 value is set to the right baud of your Nextion.  Refer to the ‘baud’ command in the Nextion Instruction Set, find the Nextion’s baud rate, and then set the value in the sketch.  i.e.  Serial1.begin(9600);.  Default is 9600
    • This is a stable version of the HMI and sketch.  I am constantly updating this project, and this blog, so the pictures above may demonstrate new features that wont be included.
    • Note – the sketch is built for a NodeMCU v1.0 ESP8266.  It may work with other MCUs, but you are on your own if you go that route.  That said, it uses the ESPs 2 hardware serial lines.  One for serial debugging and the other (Serial 1) for Nextion comms.
    • Do not expect this code will be plug-and-play. It uses other Blynk-connected projects,  the Nextion and sensors.  Blynk is easy to setup and comes with lots of examples.
    • Sketch and Nextion HMI will be sent within 24 hrs  or less of receiving notification from PayPal.
    • Donation will appear to dast****1@cogeco.ca.
    • The intent of this code is NOT to provide you with a plug-and-play solution.  However, it will work if you buy all the same hardware and follow the configuration steps I noted in this blog.  The intent here was to share useful functions and ideas that you can use in your own code.  If you choose to try to re-create WIoT-2, that is great, but please don’t expect free support.  I am happy to provide some basic guidance, but this project requires an intermediate skill level.  The intention of this donation is to get access to a wide variety of useful functions.  This donation is for code, not support 😉 There are plenty of resources online – Google is your friend! 🙂  Again, look below for a How-To on getting up and running and remember, where’s the fun in  ready-made solution anyway!? 🙂
 

WIoT-2 How To

Due to popular demand, I’ve added some notes to help you create WIoT-2.  It is not as complicated as it looks.  It only requires a few pin connections and changes to a few lines of code.  I just like to be verbose in my descriptions! 😉

  1. Parts: At a minimum, you will need a NodeMCU Mini and I suggest a 5″ Nextion TFT (links are above).   You will also need a 5V 1A (min) power supply.  I suggest a PIR sensor would be useful, but you don’t need it to make this work. It is only used for  dimming the screen.  You’ll also need to get a bezel or print one of your own (links above for the STL for the one I printed).  You will also need a MicroSD card.
  2. Nextion: Using the Nextion Editor, open the HMI file provided, compile it.  Then select File>Open Build Folder and copy the .tft file to a MicroSD card.  Then take the MicroSD card, insert it into the Nextion and provide 5V + GND to the respective wires.  Allow it to load the file, then power it down and set it aside.  Note: you can use this on other sized (base) Nextion displays, but you will have to modify the HMI file / graphics to fit as it will throw errors.  BTW – if you don’t have your Nextion yet, you can still connect your MCU to the Editor in debug mode to view it working. How cool is that!?
  3. Accounts:  These are all free to setup, but you will need a WeatherUnderground API account as well as download the Blynk software (iOS or Android).  For Blynk, you need to create a new project and email yourself the auth key.  See #5 for where these are entered.
  4. Blynk:  The Blynk app is not included as part of this project as it would not apply since you would also have to create 3 other hardware devices!  However, you need it for basic features and comms (i.e. RTC).   In the code, I have my Hot Tub monitor, Outside Temperature and Garage Monitor projects all integrated and sending info to this project.  In your case, you can simply ignore it and remove those parts later once you are comfortable with the project.  At the minimum, you will need the RTC Clock and Notification widgets added in your Blynk project as they are used in the code here.  You could simply create a blank Blynk app, add a RTC and Notification widget and use it’s auth code.
  5. NodeMCU and Arduino Sketch:
    • Connect the Node to your Arduino environment and ensure you have a connection.  Note the NodeMCU uses the CH340 USB drivers for Windows.  I suggest running the sample blink sketch to test your connection.  I encourage you NOT to try jumping right into this project. First make sure each component is working separately.
    • See notes above on what libraries you need.
    • Download and install the libraries listed at the top of this post.   You will have to make changes to the Nextion library for your Serial connection.  Read the details above (scroll up).
    • The NodeMCU will communicate to the Nextion using UART1 which is Serial1 in the sketch.
    • Sketch:  Ensure the Serial1 value is set to the right baud of your Nextion.  Refer to the ‘baud’ command in the Nextion Instruction Set, find the Nextion’s baud rate, and then set the value in the sketch.  i.e.  Serial1.begin(9600);  Default from the factory should be 9600.
    • Ensure this line is un-commented:  #define nextion Serial1  //use this when live on MCU + Nextion TFT LCD Hardware.
    • Optional:  Connect the PIR sensor to +5V and GND.  Connect the PIR data line to A0 on the NodeMCU.
    • You can refer to lines 51-57 to set timeouts for the various data parsing activities.  For example, weather is refreshed every 900000 ms (15 minutes).
    • A few lines down, you need to set your WiFI SSID and password.
    • You also need to enter the Blynk Auth code here:  char auth[] = “YourBlynkAuthCode”;   You can email this to yourself from the Blynk app you created.
    • Further down you need to enter your WU API key: const char WU_API_KEY[] = “YourWUAPIKey”;  You need to sign up at Weather Underground to get an API key. It’s free for a # of txns per day (which works for most).
    • The default weather location is set for Burlington Ontario:  char WU_LOCATION[] = “pws:IONTARIO1260”;   You can find alternate values on the WeatherUnderground site and enter it here.   I believe they are all pws:[NAMEOFSITE] but you should confirm on the WU site.
    • The section covering waste collection is a bit complicated, so I’m not providing guidance on it as it won’t apply to you as it is specific to local Burlington waste collection. You may want remove the related code later. I suggest keeping it there for now as it will work as is. Then remove it later when you have a stable setup.
    • Now’s a good time to save save save and version your code!!!  I also suggest you save a new version with each successful iteration you have.
    • This should be enough to get you up and running.  Upload the sketch, then connect your NodeMCU to the Nextion using the pin-outs referenced near the top of this post.
    • Have fun and good luck and welcome to your own WIoT Thing!
    • Note:  This is not a commercial product and neither my code or notes within this blog post carry any warranty. It is your responsibility to ensure you have purchased safe, certified parts, that you know what you are doing, and that you have ensured the hardware are safe from shock, fire etc.  This is not intended for commercial use.  It is also your responsibility to ensure this is safely mounted and uses a certified power supply.
  6. So you have it working now…:
    • You can refer to the void processNextionMessages() function.   The purpose of this is to listen to print messages sent back from touches etc on the Nextion and process them.  You can see the names are pretty clear and their functions.    You can also use this to change the other weather locations that can be changed on the fly.
    • Once you have this stabilized, you may want to remove the code related to the hot tub, outside weather and the garage door monitor components.  I suggest doing this methodically and saving new versions as you go.
    • As noted a few times prior. Don’t spend hours writing code hoping you have it right, only to find it fail and you have no idea where the failure is.  Take it in steps. Get a feature working, and save a new version.  Then move on.  Once you have it all working. Save again!  Then, if you are not creating the other projects (i.e. the Hot Tub piece, the Garage piece) you can start to comment out related code, test, then save when it works.  Repeat.  This is a large project, so take it methodically.
  7. Troubleshooting / FAQ:
    • Getting nothing on the Nextion:
      • Did you ensure the right serial speed was set as noted above?
      • If you are powering both devices separately, their grounds must be common.  I suggest 1 5V power source with >1 Amp supply.
      • Your power supply may not be able to supply sufficient current.  At least 1 amp is needed.  Don’t expect your computer’s USB port to power both the MCU and the Nextion as it typically supports 500mA max.
      • If the Nextion screen is blank, try loading one of the default sample .tft files and apply 5V and GND. You should see it show. If not, something is wrong with your Nextion.
    • Nextion is not updating with recent data:
      • Again. Ensure the correct serial setting as noted above.
      • Ensure you have followed all steps above.  Particularly the #define Serial1 note.
      • Try debugging the sketch. Disconnect the Nextion power, change debug = true, in the sketch, download it, and run the open the Serial console. Follow the results to see if the data is being parsed correctly.  I’ve got verbose debugging in there which should allow you to see all the output being generated.
    • I let out the black smoke:
      • Let’s hope you didn’t go there…  You’ve provided too much voltage to the device, or you’ve shorted a pin.  If you choose a device other than the NodeMCU, it may be only able to support 3.7V max. ie. a raw ESP8266.  Not much I can do to help other than comfort you that we have all been there at least once…  I often order > 1 unit to ensure I have backups.