No Socket Available

Hi,

I have a system that sends data to Google sheets with Arduino WIFI Rev 2 using PushingBox. After 11 iterations of sending data a "No Socket Available" message appears on the serial monitor, and the data doesn't pass to Google Sheet.

I've tried upgrading my Firmware to 1.8.13, it didn't fix it.
I've tried to use client flush(); and client stop(); , it didn't fix it.

This is the code im using:

#include <SoftwareSerial.h>
#include <WiFiNINA.h>
#include <SPI.h>

const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
const String devid = "this is my device id "; //device ID on Pushingbox for our Scenario

const char* MY_SSID = "Assaflrr";
const char* MY_PWD =  "0542402893";

int status = WL_IDLE_STATUS;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port 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(MY_SSID);
    //Connect to WPA/WPA2 network.Change this line if using open/WEP network
    status = WiFi.begin(MY_SSID, MY_PWD);

    // wait 10 seconds for connection:
    delay(10000);
  }
  
  Serial.println("Connected to wifi");
  printWifiStatus();
  
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void loop() {

   // Wait between measurements.
  delay(10000);

  int humidityData = 1;
  int celData = 2;
  int fehrData =3;
  int hifData = 4;
  int hicData = 5;

  Serial.print("Humidity: ");
  Serial.print(humidityData);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(celData);
  Serial.print(" *C ");
  Serial.print(fehrData);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hicData);
  Serial.print(" *C ");
  Serial.print(hifData);
  Serial.println(" *F\n");

Serial.println("\nSending Data to Server..."); 
  // if you get a connection, report back via serial:
WiFiClient client;  //Instantiate WiFi object, can scope from here or Globally

    //API service using WiFi Client through PushingBox then relayed to Google
    if (client.connect(WEBSITE, 80))
      { 
         client.print("GET /pushingbox?devid=" + devid
       + "&humidityData=" + (String) humidityData
       + "&celData="      + (String) celData
       + "&fehrData="     + (String) fehrData
       + "&hicData="      + (String) hicData
       + "&hifData="      + (String) hifData
         );

      // HTTP 1.1 provides a persistent connection, allowing batched requests
      // or pipelined to an output buffer
      client.println(" HTTP/1.1"); 
      client.print("Host: ");
      client.println(WEBSITE);
      client.println();
   
     Serial.println("\nData Sent"); 
   
      }
  
}

Im really stuck on this one, any help is appriciated.
Thank you!

Your instantiate new client in every run of loop().
Make this object global and re-use it instead of creating it again and again

1 Like

Agree, you may even make it static or create a class for the potentially global variable.
The Wifi Rev 2 has a bit more memory than UNO so may not be neccesary, but it always a good habbit to be precise wit variables, especcially global. What often is done is to include variables in loop, if you also create som logic to test for first use or if class / variable / object / array etc. is set

That way you remove things happening only once to be placet in setup() nedding a global variable that in fact loop is supposed to use.

We did that in one project connect to wifi, but if that failed anyhow, restart was needed.
If we instaead used this in loop (more intelligently programmed ) the Arduino may have tested every now and then and it self would reconnect / recreate an instance or similar. This was just a comment on your code and the other reply to help you investigate and dig in to this situation further. Often when you make a smal project to test something you make som simple code, then you want to expand it and make a more "productionlike" code. Then you need to rethink on anything. We had a tool running for 24 hour and ended up restarting it manually several times during that period and discussed what did happen and how to fix it on another project.

Error handling is one of the key components.

1 Like

Hi again, there are some other libraries for http-connection. Find libraries in Arduino IDE httpclient ... Just look throu alle libraries you are presented for som ideas. Search on internet / forums etc. for good and robust ideas.
Here are some include-lines for a project

#include <Wire.h>
#include <SPI.h>
#include <EEPROM.h>
#include <Adafruit_PN532.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h> // .\Documents\Arduino\libraries\ArduinoHttpClient\src
#include "lokal.h" // Include-file for secret info. Dette er inkluderingsfilen for hemmelig info. 
#include "ArduinoUniqueID.h"



1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.