Bus Stop Light / Code Review

I use the onboarrd RGB LED that shows when it is the right time to leave and catch a bus. (Green=Good to go, Orange=run!, Red=too late)

Edit:
I posted a new version on github, with more functions and better documentation and hardware list:

you can copy and paste the features that you like and create your own.

7 Likes

Very cool! I love the idea of this.

The only thing that jumped out as something to change is line #3 of the application.cpp:

It should probably be:
char request[] = "GET /php_webservice/?from=Bern&to=Bern,Inselspital HTTP/1.0"; (which works)

or

char request[] = "GET /php_webservice/index.php?from=Bern&to=Bern,Inselspital HTTP/1.0"; (which makes it really obvious whatā€™s happening)

Or something that prompts the user to change the directory name they uploaded the index.php file into. Not a big deal for most users that would install this I guess.


So when I tried running the example on my website if I go to the URL in my browser itā€™s returning (--)red but the response from the Spark Core is:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>ThĀ%
unknown command: HTTP/1.1 301 Moved Permanently
Date: Thu, 02 Jan 2014 17:40:18 GMT
Server: Apache
Location: http://technobly.com/php_webservice/index.php?from=Bern&to=Bern,Inselspital
Content-Length: 281
Connection: close
Content-Type: text/html; charset=iso-8859-1

BTW: I didnā€™t use ā€œphp_webserviceā€ as my directory, so donā€™t think that link works above :wink: ā€¦this is not whatā€™s causing the 301 error.

I also tried changing the request to and in HTTP/1.1 and get the same error. Any ideas?

Great idea!
I need something similar - but to get the weather forecast for the next 10 hours in Germany/Ramstein to run my heating (or even not), like seen on http://de.weather.com/stĆ¼ndlich/GMXX1141:1:GM
There are a lot of savings in spring and fall, when itā€™s cold in the morning but temperature is rising fast during the day.
Iā€™m new to Arduino, but I did something like that already in MS Excel (Web Query + VBA), but I donā€™t want to run a notebook the whole year after debugging is done. Do you have some links for me to understand the basics behind ? German is ok (you are from Switzerland, right?). Many thanks in advance!

Thanks for your input. I changed the url.

I cantā€™t see a reason why it should not work on your host, the names do not matter.

HTTP Version 1.0 is used to avoid chunked transfer encoding which i donā€™t want to handle manually.

@mf2105 so you want to use the expected temperature to set your heading acordingly? Do you control the heating directly, or have some led output?
You might want to make a new topic for your idea.

Only the onboard LED? How about triggering an air horn or pressing the nozzle on a can of whipped cream?

@topfunky lol, sure this would be nice; but my partner said it should not disturb too much when I actually want to stay at home (and at night). :smiley:

LOL! Itā€™s supposed to let you know when a good time to catch the bus is. With buses coming to stops pretty frequently, and air horn does not seem appropriate to retain any level of sanity :wink: However, coming home and finding your living room covered in whipped cream would be pretty funny.

Welp, remember when I said ā€œthat is not whatā€™s causing the 301 errorā€ ? lmaoā€¦ donā€™t listen to me Iā€™m obviously not a web developer. After way too much head scratching, I figured it out. I looked up a bunch of people complaining about 301 redirect errorsā€¦ and it appears that sometimes itā€™s related to mysite.com redirecting to www.mysite.com, but in my case that didnā€™t solve it. Most of the other reasons are due to the user actually putting in the wrong URL! Well it worked in my browser, so it must be right in my application.cpp right?? wrong! I changed the directory the index.php file was in to ā€œbussstopā€ and in the application.cpp I had ā€œbusstopā€ā€¦ soooo, yeah. FAIL. Bright side is now I will never forget what 301 means.

But after that ego rebasing occurred, I still had an issueā€¦ I wasnā€™t getting any kind of a response. After analyzing the request so much in the first place, I had in the back of my mind that the request was taking 1200 milliseconds to complete. Looking at the code there was a hard delay of 1000ms before attempting to process the request.

So this code change should make it more dynamicā€¦ and currently I can say itā€™s not a good time to catch a bus from Bern to Bern,Inselspital.

//wait for request to be processed.
//delay(1000);

if(DEBUG) Serial1.println("waiting for response....");
while(!client.available());

If this hangs for more than 10 - 15 seconds the core will drop off the cloud and reconnect anyway currently, so I didnā€™t code in a 5 - 7 second timeout in the while() loop, but it would probably be a good idea as well.

ah is see how this 303 happened, but I didnā€™t think about this cofigurationā€¦

this delay is a good pointā€¦ i think it should actually not be necessary, but the read should block until there is data. we might discuss it on the other thread:

So did it work in the end for you, @BDub ?

ā€œ301ā€ and donā€™t forget it :wink:

Even if the read(); blocks it never gets there because you have && client.available() in your for() loop. First time in it sees that the client is not available and skips the loop.

Yes it worked, pretty cool!

@coffee Very good project!! Iā€™m trying to modify a version for my son taking a bus every morning in Lugano.
I have troubles connecting to the server. If I try it manually I can have answer from the php script but with the firmware the program loop trying to connect. How can I check what is not working in my case?
Many thanks for every idea

Stefano

1 Like

I am happy you like it. :smile:

Try testing the php-script from the console.

telnet yourserver.com 80
GET /webservice.php?from=Bern&to=Bern,Inselspital HTTP/1.0
HOST: yourserver.com

This should give you a http response. if the http-response header is very long, the buffer on the spark might not be big enough. In my case, i have 5 lines of header, then two empty lines, then the answer starting with (--)

good luck

Thanks for the fast answer. As mentioned in my post the php script works fine and I receive the answer ( i.e. (ā€“)red, (ā€“)green etc. etc). I just tried the sketch on a Arduino Uno Ethernet and all is working fine!!
Only with the Spark Core I have problems connecting to the server :frowning:

@Steve59 see my post above: https://community.spark.io/t/bus-stop-light-code-review/1176/8

Triple check your URL, and try adding the changes I suggest in place of the hard delay();

If that doesnā€™t get it, do you have a way to do Serial or Serial1 debugging, and can share your output?

Thanks for the answer. After moving the php script on another web server, the problem is solved!!
I donnā€™t know what was the problem ā€¦ but now is all working fine.
Many thanks for the help.

1 Like

Hey everyone!
Good news! I trashed my source code :blush: and renewed from scratch.

Now everything runs only the Core, no php script any more. :wink: And in the future I will add optional support for the Adafruit OLED 16x2 display. So you can use the LED and/or the display to show the next connections. (The display strings are ready, I just donā€™t have the display yet to try it).

2 Likes

I like this project very much. It is great :-), but if I try to compile follow error message appears, any idea?

In file included from ā€¦/inc/spark_wiring.h:30:0,
from ā€¦/inc/application.h:31,
from /bus-info_older_code.cpp:1:
ā€¦/ā€¦/core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning ā€œDefaulting to Release Buildā€ [-Wcpp]
/bus-info_older_code.cpp:458:0: warning: ā€œDEBUGā€ redefined [enabled by default]
In file included from ā€¦/inc/spark_wiring.h:32:0,
from ā€¦/inc/application.h:31,
from /bus-info_older_code.cpp:1:
ā€¦/ā€¦/core-common-lib/SPARK_Firmware_Driver/inc/debug.h:77:0: note: this is the location of the previous definition
/bus-info_older_code.cpp:3:1: error: ā€˜Statusā€™ does not name a type
/bus-info_older_code.cpp:4:16: error: variable or field ā€˜updateLEDā€™ declared void
/bus-info_older_code.cpp:4:16: error: ā€˜Statusā€™ was not declared in this scope
/bus-info_older_code.cpp:11:1: error: ā€˜Statusā€™ does not name a type
make: *** [/bus-info_older_code.o] Error 1

Error: Could not compile. Please review your code.

I think a #include "" might be missing

1 Like

I posted a new version on github, with more functions and better documentation and hardware list:

you can copy and paste the features that you like and create your own.

1 Like