Editing Online Thermometer

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 11: Line 11:
 
Read the temperature from a sensor and post it online through the cloud servers of ElectricImp, Carriots and Ducksboard
 
Read the temperature from a sensor and post it online through the cloud servers of ElectricImp, Carriots and Ducksboard
  
[[File:Flow eImp carriots ducksboard en.jpg|860px]]
+
[[File:Flow eImp carriots ducksboard en.jpg]]
 
+
[[File:HW_Temperature_eImp.JPG|860px]]
+
  
 
=== Motivation ===
 
=== Motivation ===
Line 126: Line 124:
 
Detailed steps:
 
Detailed steps:
 
* Log in to electricImp to access its IDE
 
* Log in to electricImp to access its IDE
* Look for the imp device that we are using in the left navigation menu (the imp device might be under the "New Devices" tab or under the tab or any of the models that have been defined)
+
* Look for the 'imp' device that we are using in the left navigation menu (the 'imp' device might be under the "New Devices" tab or under the tab or any of the models that have been defined)
* Click on the settings icon to the right of the imp device
+
* Click on the settings icon to the right of the 'imp' device
 
* Click on the "Associated Model" drop-down list and type the name of the new model that we are going to create "Temperature Carriots".  
 
* Click on the "Associated Model" drop-down list and type the name of the new model that we are going to create "Temperature Carriots".  
 
* Click "Save Changes"
 
* Click "Save Changes"
* The "Temperature Carriots" model will appear in the left navigation menu. Click on it, the name of our device will pop up. Either automatically or after clicking on the device name, the main area of the IDE will display the code editor where we will define the application
+
* The "Temperature Carriots" model will appear in left the navigation menu. Click on it, the name of our device will pop up. Either automatically or after clicking on the device name, the main area of the IDE will display the code editor where we will define the application
 
* Our application requires code on the device as well as on the server. The server code is included in the box called "Agent" and the device code  in the "Device" box.
 
* Our application requires code on the device as well as on the server. The server code is included in the box called "Agent" and the device code  in the "Device" box.
 
* Copy and paste the code below inside the "Device" box
 
* Copy and paste the code below inside the "Device" box
Line 176: Line 174:
 
  device.on("sendTemperature", postTemperature);
 
  device.on("sendTemperature", postTemperature);
  
'''Note:''' You have to replace in the code <code>your_device_id</code> and <code>your_api_key</code> by the corresponding values ​​in Carriots (read Section [[#Detailed explanation of the agent code on ElectricImp server]] for further info
+
'''Note:''' You have to replace in the codethe strings <code>your_device_id</code> and <code>your_api_key</code> by the values ​​obtained in the previous step
  
* Click "Build and Run" button at the top of the IDE. The application compiles and any errors are displayed in the console box. If there are no errors, electric imp server will download the application into your imp device over Internet (the imp must be on and connected to a wifi network)
+
* Click "Build and Run" button at the top of the IDE. The application compiles and any errors are displayed on the console box. If there are no errors, electric imp server will download the application into your 'imp' over Internet (the 'imp' must be on and connected to a wifi network)
 
* Once the application is loaded, it will begin to run. The imp device waits to receive data from Arduino through the serial connection. When it receives a temperature reading, it forwards it to the agent on the ElectricImp server and also post a new message in the Log window of the IDE. On receiving this new reading, the agent forwards it to Carriots server and posts a new message in the Log window registering the result of this submission
 
* Once the application is loaded, it will begin to run. The imp device waits to receive data from Arduino through the serial connection. When it receives a temperature reading, it forwards it to the agent on the ElectricImp server and also post a new message in the Log window of the IDE. On receiving this new reading, the agent forwards it to Carriots server and posts a new message in the Log window registering the result of this submission
  
===== Detailed explanation of the imp device code  =====
+
===== Detailed explanation of the 'imp' device code  =====
  
In the previous tutorial we explained that the imp code is executed sequentially starting from the first line (functions are identified with the <code>function</code> key word and are skipped from the sequential flow).
+
In the previous tutorial we explained that the 'imp' code is executed sequnetially starting from the first line (functions are identified with the <code>function</code> key word and are skipped from the sequential flow).
  
 
Disclaimer: I am not an expert in squirrel, the programming language used by electricImp. I apologise for the poor quality of the code
 
Disclaimer: I am not an expert in squirrel, the programming language used by electricImp. I apologise for the poor quality of the code
Line 193: Line 191:
 
  arduino <- hardware.uart57;
 
  arduino <- hardware.uart57;
  
This variable is initialised further down in the code. On top of defining the technical parameters of the serial connection, we also indicate the function (''arduinoData()'') that the imp device will call every time that it receives new data over the connection:
+
This variable is initialises further down in the code. On top of defining the technical parameters of the serial connectiom, we also indicate the function (''arduinoData()'') that the 'imp' device will call every time that it receives new data over the connection:
  
 
  arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, arduinoData);
 
  arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, arduinoData);
 
   
 
   
The electricImp docummentation has more information about how to manage serial connections in imp [http://www.electricimp.com/docs/api/hardware/uart/]
+
The electricImp docummentation has more information about how to manage serial connections in 'imp' [http://www.electricimp.com/docs/api/hardware/uart/]
 +
 
 +
The  ''arduinoData()'' function is responsible for listening to the Arduino serial connection. When a byte is received, the 'imp' device calls this function. ''arduinoData()'' reads the input bytes and stores them as a char string in ''s''. This process continues until a byte valued ''0xFC'' is received. This value is the same one defined in the Arduino sketch to identify the end of a data transmission. If more than 10 bytes are received without receiving an end code, the function assumes that the data is corrupt and it resets variable ''s''.
  
The  ''arduinoData()'' function is responsible for listening to the Arduino serial connection. When a byte is received, the imp device calls this function. ''arduinoData()'' reads the input bytes and stores them as a char string in ''s''. This process continues until a byte valued ''0xFC'' is received. This value is the same one defined in the Arduino sketch to identify the end of a data transmission. If more than 10 bytes are received without receiving an end code, the function assumes that the data is corrupt and it resets variable ''s''.
+
When the code to send the function receives arduinoData () sends the string stored in the variable s (the data received from Arduino) the agent is running on the server in the cloud ElectricImp:
  
 
When an end code ''0xFC'' is received, ''arduinoData()'' sends the string stored in variable ''s'' to the electrcImp agent that is running in the cloud:
 
When an end code ''0xFC'' is received, ''arduinoData()'' sends the string stored in variable ''s'' to the electrcImp agent that is running in the cloud:
Line 205: Line 205:
 
  agent.send("sendTemperature", s);
 
  agent.send("sendTemperature", s);
  
Function ''agent.send()'' has two parameters. The first one is the name of the message that will be received by the agent in the server (''sendTemperature'' in our code). The second parameter is  the object that will be passed as a parameter to the agent function that will receive the message. More information can be found in the API docummentation of electricImp ([http://www.electricimp.com/docs/api/agent/send/] , [http://www.electricimp.com/docs/api/agent/] )
+
Function ''agent.send()'' has two parameters. The first one is the name of the message that will be recived by the sgent in the server (''sendTemperature'' in our code). The second parameter is  the object that will be passed as a parameter to the agent function that will receive the message. ore information can be found in the API docummentatio of electricImp ([http://www.electricimp.com/docs/api/agent/send/] , [http://www.electricimp.com/docs/api/agent/] )
  
 
===== Detailed explanation of the agent code on ElectricImp server =====
 
===== Detailed explanation of the agent code on ElectricImp server =====
 
The agent is also coded in squirrel.
 
The agent is also coded in squirrel.
  
In this project, the agent is only triggered when it receives a message from its imp associated device. We have just explained that the imp device calls function ''agent.send()'' to send a message to the agent. On the agent side, you use the function ''device.on()'' to register the function that will process this incoming message:
+
In this project, the agent is only triggered when it receives a message from its 'imp' associated device. We have just explained that the 'imp' device calls function ''agent.send()'' to send a message to the agent. On the agent side, you use the function ''device.on()'' to register the function that will process this incoming message:
  
 
  device.on("sendTemperature", postTemperature);
 
  device.on("sendTemperature", postTemperature);
Line 216: Line 216:
 
This function has two parameters. The first one is the name of the message (''sendTemperature'' in our case) and the second one is the function in the agent that will process this message (''postTemperature()'' in the example). The second parameter that the 'imp' device includes when calling the function  ''agent.send()'' is an object whose value is passed as a parameter to the function indicated by the agent.
 
This function has two parameters. The first one is the name of the message (''sendTemperature'' in our case) and the second one is the function in the agent that will process this message (''postTemperature()'' in the example). The second parameter that the 'imp' device includes when calling the function  ''agent.send()'' is an object whose value is passed as a parameter to the function indicated by the agent.
  
Focusing on our example, the imp device sends the message ''sendTemperature'' and includes the string received from Arduino as the second parameter (i.e. a temperature reading). The agent on the server receives this message and calls the function ''postTemperature()'' passing as parameter the string generated by  the Arduino (temperature reading).
+
Focusing on our example, the 'imp' device sends the message ''sendTemperature'' and includes the string received from Arduino as the second parameter (in fact, a temperature reading). The agent on the server receives this message and calls the function ''postTemperature()'' passing as parameter the string generated by  the Arduino (temperature reading).
  
The ''postTemperature()'' function just formats an http POST request according to the API of Carriots. The request includes in its BODY  a JSON string with the temperature value received from the device. This request creates a new reading in Carriots server.
+
The ''postTemperature()'' function just formats an http POST request according to API of Carriots. The request includes in its BODY  a JSON string with the temperature value received from the device. This request adds a new reading in Carriots server.
  
 
Details on how the http request works are described in the API documentation of eletricImp [http://www.electricimp.com/docs/api/http/ http], [http://www.electricimp.com/docs/api/http/post/ http POST], [http://www.electricimp.com/docs/api/httprequest/ http request].
 
Details on how the http request works are described in the API documentation of eletricImp [http://www.electricimp.com/docs/api/http/ http], [http://www.electricimp.com/docs/api/http/post/ http POST], [http://www.electricimp.com/docs/api/httprequest/ http request].
Line 228: Line 228:
 
To retrieve the API key, go to Carriots console and click on "MySettings" tab and then on "My Account" on the menu at the top of the screen. The API key value is the one named ''Full Privileges Apikey''.
 
To retrieve the API key, go to Carriots console and click on "MySettings" tab and then on "My Account" on the menu at the top of the screen. The API key value is the one named ''Full Privileges Apikey''.
  
Carriots expects a JSON object as the BODY of the http request. Its format is as follows:
+
Carriots expects a JSON object as the BODY of the http request, . Its format is as follows:
  
 
  {
 
  {
Line 240: Line 240:
 
  }
 
  }
  
The timestamp of a reading is  indicated in the parameter ''at''. In the example, we use the string ''now'' . This value tells Carriots to store the received reading with the value of the system time of its server when the message is received.  
+
The timestamp of a reading is  indicated in the parameter ''at''. In the example, we use the string ''now'' . This value indicates the Carriots to store the received reading with the system of Carriots server when the message is received.  
  
 
Replace ''your_device_id'' in the agent code with the actual identity of the device in Carriots (e.g. ''TempMSM@makespacemadrid'').
 
Replace ''your_device_id'' in the agent code with the actual identity of the device in Carriots (e.g. ''TempMSM@makespacemadrid'').
Line 251: Line 251:
 
The first step is to create a Ducksboard account [https://app.ducksboard.com/signup/] (the trial account is free for one month but then you need to pay a subscription to keep the service).
 
The first step is to create a Ducksboard account [https://app.ducksboard.com/signup/] (the trial account is free for one month but then you need to pay a subscription to keep the service).
  
After creating the account, the next step is to personalise a dashboard. We will use the "Main dashboard" generated by default when the account is created. We will add two "widgets" or windows to the dashboard. In one of them we will just display the last temperature reading received. The other widget will show a list or "timeline" of the latest reported values.
+
After creating the account, the next step is to personalise a dashboard. We will use the "Main dashboard" generated by default when the account is created. We will add two "widgets" or windows to the dashboard. In one of them we will just display the last temperature reading received. The other widget will show a list or "timeline" of the ñatest reported values.
  
  
Line 261: Line 261:
  
 
Now you can personalise the visual parameters of the widget:
 
Now you can personalise the visual parameters of the widget:
* Name: The text that will be shown to identify the widget window
+
* Name: The text that will shown to identify the widget window
 
* Color: The font color used to dsplay the sensor value
 
* Color: The font color used to dsplay the sensor value
 
* Timeframe: The time scale used to represent the historical graph in the background (hours, days, weeks, etc.)
 
* Timeframe: The time scale used to represent the historical graph in the background (hours, days, weeks, etc.)
Line 278: Line 278:
 
The second widget represents the "timeline" of the latest tempereature values received by Ducksboard. From the "Main dashboard" click again on "+" button and then on "Show your own data". Now select "Your own text" and "Timelines" and finally choose "Size: 1 column/2 rows" option. We vill use  later "PUSH DATA TO THIS URL" and "YOUR API KEY" values from "DATA&API" tab as in the previous widget.
 
The second widget represents the "timeline" of the latest tempereature values received by Ducksboard. From the "Main dashboard" click again on "+" button and then on "Show your own data". Now select "Your own text" and "Timelines" and finally choose "Size: 1 column/2 rows" option. We vill use  later "PUSH DATA TO THIS URL" and "YOUR API KEY" values from "DATA&API" tab as in the previous widget.
  
You can modify some configuration parameters of the dashboard by clicking on the settings icon (gear wheel) in the main window. For example you can change the background or the name of the dashboard. You can also share the dashboard through a public link or URL ("SHARE THIS DASHBOARD" tab inside "PREFERENCES FOR THIS DASHBOARD"). Ducksboard lets you give a name to the link for later reference. You can also protect the dashboard with a password so that only people that know the secret password can access the dashboard content. Use  "COPY LINK" to copy the public URL of the dashboard to the clipboard.
+
You can modify some configuration parameters of the dashboard by clicking on the settings icon (gear wheel) in the main window. For example you can change the background or the name of the dashboard. You can also share the dashboard through a public link or URL ("SHARE THIS DASHBOARD" tab inside "PREFERENCES FOR THIS DASHBOARD"). Ducksboard lets you give a name to the link for later reference and protect it with password so that only people that know the secret password can access the dashboard content. Use  "COPY LINK" to copy the public URL of the dashboard to the clipboard.
  
 
==== 6th Step: Publish temperature readings in Ducksboard from Carriots server ====
 
==== 6th Step: Publish temperature readings in Ducksboard from Carriots server ====
In this final step, Carriots server publishes temperature readings in the dashboard that we just created. To do this, Carriots uses  [http://dev.ducksboard.com/apidoc/slot-kinds/ Ducksboard's http API].
+
In this final step, the Carriots server publishes temperature readings in the dashboard that we just created. To do this, Carriots uses  [http://dev.ducksboard.com/apidoc/slot-kinds/ Ducksboard's http API].
  
Carriots platform lets you define "listeners", blocks of code that are excuted when certain events are triggered (e.g. the reception of a new sensor reading). This code is run in the cloud server of Carriots and is written in "groovy", a dynamic language based on the Java Virtual Machine (JVM). A listener is esentially a rule based on the result of a condition: if A then do B; otherwise do C.
+
Carriots platform lets you define "listeners", blocks of code that are excuted when certain events are triggered (e.g. the recption of a new sensor reading). This code is run in the cloud server of Carriots and is written in "groovy", a dynamic language based on the Java Virtual Machine (JVM). A listener is esentially a rule based on the result of a condition: if A then do B; otherwise do C.
  
 
[[File:Carriots_listener.jpg]]
 
[[File:Carriots_listener.jpg]]
Line 291: Line 291:
 
* Name: the name of the new listener (e.g. "temperatureDucksboard")
 
* Name: the name of the new listener (e.g. "temperatureDucksboard")
 
* Description: text explaining the function of the listener (e.g. "send temperature readings to Ducksboard")
 
* Description: text explaining the function of the listener (e.g. "send temperature readings to Ducksboard")
* Entity type: ''Device'' (this indicates that the listener applies only to a device)
+
* Entity type: ''Device'' (e.g. "This indicates that the listener applies only to a device")
 
* Id: select the id of the device creted in Step 3 (e.g. "TempMSM@makespacemadrid")
 
* Id: select the id of the device creted in Step 3 (e.g. "TempMSM@makespacemadrid")
 
* Event: ''Event Data Received'' (the listener will be triggered everytime a new reading is received from the selected device)
 
* Event: ''Event Data Received'' (the listener will be triggered everytime a new reading is received from the selected device)
Line 322: Line 322:
  
  
The code in the ''Then'' clause just sends two http POST requests to Ducksboard server. The payload/BODY of the requests follows the JSON format defined in Ducksboard's API. The first request is sent to ''Counter'' widget while the second one is sent to ''Timeline'' widget. There is a [https://www.carriots.com/tutorials/others_APIs/ducksboard detailed tutorial] by Carriots that explains this code.
+
The code in the ''Then'' clause just sends two http POST requests to Ducksboard server. The payload/BODY of the requests follows the JSON format defined in Ducksboard's API. The first request is sent to ''Counter'' widget while the second one is sent to ¡¡Timeline'' widget. There is a [https://www.carriots.com/tutorials/others_APIs/ducksboard detailed tutorial] by Carriots that explains this code.
  
'''Important:''' replace in the code ''your_ducksboard_api_key, your_ducksboard_PUSH_URL_counter_widget, your_ducksboard_PUSH_URL_timeline_widget'' by the corresponding values obtained in Step 5
+
'''Important:''' repalce in the code ''your_ducksboard_api_key, your_ducksboard_PUSH_URL_counter_widget, your_ducksboard_PUSH_URL_timeline_widget'' by the corresponding values onbtained in Step 5
  
Once the listener has been created, any new temperature reading received by Carriots from electricImp (Step 4) will be forwarded to the two Ducksboard widgets. Additionally the values will also be stored internally in Carriots server.
+
Once the listener has been created, any new temperature reading received by Carriots from electricImp (Step 4) will be forwarded to Ducksboard widgets. Additionally the values kill also be stored internally in Carriots server.
  
 
'''Note: ''' Temperature readings could be sent directly from electricImp agent to Ducksboard widgets without using Carriots server, but then we would lose the historical series of readings and the possibility to perform offline review and data mining.
 
'''Note: ''' Temperature readings could be sent directly from electricImp agent to Ducksboard widgets without using Carriots server, but then we would lose the historical series of readings and the possibility to perform offline review and data mining.

Please note that all contributions to Wiki Makespace Madrid may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Wiki Makespace Madrid:Copyrights for details). Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to type the two words you see in the box below:

Cancel | Editing help (opens in new window)