I'm having a problem with my project. the Arduino hangs after sending several post request. the codes have a timer that every timer it will send a post request. After sending to the server, the server will send a reply and I will parse it and get the data. I'm using Arduino UNO wifi rev 2. so I'm using wifi to connect to the server.
there are two cases of the problem
case #1:
after some time, the Arduino can't connect to the host. after several seconds, the Arduino can connect now.
Picture for this case: "Capture.PNG"
case #2:
after some time, it will hang on what server will reply. it contains nothing and hangs in there forever.
Picture for this case: "Capture1.PNG"
these are ALL the codes:
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>
int relayPin1 = 2; //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3; //This is the Arduino Pin that will control Relay #2
int pressurePin = A0;
int force;
int ledPin = 5;
int num_steps = 0;
int fsr = false;
int longfsr = false;
long buttonTimer = 0;
long longPressTime = 300;
int timer;
//char ssid[] = "***"; // your network SSID (name)
//char key[] = "***"; // your network key
char ssid[] = "***"; // your network SSID (name)
char key[] = "****"; // your network key
//const char* host = "***"; // server ip add
const char* host = "***"; // server ip add
int status = WL_IDLE_STATUS; // the Wifi radio's status
WiFiClient client; // initialize the client library
int portnum = ***; // portn number
String line = "";
String reply_content = "";
String Battery_using = "";
StaticJsonBuffer<2000> jsonBuffer;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
// attempt to connect to Wifi network:
Serial.print("[Attempting to connect to WEP network, SSID: ");
Serial.print(ssid);
Serial.println("]");
while ( status != WL_CONNECTED) {
Serial.println("[Connecting...]");
status = WiFi.begin(ssid, key);
// wait 5 seconds for connection:
delay(5000);
}
// once you are connected :
Serial.println("[Connection Succesful]");
Serial.println();
}
void loop() {
timer++;
digitalWrite(ledPin, LOW);
force = analogRead(pressurePin);
if(force > 600 ){
if(fsr == false ){
fsr = true;
buttonTimer = millis();
}
if ((millis() - buttonTimer > longPressTime) && (longfsr == false)) {
//short pressed
longfsr = true;
num_steps += 1;
}
} else {
if(fsr == true){
if(longfsr == true){
longfsr = false;
} else {
//long pressed
}
}
}
delay(100);
if(timer==10){
update_data(num_steps);
Get_Data();
timer=0;
}
if(Battery_using == "Battery_1"){
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
}else if (Battery_using == "Battery_2"){
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
}
}
//===================== Update Footsteps ======================
boolean update_data(int steps){
String da = "data=";
String ta = String(steps);
String data = da+ta;
Serial.print("[Attempting to connect to ");
Serial.print(host);
Serial.println("]");
if (client.connect(host, portnum))
{
Serial.println("[Connected]");
Serial.println("[Sending a request]");
client.println("POST /Piezo_DLSL/function.php?action=update_footsteps HTTP/1.1");
client.println("Host: "+ String(host));
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.println(data);
Serial.println("[Request sent]");
Serial.println("[Response:]");
while (client.connected() || client.available())
{
if (client.available())
{
char line = client.read();
reply_content = reply_content + line;
}
}
Serial.println(reply_content);
Serial.println();
Serial.println("[End Of Response]");
//client.flush();
client.stop();
Serial.println("\n[Disconnected]");
}else{
Serial.println("[Connection failed!]");
//client.flush();
client.stop();
}
}
//===================== Get Battery Data ======================
boolean Get_Data(){
digitalWrite(ledPin, HIGH);
String data = "data=Battery";
reply_content = "";
Serial.print("[Attempting to connect to ");
Serial.print(host);
Serial.println("]");
if (client.connect(host, portnum))
{
Serial.println("[Connected]");
Serial.println("[Sending a request]");
client.println("POST /Piezo_DLSL/function.php?action=Bat_update HTTP/1.1");
client.println("Host: "+ String(host));
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.println(data);
Serial.println("[Request sent]");
Serial.println();
Serial.println("[Response:]");
while (client.connected() || client.available())
{
if (client.available())
{
char line = client.read();
reply_content = reply_content + line;
if(reply_content.endsWith("Content-Type: application/json\r\n\r\n")){
reply_content = "";
}
}
}
Serial.println(reply_content);
Serial.println("[End Of Response]");
Serial.println();
JsonObject& root = jsonBuffer.parseObject(reply_content);
if (!root.success())
{
Serial.print("[parseObject]");
Serial.print("[ ");
Serial.print(reply_content);
Serial.println(" ] Failed");
jsonBuffer.clear();
//client.flush();
client.stop();
}else{
String json_value = root["status"];
Battery_using = json_value;
Serial.println("[Received Value]");
Serial.println(json_value);
jsonBuffer.clear();
}
// while(client.connected()) {
//client.flush();
client.stop();
// delay(1000);
// }
Serial.println("\n[Disconnected]");
}else{
Serial.println("[Connection failed!]");
//client.flush();
client.stop();
}
}
Thank you in advance