{"id":40002,"date":"2017-06-16T06:12:55","date_gmt":"2017-06-16T06:12:55","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=40002"},"modified":"2019-04-02T10:23:28","modified_gmt":"2019-04-02T10:23:28","slug":"sqlite-with-node-red-and-raspberry-pi","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/sqlite-with-node-red-and-raspberry-pi\/","title":{"rendered":"SQLite with Node-RED and Raspberry Pi"},"content":{"rendered":"<p>In this project you\u2019ll install an SQLite package for Node-RED and learn how to write simple SQL queries to interact with your SQLite database.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-40020\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Home-Automation-Raspberry-Pi-MQTT.jpg?resize=589%2C331&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"589\" height=\"331\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Home-Automation-Raspberry-Pi-MQTT.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Home-Automation-Raspberry-Pi-MQTT.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Home-Automation-Raspberry-Pi-MQTT.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Home-Automation-Raspberry-Pi-MQTT.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 589px) 100vw, 589px\" \/><\/p>\n<p><strong>Recommended resources:<\/strong><\/p>\n<ul>\n<li>You need a\u00a0<a href=\"https:\/\/makeradvisor.com\/raspberry-pi-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Raspberry Pi board<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-3-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Raspberry Pi Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-node-red-on-raspberry-pi\/\">Getting Started with Node-RED on Raspberry Pi<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/sqlite-database-on-a-raspberry-pi\/\">SQLite Database on a Raspberry Pi<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-phpliteadmin-database-in-a-raspberry-pi\/\">How to install phpLiteAdmin Database in a Raspberry Pi<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-publishing-dht22-readings-to-sqlite-database\/\">ESP8266 Publishing DHT22 Readings to SQLite Database<\/a><\/li>\n<\/ul>\n<p>If you like home automation and you want to build a complete home automation system, I recommend downloading my <a href=\"https:\/\/randomnerdtutorials.com\/build-a-home-automation-system-for-100\/\">home automation course<\/a>.<\/p>\n<h2>Basic Raspberry Pi setup<\/h2>\n<p>Before you continue reading this project, please\u00a0make sure you have Raspbian Operating System installed in your Raspberry Pi.<\/p>\n<p>You can read my <strong><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\">Getting Started with the Raspberry Pi Guide<\/a><\/strong>\u00a0to install Raspbian and complete the basic setup.<\/p>\n<h2>Installing SQLite<\/h2>\n<p>Follow this next tutorial to learn\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/sqlite-database-on-a-raspberry-pi\/\">how to Install SQLite database on a Raspberry Pi <\/a>. Having SQLite installed in your Raspberry Pi.<\/p>\n<h2>Installing Node-RED SQLite<\/h2>\n<p>Run the list of commands below to install node-red-node-sqlite in your Node-RED user directory. This packages gives you basic access to an SQLite database.<\/p>\n<pre>pi@raspberry:~ $ <strong>sudo apt-get install npm<\/strong>\r\npi@raspberry:~ $ <strong>sudo npm install -g npm@2.x<\/strong>\r\npi@raspberry:~ $ <strong>hash -r<\/strong>\r\npi@raspberry:~ $ <strong>cd ~\/.node-red<\/strong>\r\npi@raspberry:~\/.node-red $ <strong>npm install node-red-node-sqlite<\/strong><\/pre>\n<p>Restart your Node-RED software with the next commands for the changes to take effect:<\/p>\n<pre>pi@raspberrypi:~\/.node-red $ <strong>cd<\/strong>\r\npi@raspberrypi:~ $ <strong>node-red-stop<\/strong>\r\npi@raspberrypi:~ $ <strong>node-red-start<\/strong><\/pre>\n<h2>Checking the installation<\/h2>\n<p>When your Node-RED software is back on, you can open it entering the RPi IP address\u00a0in a web browser followed by <strong>:1880<\/strong>\u00a0as follows:<\/p>\n<pre><strong>http:\/\/<span style=\"color: #ff0000;\">YOUR_RPi_IP_ADDRESS<\/span>:1880<\/strong><\/pre>\n<p>In my case is:<\/p>\n<pre>http:\/\/192.168.1.98<strong>:1880<\/strong><\/pre>\n<p>A new node called <strong>sqlite <\/strong>should appear on the left under the <strong>storage<\/strong> tab:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-40003\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/1_sqlite_node_red_installed.png?resize=152%2C289&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"152\" height=\"289\" \/><\/p>\n<h2>Creating the flow<\/h2>\n<p>In this flow, you\u2019re going to send 5 SQL queries (CREATE, INSERT, SELECT, DELETE and DROP) to your SQLite database. Follow these next 10 steps to create your flow:<\/p>\n<p>1) Drag 5 <strong>inject<\/strong> nodes, 1 <strong>sqlite<\/strong> node and 1 <strong>debug<\/strong> node<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-40004\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/2_drag_nodes.png?resize=533%2C254&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"533\" height=\"254\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/2_drag_nodes.png?w=533&amp;quality=100&amp;strip=all&amp;ssl=1 533w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/2_drag_nodes.png?resize=300%2C143&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/><\/p>\n<p>2) Press the <strong>Add new sqlitedb<\/strong> button<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40037\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/3_configure_sqlite-f.png?resize=493%2C191&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"493\" height=\"191\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/3_configure_sqlite-f.png?w=493&amp;quality=100&amp;strip=all&amp;ssl=1 493w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/3_configure_sqlite-f.png?resize=300%2C116&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 493px) 100vw, 493px\" \/><\/p>\n<p>3) Type<strong> \/tmp\/sqlite<\/strong> in the <strong>Database<\/strong> field<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40006\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/4_sqlite_default_configs.png?resize=492%2C156&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"492\" height=\"156\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/4_sqlite_default_configs.png?w=492&amp;quality=100&amp;strip=all&amp;ssl=1 492w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/4_sqlite_default_configs.png?resize=300%2C95&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><\/p>\n<p>4) Configure your <strong>CREATE<\/strong> <strong>inject<\/strong> node as follows<\/p>\n<pre><strong>CREATE TABLE dhtreadings(id INTEGER PRIMARY KEY AUTOINCREMENT, temperature NUMERIC, humidity NUMERIC, currentdate DATE, currenttime TIME, device TEXT)<\/strong><\/pre>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40007\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/5_inject_node1.png?resize=489%2C401&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"489\" height=\"401\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/5_inject_node1.png?w=489&amp;quality=100&amp;strip=all&amp;ssl=1 489w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/5_inject_node1.png?resize=300%2C246&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 489px) 100vw, 489px\" \/><\/p>\n<p>5) Configure your <strong>INSERT<\/strong> <strong>inject<\/strong> node<\/p>\n<pre><strong>INSERT INTO dhtreadings(temperature, humidity, currentdate, currenttime, device) values(22.4, 48, date('now'), time('now'), \"manual\")<\/strong><\/pre>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40008\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/6_inject_node2.png?resize=491%2C398&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"491\" height=\"398\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/6_inject_node2.png?w=491&amp;quality=100&amp;strip=all&amp;ssl=1 491w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/6_inject_node2.png?resize=300%2C243&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 491px) 100vw, 491px\" \/><\/p>\n<p>6) Configure your <strong>SELECT<\/strong> <strong>inject<\/strong> node with<\/p>\n<pre><strong>SELECT * FROM dhtreadings<\/strong><\/pre>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40009\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/7_inject_node3.png?resize=486%2C387&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"486\" height=\"387\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/7_inject_node3.png?w=486&amp;quality=100&amp;strip=all&amp;ssl=1 486w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/7_inject_node3.png?resize=300%2C239&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/p>\n<p>7) Configure your <strong>DELETE<\/strong> <strong>inject<\/strong> node as follows<\/p>\n<pre><strong>DELETE from dhtreadings<\/strong><\/pre>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40010\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/8_inject_node4.png?resize=488%2C382&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"488\" height=\"382\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/8_inject_node4.png?w=488&amp;quality=100&amp;strip=all&amp;ssl=1 488w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/8_inject_node4.png?resize=300%2C235&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/p>\n<p>8) Configure your <strong>DROP<\/strong> <strong>inject<\/strong> node<\/p>\n<pre><strong>DROP TABLE dhtreadings<\/strong><\/pre>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40011\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/9_inject_node5.png?resize=491%2C381&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"491\" height=\"381\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/9_inject_node5.png?w=491&amp;quality=100&amp;strip=all&amp;ssl=1 491w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/9_inject_node5.png?resize=300%2C233&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 491px) 100vw, 491px\" \/><\/p>\n<p>9) Connect all your nodes<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40012\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/10_nodes_connected.png?resize=598%2C269&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"598\" height=\"269\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/10_nodes_connected.png?w=598&amp;quality=100&amp;strip=all&amp;ssl=1 598w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/10_nodes_connected.png?resize=300%2C135&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/p>\n<p>10) To save your application, you need to click the deploy button on the top right corner<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40013\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/11_deploy_button.png?resize=138%2C40&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"138\" height=\"40\" \/><\/p>\n<p>Your application is saved and ready.<\/p>\n<h2>Testing the flow<\/h2>\n<p>Let\u2019s test the simple flow. Open the <strong>debug<\/strong> window and press the first\u00a0<strong>inject<\/strong> node\u00a0to trigger the <b>CREATE<\/b> SQL query. Then, follow this procedure:<\/p>\n<ol>\n<li>CREATE<\/li>\n<li>INSERT<\/li>\n<li>SELECT<\/li>\n<li>INSERT<\/li>\n<li>SELECT<\/li>\n<li>DELETE<\/li>\n<li>DROP<\/li>\n<\/ol>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-40015 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/13_create_select.png?resize=635%2C264&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"635\" height=\"264\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/13_create_select.png?w=635&amp;quality=100&amp;strip=all&amp;ssl=1 635w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/13_create_select.png?resize=300%2C125&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/p>\n<p>As you can see, the database schema was created, data was inserted, you can select the data from the dhtreadings table, delete it and drop the table.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40014\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/12_sqlite.png?resize=354%2C868&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"354\" height=\"868\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/12_sqlite.png?w=354&amp;quality=100&amp;strip=all&amp;ssl=1 354w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/12_sqlite.png?resize=122%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 122w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/p>\n<p>This is a very basic example that can easily be extended for your own projects.<\/p>\n<h2>Wrapping up<\/h2>\n<p>This post is a quick guide to send simple SQL queries to an SQLite database with Node-RED. You can apply this concept to create a complex database that actually stores useful readings.<\/p>\n<p>Like home automation? Learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino\u00a0with\u00a0my course:\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/build-a-home-automation-system-for-100\/\" target=\"_blank\" rel=\"noopener noreferrer\">Build a Home Automation System for $100<\/a>.<\/p>\n<p><strong>Do you have any questions?\u00a0<\/strong><strong>Leave a comment down below!<\/strong><\/p>\n<p>Thanks for reading. If you like this post probably you might like my next ones, so please support me by <a href=\"https:\/\/randomnerdtutorials.com\/download\">subscribing my blog<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project you\u2019ll install an SQLite package for Node-RED and learn how to write simple SQL queries to interact with your SQLite database.<\/p>\n","protected":false},"author":1,"featured_media":40020,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"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":[301,285,273,264,268,190,287,286,192],"tags":[],"class_list":["post-40002","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-raspberrypi","category-databases","category-node-red","category-project","category-raspberry-pi-project","category-raspberry-pi","category-sql","category-sqlite","category-r-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Home-Automation-Raspberry-Pi-MQTT.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/40002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=40002"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/40002\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/40020"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=40002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=40002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=40002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}