Howard
January 1, 2015, 9:35am
1
Just a little note for those using a Spark Core. There is a know issue with the TI WiFi modem where on some slow networks the DNS server is not set up correctly. Simple to solve instead of
request.hostname = "things.ubidots.com";
Use
IPAddress ubidotsIP = (50,23,124,68);
request.ip = ubidotsIP;
Enjoy, and let’s hope your IP address does not change anytime soon.
Howard
January 2, 2015, 9:30am
2
For reason I do not understand it appears that the solution above does not work either. You need
byte ubidotsIP[] = { 50,23,124,68 };
request.ip = ubidotsIP;
aguspg
January 3, 2015, 12:44am
3
Thanks @Howard Indeed, there’s a known issue with some Sparks not doing DNS resolution. Here’s the code that solved it for us:
#include "dnsclient/dnsclient.h"
#include "HttpClient/HttpClient.h"
HttpClient http;
char resultstr[64];
float value = 0.0;
#define VARIABLE_ID "5499ba207625421efc176fdc"
#define TOKEN "j2aawKrPRmPRszEbwauSlnJEA2xxxx"
http_header_t headers[] = {
{ "Host", "things.ubidots.com" },
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL }
};
http_request_t request;
http_response_t response;
IPAddress dnsServerIP(8,8,8,8);
IPAddress remote_addr;
DNSClient dns;
char serverName[] = "things.ubidots.com";
void setup() {
request.port = 80;
dns.begin(dnsServerIP);
dns.getHostByName(serverName, remote_addr);
request.ip = remote_addr;
request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
Serial.begin(9600);
}
void loop() {
value++;
sprintf(resultstr, "{\"value\":%.4f}", value);
request.body = resultstr;
http.post(request, response, headers);
Serial.println(response.status);
Serial.println(response.body);
}
This code uses some kind of internal DNS server inside the Spark, which can be seen in this line:
IPAddress dnsServerIP(8,8,8,8);