How to open a URL with Spark Core?

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.

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

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.

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:

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.

@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:

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);
}

}

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:

1 Like

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!

1 Like

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:

If stop is not called, the core keeps opening new connections till the max it can opened (can’t remember the max offhand) and caused the failure.

Shall work on it tonight :wink:

Is there an output I should get when I call your url?

@bstolte,

You can probably know i got it working with the logs on your server side :wink:

I managed to get the sending working and made into a function added in your code.

gist.github.com/kennethlimcp/df875f7851a2a19069a8

Can you try and tell me how its goes? :smile:

That works!! :smiley:

The PIR picks up the motion and calls that URL which then sends a text to my phone. It does send 3-4 texts for each time it detects motion but it’s working.

Thank you very much for all your help, greatly appreciate it. I’ll keep tweaking it from here to see if I can get it to just text once for each time it detects motion. If you’re interested you can see each time it generates a text message here: http://bstolte.com/motion_sms/

Side note: it’s pretty cool when my phone starts getting text messages when you’re working on the code :smile:

@bstolte you need to work on the code.

I’m getting my core all cranky cos i don’t have the PIR attached and i think when the loop keeps calling the getrequest() too often crazy stuff happens!

let me edit and see how things go.

The idea is you need to restrict sending to once every X seconds

@kennethlimcp Yup my core is cranky too. It seems to detect the motion, send a few texts and then reset itself.

I’ll try and get it down to sending once every X secs and post the code back here.

It’s a big issue actually cos the core gets into some form of panic which might be hard to resolve.

Add the

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

before the return(1) and see how it turns out

Just a follow up. If I include client.stop(); it will run the code but not hit the URL and trigger the text message. If I remove client.stop(); it will run and send text messages.

In both cases, from what I’m seeing in the serial output , it’s cycling through the motion detected loop anywhere from 3-7 times.

Connected
Motion detected!
Connected
Connected
Connected
Motion ended!

“Connected” occurs in the getrequest() and “Motion detected” happens right after. It looks like it cycles back through getrequest() a few more times perhaps because the PIR sensor is still reading motion or is just slow to clear out after motion has been detected. I tried putting delays in there but didn’t really help.

I’m thinking I need to re-write so I can limit the call to getrequest() just once per motion being detected and then let it clear out.

Latest here but haven’t had much time in the past couple of days to play with it.

1 Like

Yeah you need to limit like interval before a next send can be executed! :wink:

Hi @bstolte

I think if you add small delay like delay(50); before the client.stop() you should find it will work. I think you are killing the connection before it has a chance to finish sending.

It would also be better to add rate limiting in the real application by saving the value of millis(); when you send and not sending for a certain time after than.

2 Likes

Hi @bko and @kennethlimcp

Just wanted to say thanks again for all the help. @bko suggestion was spot on, using millis();, and I found code that “solved” the problem here.

Thanks!! :smile:

3 Likes