1

It might be a stupid question but I can't figure it out. While running the code below. I want to open the box when the code matches the code in API. This all works. I can even close the box afterwords. But then I am stuck inside my loop. after the box is closed I want the screen to say box closed and then I want the void loop() to start again. so my screen asks for a code again. I tried using break and exit but no luck so far. entire code:

/* hardware libraries*/
#include <Key.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

/*Wifi and request libraries*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>

#define HIGH 0x1
#define LOW 0x0

/* wifi credentials*/
char ssid[] = "ssid";        // your network SSID (name)
char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;

int status = WL_IDLE_STATUS;
char server[] = "parcelbox.1819.caro.deweze.nxtmediatech.eu";
// 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;

LiquidCrystal_I2C lcd(0x27, 16, 2); // lcd object
Servo myservo; //servo object
boolean boxOpenend = false ;

/* Keypad*/
const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {3, 4, 5, 6};
byte colPins[COLS] = {9, 8, 7};

Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String value = "";

void setup() {
  lcd.begin(); //start lcd
  Serial.begin(9600);
  myservo.attach(A0); // attach servo to pin
  lcd.backlight(); // start screen light

  /* connect to wifi */
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

  // 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();
}

void loop() {
  lcd.clear();
  lcd.print("Geef bestelcode");
  char customKey = customKeypad.getKey();   // get the key pressed
  value = value + String(customKey);
  lcd.setCursor(0, 1);
  lcd.print(value);
  if (customKey == '#') {

    String response_Status = "";
    Serial.println("\nStarting connection to server...");

    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {

      value.remove(16);
      Serial.println(value);
      Serial.println("connected to server");

      // Make a HTTP request:
      client.println("GET /api/boxes/1444/code/" + value + " HTTP/1.1");
      client.println("Host: parcelbox.1819.caro.deweze.nxtmediatech.eu");
      client.println("Connection: close");
      client.println();
    }
    // if there are incoming bytes available
    // from the server, read them and print them:
    StaticJsonBuffer<500> jsonBuffer;

    while (client.connected()) {

      String response = "";
      bool begin = false;

      while (client.available() || !begin) {
        char c = client.read();

        if (c == '{') {
          begin = true;
        }
        if (begin) response += (c);
        if (c == '"}') {
          break;
        }
        delay(1);
      }
      int start = response.indexOf("status\":\"") + 11;
      int end = start + 3;
      response_Status = response.substring(start, end);

      JsonObject& root = jsonBuffer.parseObject(response);

      if (!root.success())
      {
        Serial.print("parseObject(");
        Serial.print(response);
        Serial.println(") failed");
        break;
      }

      Serial.println(response_Status);
      if (response_Status == "200") {

        Serial.println("200");
        boxOpenend = true;

        while (boxOpenend) {

          lcd.clear();
          lcd.print("Box openend ,press");
          lcd.setCursor(0, 1);
          lcd.print(" * to close");

          myservo.write(0);// rotate the servo to open
          delay(175);

          char key = customKeypad.getKey();   // get the key pressed
          //value = value + String(key);

          if (key == '*') {
            myservo.write(90); // rotate the servo to lock
            value = "";                           // clear the string
            boxOpenend = false;

          }
          // box gaat terug toe maar ik blijf in deze loop zitten want lcd scherm blijft staan op "Box geopend , druk * om te sluiten
        }

      }
      else if (response_Status != "200") {

        Serial.println("not 200");

        lcd.clear();
        lcd.print("wrong code or");
        lcd.setCursor(0, 1);
        lcd.print("already deliverd");

        delay(175);
        value = "";
        exit;
      }
      //hier net hetzelfde
      break ;
    }
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println();
      Serial.println("disconnecting from server.");
      client.stop();

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

  if (customKey == '*') {
    myservo.write(90);                   // rotate the servo to lock
    value = "";                           // clear the string
  }

  delay(175); // for optimal lcd screen view
}

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

  // print your board'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");
}

This is where the code get's stuck. It keeps on showing box opened press * to close. even when I press * and the box closes it keeps on saying this

while (boxOpenend) {

          lcd.clear();
          lcd.print("Box openend ,press");
          lcd.setCursor(0, 1);
          lcd.print(" * to close");

          myservo.write(0);// rotate the servo to open
          delay(175);

          char key = customKeypad.getKey();   // get the key pressed
          //value = value + String(key);

          if (key == '*') {
            myservo.write(90); // rotate the servo to lock
            value = "";                           // clear the string
            boxOpenend = false;

          }
          // box gaat terug toe maar ik blijf in deze loop zitten want lcd scherm blijft staan op "Box geopend , druk * om te sluiten
        }

      }

It might have a simple solution that I'm just not thinking of but it really bothers me!

UPDATE , We made a little progress as to figuring out why it wont work. the code that we are running now is:

/* hardware libraries*/
#include <Key.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

/*Wifi and request libraries*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>

#define HIGH 0x1
#define LOW 0x0

/* wifi credentials*/
char ssid[] = "XXXXXXX";        // your network SSID (name)
char pass[] = "XXXXXXX";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;

int status = WL_IDLE_STATUS;
char server[] = "parcelbox.1819.caro.deweze.nxtmediatech.eu";
// 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;

LiquidCrystal_I2C lcd(0x27, 16, 2); // lcd object
Servo myservo; //servo object
boolean boxOpenend = false ;

/* Keypad*/
const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {3, 4, 5, 6};
byte colPins[COLS] = {9, 8, 7};

Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String value = "";

void setup() {
  lcd.begin(); //start lcd
  Serial.begin(9600);
  myservo.attach(A0); // attach servo to pin
  lcd.backlight(); // start screen light

  /* connect to wifi */
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

  // 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();
}

void loop() {
  lcd.clear();
  lcd.print("Geef bestelcode");

  char customKey = customKeypad.getKey();   // get the key pressed
  value = value + String(customKey);

  lcd.setCursor(0, 1);
  lcd.print(value);


  if (customKey == '#') 
  {
  Serial.println("PRESSED #");
    String response_Status = "";
    Serial.println("\nStarting connection to server...");

  Serial.println("RESULT OF CLIENT.CONNECT()");
  Serial.println(client.connect(server, 80));

    // if you get a connection, report back via serial:
    if (client.connect(server, 80) == 1) 
  {
    Serial.println("IK BEN HIER");
    value.remove(4);
    Serial.println(value);
    Serial.println("connected to server");

    // Make a HTTP request:
    client.println("GET /api/boxes/1444/pin/" + value + " HTTP/1.1");
    client.println("Host: parcelbox.1819.caro.deweze.nxtmediatech.eu");
    client.println("Connection: close");
    client.println();
  } 
  else 
  {
    Serial.println("client.connect() not successfull");
  }

    // if there are incoming bytes available
    // from the server, read them and print them:
    StaticJsonBuffer<500> jsonBuffer;

    if (client.connected()) 
  {
    Serial.println("CLIENT CONNECTED");

    String response = "";
    bool begin = false;

    while (client.available() || !begin) {
      char c = client.read();

      if (c == '{') {
        begin = true;
      }

      if (begin) {
        response += (c);
      }

      if (c == '"}') {
        break;
      }

      delay(1);
    }

    int start = response.indexOf("status\":\"") + 11;
    int end = start + 3;

    response_Status = response.substring(start, end);
    Serial.println(response_Status);

    // response successfull
    if (response_Status == "200")
    {
      Serial.println("RESPONSE 200");
      Serial.println("response state == 200");

      // First open the box
      // 0° means open
      Serial.println("OPEN BOX");
      myservo.write(0);
      delay(175);

      lcd.setCursor(0, 0);
      lcd.print("                ");
      lcd.setCursor(0, 0);
      lcd.print("Box opened ,press");
      lcd.setCursor(0, 1);
      lcd.print("                ");
      lcd.setCursor(0, 1);
      lcd.print(" * to close");
    }
    else 
    {
      Serial.println("RESPONSE NOT 200");
      Serial.println("not 200");

      lcd.clear();
      lcd.print("Foute code of");
      lcd.setCursor(0, 1);
      lcd.print("al afgeleverd");

      delay(175);
    }
    }

  // reset the value
  value = "";
  }

  // lock box and reset value
  if (customKey == '*') 
  {
    Serial.println("CLOSE BOX");
    myservo.write(90); 
    value = "";
    client.stop();
  }

  delay(175); // for optimal lcd screen view
}

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

  // print your board'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");
}

this is the serial output: serial output

it's clear that the first request the check the code goes thru but when you try a second time it comes out empty. We did some research and we found that in some cases the servo could affect the request in a bad way. Just not sure if that is correct and how to fix this

  • Comments are not for extended discussion; this conversation has been moved to chat. – VE7JRO Jan 13 at 19:27
1

Although I can not verify my code, I tried to modify your program. Probably it does not compile and you have to do some corrections. I changed it in a way I would expect it to work and hopefully I understood your intentions.

At least we could use this version as a base to elaborate a working one together.

#include <Key.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>

#define HIGH 0x1
#define LOW 0x0

char ssid[] = "ssid";
char pass[] = "password";
int keyIndex = 0;

int status = WL_IDLE_STATUS;
char server[] = "parcelbox.1819.caro.deweze.nxtmediatech.eu";

WiFiClient client;

LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;

/* Keypad*/
const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {3, 4, 5, 6};
byte colPins[COLS] = {9, 8, 7};

Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String value = "";

void setup() 
{
  lcd.begin();
  Serial.begin(9600);
  myservo.attach(A0);
  lcd.backlight();

  if (WiFi.status() == WL_NO_MODULE) 
  {
    Serial.println("Communication with WiFi module failed!");
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") { Serial.println("Please upgrade the firmware"); }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) 
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }

  Serial.println("Connected to wifi");
  printWifiStatus();
}

void loop() 
{
  lcd.clear();
  lcd.print("Geef bestelcode");
  char customKey = customKeypad.getKey();
  value = value + String(customKey);
  lcd.setCursor(0, 1);
  lcd.print(value);
  if (customKey == '#') 
  {
    String response_Status = "";
    Serial.println("\nStarting connection to server...");

    if ( client.connect( server, 80 ) ) 
    {
      value.remove(16);
      Serial.println(value);
      Serial.println("connected to server");

      // Make a HTTP request:
      client.println("GET /api/boxes/1444/code/" + value + " HTTP/1.1");
      client.println("Host: parcelbox.1819.caro.deweze.nxtmediatech.eu");
      client.println("Connection: close");
      client.println();
    }

    StaticJsonBuffer<500> jsonBuffer;

    if ( client.connected() ) 
    {
      String response = "";
      bool begin = false;

      // I do not understand why you have to use  || ! begin 
      // If the client has no content available, you can not read some
      // It doesn't matter if begin is true or false. 
      // Or am I missing something?
      while ( client.available() || ! begin ) 
      {
        char c = client.read();

        if ( c == '{' )  { begin = true;    }
        if ( begin )     { response += (c); }
        if ( c == '"}' ) { break;           }

        delay(1);
      }

      // Why do you parse the json text for a special 
      // attribute and then parse it again into a json object
      // where you can read this attribute?
      int start = response.indexOf("status\":\"") + 11;
      int end = start + 3;
      response_Status = response.substring(start, end);

// I commented this out because you do not use the root variable 
// in the following code
//      JsonObject& root = jsonBuffer.parseObject(response);
//      if (!root.success())
//      {
//        Serial.print("parseObject(");
//        Serial.print(response);
//        Serial.println(") failed");
//        break;
//      }

      Serial.println(response_Status);
      if (response_Status == "200") 
      {
        Serial.println("response state == 200");

        // First open the box

        // 0° means open
        myservo.write(0);
        delay(175);

        // Then inform the user how to close it

        lcd.clear();
        // I added the cursor positioning.
        // As far as I know clear does not reset the cursor.
        lcd.setCursor(0, 0);
        lcd.print("Box openend ,press");
        lcd.setCursor(0, 1);
        lcd.print(" * to close");

        // then check if the user wants to close it
        char key;
        do 
        {
          key = customKeypad.getKey();
          if (key == '*') 
          {
            // 90° means locked
            myservo.write(90);
            value = "";
          }
        }
        while( key != '*' );

      }
      else
      {
      // I replaced the else if by a simple if
      // as (response_Status != "200") is the else case
      // of (response_Status == "200")
        Serial.println("response state != 200");

        lcd.clear();
        // "set cursor" added
        lcd.setCursor(0, 0);
        lcd.print("wrong code or");
        lcd.setCursor(0, 1);
        lcd.print("already deliverd");

        delay(175);
        value = "";
      }
      // I change the while loop into an if block
      // so you don't need a break; here
    }

// Here you could enter a halt state without a reason.
// Even you won't expect to halt here.
//    if (!client.connected()) 
//    {
//      Serial.println();
//      Serial.println("disconnecting from server.");
//      client.stop();
//
//      // do nothing forevermore:
//      while (true);
//    }
  }

  if (customKey == '*') 
  {
    // 90° means locked
    myservo.write(90); 
    value = "";     
  }

  // for optimal lcd screen view
  delay(175);
}

void printWifiStatus() 
{
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

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

EDIT

This edit is related to the updated version of your program.

If you close the box with a key press you also stop the client with the last line.

Please remove the following line from the program.

// lock box and reset value
if (customKey == '*') 
{
  Serial.println("CLOSE BOX");
  myservo.write(90); 
  value = "";
  // remove this as you can not connect a stopped client
  // client.stop();
}
| improve this answer | |
  • I tried your code it builds perfectly, I could open the box and close it but the LCD keeps displaying " box open press * to close ". Your code is much more readable and structured than mine! I can learn alot from this – imkeVr Jan 6 at 15:37
  • Thank you for the reply and extending your question. By looking at my code I saw that I made a terrible mistake. Perhaps I've programed with JavaScript for too long now. In JS "" and '' is the same. In C/C++ Its something completely different. The line while( key != "*" ) is always true and the Loop is never left. I've corrected it in my answer. while( key != '*' ). Could you please change it in your program too and try that version. – Peter Paul Kiefer Jan 7 at 17:12
  • @imkeVr I just realize that, you use a complete different code now. And it would be better if I had looked into the output earlier.;-) I don't believe the the Server disturbed the client communication. I think you are simply not able to open a connection with a client that is stopped. I'll update the answer once more. ;-) – Peter Paul Kiefer Jan 7 at 17:36
  • I found the a way that works for my program. I scratched the wifi setup in the void setup en relocated it to its own function outside any loop or setup. This way I could call for the function and reestablish a connection inside my void loop itself. It now works like it should. Thank you for all the help I learned alot by doing this! – imkeVr Jan 7 at 20:37
  • @imkeVr It was a pleasure to work with you. I first wanted to propose what you have done now by yourself, but I didn't want to make my answer too complicated. And now I see you managed it, just with the few information i gave you. This is one of the moments, that keeps me answering questions on SE. And I could imagine that, if you keep on learning quickly, our conversation in the future would be with changed rolls. I ask and you answer. I wish you and your partner success with the project. – Peter Paul Kiefer Jan 8 at 10:32
1
/* hardware libraries*/
#include <Key.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

/*Wifi and request libraries*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>

#define HIGH 0x1
#define LOW 0x0

/* wifi credentials*/
char ssid[] = "sidd";        // your network SSID (name)
char pass[] = "pass";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;

int status = WL_IDLE_STATUS;
char server[] = "parcelbox.1819.caro.deweze.nxtmediatech.eu";
// 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;

LiquidCrystal_I2C lcd(0x27, 16, 2); // lcd object
Servo myservo; //servo object
boolean boxOpenend = false ;

/* Keypad*/
const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {3, 4, 5, 6};
byte colPins[COLS] = {9, 8, 7};

Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String value = "";

void setup() {
  lcd.begin(); //start lcd
  Serial.begin(9600);
  myservo.attach(A0); // attach servo to pin
  lcd.backlight(); // start screen light

  /* connect to wifi */
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  /*
    // check for the WiFi module:
    if (WiFi.status() == WL_NO_MODULE) {
      Serial.println("Communication with WiFi module failed!");
      // don't continue
      while (true);
    }

    String fv = WiFi.firmwareVersion();
    if (fv < "1.0.0") {
      Serial.println("Please upgrade the firmware");
    }

    // 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");*/
  createConnection();
}

void loop() {
  lcd.clear();
  lcd.print("Bestelcode / pin");

  char customKey = customKeypad.getKey();   // get the key pressed
  value = value + String(customKey);

  lcd.setCursor(0, 1);
  lcd.print(value);


  if (customKey == '#')
  {
    createConnection();
    Serial.println("PRESSED #");
    String response_Status = "";
    Serial.println("\nStarting connection to server...");

    Serial.println("RESULT OF CLIENT.CONNECT()");
    Serial.println(client.connect(server, 80));

    // if you get a connection, report back via serial:
    if (client.connect(server, 80) == 1)
    { 
      int valueLen = value.length();
      if(valueLen == 5){
      Serial.println("IK BEN een pin ");
      value.remove(4);
      Serial.println(value);
      Serial.println("connected to server");

      // Make a HTTP request:
      client.println("GET /api/boxes/2025/pin/" + value + " HTTP/1.1");//1444
      client.println("Host: parcelbox.1819.caro.deweze.nxtmediatech.eu");
      client.println("Connection: close");
      client.println();
      }
      if(valueLen ==17){
      Serial.println("IK BEN een bestelcode");
      value.remove(16);
      Serial.println(value);
      Serial.println("connected to server");

      // Make a HTTP request:
      client.println("GET /api/boxes/2025/code/" + value + " HTTP/1.1");//1444
      client.println("Host: parcelbox.1819.caro.deweze.nxtmediatech.eu");
      client.println("Connection: close");
      client.println();

        }
        else{

          Serial.println("invalid amount of numbers code");
          /*lcd.clear();
          lcd.print("ongeldige code");*/

          }
    }
    else
    {
      Serial.println("client.connect() not successfull");
    }

    // if there are incoming bytes available
    // from the server, read them and print them:
    StaticJsonBuffer<500> jsonBuffer;

    if (client.connected())
    {
      Serial.println("CLIENT CONNECTED");

      String response = "";
      bool begin = false;

      while (client.available() || !begin) {
        char c = client.read();

        if (c == '{') {
          begin = true;
        }

        if (begin) {
          response += (c);
        }

        if (c == '"}') {
          break;
        }

        delay(1);
      }

      int start = response.indexOf("status\":\"") + 11;
      int end = start + 3;

      response_Status = response.substring(start, end);
      Serial.println(response_Status);

      // response successfull
      if (response_Status == "200")
      {
        Serial.println("RESPONSE 200");
        Serial.println("response state == 200");

        // First open the box
        // 0° means open
        Serial.println("OPEN BOX");
        myservo.write(0);
        delay(175);

        lcd.setCursor(0, 0);
        lcd.print("                ");
        lcd.setCursor(0, 0);
        lcd.print("Box open ,druk");
        lcd.setCursor(0, 1);
        lcd.print("                ");
        lcd.setCursor(0, 1);
        lcd.print("* om te sluiten");
        delay(5000);
      }
      else
      {
        Serial.println("RESPONSE NOT 200");
        Serial.println("not 200");

        lcd.clear();
        lcd.print("Foute code of");
        lcd.setCursor(0, 1);
        lcd.print("al geleverd");

        delay(2500);
      }
    }

    // reset the value
    value = "";
  }

  // lock box and reset value
  if (customKey == '*')
  {
    Serial.println("CLOSE BOX");
    myservo.write(90);
    value = "";
    client.stop();
  }

  delay(175); // for optimal lcd screen view
}
void createConnection() {

  // This new line is required to disconnect the previous connection otherwise
  // it will try to use the old connection and never reconnect if the connection fails
  status = WiFi.disconnect();

  // 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(10);
  }
  // you're connected now, so print out the status:
  printWifiStatus();

}

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

  // print your board'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");
}

I had to rewrite my wifi connection into a function outside my setup. Because the program only established a connection in the beginning and closed it after my first request. By giving it it's own function I could call it inside my loop whenever the user would enter a code the program could reestablish a connection. My program now runs how I need it to run.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.