Maker.io main logo

Adding WiFi to the Circuit Playground - Sparkfun WiFi Shield

2016-07-27 | By Maker.io Staff

License: None

The Adafruit Circuit Playground is a development board which packs a whole bunch of features such as sensors, actuators and some Neo Pixels for you to interact with. The Circuit Playground includes the following:

The Adafruit Circuit Playground is a development board which packs a whole bunch of features such as sensors, actuators and some Neo Pixels for you to interact with. The Circuit Playground includes the following:

  • ATmega32u4 Processor, running at 3.3 V and 8 MHz
  • MicroUSB port allows for programming and debugging with Arduino IDE
  • USB port can enables a serial port, keyboard, mouse, joystick or MIDI
  • 10 x mini NeoPixels, each one can display any color
  • 1 x Motion sensor (LIS3DH triple-axis accelerometer with tap detection, free-fall detection)
  • 1 x Temperature sensor (thermistor)
  • 1 x Light sensor (phototransistor)
  • 1 x Sound sensor (MEMS microphone)
  • 1 x Mini speaker (magnetic buzzer)
  • 2 x Push buttons, left and right
  • 1 x Slide switch
  • 8 x alligator-clip friendly input/output pins
  • Includes l2C, UART, and 4 pins that can do analog inputs/PWM output
  • All 8 pads can act as capacitive touch inputs
  • Green "ON" LED so you know its powered
  • Red LED for basic blinking
  • Reset button

You can find more information about the board and its features here.

As we are entering a new age of IoT devices where everything is now becoming connected to the internet for monitoring and control in order to make life more simple and automated. I wanted to connect the Adafruit Circuit Playground to the ESP8266 module to monitor the analog sensors on the Circuit Playground such as temperature, light, sound.

What you need

For this example, you will need the following hardware:

Software Perquisites

You will need the latest Arduino IDE to be installed along with the Circuit Playground Library. You can learn about installing third party boards from the maker.io web link. If your ESP8266 WiFI module does not come loaded with the latest AT command firmware, then you will need to follow the guide at maker.io to install that. This will allow you to issue AT commands from the Circuit Playground to the ESP8266 module. Other than that you should be ready to go.

Connecting to the ESP8266

In this example I will be using the SparkFun ESP8266 WiFI shield. The WiFi shield comes with a number of features which allow me to easily connect it to the Circuit Playground including a 3V3 regulator on the 5V label. Although conventional ESP module require a lot of power I can actually power the WiFi shield using the Circuit Playground. Of course alternatively you can power using an external power source, I even created a power circuit using a regulator and a 12V DC adaptor as shown in Figure 1 if I needed to.

Figure 1: 3V3 Power Circuit

Figure 1: 3V3 Power Circuit

Connect the Alligator test leads to the Circuit Playground as shown in Figure 2. For this example, we are going to use something called software serial as it is not possible to use the Tx and Rx lines for communicating with the ESP8266 shield whilst using the USB serial port at the same time.

Connecting Alligator Clips to the Circuit Playground

Figure 2: Connecting the Alligator Clips to the Circuit Playground

The software serial library allows us to use the digital pins on the Circuit Playground as a serial line, using software to replicate functionality. SoftwareSerial supports speeds up to 115200 baud rate and can be used in conjunction with other SoftwareSerial ports, however there are some limitations which you can check out on the Arduino reference.

Connect the other end of the alligator clips to the ESP8266 module you have.

  • 3V3 – 3V3 or 5V on shield
  • Ground – Ground
  • D9 – Tx
  • D10 – Rx

You can see the configuration in Figure 3.

Connecting alligator clips to the ESP8266 module

Figure 3: Connecting the alligator clips to the ESP8266 module

Programming the Circuit Playground

In this example I will be using the SparkFun ESP8266 AT library for ease of use, you can download the library from their GitHub page. Alternatively, you print out the AT commands through serial print in the code.  In the example code we are going to setup the Circuit Playground as a web server, which will display the values of the analog pins on the Circuit Playground including all the sensors on-board.

Before you get started if you are using the SparkFun ESP8266 AT library then there are a cou[le of things that you will need to change on the library code. Locate the SparkFunESP8266WiFi.h file in the source folder and open it in a text editor program.

Open the SparkFun ESP8266 file

Figure 4: Open the SparkFun ESP8266 file

In this file you should see the following lines near the top half of the code:

#define ESP8266_SW_RX       9

#define ESP8266_SW_TX       8 

You need to change these pin numbers to ones that aren’t used on the Circuit Playground. I had changed them to the following pins:

#define ESP8266_SW_RX       10

#define ESP8266_SW_TX       9

The code for the web server can be found below:

Copy Code
#include <SoftwareSerial.h> 
#include <SparkFunESP8266WiFi.h>

String HTTP_req;

const char mySSID[] = " ";
const char myPSK[] = " ";

ESP8266Server server = ESP8266Server(80);

const char destServer[] = "example.com";
const String htmlHeader = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n\r\n"
"<!DOCTYPE HTML>\r\n"
"<html>\r\n";

const String httpRequest = "GET / HTTP/1.1\n"
"Host: example.com\n"
"Connection: close\n\n";

void setup()
{

Serial.begin(9600);

initializeESP8266();

connectESP8266();


serverSetup();
}

void loop()
{
serverDemo();
}

void initializeESP8266()
{

int test = esp8266.begin();
if (test != true)
{
Serial.println(F("Error talking to ESP8266."));
errorLoop(test);
}
Serial.println(F("ESP8266 Shield Present"));
}

void connectESP8266()
{

int retVal = esp8266.getMode();
if (retVal != ESP8266_MODE_STA)
{
retVal = esp8266.setMode(ESP8266_MODE_STA);
if (retVal < 0)
{
Serial.println(F("Error setting mode."));
errorLoop(retVal);
}
}
Serial.println(F("Mode set to station"));

retVal = esp8266.status();
if (retVal <= 0)
{
Serial.print(F("Connecting to "));
Serial.println(mySSID);

retVal = esp8266.connect(mySSID, myPSK);
if (retVal < 0)
{
Serial.println(F("Error connecting"));
errorLoop(retVal);
}
}
}

void displayConnectInfo()
{
char connectedSSID[24];
memset(connectedSSID, 0, 24);

int retVal = esp8266.getAP(connectedSSID);
if (retVal > 0)
{
Serial.print(F("Connected to: "));
Serial.println(connectedSSID);
}


IPAddress myIP = esp8266.localIP();
Serial.print(F("My IP: ")); Serial.println(myIP);
}

void clientDemo()
{

ESP8266Client client;

int retVal = client.connect(destServer, 80);
if (retVal <= 0)
{
Serial.println(F("Failed to connect to server."));
return;
}

client.print(httpRequest);


while (client.available())
Serial.write(client.read());

if (client.connected())
client.stop();
}

void serverSetup()
{
server.begin();
Serial.print(F("Server started! Go to "));
Serial.println(esp8266.localIP());
Serial.println();
}

void serverDemo()
{

ESP8266Client client = server.available(500);

if (client)
{
Serial.println(F("Client Connected!"));

boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();

HTTP_req = c;
Serial.println(HTTP_req);

if (c == '\n' && currentLineIsBlank)
{
Serial.println(F("Sending HTML page"));
Serial.print(HTTP_req);

client.print(htmlHeader);
String htmlBody;

htmlBody = "Temperature Sensor A0";

htmlBody = ": ";
htmlBody = String(analogRead(A0));
htmlBody = "<br>\n";

htmlBody = "Sound Sensor A4";

htmlBody = ": ";
htmlBody = String(analogRead(A4));
htmlBody = "<br>\n";

htmlBody = "Light Sensor A5";

htmlBody = ": ";
htmlBody = String(analogRead(A5));
htmlBody = "<br>\n";

htmlBody = "</html>\n";
client.print(htmlBody);
break;
}
if (c == '\n')
{

currentLineIsBlank = true;
}
else if (c != '\r')
{

currentLineIsBlank = false;
}
}
}

delay(1);

client.stop();
Serial.println(F("Client disconnected"));
}

}


void errorLoop(int error)
{
Serial.print(F("Error: ")); Serial.println(error);
Serial.println(F("Looping forever."));
for (;;)
;
}

void serialTrigger(String message)
{
Serial.println();
Serial.println(message);
Serial.println();
while (!Serial.available())
;
while (Serial.available())
Serial.read();
}

Add your WiFi credentials to the ‘mySSID’ and ‘myPSK’ which is important for connecting to your local wireless router. When connected the serial window will spit out your WiFi settings for the server, such as the IP address. When a user connects the the servers URL the Circuit Playground will then respond with the HTTP code and you will get something that looks similar to figure 5.

HTML Response

Figure 5: Adafruit Circuit Playground Web Server

制造商零件编号 3000
CIRC PLAYGROUND CLASS ATMEGA32U4
Adafruit Industries LLC
制造商零件编号 2471
ESP8266 HUZZAH BREAKOUT BOARD
Adafruit Industries LLC
制造商零件编号 WRL-13287
WIFI SHIELD - ESP8266
SparkFun Electronics
制造商零件编号 2491
RF TXRX MOD WIFI TH SMD
Adafruit Industries LLC
制造商零件编号 BU-00287
TEST LEAD GATOR TO GATR 18" 10PC
Mueller Electric Co
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.