Setting up and testing Spark Core to interact with appliances

In this task, we will set up Spark Core. It is a development board that makes bringing Wi-Fi to hardware very easy. We will use the Core for this project because we love the simplicity of the setup and operation. You can read more about Spark Core at the official website, https://www.spark.io/.

You are welcome to use a similar device of your choosing. A good alternative will be to use an Arduino with a Wi-Fi adapter/shield (for example, the ESP8266 Wi-Fi module can be found at http://www.seeedstudio.com/wiki/WiFi_Serial_Transceiver_Module) or if you prefer, an Ethernet shield (as discussed in the previous task of this project).

Prepare for lift off

In this task, we will set up the Spark Core. Power up the Spark Core using a micro-USB cable connected to a power source (5 V source). It can be powered using a battery pack, a laptop, or a 5 V USB wall wart. Based on your project needs, you can use a battery pack if the sensor node for the pet monitor has to be installed outdoors.

The easiest way to connect the Spark Core to your Wi-Fi network is through a phone application. This and other mechanisms are detailed on the Spark Core website (http://docs.spark.io/connect/). We will set up our Spark Core by downloading the application onto an Android phone and Samsung Galaxy S5 through the Google Play store (https://play.google.com/store/apps/details?id=io.spark.core.android&hl=en). The app searches for Spark Cores in range and if you supply the Wi-Fi password, it sends the code to the Core(s) and lets it connect to your Wi-Fi.

Prepare for lift off

The SparkCore board

Engage Thrusters

We will warm up by implementing the LED example on Spark Core or something similar.

The documentation includes code to connect an LED with a resistor to the Core (http://docs.spark.io/examples/). We decided to do something even simpler. There is already an LED connected to pin D7 and we decided to turn this one on instead.

All the apps you need can be downloaded to a custom web page available for each Spark Core through a username and password.

Once you have set up the Core, you will create a username and password. For different Cores, you will need different usernames. Using this, you can go to the Spark home page (http://www.spark.io/) and click on the Build button. This takes you to the home page of the development environment.

Using this development environment, we created a simple LED blinking application:

void setup() {
  pinMode(7,OUTPUT);
}
void loop() {
// turn the LED on (HIGH is the voltage level)
  digitalWrite(7, HIGH);
// wait for a second 
  delay(1000);
// turn the LED off by making the voltage LOW
  digitalWrite(7, LOW);
// wait for a second
  delay(1000);
}

Note

The preceding code is written in C programming language. If you are jumping directly to this project from other projects, we suggest that you familiarize yourself with the Arduino or Spark Core platform. Some learning resources are available at https://learn.adafruit.com/search?q=Arduino

As you can see, the app is quite simple; the app sets up the pin 7 as output. The program turns on the LED for a second and turns it off for one second.

Now, let's discuss an example that is more or less similar to the example discussed in the previous task:

TCPClient client;
// IP Address of the Raspberry Pi
byte server[] = { 192, 168, 1, 89 }; 
void setup()
{
  Serial.begin(9600);

  while(!Serial.available()) SPARK_WLAN_Loop();

  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 8000)) {
    Serial.println("connected");
    client.println("Hello, World!");
    client.println();
    //Lets wait for the client to read and 
    //echo the message
    //Note: A second's delay is a bit excessive
    delay(1000);
    //If there is a response from the server
    //echo back the message
      Serial.println("Server says:");
      while(client.available()) {
      char c = client.read();
      Serial.print(c);
    }
    client.stop();
    Serial.println("Client Disconnected");
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
//Nothing to do here
}

The only difference between the previous example and this example is that the latter uses the TCPClient class while the former uses the EthernetClient class.

Objective complete – mini debriefing

That's it. You now have Spark Core, all set up and ready to go, with your username, and your own page to download apps and collect and transmit data through the Core.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.192.3