How to open a URL with Spark Core?


#2

Im not sure but you may add a new line at the end. So server get that your request header is end.

client.println("Host: 1.1.1.1"); 
client.println("User-Agent: Arduino/1.0"); 
client.println("Connection: close");
client.println();

#3

The code below has been very reliable for me:

void sendGetRequest(const char * server, const char * url)
{
    if (myTCP.connect(server, 80)) {
        myTCP.print("GET ");
        myTCP.print(url);
        myTCP.println(" HTTP/1.0");
        myTCP.println("Connection: close");
        myTCP.print("Host: ");
        myTCP.println(server);
        myTCP.println("Accept: text/html, text/plain");
        myTCP.println();
        myTCP.flush();
    } 
}

Fetch txt file from Web
#4

also doesnt work for me :frowning: Where do I find the actual sample code of TCPclient?


#5

A sample TCP client example can be found here: http://docs.spark.io/#/firmware/communication-tcpclient

@Dave @jgoggins can help out with the specifics.


#6

@mohit Thank you for the URL. I tried nearly everything right now. The sample Code works, but my code does not, but I did everything the same :frowning:

This is the URL I want to call: http://146.185.175.78/statussubmit.php?&ph=2&ec=1&temp=25&humidity=60&light=1&waterlvl=0

How would the code look like? I am really stuck.

edit: It should just call the URL once. The reply is not of interest. Simply the call to submit the values.

edit2: I somehow got it working. This is the code that worked for me:

TCPClient client;
byte server[] = { 146, 185, 175, 78 }; // Google
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  delay(5000);
  Serial.println("connecting...");

  if (client.connect(server, 80)) 
  {
    Serial.println("connected");
    client.println("GET /statussubmit.php?&ph=2&ec=1&temp=25&humidity=60&light=1&waterlvl=0 HTTP/1.0");
    client.println();
  } 
  else 
  {
    Serial.println("connection failed");
  }
}

Do I need to close the connection in order to save ressources both on the Spark and my Mysql Database?


#7

Hi @kaul,

I have an expanded HTTP get demo here, although you could remove the http basic auth stuff.

It’d be a good idea to add a:

client.flush();
client.stop();

after your request so you don’t leave sockets open. :slight_smile:

Thanks,
David


#8

I’m hosting my site on Webfaction and I don’t have a dedicated IP address so wondering if there’s a way to use my hostname instead of an IP address? I can’t seem to get the following code to hit a URL when motion has been detected. Any help or advice would be greatly appreciated.

For my project I’ve built a motion detector with a PIR sensor and want the Spark Core to hit a URL that will trigger a text message to my phone (via twilio).

The URL that will trigger the text message is:

bstolte.com/motion_sms/sendsms/4154256133/motion

The code I’m using with the Spark Core looks like the following:

#include <string.h>
char server[] = "bstolte.com";
TCPClient client;

int ledPin = D5;
int inputPin = D7;
int pirState = LOW
int val = 0;

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(inputPin, INPUT);
    Spark.variable("inputPin", &inputPin, INT);
    Serial.begin(9600);
    delay(1000);
    Serial.println("connecting...");

if (client.connect(server, 80))
    {
        Serial.println("connected");
        client.println();
    }
else
    { 
        Serial.println("connection failed");
        client.stop();
    }
}

void loop() {
     val = digitalRead(inputPin);
     if (val == HIGH) {
        digitalWrite (ledPin, HIGH); // then turn on the LED    
        
        // Call bstolte.com/motion_sms/sendsms to send a text message
        client.println("GET /motion_sms/sendsms/4154256133/hello HTTP/1.0");
        
    if (pirState == LOW) {
        pirState = HIGH;
    }
}

else {
    digitalWrite (ledPin, LOW);
    delay(300)
    if (pirState == HIGH){
        pirState = LOW;
    }
}
}

#9

There’s some missing lines after

Try adding:

client.print("Host: "); client.println(server); client.println("Accept: text/html, text/plain"); client.println(); client.flush();


#10

YES! Thank you Kenneth! That worked :smile:


#11

You should thank @bko man.

The code is actually above :smiley:


#12

Ahh I missed that last bit.

I’m actually only able to get it to trigger and send the text message once. I thought it would trigger a call to that URL each time motion was detected - each time “val” was set to HIGH I thought it would run the call to the URL by putting it into that section of code.

Apologies - I’m totally new and just learning to code.


#13

consider adding client.close(); after client.flush();


#14

Unfortunately I get an error

error: 'class TCPClient' has no member named 'close'

So I tried client.stop(); based on the documentation but unfortunately that stopped it from sending any texts.

I’m going to continue trying to tweak it until it can repeatedly send a text each time motion is detected.


#15

Hmmmm. I’m thinking there’s some logic/condition that is not right in your code.

Also, some minor stuff like your client.connect () runs only once outside loop ()

We would need it to be inside to open a connect, complete close and repeat

Try a simple loop to run the send code every 2 seconds. It should work fine.

After that, work on the logic to read the PIR :slight_smile:


#16

Today I am unable to use curl to turn on and off a LED that I was able to do last week.

Error = Invalid Function.

Function is digitalwrite using Tinker App loaded on core.

?? Hmm.


#17

@spydrop,

there’s a couple of community members mentioning this as well but haven’t pinpoint the issue as yet

  1. What language are you using for CURL? Or some online tool?

  2. I used spark-cli with Tinker and managed to call the functions

  3. I used a Python script with request module to call the digitalwrite and managed to get it to work.

You might want to comment at

and keep this thread for the new user to figure out his code :smiley:


#18

Here’s a simple for loop that should send 10 texts every 5 seconds. Unfortunately I’m just getting one text sent through. I also updated the code to be closer to what @bko posted earlier in the thread except I replaced “myTCP.foo” with “client.foo”

I’m stuck as to why we couldn’t call the block of code in the for loop multiple times.

char server[] = "bstolte.com";
TCPClient client;

void setup() {

Serial.begin(9600);
delay(1000);
client.connect(server, 80);

}

void loop() {

for (int x = 0; x < 10; x ++) {
   client.println("GET /motion_sms/sendsms/4154256133/hello HTTP/1.0");
   client.println("Connection: close");
   client.print("Host: ");
   client.println(server);
   client.println("Accept: text/html, text/plain");
   client.println();
   client.flush();
   delay(5000);
}

}

#19

Because there’s a reply from the server, the connection is not closed etc…
and you didn’t read it in so there’s some issue. Give me a second let me try to modify the code

EDIT: I haven’t been able to have a work code yet and gotta be off to school now. Try out a simple code to be working and continue from there!

This code is kinda working… Just that the no data is being read in. @bko wanna give a hand? :smiley:

gist.github.com/kennethlimcp/df875f7851a2a19069a8

Looking at the Serial Output on my terminal i get:

> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped
> connecting
> connected
> no data
> Client stopped

Looks like i can get the connection running, disconnect and redo for 10 times but only the data is not coming in

Dang! I found an example here:

http://arduino.cc/en/Tutorial/WebClientRepeating

Starting to appreciate all the Spark.variables (), Spark.functions () etc!

But im so happy I managed to almost get it working by myself! :smiley:


#20

Hi @bstolte (another Brian!)

The GET request has a Connection: close which closes the connection after your request. If you move the client.connect(server,80); inside the for loop, it should work. You can add a client.stop(); after the flush (which flushes the receive side of the client from web host to core). I would recommend that you go ahead and close the connection if you are not going to use it for 5 seconds.

void loop() {

for (int x = 0; x < 10; x ++) {
   client.connect(server, 80);
   client.println("GET /motion_sms/sendsms/4154256133/hello HTTP/1.0");
   client.println("Connection: close");
   client.print("Host: ");
   client.println(server);
   client.println("Accept: text/html, text/plain");
   client.println();
   client.flush();
   client.stop();
   delay(5000);
}

Please let us know if that works for you!


#21

Thank you @bko and @kennethlimcp.

I tried putting in the code from @bko last message but it wouldn’t send any texts. Here’s the full code for the loop that I tried.

If I removed client.stop(); the loop would send six texts before the Spark Core started flashing red. The core flashed red at least 3x (maybe 3x times a couple of times) which seem to indicate a connection failure based on the documentation here. I’m guessing it’s getting overloaded trying to keep hitting that URL? It’s why I put in the delay(5000); but that didn’t seem to help.

For reference here’s the code I’m trying to get to work. Each time the PIR sensor detects motion it’s supposed to turn on an LED and call a URL to trigger a text message to my phone.

Any suggestions are greatly appreciated. Thank-you :smile: