I am new to Arduino yun and my project is about using Arduino to send data(after receiving them from a sensor, but this is another topic which I am not discussing here) to a network printer, where they get printed. provided that I have the network IP-adresse and the port to the printer(192.168.0.120 / 9000 ), how can I realize that in code, using my Arduino Yun? At the moment- as there is no printer available now- I am connecting the Arduino Yun to the network with my computer and using the network Ip-addresse of the computer and then watching the ports and connections on it(using resources monitor/ LiveTcpUdpWatch programmes). there seems to be connections between them but not as I expect, because I see ports like 137/65474 … but I don't see connections to port 9000 which is written in my code. The code I am using is
#include <Bridge.h>
#include <YunClient.h>
#define PORT 9000
// Define our client object
YunClient client;
bool Connected;
// Make the client connect to the desired server and port
IPAddress addr(192, 168, 0, 120);
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
// Bridge startup
Bridge.begin();
Console.begin();
while (!Console)
{
digitalWrite(13, HIGH);
delay(125);
digitalWrite(13, LOW);
delay(125);
}
Console.println("Client connecting on port 9000");
Connected = false;
}
void loop()
{
// Is a connection currently active?
if (!Connected)
{
// Not currently connected, try to establish a connection
client.connect(addr, PORT);
if (client.connected())
{
Console.println("Connected to the server.");
// Send something to the client
client.println("Hello Server!");
// Remember that there is a connection
Connected = true;
}
else
{
// The connection attempt has failed.
Console.println("Could not connect to the server.");
// Give some time before trying again.
// A real program needs to be more inteligent here.
delay(5000);
}
}
// Is a connection currently active?
// Not an ELSE clause from the above IF, because a
// new connection might have just been established.
if (Connected)
{
// We think we are connected. Is that true?
if (client.connected())
{
// Yes, we really are connected.
// Send something to the client
client.println("Data...");
// Delay just to prevent too much data streaming
// to the server, a real application will need to
// be more intelligent here.
delay(1000);
// Read all incoming bytes available from the server and print them
while (client.available())
{
char c = client.read();
Console.print(c);
}
Console.flush();
}
else
{
// We think we are connected, but we aren't.
// The connection must've just closed on us.
Console.println("Server connection closed!");
// Clean up the client connection
client.stop();
// Track that we are no longer connected.
// This will try to establish a connection the next time around.
Connected = false;
}
}
}
I found it on this forum but I am not sure if it is the right one as I get on the serial monitor 'Client connecting on port 9000
could not connect to the server'