Arduino Cloud using Parse tutorial (work in progress)

How to use exchange information between an Arduino Yun and another application via the cloud using Parse

Warning! There is a bug in version 1.6.2 of the Arduino IDE that prevents these exampnles from working. Please use Arduino IDE versions 1.6.1 or 1.6.3 which are known to work.

Connect your Yun to your local wifi network

  1. Plug in your Yun and press and hold the WLAN RST button for at least 30 seconds. This will reset your Yun to the factory default.
  2. Open your Arduino IDE, select Arduino Yun in the Tools -> Board menu, then upload this example sketch:
     File -> Examples -> Bridge -> YunSerialTerminal
  3. Open the Arduino serial monitor, select Newline, and watch the Linux side of the Yun boot. If you see nothing put the cursor in the top line of the serial monitor and press Enter.
  4. The last thing you should see is:
     root@Arduino:/#

    This is the Yun’s Linux prompt, telling you it has booted and is ready for your commands.

  5. Find your Yun’s MAC address by doing the following:
    1. In the top line of your serial monitor, type
       ifconfig <ENTER>
    2. Look for the line that says:
       wlan0 Link encap:Ethernet HWaddr 90:A2:DA:F3:02:19

      the code following HWaddr is your Yun’s MAC address.

  6. Using your laptop’s network manager, connect to the network with a name like Arduino Yun-90A2DAF30219. If you see more than one Arduino Yun, look for the one with your Yun’s MAC address.
  7. In your web browser, browse to http://arduino.local
  8. When asked, the password is arduino
  9. Click Configure
  10. Go to the section titled WIRELESS PARAMETERS and enter the SSID and password of your wireless network ( CCA or wherever you are working).
  11. Go to the section titled REST API ACCESS and select OPEN
  12. Click on CONFIGURE AND RESTART
  13. Reconnect your laptop to the CCA (or whatever) network.
  14. Monitor the Yun network restarting on the Arduino serial monitor. Eventually it should finish with something like:
     wlan0: associated
  15. Verify that your Yun is connected to the internet. In the serial monitor, put the cursor in the top line of the serial monitor and press Enter. Then type:
     ping -c 3 www.yahoo.com

    You should see something like:

    root@Arduino:/# ping -c 3 www.yahoo.com
    PING www.yahoo.com (206.190.36.45): 56 data bytes
    64 bytes from 206.190.36.45: seq=0 ttl=47 time=36.976 ms
    64 bytes from 206.190.36.45: seq=1 ttl=47 time=36.503 ms
    64 bytes from 206.190.36.45: seq=2 ttl=47 time=36.226 ms
    
    --- www.yahoo.com ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 36.226/36.568/36.976 ms
    root@Arduino:/#

Create a Parse account and install the Parse software (Yun and Arduino IDE)

  1. Make an account at parse.com
  2. Name your app (can be anything)
  3. Company type: individual developer
  4. When you get to the “Welcome to Parse! Let’s get started.” select “Embedded”
  5. Select “Arduino Yun”
  6. Don’t do any of the steps in the Parse Yun Quickstart. Skip to step 4, download the Parse Arduino Starter Project, and unzip it.
  7. In the ArduinoStarterProject/linux_package folder you will see two files:
    parse-embedded_1.0.0-1_ar71xx.ipk
    parse-embedded-yun_1.0.0-1_ar71xx.ipk

    Copy these two files to a thumb drive.

  8. Remove the thumb drive from your laptop and insert it into the Arduino Yun
  9. In the Arduino serial monitor, type:
    cd /mnt/sdb1
    opkg install *.ipk
    

    This installs the necessary Parse libraries on your Arduino Yun’s Linux side.

  10. Import the Parse library into your Arduino IDE by clicking:
    Sketch -> Include Library -> Add .ZIP Library...

    and then choose the ArduinoStarterProject/libraries/Parse directory

  11. Verify that the library has been imported properly by clicking:
    Sketch -> Include Library

    you should see a new library called Parse Arduino Yun SDK

Register your Yun to your Parse account

  1.  Create the following Arduino sketch, replacing the keys with your keys:
    #include <Bridge.h>
    #include <Parse.h>
     
    ParseClient client;
     
    void setup(){
      // Initialize digital pin 13 as an output.
      pinMode(13, OUTPUT);
     
      // Initialize Bridge
      Bridge.begin();
     
      // Initialize Serial
      Serial.begin(9600);
     
      while (!Serial); // wait for a serial connection
        Serial.println("Parse Starter Project");
     
      // Initialize Parse and associate with your application:
      client.begin("YOUR_APPLICATION_ID", "YOUR_CLIENT_KEY");
    }
     
    void loop() {
    }
  2. To find your ID and key go back to the Parse web page and your account. Click on Settings and then Keys. Copy your application ID and client key into your sketch. Replace the words between the quotation marks but keep the quotation marks.
  3. (Select Docs and Arduino to get back to the Arduino instructions.)
  4. Upload the sketch and open the serial monitor. You should see something like:
    Parse Starter Project

Storing data from your Yun to the Parse cloud

  1. By adding the following to the end of setup() in the previous sketch, your Yun will create a test object in the Parse cloud, and in that object will create the key “foo” and give it the value “bar”:
ParseObjectCreate create;
create.setClassName("TestObject");
create.add("foo", "bar");
ParseResponse response = create.send();
 
Serial.println("\nResponse for saving a TestObject:");
Serial.print(response.getJSONBody());
if (!response.getErrorCode()) {
   String objectId = response.getString("objectId");
   Serial.print("Test object id:");
   Serial.println(objectId);
} else {
   Serial.println("Failed to save the object");
}
response.close(); // Do not forget to free the resource
  1. Upload the sketch and open the serial monitor. You should see:
    Parse Starter Project

    almost immediately, and after a few more seconds you should see something like:

    Response for saving a TestObject:
    {"createdAt":"2015-04-07T16:33:34.572Z","objectId":"PJqwqJkCZw"}
    Test object id:PJqwqJkCZw

    This confirms that your Yun has succesfully saved something to the Parse cloud

Reading data from your Parse cloud into the Yun (and blinking an LED)

In this complete example, the Yun is configured to accept push notifications from the cloud. When a push notification arrives, the key named alert is requested. If the value of alert is A test push from Parse!, then the pin 13 LED is turned on briefly:

#include <Bridge.h>
#include <Parse.h>
 
ParseClient client;
 
void setup(){
  // Initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
 
  // Initialize Bridge
  Bridge.begin();
 
  // Initialize Serial
  Serial.begin(9600);
 
  while (!Serial); // wait for a serial connection
    Serial.println("Parse Starter Project");
 
  // Initialize Parse and associate with your application:
  client.begin("YOUR_APPLICATION_ID", "YOUR_CLIENT_KEY");
 
  // Start push service
  client.startPushService();
  Serial.print("Push Installation ID:");
  Serial.println(client.getInstallationId());
}
 
void loop() {
  // Check if there is a new push
  // A push with message {"alert":"A test push from Parse!"}
  // will turn on LED for 3 seconds
  if (client.pushAvailable()) {
    ParsePush push = client.nextPush();
 
    String message = push.getJSONBody();
    Serial.print("New push message size: ");
    Serial.println(message.length());
    Serial.print("New push message content: ");
    Serial.println(message);
 
    String command = push.getString("alert");
    if (command == "A test push from Parse!") {
       digitalWrite(13, HIGH); // turn on LED
       delay(3000);  // wait 3 seconds
       digitalWrite(13, LOW); // turn off LED
    }
    // NOTE: ensure to close current push message
    // otherwise next push won't be available
    push.close();
  }
}
  1. As before, replace the application ID and client ID with your keys, upload the sketch, and open the serial monitor. You should see something like:
    Parse Starter Project
    Push Installation ID:9bc8a99e-6c93-48a9-9859-01320a421609
  2. Now go back to your Parse web page and the Arduino Quickstart guide. At the bottom of the page is a section that says
    Test your Changes!

    and inside it a button that says

    test
  3. Press the
    test

    button and the Pin 13 LED on your Yun should turn on for 3 seconds

Resources

 
Print Friendly
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Leave a Reply

Your email address will not be published. Required fields are marked *