{"id":6336,"date":"2020-03-18T19:48:30","date_gmt":"2020-03-18T23:48:30","guid":{"rendered":"http:\/\/www.plastibots.com\/?p=6336"},"modified":"2022-05-29T05:03:27","modified_gmt":"2022-05-29T09:03:27","slug":"covid-19-daily-stats-on-a-nextion-hmi-using-esp8266","status":"publish","type":"post","link":"https:\/\/www.plastibots.com\/index.php\/2020\/03\/18\/covid-19-daily-stats-on-a-nextion-hmi-using-esp8266\/","title":{"rendered":"COVID-19 Daily Stats on a Nextion HMI using ESP8266"},"content":{"rendered":"\n<p>While Covid-19 picks a fight with humanity,  keeping busy and your mind clear is important for all of us.  In addition to getting out on hikes and biking with the family, what better way to pass the time than to built IoT stuff!   Since I had a new 7&#8243; Nextion  HMI screen sitting around just waiting for a project, and had some vacation time, I decided to buffer up my skills and get to work.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"803\" height=\"479\" src=\"http:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/tempsnip.png\" alt=\"\" class=\"wp-image-6367\" srcset=\"https:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/tempsnip.png 803w, https:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/tempsnip-300x179.png 300w, https:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/tempsnip-768x458.png 768w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><\/figure>\n\n\n\n<p>What does it do?  Simple, it takes a Covid-19 JSON <a rel=\"noreferrer noopener\" aria-label=\"feed (opens in a new tab)\" href=\"https:\/\/github.com\/pomber\/covid19\" target=\"_blank\">feed<\/a>, processes it, parses out 3 Countries of over 100 Countries worth of data, and passes it along to the Nextion to display in a meaningful way.  Specifically, it extracts the last 7 days&#8217; data for Canada (home), and then the most recent daily stats for Italy (since that&#8217;s my roots) and for the USA. <\/p>\n\n\n\n<p>Boring alert! It&#8217;s a short video and not much action, but what did you expect for showing data on bar graphs? \ud83d\ude42  There&#8217;s a lag between the Canada dataset and the other 2 due to the need to parse through over 800kB of data (which is growing daily).  <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"COVID-19 Stats Updating on Nextion HMI \/ ESP8266\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/CPzkaaYaH2Y?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Read on for a more in-depth view and some discussion on new learning I&#8217;ve gained working with complexities of char arrays, pointers and some dyanmic variable assigment tricks.<\/p>\n\n\n\n<p><strong>Hardware:<\/strong><\/p>\n\n\n\n<p>From a hardware perspective, this is an easy build.  I&#8217;m using a NodeMCU Mini ESP866 (ESP32&#8217;s or Arduino MKR&#8217;s should work too).  The below video shows the data being refreshed on the Nextion IDE in debug mode with the NodeMCU hooked up to the PC via USB and providing the Serial feed to the debugger.  Components listed below.<\/p>\n\n\n\n<p><strong>Dataset:<\/strong><\/p>\n\n\n\n<p>The data set is large 800+ kB consisting of 100&#8217;s of Countries, and growing daily, so processing time is intensive.  Currently, I&#8217;m searching 6 Countries (Canada, Italy, USA, China, India, UK).  Because I live here, Canada gets the primary display of the last 7 days&#8217; worth of data. The rest get the most recent (yesterday) data.  For comparisons, I did some tests on both an ESP8266 and ESP32.  The former, takes ~45 seconds from boot to process all the data.  Of that, Canada and Italy come in the first 15 seconds as they are at the front of the dataset.  On an ESP32, processing is about 15 seconds total.   Once booted, re-queries are faster for both devices, but the ESP32 is clearly faster.<\/p>\n\n\n\n<p><strong>Software:<\/strong><\/p>\n\n\n\n<p>With each new project I discover better ways and new tricks to make processing data easier.  I have a continuous battle with processing char arrays, pointers, and working with them.  No matter how many times I re-visit new projects, char arrays and pointers always mess me up.   That said, I&#8217;ve got some useful code now that does some interesting things. <\/p>\n\n\n\n<p>From an example standpoint here are some tricks that I built in:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Added some new functions in the Nextion library to combine Longs and Ints with Char data to pass through for display.<\/li><li>To minimize code in loops, came up with a way to dynamically assign variables that are passed to the Nextion. (this is not dynamic variable assignment within the ESP).  <\/li><\/ul>\n\n\n\n<p>An example:  <\/p>\n\n\n\n<p>myNextion.sendCommandLong(getVarName(&#8220;d&#8221;, i, &#8220;1.val=&#8221;), covidConfirmed[i]);  <\/p>\n\n\n\n<p><code>myNextion.sendCommandLong(getVarName(&quot;d&quot;, i, &quot;1.val=&quot;), covidConfirmed[i]); <\/code>  dynamically creates <code>d11.val=, d21.val=, d31.val=<\/code> etc as variable strings passed into the Nextion.   This shrunk 70 individual variable assignments to 10 in code.<\/p>\n\n\n\n<p><strong>Heads-up!:<\/strong>  I&#8217;m using ArduinoJSON and due to the size of the document, the code processes the data as a stream in a loop which allows for each Country to be searched and then parsed.  As the processing is one-way, you may see errors if the source order of Countries changes (has happened once already).   I&#8217;ve built in error-catching, but you will notice this happening when you see entire Countries missing in the data (0&#8217;s).   If you are unsure, look at the order of processes in code and match it to the file.<\/p>\n\n\n\n<p><strong>Nextion HMI: <\/strong> This specific project uses a 7&#8243; Intelligent Nextion HMI unit.  Others can be used but there are some things you need to be aware of.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The code is base on 800&#215;480 pixels, so you would have to adjust the progress bar sizes in the HMI, and play with the math in the <code>getpos()<\/code> function as it assigns the height of the progress bar based on the screen.<\/li><li>You would have to adjust the background graphics for size.  As these graphics were borrowed, they will not be passed along in the attached sample.  <\/li><\/ul>\n\n\n\n<p><strong>Next Steps:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Building out Blynk components to add the data rendering to my phone.<\/li><li>Baking this into my <a href=\"http:\/\/www.plastibots.com\/index.php\/2017\/01\/03\/wiot-2-weather-station-nextion-tft-with-esp8266\/\">WIoT App<\/a>. <\/li><li>Continue to refine and add features as I go.<\/li><\/ul>\n\n\n\n<p><strong>Hardware:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Screen:  <\/strong><a href=\"https:\/\/s.click.aliexpress.com\/e\/_9GDYPh\" title=\"Nextion 7\" target=\"_blank\" rel=\"noreferrer noopener\">Nextion 7<\/a>&#8221; HMI model NX8048P070_011R-Y.  <\/li><li><strong>MCU:<\/strong>  <a rel=\"noreferrer noopener\" href=\"https:\/\/s.click.aliexpress.com\/e\/_ASScIr\" target=\"_blank\">NodeMCU ESP8266 Mini<\/a>.  Can be made with other WiFi-enabled MCU&#8217;s. <\/li><li>You will need to power both with a sufficient 5V power supply (suggest 2Amp +).  Powering via computer USB port may cause issues.  USB 3+ should be fine.<\/li><\/ul>\n\n\n\n<p><strong>Software:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Arduino IDE v1.8.12 but should work on others<\/li><li>ESP8266 boards library v2.6.3.  <\/li><li>Nextion library (customized) below. This needs to go into your libraries folder.<\/li><li><a rel=\"noreferrer noopener\" aria-label=\"ArduinoJSON (opens in a new tab)\" href=\"https:\/\/arduinojson.org\/\" target=\"_blank\">ArduinoJSON<\/a>  (V6.x minimum).  You will need to download. I encourage you to donate to this project and purchase his e-book. It&#8217;s been very helpful to me.<\/li><li>This project uses the Blynk libraries as I intent to later integrate this on my phone. Currently it only uses the Blynk RTC feature.  All you need to do is download the blynk app, on your phone and create a blank project with RTC widget and get the Auth code which you need in the attached code.  Read up at  <a href=\"https:\/\/blynk.io\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">https:\/\/blynk.io\/<\/a>  for how  to do this. It&#8217;s easy.<\/li><li> JSON Dataset <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/pomber\/covid19\" target=\"_blank\">Feed<\/a> (Note: I have no control over this feed. If it changes, it could break this code).  <\/li><\/ul>\n\n\n\n<p>The code is included free below.  However, I would like to ask you consider donating a few bucks (it&#8217;s a coffee! :-).  I think you will find some valuable examples that will save you time. Have a look. A lot of time goes into making and documenting these projects on my blog. Of course there are also costs involved. I use support funds toward future ideas, projects that I blog about here and share with others. Please consider supporting me at PayPal or Ko-Fi below. <\/p>\n\n\n\n<!-- Begin PayPal Donations by https:\/\/www.tipsandtricks-hq.com\/paypal-donations-widgets-plugin --> <form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" method=\"post\"> <div class=\"paypal-donations\"> <input type=\"hidden\" name=\"cmd\" value=\"_donations\" \/> <input type=\"hidden\" name=\"bn\" value=\"TipsandTricks_SP\" \/> <input type=\"hidden\" name=\"business\" value=\"AU2MLEMVKZY4Y\" \/> <input type=\"hidden\" name=\"item_name\" value=\"COVID-19 - Plastibots.com\" \/> <input type=\"hidden\" name=\"item_number\" value=\"Plastibots.com\" \/> <input type=\"hidden\" name=\"rm\" value=\"0\" \/> <input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/> <input type=\"image\" style=\"cursor: pointer;\" src=\"https:\/\/www.paypalobjects.com\/en_US\/i\/btn\/btn_donateCC_LG.gif\" name=\"submit\" alt=\"PayPal - The safer, easier way to pay online.\" \/> <img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"https:\/\/www.paypalobjects.com\/en_US\/i\/scr\/pixel.gif\" width=\"1\" height=\"1\" \/> <\/div> <\/form> <!-- End PayPal Donations -->\n\n\n\n<p><\/p>\n\n\n\n<br><br>\n<center><script type=\"text\/javascript\" src=\"https:\/\/storage.ko-fi.com\/cdn\/widget\/Widget_2.js\"><\/script><script type=\"text\/javascript\">kofiwidget2.init('Support Me on Ko-fi', '#29abe0', 'Q5Q39BHIW');kofiwidget2.draw();<\/script><\/center> <br><br>\n\n\n<div class=\"sdm_download_item \"><div class=\"sdm_download_item_top\"><div class=\"sdm_download_thumbnail\"><\/div><div class=\"sdm_download_title\">COVID-19 Package<\/div><\/div><div style=\"clear:both;\"><\/div><div class=\"sdm_download_description\"><ul>\n<li>Update Mar 25 v12: Bug fixes<\/li>\n<li>Update Mar 24 v11: Fixed issue NULLs coming through the data.\u00a0 Streamlined code.<\/li>\n<\/ul>\n<\/div><div class=\"sdm_download_link\"><span class=\"sdm_download_button\"><a href=\"https:\/\/www.plastibots.com\/?sdm_process_download=1&#038;download_id=6371\" class=\"sdm_download green\" title=\"COVID-19 Package\" target=\"_self\">Download Now!<\/a><\/span><span class=\"sdm_download_item_count\"><span class=\"sdm_item_count_number\">1060<\/span><span class=\"sdm_item_count_string\"> Downloads<\/span><\/span><\/div><\/div><div class=\"sdm_clear_float\"><\/div>\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"COVID-19 Daily Stats on a Nextion HMI using ESP8266\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/6HzxN03a_ZU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Below showing the an earlier iteration on the Nextion HMI 7&#8243; screen.   <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"668\" src=\"http:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/covid.jpg\" alt=\"\" class=\"wp-image-6338\" srcset=\"https:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/covid.jpg 1000w, https:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/covid-300x200.jpg 300w, https:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/covid-768x513.jpg 768w, https:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/covid-480x320.jpg 480w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n","protected":false},"excerpt":{"rendered":"<p>While Covid-19 picks a fight with humanity, keeping busy and your mind clear is important for all of us. In addition to getting out on hikes and biking with the family, what better way to pass the time than to built IoT stuff! Since I had a new 7&#8243; Nextion HMI screen sitting around just waiting for a project, and had some vacation time, I decided to buffer up my skills and get to work. &hellip; <a href=\"https:\/\/www.plastibots.com\/index.php\/2020\/03\/18\/covid-19-daily-stats-on-a-nextion-hmi-using-esp8266\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":6367,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[168,189,119],"tags":[64,197,170,169],"class_list":["post-6336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-2","category-iot","tag-arduino","tag-covid-19","tag-esp8266","tag-nextion","has_thumb"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/www.plastibots.com\/wordpress\/wp-content\/uploads\/2020\/03\/tempsnip.png","jetpack_shortlink":"https:\/\/wp.me\/pK41j-1Ec","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/posts\/6336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/comments?post=6336"}],"version-history":[{"count":5,"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/posts\/6336\/revisions"}],"predecessor-version":[{"id":7454,"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/posts\/6336\/revisions\/7454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/media\/6367"}],"wp:attachment":[{"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/media?parent=6336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/categories?post=6336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.plastibots.com\/index.php\/wp-json\/wp\/v2\/tags?post=6336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}