Hello, in my following code, I am trying to connect to some server and then run a motor.
I succeed to connect to the server and run the motor, but after the second time the motor is working, the client is disconnected and I do not know where is the problem.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
int motorPin = 3;
IPAddress ip(192, 168, 0, 9);
EthernetClient client;
void setup()
{
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
while (!Serial)
{
;
}
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
delay(1000);
Serial.println("connecting...");
if (client.connect("192.168.1.104", 1688))
{
Serial.println("connected");
}
else
{
Serial.println("connection failed");
}
}
void loop()
{
char c = client.read();
if(c != -1) //available chrs to read from server
{
if (c != '\0')
{
str += c;
}
else
{
analogWrite(motorPin, 255);
delay(6000);
analogWrite(motorPin, LOW);
}
}
}
Thank for the helpers!