Installing and testing the python-twisted framework

In this task, we will review the python-twisted framework (https://twistedmatrix.com/trac/). The Twisted framework is an open source event-driven network engine that can be used to send control messages to other devices (for example, a pet feeder can be operated using Raspberry Pi to dispense solid food to our animal friends). The messaging protocol is called AMP (Asynchronous Messaging Protocol) which is available at http://amp-protocol.net/.

Prepare for lift off

The Raspberry Pi (with an SD card flashed with the OS image) connected to the Internet is needed for this task along with an Arduino Ethernet Board (or any one of the boards mentioned earlier in the checklist. Network connectivity is essential). We will install the python-twisted framework and review a quick example to exchange messages between Arduino and the Raspberry Pi.

Engage thrusters

  1. The python-twisted framework can be installed from the terminal of the Raspberry Pi as follows:
    sudo apt-get install python-twisted
    
  2. Once the installation is complete, it is time to review an example of the python-twisted framework to test the framework (http://twistedmatrix.com/documents/current/_downloads/simpleserv.py).
    1. In this task, we will launch a server on the Raspberry Pi and try to communicate to the server within the local network.
    2. Let's modify the preceding example to bind the python-twisted server to the IP address of Raspberry Pi so that we can exchange messages with other devices on the network. This device can either be an Arduino or a laptop.
    3. We will modify the listenTCP method to bind the server to the IP address of the Raspberry Pi and listen to incoming messages at the port address, 8000.
      reactor.listenTCP(8000,factory,50,'192.168.1.89')
    4. The web server can be launched by executing a Python script.
    5. Now, using a laptop that is connected to the same network, let's modify the simpleclient.py script to send and receive messages to the server just launched on the Raspberry Pi (https://twistedmatrix.com/documents/14.0.1/_downloads/simpleclient.py).
    6. In order to connect to the server launched on Raspberry Pi, the connectTCP method needs to be modified to include the IP address that the client has to connect:
      reactor.connectTCP('192.168.1.89', 8000, f)
    7. In this example, the Raspberry Pi is the server and the laptop is the client. The server in this example echoes all incoming messages. Thus, when the client connects and sends a message, the output will be something like:
      Server said: Hello, World!
      connection lost
      Connection lost - goodbye!
      
  3. Now that we have installed the server, let's discuss a simple Arduino sketch to interact with the server launched on the Raspberry Pi. The sketch is available along with this project's downloads (TwistedFrameworkTest.ino).
    1. We will get started by declaring an IP address object that includes the IP address of Raspberry Pi:
      //MAC Address of the Arduino
      byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x02, 0xFC };
      //IP Address of the Raspberry Pi
      IPAddress server( 192, 168, 1, 89);
    2. The Arduino acts as a TCP client and connects to the Raspberry Pi:
      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");
      }
    3. The Arduino client connects to the Raspberry Pi and transmits the Hello, World! message. The Raspberry Pi echoes back the message to the Arduino client. A screenshot of the server's response to the client is shown as follows:
    Engage thrusters

    Interaction of the Arduino client with the Raspberry Pi

  4. We demonstrated the ability to transmit and receive messages between Arduino and the Raspberry Pi.

Objective complete – mini debriefing

At the end of this project, we will discuss making use of the python-twisted framework and an Arduino Ethernet board to help you control a camera trigger or remotely dispense treats using a pet feeder.

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

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