Powered by Blogger.

Sunday, December 11, 2016

Lesson 15 - Magicblocks.io Mobile App

In this lesson you will learn how to use the magicblocks.io mobile application with your world of IoT. Currently only the Android version is available and can be downloaded from Google playstore here
In the world of IoT it is all about connecting devices, and making them talk with each other. We learnt about the MagicWiFi, which is a fully fledged cloud connected IoT controller and how it can be configured via the cloud. We also learnt how you can connect your Arduino to the world of IoT with the MagicShield. This lesson uncovers the possibility of using your mobile phone as a device to shape your internet of things. With the magicblocks.io mobile app your Android smartphone is turned to a virtual device ready to communicate with the magicblocks.io platform with the touch of a button. Your Android smartphone is a complicated device with ample processing power, memory and network connectivity with a rich array of sensors. It basically can outdo any other IoT device that can be obtained for a similar price range. But it must be understood that you Android smartphone is not designed to be an IoT controller as it is a consumer device designed to be used in person at all times instead of autonomous operation. Therefore it services ideally as an interaction point between the user and the platform as and when needed which is what you can achieve via the mobile application. 

This lesson will introduce you to the magicblocks.io mobile app and how to connect your smartphone to your playground.

Step 1 

Download the magicblocks.io app from the playstore

Step 2
Once you open the app you will be requested for the credentials. Since your app enables you smartphone to operate as a device, you will need to create a device with a new set of credentials as in Lesson 3. Once you create a device you can login to the app using that device ID and the device key.

Click on 'Start' and the application will open.

Step 3

You will now be greeted by the 'Basic Input' panel where you will see various types of inputs that you can send to the playground.

Basic Inputs:
  • Accelerometer: this will send the data from the phone's 3-axis accelerometer as an object. This can be used to detect movement 
    • eg: {"x": "1.1", "y": "1.2", "z": "9.9"}
  • Gyroscope: This will send the data from your phone's z-axis gyroscope as an object. This can be used to identify the orientation of your phone
    • eg: {"gamma": "180"}
The accelerometer and gyroscope readings are sent once every 1 second
  • Location: This will send the location of your phone based on GPS.
    • eg: {"lat": "5.4372", "long": "79.173284"}
The location reading will be updated if the location is changed.
  • Flip: This identifies a flip gesture of your smartphone. That is if you turn on flip and turn the phone upside down and bring it back to normal, it will be recognized as a flip and the flip message will be sent
    • eg: { "flip": "1" } 
  • Buttons 1,2,3: These are push to on buttons which are triggered by touch. One touch generates a button click action. These button clicks can be used to trigger any input in your playground
    • eg: { "btn1": "click" }, { "btn2": "click" }, { "btn3": "click" }
  • Switches 1,2,3: Similar to the buttons but these switches have two distinct states.
    • eg: { "switch1": "1" }, { "switch1": "0" } { "switch2": "1" }, { "switch2": "0" }  { "switch3": "1" }, { "switch3": "0" }
  • Slider 1 &2: You can use the slider to generate a variable input to your playground. The value of the slider will vary between 0 & 100
    • eg: { "slider1": "18" } 

Step 4
Now that you are familiar with the inputs that can be generated from the magicblocks.io mobile application, next step is to attach it to your playground. Since you have already created the device (which is your smartphone) this is fairly easy. On the playground drag a 'Magic Link In' block to the flow and set the endpoint Id as the device id of your mobile app. Set the 'type' to 'JSON' as shown below. Connect the output of the 'Magic Link In' node to a debug node so that you can easily see the data from the app.

Step 5
Click on the accelerometer and see the data flowing into the debug node. You will notice that the data logged in the debug tab looks more complicated than whats mentioned above. The actual message data is contained within the message field in the payload object. You can reference this value in you own javascript function block or you can feed the object directly to another block such as dashboard block.

Friday, December 9, 2016

Project 5: Gas Sensor

giphy (2)

In this proect we will make a LPG gas detector with magicblocks.
What You Need:
MQ-2 Gas sensor can detect LPG gas and increase output voltage with the gas concentration.Since output is between 0-5V we need to add voltage divider before analog input of the magic WiFi. MagicSensor-GAS can be directly plug in to MagicWiFi.Then twitter accounts need to be configured.

Monday, December 5, 2016

Project 3: Analog Input


Only one analog input pin availble in MagicWiFi and we need trigger it periodically to read analog value as same as digital in. Anlog input voltage range is 0-1V. If you need to measure more than 1V , use a resistor voltage divider as shown below.
voltage+divider.gif (389×300)

Resistor Values for some input voltages,
0 - 3.3V –> R1=1k   R2=2.2k
0 - 5V –> R1=1k   R2=3.9k
0 - 12V –> R1=1k   R2=10k
0 - 3.3V –> R1=10k   R2=22k
0 - 5V –> R1=10k   R2=39k
0 - 12V –> R1=10k   R2=100k

Project 2: Button

Digital In block can be used detect button press state. Push button can be connect either of following configurations,


We need to check state of the Digital In periodically. We have used inject block with triggering interval 1 second.

Sunday, December 4, 2016

Lesson 14: Playground-ESP8266 Blocks


ESP blocks set can be used with any ESP8266 based device including MagicWiFi. Following blocks are available,
  • Digital in
  • Analog In
  • Digital Out
  • PWM (Analog Out)
  • Servo
  • Serial In
  • Serial Out
  • Camera
  • NFC in

This block set enables you to control individual pins of the device from the playground. The functionality of each block is described below:

How to attach a device

Every block has a property called Device ID where you need to specify to which device this block belongs to. This is important because you will be working with multiple devices in a typical IoT project. To control a device (eg: MagicWiFi) via the playground you need to add the device as in Lesson 3 and if you have already done so, attach it to the playground according to the description below.

To attach a device ID for the first time, select 'Add new esp' and click the edit button as shown below. If you have already added the device as in Lesson 3, it will be visible to you in the dropdown and you can select the device from there.


In the next dialog click the search button which will display all your added devices. Select the device you want to add and click 'Add'. Once you have added a device you can select it from the dropdown in the respective node.


Except serial blocks every other block have property called Pin which will identify specific pin of the module.
  • Pin 2 connected to the LED on ESP module
  • Pin 6-11 not available for the user

1. Digital In

This block will read Input status of a pin. Block need to triggered to get input status. Any input will serve as a trigger. Inject node can be used for this purpose.Input status can be passed to a another block or viewed on the debug window.

  • Configuration:
    • Pin: pin number of the digital pin to read (Required)
    • Name: Any name desired
  • Input
    • Any input. Used as a trigger
  • Output
    • Value of the pin as 1 or 0 in the following format and the pin number as the topic
      • eg: {"payload": 1 , "topic": "2"}
[{"id":"bc9d8815.652678","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"","payloadType":"none","repeat":"1","crontab":"","once":false,"x":110,"y":100,"wires":[["a579225b.cb11d"]]},{"id":"a579225b.cb11d","type":"ESP Digital In","z":"6e9053f8.02fcdc","name":"","pin":"2","esp":"7708a34e.0e41dc","x":260,"y":100,"wires":[[]]},{"id":"7708a34e.0e41dc","type":"esp","z":"6e9053f8.02fcdc","device":"id14-1475889500675"}]

2. Digital Out


This block is used to set a digital output pin to 1 or 0 based on the input. An input of 1 or true will make the configured pin go HIGH and vice versa
  • Configuration
    • Pin: pin number of the Digital pin to write to
    • Name: Any name desired
  • Input
    • Value to be written to the pin. Accepts 1 (true) or 0 (false)
      • eg: {"payload": 1}
[{"id":"bc9d8815.652678","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"1","payloadType":"string","repeat":"1","crontab":"","once":false,"x":90,"y":60,"wires":[["22cd7e7b.05c302"]]},{"id":"22cd7e7b.05c302","type":"ESP Digital Out","z":"6e9053f8.02fcdc","name":"","pin":"2","esp":"7708a34e.0e41dc","x":280,"y":100,"wires":[]},{"id":"a0b73ca.fe2c3c","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"0","payloadType":"string","repeat":"1","crontab":"","once":false,"x":90,"y":140,"wires":[["22cd7e7b.05c302"]]},{"id":"7708a34e.0e41dc","type":"esp","z":"6e9053f8.02fcdc","device":"id14-1475889500675"}]

3. Analog In

This block will read analog value of the ADC pin of the module. Similar to the digital in block you need to trigger the block to read the value. Any input sent to the block will serve as the trigger.

  • Configuration:
    • Pin: pin number of the analog pin to read (Required)
    • Name: Any name desired
  • Input
    • Any input. Used as a trigger
  • Output
    • Value of the pin from 0 to 1024 and the pin number as the topic with prefix A
      • eg: {"payload": 965, "topic":"A0"}
  • Only one analog pin available hence pin should be 0 always
  • Input voltage range 0-1 V
  • ADC value range 0-1024
[{"id":"bc9d8815.652678","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"1","payloadType":"none","repeat":"1","crontab":"","once":false,"x":110,"y":60,"wires":[["a3589573.daf568"]]},{"id":"a3589573.daf568","type":"ESP Analog In","z":"6e9053f8.02fcdc","name":"","pin":"0","esp":"7708a34e.0e41dc","x":260,"y":60,"wires":[[]]},{"id":"7708a34e.0e41dc","type":"esp","z":"6e9053f8.02fcdc","device":"id14-1475889500675"}]

4. PWM (Analog Write)


This block outputs PWM signal from the selected pin

  • Configuration:
    • Pin: pin number for the PWM signal (Required)
    • Name: Any name desired
  • Input
    • value from 0 to 100
      • eg: {"payload": 55}
[{"id":"bc9d8815.652678","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"150","payloadType":"string","repeat":"1","crontab":"","once":false,"x":110,"y":60,"wires":[["ff79f939.d135f8"]]},{"id":"ff79f939.d135f8","type":"ESP PWM","z":"6e9053f8.02fcdc","name":"","pin":"2","esp":"7708a34e.0e41dc","x":250,"y":60,"wires":[]},{"id":"7708a34e.0e41dc","type":"esp","z":"6e9053f8.02fcdc","device":"id14-1475889500675"}]

5. Servo


This block can be used to control a servo motor with the MagicWiFi module.

  • Configuration:
    • Pin: pin number to which servo motor is attached (Required)
    • Name: Any name desired
  • Input
    • value from 0 to 180 translating from 0 degrees to 180 degrees for servo motor shaft rotation.
      • eg: {"payload" : 120}
[{"id":"bc9d8815.652678","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"0","payloadType":"string","repeat":"1","crontab":"","once":false,"x":110,"y":60,"wires":[["6a6915e5.3af26c"]]},{"id":"6a6915e5.3af26c","type":"ESP Servo","z":"6e9053f8.02fcdc","name":"","pin":"2","esp":"7708a34e.0e41dc","x":260,"y":100,"wires":[]},{"id":"66a4a64a.c67e88","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"90","payloadType":"string","repeat":"1","crontab":"","once":false,"x":110,"y":140,"wires":[["6a6915e5.3af26c"]]},{"id":"7708a34e.0e41dc","type":"esp","z":"6e9053f8.02fcdc","device":"id14-1475889500675"}]
6. Serial In

The Serial In block will output anything written to the RX0 of the MagicWiFi module at 115200 baud. You do not need to trigger the module as the data will be delivered in real time.

  • Configuration:
    • Name: Any name desired
  • Output
    • data read from the serial RX0 pin of the MagicWiFi module and the topic as 0x63 to indicate a serial read
      • eg: {"payload": "Your Serial Data Buffer","topic": 0x63}
[{"id":"aae18e9e.75388","type":"ESP Serial In","z":"6e9053f8.02fcdc","esp":"7708a34e.0e41dc","x":120,"y":180,"wires":[["8ae2f692.93e198"]]},{"id":"8ae2f692.93e198","type":"debug","z":"6e9053f8.02fcdc","name":"","active":true,"console":"false","complete":"false","x":300,"y":180,"wires":[]},{"id":"7708a34e.0e41dc","type":"esp","z":"6e9053f8.02fcdc","device":"id14-1475889500675"}]

7. Serial Out


The serial out block will write anything you send to the TX0 of the MagicWiFi module at 115200 baud.
  • Configuration:
    • Name: Any name desired
  • Input
    • data to be written to the serial pin
      • eg:
        • To write a string ==> {"payload": "Your Serial Data to be sent"}
        • To write a byte buffer ==> {"payload": buffername}
[{"id":"be082443.01c698","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"Hello","payloadType":"string","repeat":"","crontab":"","once":false,"x":130,"y":160,"wires":[["b48ac6fe.002a18"]]},{"id":"b48ac6fe.002a18","type":"ESP Serial Out","z":"6e9053f8.02fcdc","x":320,"y":160,"wires":[]}]

8. NFC In


The NFC In block will display the UID of an NFC card when tapped. To use this module you have to connect the MagicWiFi to a PN532 based NFC reader. The PN532 reader should be configured to communicate via Serial at 115200 baud and connected to the RX0 & TX0 of the MagicWiFi module (TX0 of MagicWiFi to RX0 of PN532 and RX0 of MagicWiFi to TX0 of PN532) and supplied 3.3V power

After connecting properly and powering up a NFC card tap on the PN532 module will be read and the data will be available in real time at the output of this block.

Note: You cannot use the Serial In block while using the NFC In block

  • Configuration:
    • Name: Any name desired
  • Output
    • UID of the NFC card tapped
      • eg: {"payload": [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07],"topic": 0x71}
[{"id":"b117fca3.9792d","type":"ESP NFC In","z":"6e9053f8.02fcdc","esp":"7708a34e.0e41dc","x":220,"y":160,"wires":[["bdafd474.db50d8"]]},{"id":"bdafd474.db50d8","type":"debug","z":"6e9053f8.02fcdc","name":"","active":true,"console":"false","complete":"false","x":390,"y":160,"wires":[]},{"id":"7708a34e.0e41dc","type":"esp","z":"6e9053f8.02fcdc","device":"id14-1475889500675"}]

9. Camera In


The Camera In block will function when the MagicWiFi is connected to a Serial Camera based on VIMICRO VC0706. On a trigger input it will capture an image and the byte buffer of the JPEG image captured will be available on the output.

You need to connect the VIMICRO VC0706 serial camera to the MagicWiFi ((TX0 of MagicWiFi to RX0 of camera and RX0 of MagicWiFi to TX0 of camera) and supply 5V power.

  • Configuration:
    • Name: Any name desired
  • Input

    • Any value as trigger
    [{"id":"bdafd474.db50d8","type":"debug","z":"6e9053f8.02fcdc","name":"","active":true,"console":"false","complete":"false","x":350,"y":220,"wires":[]},{"id":"dff2b2af.1d8c9","type":"ESP Camera","z":"6e9053f8.02fcdc","name":"","x":200,"y":160,"wires":[["bdafd474.db50d8"]]},{"id":"195c3991.463216","type":"inject","z":"6e9053f8.02fcdc","name":"","topic":"","payload":"","payloadType":"none","repeat":"5","crontab":"","once":false,"x":90,"y":120,"wires":[["dff2b2af.1d8c9"]]}]

    Friday, December 2, 2016

    Lesson 13: Playground-Debug Block


    Debug block in a another frequently used node in magicblocks. As name implies it is very useful for debugging and viewing data as similar to the serial terminal or arduino. Data can be viewed on debug window. There can be any number of debug blocks on the playground and each block can be turned on or off as shown in the figure.

    Debug Window

    Debug window is a one tab of the side bar. It can be opened as shown in above figures.
    Default settings works well in most cases.

    Example 1

    This example shows timestamp in every 1 seconds on debug window

    Example 2

    This example shows reading analog data in every 1 seconds and display on debug window

    Example 3


    This example shows data from the MagicShield or mobile phone, display on the debug window. Refer the MagicShield here on more information about connecting your Arduino to the cloud.

    Lesson 12: Playground-Inject Block

    This is a frequently used block in magicblocks. There are a few functions of this block
    • Trigger something once
    • Trigger something periodically
    • Input text once
    • Input text periodically
    • Get time stamp
    • Trigger task on a specific time
    • Manual trigger from the playground( As shown in figure)


    payload is the output type of the inject block. It can be either
    • timestamp
    • string
    • blank
    repeat is the triggering mode of the inject block. It can be either
    • none
    • interval
    • interval between times
    • at a specific time
    Name is for identification purpose of the block


    This example shows setting up digital output HIGH at exact time of the day.


    This example shows reading analog input from specific pin at 1 second interval


    This example shows setting up digital output HIGH from one block and LOW from another node.
    Download Examples