Skip to content

Instantly share code, notes, and snippets.

@tomconte
Last active September 4, 2017 17:54
Show Gist options
  • Select an option

  • Save tomconte/4fe5ab0f29d60253d1a3 to your computer and use it in GitHub Desktop.

Select an option

Save tomconte/4fe5ab0f29d60253d1a3 to your computer and use it in GitHub Desktop.
This sample Arduino sketch uploads telemetry data to Azure Mobile Services
/*
** This sample Arduino sketch uploads telemetry data to Azure Mobile Services
** See the full article here: http://hypernephelist.com/2014/07/11/arduino-uno-azure-mobile-services.html
**
** Thomas Conté @tomconte
*/
#include <SPI.h>
#include <Ethernet.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";
EthernetClient client;
char buffer[64];
/*
** Send an HTTP POST request to the Azure Mobile Service data API
*/
void send_request(int value)
{
Serial.println("\nconnecting...");
if (client.connect(server, 80)) {
Serial.print("sending ");
Serial.println(value);
// POST URI
sprintf(buffer, "POST /tables/%s HTTP/1.1", table_name);
client.println(buffer);
// Host header
sprintf(buffer, "Host: %s", server);
client.println(buffer);
// Azure Mobile Services application key
sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);
client.println(buffer);
// JSON content type
client.println("Content-Type: application/json");
// POST body
sprintf(buffer, "{\"value\": %d}", value);
// Content length
client.print("Content-Length: ");
client.println(strlen(buffer));
// End of headers
client.println();
// Request body
client.println(buffer);
} else {
Serial.println("connection failed");
}
}
/*
** Wait for response
*/
void wait_response()
{
while (!client.available()) {
if (!client.connected()) {
return;
}
}
}
/*
** Read the response and dump to serial
*/
void read_response()
{
bool print = true;
while (client.available()) {
char c = client.read();
// Print only until the first carriage return
if (c == '\n')
print = false;
if (print)
Serial.print(c);
}
}
/*
** Close the connection
*/
void end_request()
{
client.stop();
}
/*
** Arduino Setup
*/
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.println("ethernet");
if (Ethernet.begin(mac) == 0) {
Serial.println("ethernet failed");
for (;;) ;
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
/*
** Arduino Loop
*/
void loop()
{
int val = analogRead(A0);
send_request(val);
wait_response();
read_response();
end_request();
delay(1000);
}
@coelho5br
Copy link
Copy Markdown

Hi there,

I am neewbie, but it seems that Azure Mobile Services was discontinued. What do you recommend to record that on Azure?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment