The Arduino UNO can be programmed to take the role as an all-purpose signal generator, and that's what we're using for this demonstration; visualise a live data stream in preparation to do analytics and charting. We're going to use framework called SA Studio by Swedish developer Stream Analyze, with which you can connect lots of devices, and run in a browser-based dashboard. It's based on a small (<100kB) run-time called SA Engine, which can run across a range of operating systems for computers, phones and embedded devices. The examples in this tutorial are customized for someone using a mac computer.
SA Studio offers its own high-level query and modelling language called OSQL, which is super-compact, and holds many built-in functions for analytics, such as FFT, running ML models et cetera. This time we'll be running the code locally on the Mac (yes, SA Engine works under Linux and Windows as well), while there are run-time engines available for many kinds of embedded devices as well -- all cleverly addressable in realtime from the browser dashboard. Sounds good, but a bit complicated? It's not, so let's dive in so you can see for yourself!
In this article, we'll stick to how SA Engine runs locally on Mac, so that you'll get the hang of the Stream Analyze workflow. Did I mention you can do a lot of edge AI as well, and that you can bring your own ML models in once you get the hang of it?
- Summary of the steps in this tutorial:* Download the SA Engine runtime software for your device (e.g. local on your mac)* Create your free account for SA Studio (no credit cards or strings attached)* Connect your device to the cloud, from the device tab in a browser* Using the good ol' Arduino SDK to get the C++ sketch onto an Arduino (e.g. the UNO), see code section.*Copy a three-line OSQL script to set the whole thing in motion and press play!
So, here we go!
1) Download SAEngine for the device. In this case, our target is mac computer so get the latest binary here https://studio.streamanalyze.com/download/#tOSX Please follow the install instructions for OSX using the Terminal app, and when you're done, come back here. Once successful, you should see the prompt
[sa.engine] 1>
in your Terminal app window -- you're now up and running your first instance of SA Engine on your mac.
2) Getting your SA Studio instance up and running. Go to the Stream Analyze website [link] and sign up using your Google account or username/password. Click the "GO TO STUDIO" button after a bit of waiting, while your personal SA Studio instance spins up in the cloud.
3. Connect your device SAEngine to SA Studio. Click on "Connect edge", name the SA Engine on the device something useful like "macedge" and copy/paste the connect command right below where it says "Connect as edge using base64 blob from within sa.engine" in the black box, starting with (where BLOB-HERE is your connection key):
connect_using_config_blob( 'BLOB_HERE' , 'macedge' , true);
It is a quite long key. It's also safe, once connected nobody else can connect to it.
Paste the whole thing in your terminal app at the prompt, when it reads:
[sa.engine] 1>
The local SA Engine client and SA Studio server now negotiate a bit, and then some connect info is rendered:
2023-02-08T23:51:28.667 [Connection (1/7) - Connecting as: macedge@13.49.231.70:35021 with TLS]
2023-02-08T23:52:37.712 [Connection (2/7) - Registered as "macedge@13.49.231.70:35021"]
2023-02-08T23:52:37.985 [Connection (3/7) - Connected to SERVER with: #[socket "13.49.231.70" 35021 SERVER 4 SECURE WS]]
2023-02-08T23:52:38.098 [Connection (4/7) - Uplink opened: #[socket "13.49.231.70" 35021 SERVER 27 SECURE WS]]
2023-02-08T23:52:38.104 [Connection (5/7) - Registered as edge]
2023-02-08T23:52:38.104 [Connection (6/7) - Uplink socket registered with edge manager]
2023-02-08T23:52:38.104 [Connection (7/7) - Established OK]
You've now registered your MacBook as an edge named "macedge" in SA Studio, so the only thing remaining is to load firmware for generating signals on the Arduino, and run while plugged into the mac with the USB cable:
4) Upload the arduino sketch. Copy the enclosed arduino sketch, and select "Arduino UNO" and the serial port where your Arduino has connected, e.g. in my case "/dev/usbmodem-414010.tty" to upload the sketch. If you don't know the name of the serial port, look for "usbmodem" or "tty*" in the /dev folder of your mac. If you'd want to see the data stream from within the Arduino SDK, you can peek using the "Serial monitor" menu option, at 115200 baud, When successful, you can quit the Arduino SDK, as it may hog the serial port from other software.
5) Builtintothe OSQL query language, there are many useful commands. Open a "new OSQL editor" in SA Studio in your browser, and write on the first line:
this_peerid();
That will give you the name of the device you're interacting with. Other useful commands to test include:
startup_dir(); // in which folder your binary runs on that device
you can ask the server for the names of all devices listening:
listening_edges();
Or a device a list of all the network interfaces it has access to:
get_all_addrs();
Before you press "play", make sure that you're sending the sequence of OSQL statements to the right SA Engine instance in the drop-down menu. Another device, without the UNO, won't know anything about the serial stream you're asking for.
Select "macedge" or whatever you called your SA Engine(running on the mac) in the connect script (in step 3 above), and press play.
To complete this tutorial, we'll be using the serial:streamer() command, to pick up six bytes between the designated start byte "55" and end byte "AA", as declared in the enclosed arduino sketch loaded onto the UNO.
select v from vector v, Stream of Binary sb
where sb = serial:streamer("/dev/tty.usbmodem144301",115200, 0x55,0xAA,6)
and v in unpack(sb, "z16u08u08i16");
You've come to the end of this tutorial to get you up and running, the next steps could be to edit the Arduino code to more things, add some real sensors, add more analytics on the SA Studio side, such as an FFT or why not a ML model of your own?
Thanks for following this tutorial, and happy analyzing!
Comments