In part 1 of this blog, we saw how to configure the ESP8266 as an access point that runs a server that can communicate with a client.

In this blog we will configure the CC3100 WiFi booster pack as a client that will communicate with the server running on the ESP8266.

 

For this project, I've decided to use the Energia IDE for code development. Energia is based on the Wiring and Arduino framwork with lots of libraries, APIs and examples that simplify the code development process a great deal (at least for me). The downside is, of course, the lack of a debugging environment with the ability to access registers, advance step-by-step, etc. But since Energia has plenty of WiFi connectivity examples, which is exactly what I need for this project, it will be a shame to give up on.

 

1) Setting up the hardware and IDE

 

In spite of all its benefits, I ran into a very annoying issue with Energia: for the life of me, I couldn't get the on-board serial port working. As you probably know the Launchpad has an on-board serial-to-usb emulator that is connected to one of the MSP432 UART pins. This is extremely convenient for interface and debug purposes. I've tried installing and uninstalling the drivers multiple times and on multiple computers. Nothing seemed to work. The weird thing is that when the MSP was programmed using Code Composer the serial port worked fine, but when programmed using Energia it simply wouldn't work. Very frustrating.

Since the serial port is critical for developing with Energia (no debugging) I've finally decided to try and use an external UART-to-USB converter. And what do you know? Worked like a charm.

I hope you don't run into this issue. But if you do, simply disconnect the two jumpers labeled: "RXD" and "TXD" and connect your external converter instead.

 

So, in summary, if you run into a problem with the serial port while working with the MSP423 Launchpad and Energia, follow these steps:

  • Install Energia (don't forget to install the support for the MSP432 through the Board Manager).
  • Remove the two jumper labeled: "RXD" and "TXD".
  • Connect an external UART-to-USB converter to the "RXD" and "TXD" pins (on the MSP432 side).
  • Finally, connect the CC3100 booster pack to the Launchpad. Connect the Launchpad and booster pack to the PC via the micro-usb connectors (note that the micro-usb connector on the CC3100 is used as only to power the booster-pack. Although I've found it to work even when powered from the Launchpad, it is recommended to power the WiFi booster-pack directly from the USB).

 

 

2) Energia Sketch

The CC3100 is a wireless network processor that manages the WiFi and internet connections. It doesn't have an internal MCU, which means that it depends the MSP432 for configuration and control, which is done using via the SPI interface.

The sketch programs the MSP432 to configure the CC3100 as a client that transmits and receives a string to and from the server. The MSP waits for an input from the serial port, passes the string using SPI to the CC3100 which transmits it to the server. The client then receives the server's response and sends it via SPI to the MSP. The MSP then displays the response on the serial port.

 

A shortened Energia sketch,for explanations purposes, is outlined below. The full sketch is also attached. The code below is based on the "ClientSendReceiveString" example.

 

  • Define the Access Point wireless network parameters. These should match the parameters that were programmed into the ESP8266.

 

// your network name also called SSID
char ssid[] = "SafeAndSound";
// your network password
char password[] = "Element14";

 

  • Define the server parameters:
uint16_t port = 9999;     // port number of the server
IPAddress server(192, 168, 4, 1);   // IP Address of the server
WiFiClient client;

 

  • Configure the (infamous) serial port and connect to the wireless network:
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  WiFi.begin(ssid, password); // Connect to WiFi network
  while ( WiFi.status() != WL_CONNECTED) {} // Wait while we are connecting to network
  while (WiFi.localIP() == INADDR_NONE) {} // Wait while obtaining an IP address
  }

 

  • Connect to server. Note that the server connection is done inside the "loop" function. This is because the server (ESP8266) disconnects the client after a single transaction and we must renew the connection.
void loop() {  
  ConnectToServer(); 

 

void ConnectToServer() // This function connects to the server if not already connected. 
{
         if(!client.connected()) //If already connected then return to main loop. 
       {
            while (client.connect(server, port) == false) {}
            Serial.println("\nConnected to the server!");
       }
}

 

  • Accept a command from the serial port. The command is registered when pressing the "Enter" key.
  if (Serial.available()) {
    char buffer[255] = {0};
    c = Serial.read();
    while(c!='\r')
    {
      Serial.print(c);
      buffer[i]=c;
      i++;
      while(!Serial.available()){delay(1);}
        c = Serial.read();
    }

 

  • Send a command to the server
client.println(buffer);

 

  • Wait for a response and send it to serial port:
  if (client.available()) {
    char buffer[255] = {0};
    client.read((uint8_t*)buffer, client.available());
    Serial.print("Received: ");
    Serial.println(buffer);
  }

 

Here is the full sketch:

 #ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>
#include <WiFiClient.h>


// your network name also called SSID
char ssid[] = "SafeAndSound";
// your network password
char password[] = "Element14";


char c;


uint16_t port = 9999;     // port number of the server
IPAddress server(192, 168, 4, 1);   // IP Address of the server
WiFiClient client;


void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);


  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }


  Serial.println("\nIP Address obtained");
  
  // you're connected now, so print out the status  
  printWifiStatus();
  
}


void loop() {  
  ConnectToServer(); // Periodically connect to server. This is required since the ESP8266 disconnects the client after a single transmission. See blog #3. 
  int i=0;
  // Wait for input from serial port (the input should end with a "carriage return"). 
  if (Serial.available()) {
    char buffer[255] = {0};
    c = Serial.read();
    while(c!='\r')
    {
      Serial.print(c);
      buffer[i]=c;
      i++;
      while(!Serial.available()){delay(1);}
        c = Serial.read();
    }
    buffer[i]='\r';
    //send the serial command to the server
    client.println(buffer);
    Serial.print("\n\rSent: ");
    Serial.println(buffer);
  }
  
  if (client.available()) {
    char buffer[255] = {0};
    client.read((uint8_t*)buffer, client.available());
    Serial.print("Received: ");
    Serial.println(buffer);
  }
  
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("Network Name: ");
  Serial.println(WiFi.SSID());


  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);


  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}


void ConnectToServer() // This function connects to the server if not already connected. 
{
    if(!client.connected()) //If already connected then return to main loop. 
  {
      // attempt to connect to the server
  Serial.println("Attempting to connect to server");


  uint8_t tries = 0;
  while (client.connect(server, port) == false) {
    Serial.print(".");
    if (tries++ > 100) {
      Serial.println("\nThe server isn't responding");
      while(1);
    }
    delay(100);
  }
  
  //we've connected to the server by this point
  Serial.println("\nConnected to the server!");
  Serial.print("Please enter a command:");


  }
}

 

2) Testing it out

 

Ok, finally we are ready to test it all out:

  • Compile the sketch and download the code to the launchpad.
  • Connect the UART-to-USB and the previously programmed ESP8266.
  • Open two serial ports: one for the client, one for the server.
  • Make sure that client has connected successfully and now we can start transmitting and receiving information.

 

 

3) Summary

  • We have successfully established a direct WiFi link between the Launchpad+CC3100 and the ESP8266.
  • The safety glasses will be equipped with the CC3100 and the power tool with the ESP8266. Depending on the sensor readings, the CC3100 will transmit the words "ON" or "OFF" and ESP8266 will switch the tool on or off accordingly.
  • Future blog posts will re-examine the capacitive touch sensor when implemented using the MSP432.