Hi guys!
i’m using an arduino uno with wifi shield to switch a relay on and off (arduino gets the instructions from a server, to which i have access via a website).
no matter what i do, i keep getting the “No Socket Available” error, even though i flush() and stop() the client. everything else works fine, but after several runs of the loop i get the error mentioned above.
maybe somebody can help me. i’m posting the code i use below:
#include <Time.h>
#include <TimeAlarms.h>
#include <WiFi.h>
#include <WiFiUdp.h>
char ssid[] = "*********"; // your network SSID (name)
char pass[] = "********"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
IPAddress server(**,***,***,**);
WiFiClient client;
int pin = 2;
int a;
void setup() {
Serial.begin(9600);
while(!Serial) {
}
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true);
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
pinMode(pin, OUTPUT);
}
//==================LOOP=======================
void loop() {
//get command
Serial.println("start loop");
a = 0;
if(status!=WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(10000);
}
ConnectToClient(server, 80);
//DisconnectClient();
a = GetCommand();
DisconnectClient();
Serial.print("command: ");
Serial.println(a);
if (a!=0) {
SwitchPinOn(a, pin);
if(status!=WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(10000);
}
deleteit:
Serial.println("delete it");
DisconnectClient();
DeleteCommand2(server, 80);
delay(500);
ConnectToClient(server, 80);
//DisconnectClient();
a = GetCommand();
Serial.println(a);
DisconnectClient();
if (a!=0) goto deleteit;
}
Serial.println("done");
Serial.println();
delay(10000);
}
//=============================================
void ConnectToClient(IPAddress server,int port) {
retry:
if(status!=WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(10000);
}
if (client.connect(server, port)) {
Serial.println("ConnectToClient: connected to server");
client.println("GET http://******.php HTTP/1.1");
client.println("Host: ******");
client.println("Connection: close");
client.println();
return;
}
else {
Serial.println("ConnectToClient: try again");
client.flush();
client.stop();
unsigned long timer = millis();
while (millis() < timer + 5000) ;
goto retry;
}
}
int GetCommand() {
int pos;
int command = -1;
String httpReturn="";
while (client.connected()) {
while (client.available()) {
char c = client.read();
httpReturn += c;
}
}
pos = httpReturn.indexOf("mins: ");
command = httpReturn.substring(pos + 6, pos + 8).toInt();
return command;
}
void DisconnectClient() {
client.flush();
client.stop();
/*if (!client.connected()) {
client.flush();
client.stop();
}
else {
client.flush();
client.stop();
}*/
unsigned long timer = millis();
while (millis() < timer + 5000) ;
Serial.println("DisconnectClient: disconnected");
}
void DeleteCommand(IPAddress server,int port) {
retry:
if(status!=WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(10000);
}
String postmsg = "minutes=0";
if (client.connect(server, port)) {
Serial.println("DeleteCommand: connected to server");
//client.println("POST http://*******.php HTTP/1.1");
client.println("POST /******.php HTTP/1.1");
client.println("Host: **********");
client.println("User-Agent: Arduino/1.0");
//client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(postmsg.length());
client.println();
client.println(postmsg);
return;
}
else {
Serial.println("DeleteCommand: try again");
client.flush();
client.stop();
unsigned long timer = millis();
while (millis() < timer + 5000) ;
goto retry;
}
}
void DeleteCommand2(IPAddress server, int port) {
retry:
if(status!=WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(10000);
}
if (client.connect(server, port)) {
client.println("GET http://************.php HTTP/1.1");
client.println("Host: **********");
client.println("Connection: close");
client.println();
return;
}
else {
client.flush();
client.stop();
unsigned long timer = millis();
while (millis() < timer + 5000) ;
goto retry;
}
}
void SwitchPinOn(int in, int pin) {
/*
long minu = 60000;
long dauer = (long) minu * in;
digitalWrite(pin, HIGH);
delay(dauer);
digitalWrite(pin, LOW);
*/
unsigned long minu = 60000;
unsigned long dauer = (unsigned long) minu * in;
unsigned long timer = millis();
digitalWrite(pin, HIGH);
while (millis() < timer + dauer) ;
digitalWrite(pin, LOW);
}