Skip to content

Instantly share code, notes, and snippets.

@tomconte
Created August 19, 2014 07:51
Show Gist options
  • Save tomconte/3ec8e8dae6296250e79e to your computer and use it in GitHub Desktop.
Save tomconte/3ec8e8dae6296250e79e to your computer and use it in GitHub Desktop.
/*
** This sample Arduino sketch uploads telemetry data to Azure Mobile Services on an Arduino Yun
** The Bridge library is used to run cURL to send the data securely using SSL.
** See the full article here: http://hypernephelist.com/2014/08/19/https_on_arduino_yun.html
**
** Thomas Conté @tomconte
*/
#include <Process.h>
// Ethernet shield MAC address (sticker in the back)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Azure Mobile Service address
// You can find this in your service dashboard
const char *server = "arduinouno.azure-mobile.net";
// Azure Mobile Service table name
// The name of the table you created
const char *table_name = "telemetry";
// Azure Mobile Service Application Key
// You can find this key in the 'Manage Keys' menu on the dashboard
const char *ams_key = "YaRWxxxxzKGRxxxxLPiNxxxxXYOvxxxx";
Process proc;
char buffer[64];
/*
** Send an HTTP POST request to the Azure Mobile Service data API
*/
void send_request(int value)
{
Console.print("sending ");
Console.println(value);
proc.begin("curl");
proc.addParameter("-k");
proc.addParameter("-X");
proc.addParameter("POST");
proc.addParameter("-H");
proc.addParameter("Content-Type:application/json");
// Azure Mobile Services application key
proc.addParameter("-H");
sprintf(buffer, "X-ZUMO-APPLICATION:%s", ams_key);
proc.addParameter(buffer);
// POST body
proc.addParameter("-d");
sprintf(buffer, "{\"value\": %d}", value);
proc.addParameter(buffer);
// POST URI
sprintf(buffer, "https://%s/tables/%s", server, table_name);
proc.addParameter(buffer);
// Run the command synchronously
proc.run();
}
/*
** Wait for response
*/
void wait_response()
{
while (!proc.available()) {
delay(100);
}
}
/*
** Read the response and dump to console
*/
void read_response()
{
bool print = true;
while (proc.available()) {
char c = proc.read();
// Print only until the first carriage return
if (c == '\n')
print = false;
if (print)
Console.print(c);
}
Console.println();
}
/*
** Arduino Setup
*/
void setup()
{
Bridge.begin();
Console.begin();
while (!Console){
; // wait for Console port to connect.
}
Console.println("ready.");
}
/*
** Arduino Loop
*/
void loop()
{
int val = analogRead(A0);
send_request(val);
wait_response();
read_response();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment