I spent today poking around with SAMI, Samsung's data exchange platform.
The power of SAMI is awesome. It's platform agnostic. That means that it will take data from any type of device - FitBit, smart lightbulb, connected washing machine - and make sense of it. How does it make sense of data that could range from strings to integers to hexadecimal to binary, or even be a combination of all of the above?
It uses something called the manifest. We'll get into that in a bit when I show you how to write your own manifest. And I promise you that this is truly a beginner tutorial.
I've borrowed the Javascript in this tutorial from Samsung's Blog.
Step 1:
If you've ever used a Seeedstudio Grove Kit, this first bit will be familiar to you. Simply plug the Grove shield into your Arduino (or any other device that's compatible with the Grove Shield - Intel Edison, Linkit ONE, etc) and connect the temperature sensor to A0.
There's a great sketch on the temperature sensor page that will work well for our needs with a few changes.
Upload it to your Arduino and check the serial monitor.
You should see the temperature being output in Celsius once every second.
We don't want that much data, so we're going to modify the sketch to only transmit every half minute. Normally you'd probably only want to take the temperature every five minutes or so.
Add this at the beginning of your loop:
delay(30000); // 30 seconds
Also, if the data isn't a number we should turn it into a number so that our code doesn't crash. If we ever see the temperature spike to 1000 we'll know that there was an error.
if (isnan(temperature)) {
temperature == 1000; // error
return;
}
Finally, I've changed the data type for the temperature variable to a Double:
double temperature;
Step 2:
Sign into the SAMI User Portal. If you don't have a User Account you can register for one now.
SAMI has two portals, the User Portal and the Developer Portal.
They look very similar, so don't get confused. On the User Portal you can connect, manage and view your devices and their data.
On the Developer portal you can add applications and device types (those manifest things we were talking about earlier).
Step 3:
Open the Developer Portal in a new tab. We're going to create a manifest for talking to an Arduino with a temperature sensor.
Select "+ New Device Type."
We'll name it "Temperature Sensor" and give it the unique name "io.hackster.temperaturesensor". Then click "Create Device Type."
This takes us to the Simple Manifest wizard. In this tutorial we'll keep it simple - Advanced Manifests come later.
The Simple Manifest lets you choose from predefined fields. We want a field for temperature data. Start typing "temperature" in the field name, and you'll see this fill in automatically:
You can still rename the field once the type has been selected. I'll go ahead and rename "temp" to "temperature."
We want our data types to match the data coming from the Arduino. We know the temperature is in Celsius. I've selected Double as the data type because I want to keep the decimal point (you can use a Long if you're fine with less precise data - you might want to do this if you're converting to Fahrenheit for instance)
Now go ahead and click "Next: Actions."
Actions are commands to do things like turning a light on, setting a temperature on a thermostat, or locking a door. We don't actually want to add any actions in this tutorial because we only want to take information.
All you need to do on this screen is click "Save New Manifest."
Select "Save Manifest."
That's it! Simplest Manifest ever!
Step 4:
Now go back to your User Portal.
You'll be prompted to connect a device. Choose the device type that you just defined: “Temperature Sensor.”
Click “Connect Device…”. You’re taken back to the dashboard.
Name your device (e.g., “Monica Temperature Sensor”). Click the name of the device you just added.
In the pop-up, click “Generate Device Token…”. A Device Token will be generated.
Copy the device ID and device token on this screen. You will use these in the code.
Step 4:
Download the javascript file included at the end of this tutorial. Now make a new directory on your computer to run the Node server:
$ mkdir "Node-Server"
$ cd "Node-Server"
Make sure you have Node installed on your computer and the node package manager (npm) installed on your computer.
$ npm install node-rest-client
Great. Now all you need to do is edit the code with your Device ID and Access Token and we'll be up and running!
Step 5:
Open temperature.js in your favorite editor (I have the Sublime CLI installed so I can launch Sublime from the command line):
$ sublime temperature.js
Now we're going to want to swap out the lines of code that say "INSERT_DEVICE_TOKEN" for the Device Token you saved in Step 3.
We're going to swap out "INSERT_DEVICE_ID" for you-guessed-it! the Device ID we saved in Step 3.
2 var bearer = "Bearer INSERT_DEVICE_TOKEN";
3 var sdid = "INSERT_DEVICE_ID";
You also need to replace "/dev/ttyACM0" in line 7 for your Arduino's com port. It might be something like "/dev/tty.usbmodem621."
6 var SerialPort = serialport.SerialPort;
7 var sp = new SerialPort("/dev/ttyACM0", {
(Fun fact: "TTY" stands for "teletype," like an old typewriter. Having trouble or want to know more about COM ports? Check out this tutorial on Sparkfun)
Now we need to add the "build args" to the javascript. This is what will create the JSON that encodes our data.
Step 6:
Go back to the Developer Portal and take a look at your manifest by clicking on its.
There is a button to "View Sample JSON:
Clicking it should bring up a JSON sample that looks something like this:
There's only one value that we're collecting, temperature. With that in mind, we know that each time we transmit data from the device we'll need the device ID, the message type, and the data.
The middle part of your javascript formats the JSON:
Function build_args (temp) {
var args = {
headers: {
"Content-Type": "application/json",
"Authorization": bearer
},
data: {
"sdid": sdid,
"type": "message",
"data": {
"temperature": temp
}
}
};
return args;
}
Step 7:
You're ready to launch the Node server and start transmitting data!
Launch temperature.js by typing in the terminal:
$ node temperature.js
This will start the data transmitting from the Arduino to SAMI.
You should see it transmitting in the terminal every 30 seconds. It should look something like this:
{ data: { mid: 'ab720a40c9c3431e86ab5328ef692831' } }
{ data: { mid: '7465076eaed64db58512e20f156d2ff1' } }
{ data: { mid: '4b510c8d8abf4fd99e50b89919561ca0' } }
The gibberish you see is actually the Message ID (mid).
Troubleshooting:
Make sure you close the serial monitor if it's still open, otherwise you'll get this error:
Error: Cannot open /dev/tty.usbmodem621
at Error (native)
If you've input the COM port, Device Token or ID incorrectly, you'll will see an output like this:
<Buffer 7b 22 65 72 72 6f 72 22 3a 7b 22 63 6f 64 65 22 3a 34 30 31 2c 22 6d 65 73 73 61 67 65 22 3a 22 50 6c 65 61 73 65 20 70 72 6f 76 69 64 65 20 61 20 76 ... >
Step 8:
Open up your SAMI User Portal. Remember that this is where we can see the data being transmitted?
Click on "Charts."
You can see the data coming in, slowly but surely. Use the magnifying glass or click on the times to change the time increment shown.
You can also check out the data logs. If you click on the icon on the left, right below "Export Data," you can choose the field types that you want to see.
Want to take this to a more permanent setup? You could run the server from a Raspberry Pi, like they did in this tutorial on Samsung's Blog that this is based on.
Comments