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.
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.
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?
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
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);
}
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
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
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
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” 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.
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.