you should use Socket, see here Telnet is a different protocol witch can give some problem, see here
php connects arduino telnet server very slow
Question
im building an arduino project using Ethernet Shield as Telnet server connecting to server using php telnet class almost takes 3 seconds to load
add to that the sending commands time it ranges between 3 and 3.06 seconds though arduino response very well through a normal telnet clinets
i dont know where the problem exactly
this is my arduino code
#include <SPI.h>
#include <Ethernet.h>
#define MAX_CMD_LENGTH 25
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xE3, 0x5B };
IPAddress ip(169, 254, 7, 155);
IPAddress gateway(169, 254, 7, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetServer server = EthernetServer(23);
EthernetClient client;
boolean connected = false;
String cmd;
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
pinMode(13, OUTPUT);
pinMode(7, INPUT_PULLUP);
}
void loop()
{
client = server.available();
if (client == true) {
if (!connected) {
client.flush();
connected = true;
server.println("--- Please Type on or off ---");
}
if (client.available() > 0) {
readTelnetCommand(client.read());
}
}
// check for input
if(digitalRead(7) == LOW) {
while(digitalRead(7) == LOW);
server.println("Input triggered :-)");
}
delay(10);
}
void readTelnetCommand(char c) {
if(cmd.length() == MAX_CMD_LENGTH) {
cmd = "";
}
cmd += c;
if(c == '\n') {
if(cmd.length() > 2) {
// remove \r and \n from the string
cmd = cmd.substring(0,cmd.length() - 2);
parseCommand();
}
}
}
void parseCommand() {
if(cmd.equals("quit")) {
client.stop();
connected = false;
} else if(cmd.equals("help")) {
server.println("--- Telnet Server Help ---");
server.println("on : switch on the Main Power");
server.println("off : switch off the Main Power");
server.println("quit : close the connection");
} else if(cmd.equals("on")) {
digitalWrite(13, HIGH);
server.println("--- Led Has Been Activated Ya FOX ---");
} else if(cmd.equals("off")) {
digitalWrite(13, LOW);
server.println("--- Led Has Been Deactivated Ya FOX ---");
} else {
server.println("Invalid command, type help");
}
cmd = "";
}
and im using this class i found on GitHub Php Telnet class
and this is my simple php script to turn a led on and off
<?php
require_once 'telnet.class.php';
$status = '';
$telnet = new Telnet();
if(!$telnet->connect())
{
echo "failed to connect";
}
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if(isset($_POST["on"]))
{
$telnet->exec('on');
$status = "its ON";
}
if(isset($_POST["off"]))
{
$telnet->exec('off');
$status = "its OFF";
}
}
?>
<div id="controlshit">
<form action="test.php" method="post">
<div>
<input type="submit" value="on" name="on">
</div>
<div>
<input type="submit" value="off" name="off">
</div>
</form>
<div id="status">
<p><?php echo $status; ?></p>
</div>
</div>
Solution
OTHER TIPS
i found out where the problem exactly ... the problem happens depending over the C- layer of fsockopen it connects everytime a request happens instead i used a persistent function which pfsockopen what happens really that pfsockopen connects faster and keeps the connection alive all the time during requests