Making a GET or POST request from the core

I’m very new to this and in all the documentation and community forums I haven’t found how I would make the core do a POST or GET request in response to a pin being set high or low.

I can’t imagine I’m the only one wanting to do this, so if someone could point me in the right direction I would greatly appreciate it.

A while back SaraJo made a great example to tweet from your core, that would be an example of sending a POST request:

2 Likes

Hi @aarobc Where you able to send a GET from the core?

Cheers
Manu

Hi @manuelmasri,

A GET request should be really similar, just switch the method and include the parameters in the url instead of in the body, something like… (I haven’t tested this), where LIB_DOMAIN is a hostname from above:

client.connect(LIB_DOMAIN, 80);
client.println("GET /example.php?arg1=value&arg2=another HTTP/1.0");
client.println("Host: " LIB_DOMAIN);
client.println("Content-Length: 0");
client.println();

I’m very new all around (both to Spark Core and coding) and I was working on a project using the Spark Core and a PIR sensor to send me a text message when motion is detected (I had seen the idea for the Spark Core referenced in Make Magazine issue #36).

In the article they mentioned using Twilio to send the text message. I’m wondering if anyone has written similar code but using Twilio to send a message?

Thanks @Dave!! You are really good!

1 Like

Hi @bstolte,

I suspect the easiest way to do this right now would be for you to have code running on a website or server somewhere running functions or checking variables on the core, and then perform the Twilio API hits from your server. Here’s their quickstart guide for sending SMS: http://www.twilio.com/docs/quickstart/php/sms/sending-via-rest

I think this will become a lot easier when the Spark Cloud supports postback requests, in which case you’d be able to trigger a Twilio SMS with a simple Spark.Event call.

Thanks!
David

Does anyone know exactly how much it would cost to send SMS via Twilio? Looking at the pricing here, it seems like 2 cents USD per SMS. Anyone have experience with this?

http://www.twilio.com/voice/pricing

I think, if you’re using a developer account, you can text and call your main phone # almost as much as you want. When you start texting or calling other phone numbers then you have to pay. – I think if you stay within your country, it’s only 0.75 cents (3/4 of a cent) per message?

1 Like

@BDub I had 3-4 years ago send bulk sms a website. I worked with Mobiweb (http://www.solutions4mobiles.com/) system and used their own API…

Their prices are very reasonable and you can send sms anywhere in the world…If you have any question or request will be here…

1 Like

Worked really good @Dave!!!

2 Likes

Hi @Dave,

how to read out the data on Server Side. Any Ideas?

Hi @dongamelo,

Good question! There are a lot of ways to accomplish this server side. If you want to cause something to happen, or save information when your core makes a HTTP request (GET, POST, etc), then you probably want some code running on the server. This depends on what your hosting environment is (Windows, Linux, etc), and what languages your web server supports ( for example: PHP, Python, Java, Node.js, C#, etc). Most low-cost shared hosting plans support PHP, so for example, if you wanted to POST a variable to a page, you could create a page test.php that contained:
( http://www.php.net/manual/en/reserved.variables.post.php )

<?php
echo 'Hello ' . $_POST["some_argument"] . '!';
?>

That would be sent back to your core. If you wanted to save that data to a file, or a database, then I might recommend something like Mongo which is really easy to get started in. Here’s a tutorial for that: http://blog.mongodb.org/post/24960636131/mongodb-for-the-php-mind-part-1

Thanks!
David

1 Like

Thx Dave,

my Problem was on how to get the variable which i send out of the POST variable. I changed the protocoll and the content type. Now it works. For testing i wrote the POST Request in a axtra .txt file. Everything works fine.

Thanks!
DongiDong

2 Likes

I’m getting the red flash of “Out of heap memory” here. Code is this:

#include <string.h>
#define LIB_DOMAIN "exampleapp.com"

TCPClient client;

void setup() {
}

void loop() {
    client.connect(LIB_DOMAIN, 80);
    client.println("GET /status HTTP/1.0");
    client.println("Host: " LIB_DOMAIN);
    client.println("Content-Length: 0");
    client.println();
}

The app I’m sending the GET request to just returns a text response of “success” so I’m not sure exactly what’s overloading the memory. Thoughts?

Hi @efatsi

You are never reading the response back from the web server and you are looping doing the GET requests as fast as possible. You need to wait for a response with client.available() and then either read or flush those bytes.

1 Like

Thanks @bko I’ll give that a shot. I was tailing the HTTP logs on the server I was making the request to and only saw 1 request come in, so I assumed the core was locking up after the first. But will try again tonight and report back.

Hi @bstolte,

I’ve built a similar project using Ubidots; you can push the PIR sensor data in one line, and then setup the SMS feature inside your Ubidots account. Here two examples:
Logging temp and humidity with Spark Core
Logging light levels with Spark Core