I have made sketch with arduino uno and esp8266 in which i send measures of HC-SR04 to a website. After a few days stop working.I have three same projects in three different parts with different internet connections. Τhe two of them have been operating normally for over five months and only one of them is causing the problem I have described above. Ιn this part the internet has some "cuts". Is it due to poor internet connection the above problem?
My code is:
#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(2,3);
//Wifi network SSID password
String ssid="ffffff";
String password ="*******";
boolean DEBUG=true;
int echoPin = 12;
int trigPin = 13;
long duration, distance;
int alarm;
String alarms;
String data;
String server = "www.example.com"; // Send data to this site
String uri = "/add2.php";
int error = 0;
int cancel = 0;
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}
}
boolean websiteWrite(long value1, int value2){
if (value2 == 0){
alarms = "OFF";
}else{
alarms = "ON";
}
data = "distance=" + String(value1) + "&alarm=" + alarms;
espSerial.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
if( espSerial.find("OK")) {
Serial.println("TCP connection ready"); cancel = 0;
}
else{
cancel = cancel + 1; Serial.println("cancel="); Serial.println(cancel);
}
delay(1000);
String postRequest =
"POST " + uri + " HTTP/1.0\r\n" +
"Host: " + server + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n" + data;
String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
espSerial.print(sendCmd);
espSerial.println(postRequest.length() );
delay(500);
if(espSerial.find(">")) { Serial.println("Sending.."); espSerial.print(postRequest);
if( espSerial.find("SEND OK")) { Serial.println("Packet sent");
while (espSerial.available()) {
String tmpResp = espSerial.readString();
Serial.println(tmpResp);
}
// close the connection
espSerial.println("AT+CIPCLOSE");
}
error = 0;
}else{
error = error + 1;
Serial.println("error=");
Serial.println(error);
}
if(cancel >=15 || error >= 15){software_Reset() ;}
}
void setup() {
delay(150000);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
DEBUG=true;
Serial.begin(9600);
espSerial.begin(9600);
//espSerial.println("AT+RST");
//showResponse(1000);
//espSerial.println("AT+UART_DEF=9600,8,1,0,0");
//showResponse(1000);
espSerial.println("AT+CWMODE=1");
showResponse(1000);
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");
showResponse(5000);
if (DEBUG) Serial.println("Setup completed");
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
if (distance<=8){
alarm=1;
}else{
alarm=0;
}
if (isnan(distance)) {
if (DEBUG) Serial.println("Failed of calculation the distance");
}
else {
if (DEBUG) Serial.println("Distance="+String(distance)+" cm");
websiteWrite(distance,alarm); // Write values to website
}
//Needs 60 sec delay between updates
delay(60000);
}
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile (" jmp 0");
}