KalbeAbbasPragmaticPhilBjarke Gotfredsen ☒
Published © MIT

Panic Button Using XinaBox, micro:bit and Ubidots

Learn how to create a panic button, which will send text message directly to your mobile phone when pushed.

IntermediateFull instructions provided1 hour275
Panic Button Using XinaBox, micro:bit and Ubidots

Things used in this project

Hardware components

BM01
XinaBox BM01
×1
CW01
XinaBox CW01
×1
IP01
XinaBox IP01
×1
XC10
XinaBox XC10
×1
BBC micro:bit board
BBC micro:bit board
×2
(Optional) Battery pack with 2x AAA batteries
×1
(Optional) USB extension cable
×1

Software apps and online services

Ubidots
Ubidots
XinaBox Uploader

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Code

MBGateway.py

Python
micro:bit Gateway
from microbit import *
import radio
radio.on()
radio.config(length=64, queue=8, channel=11, power=6)
#   NOTE: radio settings are identical to those in the MBclient.py code

uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin20, rx=pin19)
# NOTE: gets the uart (serial / USB) port on the micro:bit ready to communicate with the CW01

numberOfClassroomMicrobits = 10

def sendMessageToCW01(parm):
    display.clear()
    uart.write(parm)
    sleep(500)
    data = uart.readline()
    while(data is None):
        data = uart.readline()

    if(len(str(data)) >0):
        uartMessageID = getIntegerFromString(data[:1])
        if(uartMessageID == 1):     display.show(Image.YES)     # NOTE: a tick is displayed when data is being transmitted correctly to the CW01
        else:                       display.show(Image.NO)      # NOTE: this means that a cross is displayed when an attempt to send data fails

    sleep(500)


def processRadioSignal(radioSignal, numberOfClassroomMicrobits):        #   NOTE: a 'valid' signal looks like this "0_a" or this "11_b"

    if(len(str(radioSignal)) < 3):   return False                       #   NOTE: valid radio signals are at least 3 or more characters long

    locationOfUnderscore = getLocationOfUnderscore(radioSignal)
    if(locationOfUnderscore == -1): return False                        #   NOTE: valid radio signals contain an underscore

    currentMicrobitID = getIntegerFromString(radioSignal[0:locationOfUnderscore])
    if(currentMicrobitID < 0):    return False                          #   NOTE: valid radio messages begin with an integer starting at 0.
    if(currentMicrobitID >= numberOfClassroomMicrobits): return False   #   NOTE: IDs should go from 0 to (numberOfClassroomMicrobits - 1)

    #   NOTE: If we've reached this point of the code the radioSignal has passed all our validation checks.  It is 'safe' to process it.
    return sendValidMessageToCW01(radioSignal, locationOfUnderscore, currentMicrobitID)

def sendValidMessageToCW01(radioSignal, locationOfUnderscore, currentMicrobitID):

    messageType = str(radioSignal[locationOfUnderscore +1 : locationOfUnderscore +2])
    #   NOTE: you could experiment with sending new types of data from the classroom microbit - how about temperature ("0_c_25")

    if(messageType == "a"):
        sendMessageToCW01("+4@PANIC@help@1$")
        sleep(500)
        sendMessageToCW01("+4@PANIC@help@0$")
        return True

    if(messageType == "b"):
        sendMessageToCW01("+4@PANIC@ok@1$")
        sleep(500)
        sendMessageToCW01("+4@PANIC@ok@0$")
        return True

    return False


def getLocationOfUnderscore(radioSignal):
    #   NOTE: The underscore can only be in 1 of 2 places in the string, so KISS:
    radioSignalStr = str(radioSignal)
    if(radioSignalStr[1:2] == "_"):    return 1
    if(radioSignalStr[2:3] == "_"):    return 2
    return -1


def getIntegerFromString(uncheckedString):
    try: return int(uncheckedString)
    except ValueError: return -1


#   INIT:

display.show(Image.SQUARE)          # NOTE: the square is shown on your micro:bit while it is connecting to Ubidots
sleep(2000)
uart.write("$")                     # NOTE: Cleans out the serial buffer
sleep(100)
uart.write("+9@?$")                 # NOTE: Reboots the CW01
sleep(5000)                         # NOTE: long delay is necessary - we need to give Wi-Fi time to sort itself out.
uart.write("$")                     # NOTE: Clean out Serial buffer, again.
sleep(500)

sendMessageToCW01("+1@SSID@SSID-PASSWORD$")
# EDIT GUIDELINES: you MUST enter the name and password of the Wi-Fi network you are trying to connect to in the line above.

sendMessageToCW01("+2@UBIDOTS-TOKEN@?$")
# EDIT GUIDELINES: you MUST enter the DEFAULT TOKEN from Ubidots in the line above.

sendMessageToCW01("+3@things.ubidots.com@1883$")
# NOTE: above line tells the micro;bit where to send the data to - its a Ubidots URL.

while True:
    if(processRadioSignal(radio.receive(), numberOfClassroomMicrobits)):
        display.show(Image.HAPPY)
        sleep(100)
    else:
        display.show(Image.SAD)

    sleep(100)

MBClient.py

Python
mirco:bit Client
from microbit import*
import radio
radio.on()
radio.config(length=64, queue=8, channel=11, power=6)
#   NOTE - radio settings are identical to those in the gatewayMicrobit.py code

thisMicrobitID = 0
# EDIT GUIDELINES: remember to name each classroom microbit with a unique integer.  Start from 0 and count upwards.

while True:

    display.show(str(thisMicrobitID))

    if(button_a.was_pressed()):
        radio.send(str(thisMicrobitID) + "_a")
        display.show("a")
        sleep(500)

    if(button_b.was_pressed()):
        radio.send(str(thisMicrobitID) + "_b")
        display.show("b")
        sleep(500)

CW01.bin

C/C++
No preview (download only).

Credits

KalbeAbbas

KalbeAbbas

25 projects • 7 followers
An enthusiastic and dedicated Electronic engineer graduated from SSUET Karachi, Pakistan. Loves to discover Embedded electronics projects.
PragmaticPhil

PragmaticPhil

13 projects • 10 followers
Hobbyist: some of this I write from scratch, some I work with developers and test / edit. Java + micro:bit are my thing. Work at XinaBox.cc.
Bjarke Gotfredsen ☒

Bjarke Gotfredsen ☒

3 projects • 14 followers
CEO, Founder and Inventor of the XinaBox tech.
Thanks to XinaBox.

Comments