IoT Sensor Hello World

What you will learn in this tutorial:

Preconditions

After connecting your Raspberry Pi:

you can follow this tutorial.

Hardware setup

In this exercise we are going to use pin #12 as an input (sensor) to capture the state of the push button

Raspberry Pi 1 Rev 1 Pinout reference here

Raspberry Pi 1 Rev 2 Pinout reference here

Raspberry Pi 2/3 Pinout reference here

Raspberry Pi zero w Pinout reference here

Let’s use Iottly to configure an event to be triggered when the button is pressed

Setup button callback in Iottly

In the “Management Scripts” panel edit the following snippets:

def init():
  #...

  #-------------------------------
  # here your code!!

  pin = "7"
  GPIO.setup(int(pin), GPIO.OUT)  
  pin = "12"
  GPIO.setup(int(pin), GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.add_event_detect(int(pin), GPIO.RISING, callback=button_pressed, bouncetime=200)

  #---------------------------------
# ...
def button_pressed(channel):
  status = GPIO.input(7)
  send_msg({"button_pressed": {"GPIO": channel, "state": status}})

Edge computing in Iottly

Let’s add an edge feedback so that when the button is pressed, the green LED is switched on.

Once flashed over-the-air this feedback will be triggered by the Raspberry PI, even if it is disconnected from the Internet.

# ...
def button_pressed(channel):
  status = GPIO.input(7)

  # -------- edge feedback -------------
  status = not status
  GPIO.output(7,status)
  # -------- edge feedback -------------

  send_msg({"button_pressed": {"GPIO": channel, "state": status}})