No Socket Available...?

Hey guys, still trying to get my code to work and its not looking so great :frowning: So i'm trying to send datavalues to my database through the wifi shield but for some reason after 4 values it just gives me the no socket available error... I have tried using client.flush() and client.stop() when getting the HTTP request but still doesn't work. If anyone could help that'd be great! thanks :slight_smile:

#include <Ethernet.h>
#include <SPI.h>
#include <WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

//byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS


const int buttonPin = 2;
const int cancelPin = 3;

IPAddress server_addr(82,197,130,55);  // IP of the MySQL *server* here
char user[] = "sks";              // MySQL user login username
char password[] = "dlaledma";        // MySQL user login password

// WiFi Information
char ssid[] = "Kalan's iPhone";    // your SSID
char pass[] = "0987654321";       // your SSID Password
int keyIndex = 0;

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");
}

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(198,199,95,237);  // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
//Scale stuff
pinMode(buttonPin, INPUT);
pinMode(cancelPin, INPUT);
  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);
  } 
  Serial.print("Firmware version: ");
  Serial.println(WiFi.firmwareVersion());
  // 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);
  } 
  Serial.println("Connected to wifi");
  printWifiStatus();
  
  //Server Connect Do here 

  /*
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
  */  
   
    


//HERE IS WHERE WE PUT IN THE SCALE STUFF
Serial.println("Hi Welcome to the arduino MCU!");
delay(1000);
Serial.println("to begin please hit enter");
enterpress();
Serial.println("Please place item on scale to tare");
float offset = calibrate();
waitforenter();
Serial.println("Please place the item on the scale");
readData(offset);
int reloop = 1;

while (reloop == 1)
{  
int YesOrNo = nextingredient();

if (YesOrNo == 1)
{
  readData(offset);
}
else 
{
  delay(1000);
  Serial.println("Ending the program now");
  reloop = ++reloop;
}

}





  
}


void loop() {
// if there are incoming bytes available 
  // from the server, read them and print them:
     //while(client.connected()) {

  while (client.available()) {
    char c = client.read();
    Serial.write(c);
    //client.flush();
    //client.stop();
   }
    
        
         
  // if the server's disconnected, stop the client:
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.flush(); //remove if needed
    client.stop(); //remove if needed

    // do nothing forevermore:
    while(true);
  } 
  
}



void enterpress()
{

  int i = 0;

while (i == 0)
{
  int buttonstate = 0;
  buttonstate = digitalRead(buttonPin);

  if (buttonstate == LOW)
  {
    delay(1000);
    Serial.println("still waiting for the enter");  
    delay(500);
  }
  else if (buttonstate == HIGH)
  {
  Serial.println("thanks for hitting enter!");
  i = i++;
  break;
  }
}
}

void readData(float offset)
{
  Serial.println("Reading the weight....");
  delay(1000);
 
  int sensorvalue = analogRead(A0);
  Serial.print("THE OFFSET VOLTAGE IS ");
  Serial.println(offset,8);
  float voltage = sensorvalue*(5/1023.0); 
  Serial.print("the voltage is ");
  Serial.println(voltage,8);
  float grams = (voltage - offset)/0.0039664;
  Serial.print("the grams is ");
  Serial.println(grams);
  
  
  //MAKE HTTP REQUEST@!@@!
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
  //client.flush(); // remove if needed
  Serial.println("connected to server");
  delay(1000);
  Serial.println("Sending weight to database... please wait");
  delay(1000);
  
  client.print("GET /recipes/scaledata/");
  client.print(grams);
  client.println(" HTTP/1.1"); 
  client.println("Host: www.google.com");
  client.println("Connection: close");
  client.println();
  //client.flush(); // remove if needed
  //client.stop(); // remove if needed

  }
  else
  Serial.println("Connection failed."); 
  
  Serial.print("the weight ");
  Serial.print(grams);
  Serial.println(" was successfully sent to the database!");
  delay(3000);
  Serial.println("exiting the reading data loop now...");
  delay(2000);
}

float calibrate()
{
int sensorvalue = analogRead(A0);

delay(1000);
Serial.println("TESTING if it enters the enterpress function");
delay(1000);
  int i = 0;

while (i == 0)
{
  int buttonstate = 0;
  buttonstate = digitalRead(buttonPin);

  if (buttonstate == LOW)
  {
    Serial.println("still waiting for the enter");  
    delay(500);
  }
  else if (buttonstate == HIGH)
  {
  Serial.println("thanks for hitting enter!");
  delay(1000);
  Serial.println("running the calibration");
  float calibrate = sensorvalue*(5/1023.0);
  return calibrate; 
  i = i++;
  break;
  }
}
}

float waitforenter()
{
int sensorvalue = analogRead(A0);

delay(1000);
Serial.println("Congrats you are done calibration");
delay(1000);
  int i = 0;

while (i == 0)
{
  int buttonstate = 0;
  buttonstate = digitalRead(buttonPin);

  if (buttonstate == LOW)
  {
    Serial.println("Please place weight on scale");  
    delay(500);
  }
  else if (buttonstate == HIGH)
  {
  Serial.println("Thanks for hitting enter, going to do conversion now");
  delay(1000);
  i = i++;
  break;
  }
}
}

int nextingredient()
{
  int yes;
  int i = 0;
  while (i == 0)
  {
  Serial.println("Would you like to weigh next ingredient? Y/N");  
  int cancelState = 0;
  cancelState = digitalRead(cancelPin);
  int buttonstate = 0;
  buttonstate = digitalRead(buttonPin);
  if (buttonstate == HIGH)
  {
   yes = 1; 
   return yes;
   i=i++;
   break;
  }
  else if (cancelState == HIGH)
  {
    delay(1000);
    Serial.println("Thank you for using Smart Kitchen Scale!");
    delay(500);
    Serial.println("Exiting now....");
    i = i++;
    break;
  }
  else 
  {
    delay(500);
    Serial.println("did not get a response");
  }
  }
}

Hey guys, still trying to get my code to work and its not looking so great

Starting another thread is NOT how to get help. Stick with the ONE thread. Answer ALL the questions asked there.

Cross-post thread locked.