1) easily set a static IP address to the Yùn through a dedicated function directly in the sketch
That is done through Linux, not the sketch. It's the Linux side that manages the network. You can do it using the advanced configuration web pages of the Yun. Remember that the network is used by a lot more than just the sketch, so the sketch does not have the ability to selfishly reprogram that level of network configuration out from under Linux.
2) set a communication port (of my choice); by the way is the default port 5555 (as specified in the Yùn guide) or 80 (as always in Http)?
Actually, the default is that it is listening on port 5555 for connections from the localhost (the Linux processor.) The Linux processor is listening on port 80, handling the HTTP overhead, and then passing the payload along to the sketch on port 5555.
But you can manually configure the port. On the YunServer, the port to listen on is an optional parameter. In that case, you should use server.nolistenonlocalhost() to start the server, rather than server.listenonlocalhost(). The former says listen to incoming requests on that port from the general network, the latter says just listen for pre-digested connections from the processes on the Linux side.
To manually configure the port on an outgoing connection, the YunClient constructor has IP address and port number parameters.
3) quickly send simple TCP strings without the need of complicated and slower HTTP GET requests
All of the formal examples use the extra overhead of using port 80 and going through the Linux processor because using port 80 and HTTP protocols make it easy to access the data using a web browser and JavaScript. But there are alternatives.
A simple example I wrote of listening for basic incoming TCP stream connections
is HERE. That example listens for raw TCP connections coming in on port 255 (an arbitrary choice on my part.) You could just as easily use other ports. For example, you could use port 23, so you can connect to it with any Telnet client, but then you will get a bit of garbage at the beginning as the Telnet client tries in vain to negotiate settings. (If you do want to implement a true Telnet server, you will need to handle this negotiation yourself.)
Now, for outgoing connections. The idea is to create a YunClient object, and then use the connect() method to open an outbound connection. The function takes arguments to specify the server and the port number. The server could be a server name, or it could be an IP address in one of three forms: four discrete bytes, an array of four bytes, or a single 32 bit unsigned number.
Here's a simple example. It opens an outbound connection, sends something, and echos everything received to the serial port.
#include <Bridge.h>
#include <YunClient.h>
#define PORT 255
// Define our client object
YunClient client;
void setup()
{
// Bridge startup
Bridge.begin();
Serial.begin(9600);
while (!Serial); // wait for a serial connection
}
void loop()
{
// Make the client connect to the desired server and port
IPAddress addr(192, 168, 42, 185);
// Or define it using a single unsigned 32 bit value
// IPAddress addr(0xc0a8sab9); // same as 192.168.42.185
// Or define it using a byte array
// const uint8 addrBytes = {192, 168, 42, 185};
// IPAddress addr(addrBytes);
client.connect(addr, PORT);
// Or connect by a server name and port.
// Note that the Yun doesn't support mDNS by default, so "Yun.local" won't work
// client.connect("ServerName.com", PORT);
if (client.connected())
{
Serial.println("Connected to the server.");
// Send something to the client
client.println("Something...");
// Cheap way to give the server time to respond.
// A real application (as opposed to this simple example) will want to be more intelligent about this.
delay (250);
// Read all incoming bytes available from the server and print them
while (client.available())
{
char c = client.read();
Serial.print(c);
}
Serial.flush();
// Close the connection
client.stop();
}
else
Serial.println("Could not connect to the server.");
// Give some time before trying again
delay (10000);
}
So, I loaded
my server example code on one Yun (IP address 192.168.42.185) and loaded the above client code on another. When I run them, I get this from the server:
New client connection.
From client: "Something...
"
Client disconnected.
And I get this from the client:
Connected to the server.
Hello Client!
Hello Client!
Hello Client!
Hello Client!
Hello Client!
Hello Client!
Hello Client!
Hello Client!
Hello Client!
Voila! Incoming and outgoing raw TCP connections.
Any questions?