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/.
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.
python-twisted
framework can be installed from the terminal of the Raspberry Pi as follows:sudo apt-get install python-twisted
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.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')
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).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)
Server said: Hello, World! connection lost Connection lost - goodbye!
TwistedFrameworkTest.ino
).//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);
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.138.105.124