Virtual Open Sign
Virtual Open Sign

This Instructable is my project from the YuKonstruct Spark Core Build Night!

In this Instructable, you will learn how to use a Spark Core to make a switch that posts its position to a website. We plan to use this at our YuKonstruct Makerspace to allow us to indicate open and closed hours when the schedule varies.

Bill Of Materials.

  • A Spark Core
  • A Solderless Bread-Board
  • A USB Power Supply
  • A Micro-Switch
  • Two Jumper Wires
  • A Free Google Account

    Step 1: Setting Up Your Spark Core

    Virtual Open Sign
    Virtual Open Sign

    Plug the USB cable into your spark core (first image)

    Make sure you are connected to a Wi-Fi network.

    Download the IOS or Android App "Spark Core" and Create a Spark IO account.

    Put in your Wi-Fi password (it should autofill the SSID field).

    Wait for the spark core to set itself up.

    Watch the main light on your spark core for the stages of progress:

  • Blinking blue: listening for Wi-Fi credentials.
  • Solid blue: getting Wi-Fi information from app.
  • Blinking green: connecting to the Wi-Fi network.
  • Blinking cyan: connecting to the Spark Cloud.
  • Blinking magenta: updating to the newest firmware.
  • Breathing cyan: connected!

    Next it will ask you to name your core, once finished, it will take you to the Tinker user interface (see second image).

    To test that your core is working right:

  • Tap "D7"
  • Tap digitalWrite
  • Tap "D7" again so that it says "High" beside "D7"

    A second light on you spark core should illuminate, this happens because the pin "D7" is linked to a digital pin and a LED, amazing right!

    Step 2: Plugging In Your Breadboard

    Virtual Open Sign

    Stick the spark core pins into the breadboard straddling the ridge that goes down the middle.

    Plug your micro switch into the breadboard so that it is parallel to the ridge running down the middle and either above or below where the pins end on the spark core.

    Attach a jumper wire from the hole beside the middle pin on your micro switch to the hole beside the pin labeled "D6" on the spark core.

    Now attach a jumper wire from the hole beside the top pin on your micro switch to the hole beside the pin labeled "D7" on the spark core.

    Step 3: The BackEnd

    Virtual Open Sign

    The BackEnd is where you put the coding to tell the Spark Core what you want it to do. I wrote the following code to tell the Spark Core to read the position of the switch (up or down) and then send this information to the Google script (we will configure the Google script later).

    Okay lets do this.

    On your computer, go to https://www.spark.io/build/.

    Sign in with your Spark account.

    Paste the following code into the window and then click the lightning bolt to flash this code to your Spark Core.

    char resultstr[64]; void setup(){ pinMode(D7, INPUT); pinMode(D6, OUTPUT); Spark.variable("result", &resultstr, STRING); } void loop() { digitalWrite(D6, HIGH); int data1 = digitalRead(D7); sprintf(resultstr, "{\"data1\":%d}", data1); delay(1000); }

    Step 4: Setting Up The FrontEnd

    Virtual Open Sign
    Virtual Open Sign

    The FrontEnd is what the user sees. Here we will configure the Google script or logic, so that the computer outputs either open, or closed, depending on the position of the micro switch (up or down).

    Go to https://drive.google.com and sign in with your Google account.

    To get Google Script working:

  • Click New>More>Connect more apps. (image one).
  • Search for Google Apps Script when it comes up click connect (image two).
  • To open the app click New>More>Google Apps Script.

    When Google Apps Script opens:

  • Click create Script as a web app
  • Delete everything (it's just example code).
  • Copy and paste in my code below:function doGet() { var app = UiApp.createApplication(); var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/PUT YOUR DEVICE ID HERE/result?access_token=PUT YOUR ACCESS TOKEN HERE"); try { response = JSON.parse(response.getContentText()); var result = unescape(response.result); try { var p = JSON.parse(result); var d = new Date(); if (p.data1 == 1) { var label = app.createLabel("Open") //adjust text for open .setId('statusLabel') .setVisible(true); app.add(label); } else { var label = app.createLabel("Closed") //adjust text for closed .setId('statusLabel') .setVisible(true); app.add(label); } } catch(e) { Logger.log("Unable to do second parse"); } } catch(e) { Logger.log("Unable to returned JSON"); } app.close(); return app; }

    Step 5: The FrontEnd

    Virtual Open Sign
    Virtual Open Sign

    Continuing in Google Apps Script, find the text "PUT YOUR DEVICE ID HERE" in the code and replace it with your device ID . Your device ID can be found in the "Cores Page" of spark.io/build (Click the Core Page icon on the bottom left, to open the Cores Page - first image).

    Return to your Google Apps Script and find the text "PUT YOUR ACCESS CODE HERE" in the code and replace it with your access code. Your access code can be found on the "Settings Page" of spark.io/build (Click the gear icon on the bottom left below the Core Page icon to open the Settings Page).

    At the top of the Google Apps Script page click Run>doGet. The app will then prompt you for permission to send a signal to the spark.io server. Click allow. It looks like it did absolutely nothing! However, this was important because you needed to give the script access to run the UrlFetchApp.fetch() line.

    At the top of the Google Apps Script page again, click Publish>Deploy As A Web App. A window will popup allowing you to click "Save New Version" and then "Deploy".

    That's it! Copy the link it gives you and paste it into your web browser. Voila! a working Virtual Open/Closed Sign!

    When you go to this webpage it will say either "Open" or "Closed" depending on the position of your micro switch (up or down). Try changing the position of the switch and reload the webpage to confirm that it's working (you might need to reload a couple of times).

    Note: When the switch is in the Open (up) position a second light (blue) will come on on the Spark Core.

    Have fun!

    You can change what you see on the webpage by editing the code where it says ("Open") or ("Closed"), just remember to republish the code as described above.

  •  
     

    Tag cloud

    make build easy simple arduino making homemade solar laser printed portable cheap mini building custom cardboard wooden create super lego turn paracord chocolate your paper light intel