In a previous post, we connected AdaFruit’s cc3000 wiFi breakout board connected to the AdaFruit server and read some html. This time we will talk from our cc3000 to a web server running PHP. To do this we will need:
- Access to a web server. For this I installed and started up MAMP on my Mac.
- A web service that can take in and show data coming in from our cc3000. We’ll use a simple PHP script.
- An Arduino sketch that sends data over the WiFi to the web service.
- Arduino wired to cc3000 with the ability to load and run sketches (see the previous post)
What luck! Marco Schwartz wrote a blog post on the Open Home Automation web site that does pretty much what we want to do. Marco’s example takes temperature readings and sends them via WiFi to a Web Server that is running HTML embedded with simple PHP scripts. As noted in Marco’s post, the WiFi Weather Project’s GitHub repository is located here.
I used Marco’s wifi_weather_station.ino as well as a few of the example sketches that are included in AdaFruit’s cc3000 libraries.
Getting the Server side Up and Running
Follow Marco’s instructions and code to get the PHP scripts up and running on your local web server.
Getting the Arduino Sketch Up and Running
I wrote a sketch that sends a PHP GET request to my local web server. I borrowed heavily from Marco’s sketch as well as the examples that come from Adafruits c3000 library. Thank you to both. The sketches were valuable learning tools and did the majority of the “heavy lifting” needed to be done to send data to a PHP service from an Arduino that communicates with a cc3000.
First, download the sketch I am about to walk through and open in up in your Arduino IDE.
Once that is done, load the simplePHPGet.ino sketch into your Arduino IDE.
Make sure to modify the lines that are unique to your setup. These include:
// WiFi network (change with your settings !) #define WLAN_SSID "myNetwork" #define WLAN_PASS "myPassword" // PHP's server IP, port, and repository (change with your settings !) uint32_t ip = cc3000.IP2U32(192,168,1,5); String repository = "/wifiweather/";
For this step I am not going to hook up a sensor. Rather, I’ll set random values for the temperature and humidity and then create a “GET” request. This request is then sent to my MAMP Web Server.
The MAMP Web Server looks for files in the wifiweather folder that I created and put Marco’s files in.
The PHP script in sensor.php runs which updates the Web Page with the (randomly generated) values for temperature and humidity.
The Web Server sends back the results.
…and repeat.
Here is a screen shot of the web client showing the latest readings for temperature and humidity:
And here is debug output sent to the serial monitor while running the sketch:
Initializing...OK. Connecting to network...Started AP/SSID scan Connecting to henry2...Waiting to connect...connected! Requesting address from DHCP server.......waiting ....waiting ....waiting OK 30.00 424.00 ...Connecting to Server Connect to 192.168.1.128:8888 Connected ...Sending request:GET /wifiweather/sensor.php?temp=30.00&hum=424.00 HTTP/1.0 Connection: close ...Reading response ------------------------------------- HTTP/1.1 200 OK Date: Fri, 20 Sep 2013 21:20:07 GMT Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8x DAV/2 PHP/5.4.10 X-Powered-By: PHP/5.4.10 Content-Length: 0 Connection: close Content-Type: text/html Cleaning up... ...closing socket 390.00 17.00 ...Connecting to Server Connect to 192.168.1.128:8888 Connect to 192.168.1.128:8888 Connected ...Sending request:GET /wifiweather/sensor.php?temp=390.00&hum=17.00 HTTP/1.0 Connection: close ...Reading response ------------------------------------- HTTP/1.1 200 OK Date: Fri, 20 Sep 2013 21:20:17 GMT Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8x DAV/2 PHP/5.4.10 X-Powered-By: PHP/5.4.10 Content-Length: 0 Connection: close Content-Type: text/html Cleaning up... ...closing socket
Mego said:
This is a great job … I am trying to do the same but with wamp because I am using windows 7. Unfortunately I couldn’t until now.
If you don’t mind can you tell me what is the prototype ?
Whats about the web files where exactly should place it and should we change the name for the html file
Thank you
bitknitting said:
Hello Mego,
Thank you for the kind words. I assume you are referencing the .php files and such for the MAMP? I used Marco Schwartz’s files from https://github.com/openhomeautomation/wifi-weather-station
You put the files within your htdocs folder. For example, mines is in \mamp\htdocs\wifiWeather. I hope that helps….and I wish your projects success!
HC said:
Hi,
have you tried to use a smart phone as the access point in hotspot mode?
I have a similar application as yours, but with the wifly from microchip.
Unfortunately mine is not stable when using any smart phone as the access point.
I keep getting “connection failed” when attempting to open connections.
Microchip has replicated the issue and confirmed it. Therefore, I am planning on testing it on a cc3000. If you have any experience, or if you are able to test it with a smart phone, please let me know if you can.
bitknitting said:
A Happy Hello to You!
I am not using a smart phone for an access point. My scenario is one that stays in a house and assumes a wireless router. In fact, I am exploring moving away from 802.11 and using Ethernet. Thus having the Base Station connected into Ethernet at the router. This allows me to use the RFM69HW for in-home send and receive, the Base Station to gather up the data and use Ethernet to send over the Internet. For my first pass, I will have an iOS app (only because I know how to program in iOS…and I use an iPhone) connect with the Base Station to receive info and send commands.
Thanks very much for your comment!
HC said:
Hi!
Thank you for your quick reply! Would it be much trouble for you to have a quick test to see if you get 100% successful connections by using your iPhone as a hotspot instead of your home wireless router? If not, I totally understand 🙂 I am just worried that the fault I experience may not be with the WIFI module at all, but perhaps some bugs/restrictions in the iphone/android/3g firmware on the phone…
bitknitting said:
Hello,
Unfortunately it will take more time to do this test than I have at the moment – I wish I could be more helpful!
luisdiazugena said:
HI! I’m following this tutorial (as well as the one from http://www.openhomeautomation.net/arduino-wifi-cc3000/ ) but i can’t make it works.
I’ve changed some data (like dht pin and dht type) also, i’ve configured the network settings and move the files from github (all php, js,css and html) to htdocs inside mamp folder.
what i don’t understand is what i have to write in the ip string. The ip from mac where the mamp is installed?
thanks!
bitknitting said:
Hello,
I am sorry this is not working for you. It has been quite some time now since i wrote this up…it would take me a chunk of time to pull this stuff up and answer your question. Time unfortunately that right now i do not have. i am better answering questions from stuff that is more current :-). i write this to also inform my own learning. so i have moved on from this scenario and can’t remember the details enough to help you. my apologies.
luisdiazugena said:
Don’t worry!! I understand it 😀
I’ve move from this to a telnet communications that also work for me, so, no need to work with this anymore, but it’s still interesting.
Will try to learn more in summer time 😀
I’ll post about it on my blog: http://www.drmaker.es. it’s writted in spanish, but maybe you can translate it with G translator.
Grettings
bitknitting said:
Thanks for letting me know. All the best to you!
luisdiazugena said:
Take care! Will check your blog! Seems interesting!
subrat said:
Hello bitknitting,
I am quite naive to all the three php, arduino and c.
With that background, my questions might sound quite primitive. Since you have been kind enough to respond other queries, I am trying my luck 😀
I am in a rather easier situation, where I plan to pick data from analog sensors in few channels of A0-A6, hooked on to Arduino and send them across over wifi shield to a web server, located in a computer/laptop.
Now, if I have been able to draw the analogies right, with reference to your work, here are my questions:
1. Are you in a situation, where the wifi shield (CC3000) and the laptop talks to the same router? and then under such a condition, you are deriving the (static) ip of the laptop to feed in the sketch?
2. Can I instead make the laptop as an adhoc network and ask the CC3000 to connect to this network over wifi? Do you think such a setup is possible, this would rather simplify my job, since I dont want an external (wifi network) intervention
3. I also would like to log the data at a much faster rate than say 1 sample per second? Do you think I can manage a 100-1000 fold increase in this data logging and sending over a decent wi fi network both in #1 and #2 scenario?
Will appreciate a response from you.
bitknitting said:
Hello,
First – my apologies for a slow response. I was hoping i would have more time. regarding your questions, i do not focus on php, wireless right now – so i apologize i cannot provide a worthwhile answer to your questions. for questions like yours, i have had great luck on stackexchange and various reddits. perhaps these are better places since there are folks there who are better prepared to answer your questions.
subrat said:
Hi
Thanks anyway for dropping a response.
I appreciate that..
I might come back with some questions later on, please spare some time when you have it 🙂
No worries on delayed response.
Thanks in advance..
Cheers,
Subrat
bitknitting said:
i hope i learn from you soon! i’m most interested in tech for folks that are lazy and grow plants using hydroponics techniques.
side sleep said:
Hi! This is kind of off topic but I need some help from
an established blog. Is it difficult to set up your own blog?
I’m not very techincal but I can figure things out pretty fast.
I’m thinking about creating my own but I’m not sure where to begin. Do
you have any tips or suggestions? Cheers
bitknitting said:
And Cheers to you too! I find the easiest way to start a blog is to go get a free wordpress.com blog. i.e.: start at wordpress.com …then, if you need any additional help there is nothing a google query can’t answer. What I like about doing a blog is the tremendously smart and helpful folks whom I have had the honor of communicating with because of something I wrote. It has also allowed me to come to conclusions on what I’d like to be focused on moving forward. All the very best in your efforts.
Yichen Huang said:
Hi Bitknitting. Thank you very much for the awesome tutorial. I want to send data to a PHP file from Arduino through CC3000. I think this tutorial is exactly what I want to learn. However, I couldn’t find the PHP scripts example. It seems like Marco Schwartz has updated his work and no longer use PHP to receive the data. Do you still have the PHP scripts? If you do, would you be so kind to share with me? Thank you very much!
bitknitting said:
Oh my. It has been such a long time since I tried this stuff. I can’t even remember. My apologies for not being more helpful.
Victor Manuel R G said:
Hi:
Nice article, I’m trying but I can’t find the script sensor.php, where is it?
bitknitting said:
Hello Victor, thank you. The article was written so long ago. If it isn’t posted, I am afraid I do not have the file! My apologies.