I’m trying to get my spark core to send an SMS using the twilio REST api. Been really unsuccesful so far. Any tips or pointings out mistakes would be greatly appreciated. I’m new to spark aswell as the REST API scene.
My acc SID and my auth token were replaced in the code with ACC_SID_GOES_HERE and AUTH_TOKEN_HERE.
Twilio requires you to send info such as To, From, Body and I’m not sure how that would be implemented as parameters of the Post function ?
F.ex
to : “my Phone number"
from :” twilio phone number"
body: “nice text im sending you”
how would I send this info to twlio ? and am i even using the right approach to the REST api ?
https://www.twilio.com/docs/api/rest/sending-messages - here’s what I found so far and here’s my code
// This #include statement was automatically added by the Spark IDE.
#include "application.h"
#include "HttpClient/HttpClient.h"
int led=D7;
unsigned int nextTime = 0; // Next time to contact the server
HttpClient http;
http_header_t headers[] = {
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
void setup() {
pinMode(led, OUTPUT); // set the D7 LED as output
Spark.function("blink",blinkfunc); // a POST request for "blink" will reference blinkfunc, defined below
request.hostname = "https://ACC_SID_GOES_HERE:AUTH_TOKEN_HERE@api.twilio.com/2010-04-01";
request.port = 80;
request.path = "/Accounts/ACCSID_GOES_HERE/Messages";
http.post(request, response, headers);
}
// call the below function when the POST request matches it
int blinkfunc(String command) {
http.post(request, response, headers);
digitalWrite(led, HIGH); // turn it on
delay(1000); // wait 1 second
digitalWrite(led, LOW); // turn it off
delay(1000); // wait another second
return 1; // return 1 to show that this worked.
}
void loop() {
//not doing anything here
}
I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy