[solved] Arduino wifi shield - “No Socket available”
hi,
i'am building weather station sends data own web server (like wifi pachube sensor client).
i using:
- visual studio plug in arduino. (same issue when use arduino 1.0.5)
- arduino uno r3
- arduino wifi shield wifi firmware version: 1.1.0. (upgraded firmware last version).
i have following issue:
after 4 cycles error message "no socket available" raised , no data transmitted via wifi. cycles continue run in ever endless loop, , in every new cycle message "no socket available" raised.
i have don following:
- tested arduino example wifiwebclient , same error.
- compared , used parts of code in sketch (wifipachubeclient).
- search google similar issues starting run out of ideas...
- checked power , test reason connected +5 v power supply direct on shield.
code
output serial monitor.
se attached file:
thanks comments.
john
i'am building weather station sends data own web server (like wifi pachube sensor client).
i using:
- visual studio plug in arduino. (same issue when use arduino 1.0.5)
- arduino uno r3
- arduino wifi shield wifi firmware version: 1.1.0. (upgraded firmware last version).
i have following issue:
after 4 cycles error message "no socket available" raised , no data transmitted via wifi. cycles continue run in ever endless loop, , in every new cycle message "no socket available" raised.
i have don following:
- tested arduino example wifiwebclient , same error.
- compared , used parts of code in sketch (wifipachubeclient).
- search google similar issues starting run out of ideas...
- checked power , test reason connected +5 v power supply direct on shield.
code
code: [select]
#include <spi.h>
#include <wifi.h>
#define apikey "crwlknas" // api key
#define feedid "00001" // feed id
#define useragent "environment data" // project name
byte mac[] = {0x90, 0xa2, 0xda, 0x0e, 0x96, 0xca}; // media access control (ethernet hardware) address shield:
// 78:c4:0e:01:9b:57
byte ip[] = { 192,168,0,198}; // ip address shield:the arduino device ip address
//byte subnet[] = { 255,255,255,0}; // subnet:
//byte gateway[] = { 192,168,0,1}; // router's gateway address:
char ssid[] = "***"; // network ssid (name)
char pass[] = "***"; // network password (use wpa, or use key wep)
int keyindex = 0; // network key index number (needed wep)
int status = wl_idle_status; // status of wifi connection
wificlient client; // initialize wifi client library
char server[] = "www.***.se"; // server address:
unsigned long lastconnectiontime = 0; // last time connected server, in milliseconds
boolean lastconnected = false; // state of connection last time through main loop
const unsigned long postinginterval = 10*1000; // delay between updates, in milliseconds
boolean incomingdata = false;
void setup()
{
//--------------------------------------------------------------
//initialize serial , wait port open:
serial.begin(9600);
while (!serial) {
; // wait serial port connect. needed leonardo only
}
//--------------------------------------------------------------
// check presence of shield:
if (wifi.status() == wl_no_shield) {
serial.println("--- wifi shield not present");
// don't continue:
while(true);
}
//--------------------------------------------------------------
// attempt connect wifi network:
while ( status != wl_connected) {
serial.print("--- attempting connect ssid: ");
serial.println(ssid);
// connect wpa/wpa2 network. change line if using open or wep network:
status = wifi.begin(ssid, pass);
// wait 10 seconds connection:
delay(10000);
}
//--------------------------------------------------------------
// you're connected now, print out status:
printwifistatus();
}
void loop()
{
serial.println("========================");
serial.println("--- start ---");
//--------------------------------------------------------------
// todo: read sensors:
//
//--------------------------------------------------------------
// if there's no net connection, there 1 last time
// through loop, stop client:
if (!client.connected() && lastconnected) {
serial.println();
serial.println("--- disconnecting_client_1.");
client.stop();
}
//--------------------------------------------------------------
// if you're not connected, , ten seconds have passed since
// last connection, connect again , send data:
if(!client.connected() && (millis() - lastconnectiontime > postinginterval)) {
// if there's successful connection:
senddataoverwifi();
}
//--------------------------------------------------------------
// store state of connection next time through
// loop:
lastconnected = client.connected();
//--------------------------------------------------------------
// wait 60 seconds
serial.println("--- wait 10 seconds ---");
delay(10000);
serial.println("--- wait done");
serial.println("--- end ---");
}
// method makes http connection server:
void senddataoverwifi() {
serial.println("--- connecting server ---");
if (client.connect(server, 80)) {
serial.println();
serial.println("--- sending data");
// send http put request:
client.print("put /");
client.print(feedid);
client.println(".csv http/1.1");
client.println("host: www.***.se");
client.print("apikey: ");
client.println(apikey);
client.print("user-agent: ");
client.println(useragent);
client.print("content-length: ");
// calculate length of sensor reading in bytes:
// 8 bytes "sensor1," + 8 bytes "sensor1," + number of digits of data:
int thislength = 27; //getlength(sensor1) + getlength(sensor2);
client.println("27");
// last pieces of http put request:
client.println("content-type: text/csv");
client.println("connection: close");
client.println();
// here's actual content of put request:
client.print("sensor1;"); // air
client.println("18.8");
client.print("sensor2;"); // water
client.println("12.3");
client.println(" ");
// wait 10 seconds:
delay(1000);
// note time connection made:
lastconnectiontime = millis();
serial.println("--- sending data done");
}
else {
// if couldn't make connection:
serial.println("--- connection failed");
serial.println("--- disconnecting_client_2.");
}
//-------------------------------------------------------------
// give server time respond
delay(1000);
//--------------------------------------------------------------
// if there's incoming data net connection.
// send out serial port. this debugging
// purposes only:
while (client.available()) {
if (incomingdata == false)
{
serial.println();
serial.println("--- incoming data start ---");
incomingdata = true;
}
char c = client.read();
serial.write(c);
}
// debugging --------------------------------------------------
if (incomingdata == true)
{
serial.println("--- incoming data end");
incomingdata = false;
}
if (status == wl_connected){
serial.println("--- wifi connected");
}
else{
serial.println("--- wifi not connected");
}
}
// method calculates number of digits in the
// sensor reading. since each digit of ascii decimal
// representation byte, number of digits equals
// number of bytes:
int getlength(int somevalue) {
// there's @ least 1 byte:
int digits = 1;
// continually divide value ten,
// adding 1 digit count each
// time divide, until you're @ 0:
int dividend = somevalue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return number of digits:
return digits;
}
output serial monitor.
se attached file:
thanks comments.
john
once data client, not close client connection. after while, sockets in use. appears issue having. closing client connection after getting data needed.
your while loop client data needs check client.connected(), too. when client no longer has data available, call client.flush() , client.stop().
your while loop client data needs check client.connected(), too. when client no longer has data available, call client.flush() , client.stop().
Arduino Forum > Using Arduino > Programming Questions > [solved] Arduino wifi shield - “No Socket available”
arduino
Comments
Post a Comment