-->
Page 1 of 1

WebClientRepeating stoped after 1 day run

PostPosted: Wed Feb 26, 2020 9:29 am
by mohammadss
Hi . i'm trying to create smarthouse using this codes .
what my system does :
sending get request every 1 sec to server and checks for new messages . if any new messages exist , system will decide to turn on or off relay .
my system hardware is an esp-1 , arduino uno and one relay . i connected them with out any resistors or capacitors .
i'm trying to do something like https://github.com/bportaluri/WiFiEsp/blob/master/examples/WebClientRepeating/WebClientRepeating.ino.

my codes :
Code: Select all#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[] = "mazimi.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;
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 80;
StaticJsonDocument<85> doc;
DeserializationError error ;

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;
  }
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/?token=12345678&client=12 HTTP/1.1"));
    client.println(F("Host: mazimi.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 ?

thanks

Re: WebClientRepeating stoped after 1 day run

PostPosted: Thu Feb 27, 2020 12:40 am
by rudy
Have you tried to use a longer connection period? Change from 1 second to 10 seconds.

Re: WebClientRepeating stoped after 1 day run

PostPosted: Thu Feb 27, 2020 6:27 am
by mohammadss
No , but i need period be very short and works each 1 or 2 secs .