I'm new to the Arduino Yun and I'm trying to convert a sketch written for the Arduino Uno with a Ethernet Shield. The sketch does HTTP posts to Azure Mobile Services.
I've added in the Bridge.h and YunClient.h libraries in place of the Ethernet.h one for the Uno.
My modified sketch compiles, however, I don't think the http posts are working. I don't see my table updating in Azure.
/*
** This sample Arduino sketch uploads telemetry data to Azure Mobile Services
** See the full article here: <a href="http://hypernephelist.com/2014/07/11/arduino-uno-azure-mobile-services.html" rel="nofollow noreferrer">http://hypernephelist.com/2014/07/11/arduino-uno-azure-mobile-services.html</a>
**
** Thomas Conté @tomconte
*/</p>
#include <Bridge.h>
#include <YunClient.h>
//#include <Ethernet.h> // removed as not used by Yun
#include <SPI.h>
// not sure if these libraries are required?
#include <HttpClient.h>
#include <Process.h>
// Ethernet shield MAC address (sticker in the back)
//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// 90:A2:DA:F8:28:38
// Azure Mobile Service address
// You can find this in your service dashboard
const char *server = "myhost.azure-mobile.net";
// Azure Mobile Service table name
// The name of the table you created
const char *table_name = "my_data";
// Azure Mobile Service Application Key
// You can find this key in the 'Manage Keys' menu on the dashboard
const char *ams_key = "HJRxFXXXXXXXXXXXXXXXXXXXNQoMcXXXXXXx99";
//EthernetClient client;
YunClient client;
char buffer[64];
/*
** Send an HTTP POST request to the Azure Mobile Service data API
*/
void send_request(int value)
{
Serial.println("\nconnecting...");
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);
}
/*
** 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);
Serial.println("Starting Bridge");
Bridge.begin();
}
/*
** Arduino Loop
*/
void loop()
{
int val = analogRead(A0);
send_request(val);
wait_response();
read_response();
end_request();
delay(1000);
}