Hi,
I'am Building a weather station that sends data to my own web server (like the Wifi Pachube sensor client).
I am using:
- Visual Studio with plug in for Arduino. (Same issue when i use the Arduino 1.0.5)
- Arduino uno R3
- Arduino wifi shield with WiFi firmware version: 1.1.0. (upgraded firmware to last version).
I have following issue:
After 4 cycles is the error message “No Socket available” raised and no data is transmitted via WiFi. The cycles continue to run in ever endless loop, and in every new cycle is the message “No Socket available” raised.
I have don following:
- tested with Arduino example WiFiWebClient and get the same error.
- compared and used parts of the code in the sketch (WiFiPachubeClient).
- search Google for similar issues but starting to run out of ideas...
- checked the power and for test reason connected an extra +5 V power supply direct on the shield.
Code
#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}; // the media access control (ethernet hardware) address for the shield:
// 78:c4:0e:01:9b:57
byte ip[] = { 192,168,0,198}; // the IP address for the shield:The Arduino device IP address
//byte subnet[] = { 255,255,255,0}; // the subnet:
//byte gateway[] = { 192,168,0,1}; // the router's gateway address:
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; // status of the wifi connection
WiFiClient client; // Initialize the Wifi client library
char server[] = "www.***.se"; // server address:
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
boolean incomingData = false;
void setup()
{
//--------------------------------------------------------------
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
//--------------------------------------------------------------
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("--- WiFi shield not present");
// don't continue:
while(true);
}
//--------------------------------------------------------------
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("--- Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
//--------------------------------------------------------------
// you're connected now, so print out the status:
printWifiStatus();
}
void loop()
{
Serial.println("========================");
Serial.println("--- START ---");
//--------------------------------------------------------------
// ToDo: read the sensors:
//
//--------------------------------------------------------------
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("--- disconnecting_client_1.");
client.stop();
}
//--------------------------------------------------------------
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// if there's a successful connection:
sendDataOverWiFi();
}
//--------------------------------------------------------------
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
//--------------------------------------------------------------
// wait 60 seconds
Serial.println("--- wait 10 seconds ---");
delay(10000);
Serial.println("--- wait done");
Serial.println("--- END ---");
}
// this method makes a HTTP connection to the server:
void sendDataOverWiFi() {
Serial.println("--- connecting to server ---");
if (client.connect(server, 80)) {
Serial.println();
Serial.println("--- sending data");
// send the 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 the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + 8 bytes for "sensor1," + number of digits of the data:
int thisLength = 27; //getLength(sensor1) + getLength(sensor2);
client.println("27");
// last pieces of the HTTP PUT request:
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();
// here's the actual content of the 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 the time that the connection was made:
lastConnectionTime = millis();
Serial.println("--- sending data done");
}
else {
// if you couldn't make a connection:
Serial.println("--- connection failed");
Serial.println("--- disconnecting_client_2.");
}
//-------------------------------------------------------------
// give the server time to respond
delay(1000);
//--------------------------------------------------------------
// if there's incoming data from the net connection.
// send it out the serial port. This is for 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");
}
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
Output to serial monitor.
Se attached file:
Thanks for comments.
John
Output To Serial Monitor.pdf (103 KB)