Hardware Reference
In-Depth Information
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");
}
 
Search WWH ::




Custom Search