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.
Here is a screen shot of the web client showing the latest readings for temperature and humidity:
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