A simple exercise tool using the Raspberry Pi

In this task, we will review a fun example that enables a person to be physically active every 60 minutes.

Prepare for lift off

This example is based on the Twisted framework discussed in the previous project. It would be great to have two Raspberry Pi boards (set up with the SD card images and powered up) or an Arduino Ethernet board. A laptop that is connected to the same network as the Raspberry Pi is just as sufficient.

If you missed installing the Twisted framework in the previous project, the Twisted framework can be installed as follows:

sudo apt-get install python-twisted

Note

If the second device is a Raspberry Pi or a laptop that runs Linux, Mac, or Windows operating systems, the Twisted framework needs to be installed on the second device as well.

Engage thrusters

  1. Let's perform a quick review of what we will build in this project:
    • Let's consider two devices that can be connected to a network. One of the devices is a Raspberry Pi while the other device could either be an Arduino or a Raspberry Pi. We will review the code required to build this tool for both cases.
    • These devices have to be installed at two extreme corners inside a house. This can be different floors of the house or the farthest corners of the house.
    • A buzzer and a button will be connected to a Raspberry Pi.
    • The buzzer would go off every 60 minutes on one of the devices. Someone has to walk up to the device and turn off the buzzer by pressing a button.

    This enables a person to get some physical activity by walking back and forth between the two devices (assuming the Raspberry Pi devices are installed far away from the person in the interest of gaining some physical activity).

    Note

    This example is just a motivational tool and a casual reminder for the concerned person to remain physically active.

  2. The buzzer and the switch are connected to the Raspberry Pi as shown in the following diagram:
    Engage thrusters

    A button and buzzer schematic created using Fritzing

    • The preceding breadboard representation shows an Adafruit Pi Cobbler mounted on a breadboard. Refer to Project 4, Christmas Light Sequencer, for a pictorial representation of how the Pi Cobbler needs to be connected to the Raspberry Pi.
    • GPIO #25 of the Raspberry Pi is connected to the base of the NPN transistor, BC547. The transistor's collector pin is connected to the negative terminal of the buzzer. The other end of the buzzer is connected to 5 V. The emitter pin of the transistor is connected to the ground. The transistor acts as a switch and turns on the buzzer when the base pin is set to high.
    • GPIO #18 of the Raspberry Pi is pulled up to 3.3 V and a tactile switch is connected to the GPIO switch. The other end of the switch is connected to the ground.
  3. Let's perform a quick review of the twisted server code that runs on the Raspberry Pi. Similar to the previous project, this example is also a simple modification of the Twisted framework example, simpleserver (http://twistedmatrix.com/documents/current/_downloads/simpleserv.py).
    • Let's create a class that takes care of setting off the buzzer when there is an incoming message:
      #Declare inputs and outputs.
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(18,GPIO.IN)
      GPIO.setup(25,GPIO.OUT)
      GPIO.setwarnings(False)
      
      class AsyncTask:
        def __init__(self):
          self.run_state = True
        #avoiding the channel argument throws an error
        def terminate(self,channel):
          self.run_state = False
      
        def add_callback(self):
          GPIO.add_event_detect(18,GPIO.FALLING, callback=self.terminate)
      
        def run(self):
          while self.run_state == True:
            GPIO.output(25,GPIO.HIGH)
            time.sleep(1)
            GPIO.output(25,GPIO.LOW)
            time.sleep(1)
          GPIO.remove_event_detect(18)
    • In the AsyncTask class, the buzzer can be set off by triggering a separate thread to call the run function. This sets off the buzzer with a one-second interval.
    • The add_callback method is used to turn off the buzzer when the button is pressed. The add_event_detect method waits for the state of GPIO #18 to change from high to low. This turns off the buzzer by setting run_state to False. While exiting the infinite loop, we remove the callback functions using the remove_event_detect method.
    • When a client (Arduino, laptop, or another Raspberry Pi) sends a message to the server, the thread is initialized and a callback function is registered as follows:
        async_task = AsyncTask()
        async_task.add_callback()
        thread = Thread(target=async_task.run, args=())
        thread.start()
    • To summarize, an incoming message triggers the buzzer and a person has to walk to the Raspberry Pi's location to turn off the buzzer.
  4. If the client device that sends a message to the Raspberry Pi is a laptop that runs a Linux, Mac, or Windows operating system or the Raspberry Pi, it is sufficient to run the simpleclient.py example after modifying the code to change the server address to that of the Raspberry Pi (https://twistedmatrix.com/documents/14.0.1/_downloads/simpleclient.py).
  5. In the preceding example, we demonstrate a single cycle to set off the buzzer. This can be repeated in cycles of 60 minutes using a batch script (in a Windows environment) or a shell script (Linux or Mac environment).
  6. If the device is an Arduino, EthernetClient has to be initialized and connected to the Raspberry Pi to send a test message. This can be repeated in a 60-minute cycle:
     Serial.println("connecting...");
      if (client.connect(server, 8000)) {
        Serial.println("connected");
        client.println("Hello, World!");
        client.println();
        //Lets wait for the client to read and 
        //echo the message
        //Note: A second's delay is a bit excessive
        delay(1000);
        //If there is a response from the server
        //echo back the message
         Serial.println("Server says:");
         while(client.available()) {
          char c = client.read();
          Serial.print(c);
        }
        client.stop();
        Serial.println("Client Disconnected");
      } else {
        Serial.println("connection failed");
      }

A mini project idea

It will be fun to install a number of Raspberry Pi boards in the same network and implement an asynchronous messaging protocol. This protocol can set off buzzers as a chain reaction to encourage more physical activity. Please note that this may annoy other members of the household.

Objective complete – mini debriefing

We discussed a tool that can annoy a person to remain physically active.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.168.2