Publish sensor data via your smartphone, dweet.io and Evothings Studio

Hammad TariqTutorials

ss
Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

In this tutorial you will learn about publishing and retrieving sensor data from dweet.io cloud using a mobile app developed with Evothings Studio. The example build is 100% Apache Cordova compliant, and this way you can also easily add Dweet capabilities to your existing projects!

An IoT sensor main task is to generate data and passing it on to the cloud. Often we want to publish its data in real time, or at least in “near real time”. To store and visualize the readings of such sensors, perfect for those cases without need of large local storage, the cloud messaging company Dweet.io came up with an interesting concept: They use each sensor’s reading as a somewhat of a short message string in tweet-like format (from twitter) if you will. The difference is that a tweet can only contain a payload of 140 characters (plus resources) while a so called dweet can engulf a payload of 2000 characters (means you can also post longer JSON strings and other text formats). Other than so, it’s a completely no-frills service, you don’t have to register for it and you don’t need any API keys et cetera to get started!

Dweet.io offers a REST API, it automatically registers and allows each “thing” handle name to be created once a new device starts communicating with the service, you can also specify a unique string as your thing’s name upon doing the communication for the first time. After that, it’s simply about publishing these dweets and correspondingly listening for and retrieving either the latest one or all of the most recent 500 dweets from the cloud in one go.

We’ll also be using open source Evothings Studio (Apache v2 licensed), a novel coding framework which allows you as a developer to quickly create, prototype and test JavaScript mobile apps that can connect effortlessly with relevant modern APIs. It also holds a live preview function that allows you to review the effect of your changes in real time without the need of compiling the app code again and again.

Let’s develop a mobile app with Evothings Studio and send some dweets using sensor readings from mobile phone’s accelerometer. Just follow the below steps:

  1. Download Evothings Workbench on your computer from the Evothings Download page
  2. Get an anonymous Cloud Token from the same download page, and save it somewhere
  3. Download Evothings Viewer app from an app store (Android, iOS)
  4. Generate a connection key by clicking “Get Key” button on the Connect screen of Evothings Workbench
  5. Provide the connection key to the Evothings Viewer(s) and tap the “Connect” button in the Evothings Viewer app.
  6. Now test your setup; click the Examples tab on Evothings Workbench and press the “Run” button for the “Hello World” example, and see the app load in the client
  7. Make your copy of the “Hello World” example, using the COPY button, give it a name press the RUN button by your new project copy. Now make changes to the source code using your code editor, and see the app automagically reload on your phone or tablet.

Now you know how Evothings Studio works (easy, right?). Let’s move forward with our tutorial. dweet_app

For your ease, I have already completed and uploaded example code described below to my Github repository. Clone or download the repository to your computer, open the publish-dweet-cloud example folder, find index.html file and drag & drop it to the “My Apps” tab of the Evothings Workbench. Press the “Run” button for your new project, it will be loaded into the Evothings Viewer. Now, hold your mobile phone in hand as movement will provide different accelerometer sensor readings while the app will publish 5 dweets to the dweet.io cloud. Once published, it will retrieve and list the last 5 dweets!

Code Explanation

Open the index.html file of the example in your code editor. Scroll down and analyze the JavaScript function of the initialiseAccelerometer() function. This is where we are reading mobile phone’s accelerometer data every 2 seconds. Then in the handler function accelerometerHandler(), we create a JSON string using the x,y and z value respecticvely and publishing it to a dweet, managed via the dweet.io cloud using dweetio.dweet_for function of the dweet.io JavaScript client library. Now, please review the getDweets() function that gets called once we finish publishing five dweets. In this function we are using dweetio.get_all_dweets_for function of the client library to retrieve all the published tweets in descending order. Once received, we parse the data object and list only last five dweets on the mobile app.

Discussion

The dweet.io JavaScript client library does provides a dweetio.listen_for function to capture a dweet as soon as it gets published. If you would publish a train of dweets very quickly in sequence, this can derail transport a bit hence it is better to make one longer dweet with a larger data set in one to. The dweet.io cloud is very reliable for communications which more seldom than say every fifth second or so, which is more than enough for the lion part of most private and public settings. The same JavaScript client and example app code can also be used for Verizon’s Thingspace.io cloud. Only difference at the moment is that the thingspace.io has limited number of API methods available.

Let’s see some data up in the cloud!

For more discussions, visit our forum at evothings.freshdesk.com and use our Gitter chat channel to hangout with likeminded IoT developers!