0

i'm trying to create a system that arduino and esp-1 send an GET request each 2 secs to a server and depend on server reply turns a LED on or off . system works good for 6-7 hours but stops after one day . actually i checked server logs after one day and i realized no request is sending to serve . my hardware : Arduino Uno ESP-1 LED or Relay

and my arduino codes : ( in know my host is not working now . i turned it off )

#include <ArduinoJson.h>
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <SoftwareSerial.h>




SoftwareSerial Serial1(10,11);


char ssid[] = "AZaz";            // your network SSID (name)
char pass[] = "Az8833394";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
int Relay = 13;


char server[] = "azimi.ir";
unsigned long lastConnectionTime = 0;         // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 1000L; // delay between updates, in milliseconds
WiFiEspClient client;


void setup() {
  // put your setup code here, to run once:

  pinMode(Relay, OUTPUT); 
  Serial.begin(115200);
  Serial1.begin(9600);
  WiFi.init(&Serial1);
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue
    while (true);
  }


  while ( status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }


  Serial.println(F("You're connected to the network"));
//  printWifiStatus();
}


void loop() {
  while (client.available()) {
  char c = client.read();
  char endOfHeaders[] = "\r\n\r\n";
  if (!client.find(endOfHeaders)) {
    Serial.println(F("Invalid response"));
    return;
  }
  const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 80;
  DynamicJsonDocument doc(capacity);
  DeserializationError error = deserializeJson(doc, client);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }
      int count = doc["counts"];
      if(count == 1){
        int relay = doc["message"];
        int cmd = doc["val"];
        digitalWrite(relay , cmd);
      }




  }
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();

  }
}


void httpRequest()
{

  Serial.println();

  // close any connection before send a new request
  // this will free the socket on the WiFi shield
  client.stop();


  // if there's a successful connection
  if (client.connect(server, 80)) {
    client.println(F("GET /check/ HTTP/1.1"));
    client.println(F("Host: azimi.ir"));
    client.println("Connection: close");
    client.println();




    // note the time that the connection was made
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection
    Serial.println(F("Connection failed"));
  }
}

now i have some questions :

1- why system stops working after more hours ? what is the problem ? my codes or my hardware ?

2- if problem is my hardware , what kind of wifi module or arduino you think will work ?

3- i want this project to make someone else's home . it will be a long term project so i want my system works for one or two years at least . is arduino suitable for this kind of projects ?! if its not , what hardware is ?

Your Answer

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

Browse other questions tagged or ask your own question.