Last active
December 24, 2015 20:28
-
-
Save WebReflection/6857627 to your computer and use it in GitHub Desktop.
Basic example on how to connect through Linux Linino to a sqlite3 database via Arduino Yun and over WiFi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * How to query a sqlite3 database through Arduino Yun | |
| * | |
| * The ability to share a database between Linux Linino | |
| * and the arduino board opens endless possibilities | |
| * in terms of interoperability between these two platforms. | |
| * | |
| * This sketch shows an example through the console | |
| * The output could be used for other goals too. | |
| * Tested over WiFi, use Serial instead of Console for USB | |
| * | |
| * Best Regards, | |
| * Andrea Giammarchi | |
| */ | |
| #include <Console.h> | |
| #include <Process.h> | |
| void setup() { | |
| // initialize Bridge and Console | |
| Bridge.begin(); | |
| Console.begin(); | |
| // wait for the console | |
| while (!Console); | |
| Console.println("Ready to execute SQL"); | |
| } | |
| // accepting SQL inputs | |
| String sql; | |
| void loop(){ | |
| if (Console.available() > 0) { | |
| char c = Console.read(); | |
| if (c == '\n') { | |
| query(sql); | |
| sql = ""; | |
| } else { | |
| sql += c; | |
| } | |
| } | |
| } | |
| // performaing a query | |
| void query(String sql) { | |
| // the process | |
| Process p; | |
| // the output flag for operations | |
| // where nothing returned | |
| bool avl = false; | |
| // opkg install sqlite3 | |
| p.begin("sqlite3"); | |
| // always use csv as output | |
| p.addParameter("-csv"); | |
| // shows headers as first line | |
| p.addParameter("-header"); | |
| // the database file name | |
| // requires a microSD card with accessible filesystem | |
| p.addParameter("/mnt/sda1/database.sqlite"); | |
| // the query to run | |
| p.addParameter(sql); | |
| p.run(); | |
| // common loop to send back data | |
| while (p.available()>0) { | |
| // as long as there is something to show | |
| // flag the output as happened/available | |
| avl = true; | |
| char c = p.read(); | |
| Console.print(c); | |
| } | |
| // if nothing was returned | |
| if (!avl) { | |
| // notify that everything was OK | |
| Console.println("OK"); | |
| } | |
| // be sure the Console showed everything | |
| Console.flush(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment