The use of the ESP8266 in the world of IoT

User avatar
By mohammadss
#85859 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