# WiFi Candy Bowl Monitor

## Overview

http://www.youtube.com/watch?v=AE4k-XN49sU

Build this candy bowl you can monitor remotely over a WiFi network so trick-or-treaters never leave your home empty handed. A simple infrared light sensor detects when the bowl is empty or full, and a CC3000 WiFi chip exposes the sensor data to your wireless network. You can telnet to a simple server running on the Arduino and ask it if the bowl is full of candy!  
  
This tutorial is a great demo of using the CC3000 breakout/shield in server mode, and also with mDNS so you don't need to know the IP address of the module!

![](https://cdn-learn.adafruit.com/assets/assets/000/011/909/medium800/microcontrollers_DSC00334.jpg?1383000181)

## Background
Before you get started it will help to familiarize yourself with the CC3000 and IR sensors by reading these related guides:  

- [Adafruit CC3000 WiFi](http://learn.adafruit.com/adafruit-cc3000-wifi "Link: http://learn.adafruit.com/adafruit-cc3000-wifi")
- [IR Sensor](http://learn.adafruit.com/ir-sensor)

Continue on to learn about the hardware for this project. # WiFi Candy Bowl Monitor

## Hardware

## Parts
To build this project you'll need the following hardware and tools:  

- [Arduno Uno](http://www.adafruit.com/products/50), [Mega](http://www.adafruit.com/products/191), or Nano

  - You need to use an Arduino which is compatible with the CC3000--check the [product page](http://www.adafruit.com/products/1469) for the latest compatibility information.

- [CC3000 shield](http://www.adafruit.com/products/1491) or [breakout](http://www.adafruit.com/products/1469)
- [Infrared light sensor](http://www.adafruit.com/products/157) and [Infrared LED](http://www.adafruit.com/products/387 "Link: http://www.adafruit.com/products/387")

  - Make sure the IR sensor and LED are matched to the same light wavelength (usually 940nm). The IR sensor should be one tuned to detect 38khz signals.

- [LED](http://www.adafruit.com/products/777) (any color) as an indicator of the IR sensor receiving signals.
- Two 1/4 watt resistors:

  - R1: ~100 to ~1000 ohm resistor to limit the current to the IR LED.
  - R2: ~200 to ~1000 ohm resistor to limit the current to the indicator LED.

- Power source for the Arduino, such as a [9-volt battery](http://www.adafruit.com/products/67) or [wall wart](http://www.adafruit.com/products/63).
- [Solid core hookup wire](http://www.adafruit.com/products/1311)
- [Breadboard](http://www.adafruit.com/products/64) or [perf-board](http://www.adafruit.com/products/571)
- Candy bowl or container you can mount the IR sensor and LED within.
- Hot glue or double stick tape to secure the sensor and LED inside the bowl.
- [Soldering tools](http://www.adafruit.com/products/136) to attach wire to the IR LED and sensor

  

## Wiring
Connect your hardware as follows:  

![](https://cdn-learn.adafruit.com/assets/assets/000/011/912/medium800/microcontrollers_CandyBowlServer4.png?1383068791)

For the CC3000, connect it to the Arduino in the [same way as this CC3000 tutorial](http://learn.adafruit.com/adafruit-cc3000-wifi/assembly-and-wiring):

- Arduino 5V to CC3000 VIN
- Arduino ground to CC3000 ground
- Arduino digital pin 13 to CC3000 CLK
- Arduino digital pin 12 to CC3000 MISO
- Arduino digital pin 11 to CC3000 MOSI
- Arduino digital pin 10 to CC3000 CS
- Arduino digital pin 5 to CC3000 VBEN
- Arduino digital pin 3 to CC3000 IRQ

For the IR LED, solder long wires (long enough to reach from the bowl to the breadboard) to each leg--don't forget which leg is positive (longer) and negative! Connect the positive wire from the IR LED through to 5V power from the Arduino. Connect the negative wire from the IR LED through the R1 ~100 ohm resistor to Arduino digital pin 8.  
  
For the IR sensor, again solder long wires to each leg. Connect the Vs leg to 5V power, the ground leg to ground, and the output leg to both Arduino digital pin 7 and the negative leg of the indicator LED. [Check the datasheet](http://www.adafruit.com/datasheets/tsop382.pdf "Link: http://www.adafruit.com/datasheets/tsop382.pdf") if you're unsure which pin is which on the sensor. Finally, hook up the positive leg of the indicator LED through the R2 ~220 ohm resistor to 5V power.  
  
As a quick test, with the Arduino powered on you should be able to aim a remote control at the IR sensor and press buttons to see the indicator LED flash.  
To finish the hardware, attach the IR sensor and IR LED to opposite sides that are facing each other inside the bowl. To the left you can see how I hot glued my sensor and LED about 1/3 of the way up the side of the bowl. You can also see the electronics that I built using an Arduino Nano, and a transistor to control current through the IR LED--the transistor isn't really necessary so it can be ignored.  
  
If your bowl is big enough, you might be able to build a false floor with cardboard to hide the electronics neatly inside the bowl. Otherwise, run the wires outside the bowl and hide the electronics nearby.

![microcontrollers_DSC00329.jpg](https://cdn-learn.adafruit.com/assets/assets/000/011/905/medium640/microcontrollers_DSC00329.jpg?1382999769)

![microcontrollers_DSC00328.jpg](https://cdn-learn.adafruit.com/assets/assets/000/011/906/medium640/microcontrollers_DSC00328.jpg?1382999837)

![microcontrollers_DSC00330.jpg](https://cdn-learn.adafruit.com/assets/assets/000/011/907/medium640/microcontrollers_DSC00330.jpg?1382999893)

![microcontrollers_DSC00334.jpg](https://cdn-learn.adafruit.com/assets/assets/000/011/908/medium640/microcontrollers_DSC00334.jpg?1382999955)

Continue on to learn about the software used in this project.

# WiFi Candy Bowl Monitor

## 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](http://learn.adafruit.com/adafruit-cc3000-wifi/cc3000-library-software "Link: http://learn.adafruit.com/adafruit-cc3000-wifi/cc3000-library-software").  
  
Next download the two sketches for this project with the link below:

[Download Arduino Sketches](https://github.com/tdicola/wifi_candybowl_monitor/archive/master.zip)
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](http://learn.adafruit.com/adafruit-cc3000-wifi/cc3000-library-software) 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](http://learn.adafruit.com/adafruit-cc3000-wifi/cc3000-library-software) 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](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) 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:  
```auto
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() &gt; 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](http://arduino.cc/en/reference/ethernet "Link: http://arduino.cc/en/reference/ethernet"). 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.

# WiFi Candy Bowl Monitor

## Multicast DNS

One enhancement to the candy bowl monitor server is to use [multicast DNS](http://en.wikipedia.org/wiki/Multicast_DNS "Link: http://en.wikipedia.org/wiki/Multicast\_DNS") to simplify connecting to the server. With MDNS the server can be assigned a local address like ' **candybowl.local**' which is used instead of the IP address to connect to the server. This means you don't need to know the IP address of the server (which can change each time the server connects to your network) and instead only need to know the fixed local address/name of the server.  
  

To use MDNS with the candy bowl monitor, first download and install the [CC3000 MDNS Arduino library](https://github.com/tdicola/CC3000_MDNS). A direct download link is below:[Download CC3000 MDNS Library](https://github.com/tdicola/CC3000_MDNS/archive/master.zip)
Unzip the library into a folder in your Arduino **sketchbook/libraries** directory. See the [guide on Arduino libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use) if you aren't sure exactly what to do. Make sure to restart the Arduino IDE after installing the library.  
  
Next you will want to make sure you have MDNS (or ZeroConf/Bonjour) support available to your operating system.

- For Mac OSX support should be provided automatically by Bonjour.
- For Linux, make sure [Avahi](http://avahi.org/) is installed. For recent versions of Ubuntu this should already be installed.
- For Windows, make sure [Bonjour for windows](http://support.apple.com/kb/DL999) is installed.

Now load the ' **Candybowl\_Server\_MDNS**' sketch that was downloaded earlier. Configure this sketch in the same way as the first one, compile it, and load it on your Arduino.  
  
Once the serial monitor displays the 'Listening for connections...' message, telnet to the ' **candybowl.local**' address. For example using a terminal session type:  
  
telnet candybowl.local  
  
After a short period of time you should see the telnet server find the IP address of the server and connect to it. You can send the same question mark command as the first server example to check the candy bowl status.  
  
If you want to change the MDNS address of the server, just modify the call to the **mdns.begin()** function line in the **setup()** function of the sketch. By default the call looks like:  
  
mdns.begin("candybowl", cc3000, 3600)  
  
This means the server will listen for the ' **candybowl.local**' address (by convention all MDNS addresses end in ' **.local**'). Try changing the value of the first parameter, recompiling, and uploading the sketch to have the server respond to a new address.  
  
Also for reference the last parameter to the call specifies the time to live for the DNS record in seconds. In this case the sketch is setting a TTL of 3600 seconds, or one hour. This means your computer will cache the mapping of ' **candybowl.local**' to the server IP address for up to an hour. If you frequently disconnect and reconnect the server so the IP address changes, you might want to drop this down to a lower value like a few minutes. If you don't specify this third parameter, a default TTL of 1 hour will be used.  
  
Feel free to use this simple MDNS library in your own server projects!  
  
Continue on wrap up and look at future work you can do with this project.# WiFi Candy Bowl Monitor

## Future Work

This project is a great example of how to build a server that can remotely monitor a sensor over WiFi with an Arduino and the CC3000. There are lots of interesting ways you can extend this project, such as:

- Use a [strain gauge](http://www.ni.com/white-paper/3642/en/ "Link: http://www.ni.com/white-paper/3642/en/") to measure how much weight is in the candy bowl and calculate exactly how many pieces of candy are inside.
- Continuously pulse the IR LED and sensor to count how often a hand reaches into the candy bowl.
- Have the [CC3000 send tweets](http://learn.adafruit.com/adafruit-cc3000-wifi/sendtweet) when the candy bowl is full or empty.
- Decorate the bowl with LEDs wired to a free digital pin and add a command to remotely blink or flash the LEDs from the telnet server.
- Make a simple web server to display the status of the candy bowl in a web browser. See the [Arduino Ethernet library WebServer example](http://arduino.cc/en/Tutorial/WebServer) for the basic code you can make work with the CC3000 library.

Have fun with the WiFi candy bowl monitor project!  

## Featured Products

### Adafruit HUZZAH CC3000 WiFi Breakout with Onboard Antenna

[Adafruit HUZZAH CC3000 WiFi Breakout with Onboard Antenna](https://www.adafruit.com/product/1469)
For years we've seen all sorts of microcontroller-friendly WiFi modules but none of them were really Adafruit-worthy. Either they were too slow, or too difficult to use, or required signing an NDA, or had limited functionality, or too expensive, or too large. So we shied away from carrying...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1469)
[Related Guides to the Product](https://learn.adafruit.com/products/1469/guides)
### Adafruit HUZZAH CC3000 WiFi Shield with Onboard Antenna

[Adafruit HUZZAH CC3000 WiFi Shield with Onboard Antenna](https://www.adafruit.com/product/1491)
For years we've seen all sorts of microcontroller-friendly WiFi modules but none of them were really Adafruit-worthy. Either they were too slow, or too difficult to use, or required signing an NDA, or had limited functionality, or too expensive, or too large. So we shied away from creating...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1491)
[Related Guides to the Product](https://learn.adafruit.com/products/1491/guides)
### Adafruit CC3000 WiFi Breakout with uFL Connector for Ext Antenna

[Adafruit CC3000 WiFi Breakout with uFL Connector for Ext Antenna](https://www.adafruit.com/product/1510)
For years we've seen all sorts of microcontroller-friendly WiFi modules but none of them were really Adafruit-worthy. Either they were too slow, or too difficult to use, or required signing an NDA, or had limited functionality, or too expensive, or too large. So we shied away from carrying...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1510)
[Related Guides to the Product](https://learn.adafruit.com/products/1510/guides)
### Adafruit CC3000 WiFi Shield with uFL Connector for Ext Antenna

[Adafruit CC3000 WiFi Shield with uFL Connector for Ext Antenna](https://www.adafruit.com/product/1534)
For years we've seen all sorts of microcontroller-friendly WiFi modules but none of them were really Adafruit-worthy. Either they were too slow, or too difficult to use, or required signing an NDA, or had limited functionality, or too expensive, or too large. So we shied away from creating...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1534)
[Related Guides to the Product](https://learn.adafruit.com/products/1534/guides)
### Adafruit METRO 328 Fully Assembled - Arduino IDE compatible

[Adafruit METRO 328 Fully Assembled - Arduino IDE compatible](https://www.adafruit.com/product/50)
We sure love the ATmega328 here at Adafruit, and we use them&nbsp;_a lot_&nbsp;for our own projects. The processor has plenty of GPIO, Analog inputs, hardware UART SPI and I2C, timers and PWM galore - just enough for most simple projects. When we need to go small, we use a <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/50)
[Related Guides to the Product](https://learn.adafruit.com/products/50/guides)
### IR (Infrared) Receiver Sensor

[IR (Infrared) Receiver Sensor](https://www.adafruit.com/product/157)
IR sensor tuned to 38KHz, perfect for receiving commands from a TV remote control. Runs at 3V to 5V so it's great for any microcontroller.  
  
To use, connect pin 3 (all the way to the right) to 5V power, pin 2 (middle) to ground and listen on pin 1. It doesn't do any decoding...

In Stock
[Buy Now](https://www.adafruit.com/product/157)
[Related Guides to the Product](https://learn.adafruit.com/products/157/guides)
### Super-bright 5mm IR LED

[Super-bright 5mm IR LED](https://www.adafruit.com/product/387)
Infrared LEDs are used for remote controls (they're the little LED in the part you point at your TV) and 'night-vision' cameras, and these little blue guys are high powered ones! They are 940nm wavelength, which is what nearly all devices listen to. They're 20 degree beamwidth,...

In Stock
[Buy Now](https://www.adafruit.com/product/387)
[Related Guides to the Product](https://learn.adafruit.com/products/387/guides)
### NPN Bipolar Transistors (PN2222) - 10 pack

[NPN Bipolar Transistors (PN2222) - 10 pack](https://www.adafruit.com/product/756)
Transistors are powerful little electronic switches, and we really like these NPN transistors whenever we need to control medium-power electronics such as small motors, solenoids, or IR LEDs. We find them so handy, they come in a pack of 10!

Each transistor is a general-purpose...

In Stock
[Buy Now](https://www.adafruit.com/product/756)
[Related Guides to the Product](https://learn.adafruit.com/products/756/guides)

## Related Guides

- [Wireless Gardening with Arduino + CC3000 WiFi Modules](https://learn.adafruit.com/wireless-gardening-arduino-cc3000-wifi-modules.md)
- [Arduino Prototyping Mounting Plate](https://learn.adafruit.com/arduino-prototyping-mounting-plate.md)
- [Adafruit Proto Shield for Arduino](https://learn.adafruit.com/adafruit-proto-shield-arduino.md)
- [Electronic Demon Costume](https://learn.adafruit.com/electronic-demon-costume.md)
- [Memories of an Arduino](https://learn.adafruit.com/memories-of-an-arduino.md)
- [Arduino Lesson 5. The Serial Monitor](https://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor.md)
- [Multi-tasking the Arduino - Part 1](https://learn.adafruit.com/multi-tasking-the-arduino-part-1.md)
- [Bluefruit LE Connect for iOS and Android](https://learn.adafruit.com/bluefruit-le-connect.md)
- [How to Choose a Microcontroller](https://learn.adafruit.com/how-to-choose-a-microcontroller.md)
- [Adafruit Proto Screw Shield](https://learn.adafruit.com/adafruit-proto-screw-shield.md)
- [Trinket Audio Player](https://learn.adafruit.com/trinket-audio-player.md)
- [RePaper eInk Development Board](https://learn.adafruit.com/repaper-eink-development-board.md)
- [Arduino Lesson 13. DC Motors](https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors.md)
- [FTDI Friend](https://learn.adafruit.com/ftdi-friend.md)
- [Ladyada's Bento Box](https://learn.adafruit.com/lady-adas-bento-box.md)
