Software

Once the hardware is setup, you can move on to running the software for the candy bowl monitor.

First make sure you have the latest version of the Adafruit CC3000 library installed. This project uses a server class which was recently added to the library so you need to update it to the latest version even if you have the library already installed. Check the CC3000 tutorial for information on installing the latest version of the CC3000 library.

Next download the two sketches for this project with the link below:
Unzip the archive and you'll find two Arduino sketches:
  • Candybowl_Server
  • Candybowl_Server_MDNS
Load the first sketch, Candybowl_Server, in the Arduino IDE and modify the defines at the top of the program. You can change which pins are hooked up to the CC3000, IR sensor, and IR LED. If you built the hardware on the previous page you shouldn't have to change any of the default values.

Also update the WLAN_SSID, WLAN_PASS, and WLAN_SECURITY defines to the appropriate values for your wireless network. See the CC3000 guide for more information on configuring the CC3000--both these candy bowl sketches use the same configuration.

Compile the sketch and load it on your Arduino. Open the serial monitor at 115200 baud and watch for status from the CC3000 initialization to appear. After a few seconds you should see some details about the network and the message "Listening for connections..." printed. For example when connected to my network I see:

Hello, CC3000!

Initializing...
Started AP/SSID scan

Connecting to tdicola...Waiting to connect...Connected!
Request DHCP

IP Addr: 192.168.1.135
Netmask: 255.255.255.0
Gateway: 192.168.1.1
DHCPsrv: 192.168.1.1
DNSserv: 192.168.1.1
Listening for connections...

Make sure you see the listening for connections message before moving forward. If you see errors or have problems, check the CC3000 tutorial to make sure you have everything hooked up and configured correctly.

Usage

To connect to the candy bowl server, open a telnet session on port 23 to the CC3000's IP address (you can see the IP address printed in the serial monitor, in my example it's 192.168.1.135).

On Mac OSX or Linux, open a terminal session and type:

telnet (CC3000 IP address)

For example if your CC3000 is on the IP address 192.168.1.135 you would type:

telnet 192.168.1.135

On Windows you'll need to install and use a telnet client. PuTTy is a good, free graphical client to use.

Once connected to the server you can type a question mark character and press enter to have the server respond with the current candy bowl status. For example here's what you should see if the bowl is empty and queried, then filled with candy, and queried again:

> telnet 192.168.1.135
Trying 192.168.1.135...
Connected to 192.168.1.135.
Escape character is '^]'.
?
Candy bowl status: LOW
?
Candy bowl status: FULL

Note: Because this project uses light to detect if the bowl is empty or full, you might have problems if the candy inside the bowl is clear or see-through. Try to use candy that's dark and blocks light.

To close the telnet connection, on Mac OSX or Linux press ctrl-] to bring up a telnet command prompt and type quit. For PuTTY, just close the terminal window.

Connect to the server any time and query it for the current state of the candy bowl!

Code

If you are curious how the server works, look at the loop() function in the sketch. You can see the code below:
Download: file
void loop(void)
{
  // Handle a connected client.
  Adafruit_CC3000_ClientRef client = candyServer.available();
  if (client) {
     // Check if there is data available to read.
     if (client.available() > 0) {
       uint8_t ch = client.read();
       // Respond to a candy bowl status query.
       if (ch == '?') {
         client.fastrprint("Candy bowl status: ");
         if (isBowlFull()) {
           client.fastrprintln("FULL");
         }
         else {
           client.fastrprintln("LOW");
         }
       }
     }
  }
}
The Adafruit CC3000 library exposes a server interface that is very similar to the Arduino Ethernet library's server class. If you have existing code that uses the Ethernet library server class, you should be able to port it to the CC3000 without many changes. The only difference is that the CC3000 library returns an 'Adafruit_CC3000_ClientRef' instance instead of an 'Adafruit_CC3000_Client' instance from the server's available() function. However you can use the client ref instance just like a client class and send or receive data to the connected client.

Continue on to learn about an enhancement to the server using multicast DNS.
This guide was first published on Oct 29, 2013. It was last updated on Oct 29, 2013. This page (Software) was last updated on Aug 25, 2019.