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