To test an Arduino sending data to a web server, we need:
- An Arduino. I used an Arduino Uno.
- A Solderless BreadBoard
- Some copper wires
- An 802.11 chip that can send and receive data from an Arduino. I purchased the CC3000 from AdaFruit. AdaFruit has started to provide TI’s CC3000 WiFi chip. They wrote a library to talk to the CC3000 from an Arduino Sketch. Adafruit is an excellent company to buy prototype parts from. The forums are awesome. Their tutorials are extremely helpful. I’ve been very happy with the product, support, and learning offered from Adafruit.
- A web server
- An Arduino sketch that communicates with the Web Server.
The CC3000 is Connected…to the Arduino
The first step is to get the CC3000 talking to the Arduino.
The CC3000 uses SPI to talk to the Arduino. SPI supports multiple components talking to an Arduino. The Arduino acts as a master SPI device. The wiFi breakout acts as a slave. Different Arduinos use different pins to connect to SPI devices. The Arduino Uno uses pins:
- 13 to connect to a slave’s SCLK
- 12 to connect to a slave’s MISO
- 11 to connect to a slave’s MOSI
- and usually a default of 10 to connect to a slave’s SS.
You may want to refer to the SPI entry in Wikipedia if the acronyms and what they represent are unfamiliar to you.
Each slave device needs a different pin to talk to the slave’s SS device. This can be a challenge because it means the component and the library that speaks to the Arduino must support configuring a different pin to SS. Components/libraries should do this easily so that multiple components can use SPI. Luckily Adafruit’s breakout does do this easily. Yay!
There will also be another RF component to talk between Arduinos within the house. This component will also use SPI. How this is handled will be discussed in a later post.
Once you have the cc3000 wired up to the Arduino, grab AdaFruit’s cc3000 library on GitHub library and run the buildtest sketch. Don’t forget to set the WiFi’s SSID and password to the values used on the WiFi you wish to connect to!
I got the following results when a ran the buildtest Example sketch that comes with Adafruit’s library:
Hello, CC3000! RX Buffer : 131 bytes TX Buffer : 131 bytes Free RAM: 1236 Initialising the CC3000 ... Firmware V. : 1.19 MAC Address : 0x08 0x00 0x28 0x57 0x35 0x8E Started AP/SSID scan Networks found: 3 ================================================ SSID Name : NETGEAR-lp RSSI : 54 Security Mode: 3 SSID Name : henry2 RSSI : 78 Security Mode: 3 SSID Name : henry1 RSSI : 54 Security Mode: 3 ================================================ Deleting old connection profiles Attempting to connect to henry2 Started AP/SSID scan Connecting to henry2...Waiting to connect...Connected! Request DHCP IP Addr: 192.168.1.218 Netmask: 255.255.255.0 Gateway: 192.168.1.1 DHCPsrv: 192.168.1.1 DNSserv: 192.168.1.5 www.adafruit.com -> 0.0.0.0 Pinging 0.0.0.0...Pinging 0.0.0.0 5 times Req report Reports: 1 Sent: 0 Recv: 0 MinT: 0 MaxT: 0 AvgT: 0 0 replies Closing the connection
Yippee! The CC3000 connected to the wireless network. BUT bummer…name resolution of http://www.adafruit.com did not return its address and pinging http://www.adafruit.com didn’t work. Typical isn’t it? There is excitement on what works “easily” in our prototypes and confusion (leading to frustration) on a few things that should work.
After several runs, the call to cc3000.getHostByName(WEBSITE, &ip) resolved. This is certainly not robust. I assume I’ll run into this “bug” in the near future and thus will wait to debug further.
www.adafruit.com -> 184.108.40.206 Pinging 220.127.116.11...Pinging 18.104.22.168 5 times Req report Reports: 2 Sent: 0 Recv: 0 MinT: 0 MaxT: 0 AvgT: 0 0 replies
However, pinging still doesn’t work. I can’t figure out why so I am asking about it within Adafruit’s forum.
Conversing with AdaFruit’s Web Server
The Adafruit library includes another example sketch – WebClient.ino – that connects to http://www.adafruit.com and then sends an HTTP GET command:
GET /testwifi/index.html HTTP/1.0 Host: www.adafruit.com Connection:close
The sketch returned:
Hello, CC3000! Free RAM: 1125 bytes Initializing... Started AP/SSID scan Connecting to henry2...Waiting to connect...Connected! Request DHCP IP Addr: 192.168.1.218 Netmask: 255.255.255.0 Gateway: 192.168.1.1 DHCPsrv: 192.168.1.1 DNSserv: 192.168.1.5 www.adafruit.com -> 22.214.171.124Adafruit_CC3000_Client::Creating socket ... Adafruit_CC3000_Client::Created socket:0) Adafruit_CC3000_Client::Connect to 126.96.36.199:80 Adafruit_CC3000_Client::Connecting socket ... Adafruit_CC3000_Client::DONE ------------------------------------- HTTP/1.1 200 OK Date: Mon, 16 Sep 2013 15:45:22 GMT Server: Apache Access-Control-Allow-Origin: http://learn.adafruit.com Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding, Authorization, Referer, User-Agent Access-Control-Allow-Methods: GET, POST, OPTIONS Access-Control-Allow-Credentials: true Access-Control-Max-Age: 1728000 Last-Modified: Thu, 27 Jun 2013 14:13:27 GMT Accept-Ranges: bytes Content-Length: 74 Connection: close Content-Type: text/html This is a test of the CC3000 module! If you can read this, its working :) Adafruit_CC3000_Client::No more data, and closed! -------------------------------------