Chat freely about anything...

User avatar
By abzaatari
#79609 Hi, in my mqtt mosquito project I want to connect my raspberry pi broker and esp8266s to the router with no access to internet (local server only). The code and the connection are OK, but every 1 minute the ESP reboot automatically maybe because no web connection and sometime it need hard reset. So how can I disable WIFI CHECK STATUS which is concerning if the ESP is connected to internet or extend the interval from 1 minute to infinity?


thanx
Last edited by abzaatari on Sun Dec 23, 2018 3:41 pm, edited 2 times in total.
User avatar
By QuickFix
#79648
abzaatari wrote:reboot automatically maybe because

First you have to rule out the "Maybe"; computers & controlers don't work with maybe's, only with definitive yes & no (true & false, 1 & 0, ...). :idea:

If your ESP resets, there's something wrong with your code.
We can't tell you what if you don't share it here though.
User avatar
By abzaatari
#79680 Hi, this is my simple code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "zaatari";
const char* password = "EC6D0D7169";
const char* mqtt_server = "192.168.1.91";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

const int curstate =14;
int inplaststate =0;
int outs =0;
const int outpt =2;
int inpcurstate=0;

void setup_wifi() {

delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

randomSeed(micros());

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();

// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}

}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("outTopic", "hello world");
// ... and resubscribe
// client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
pinMode(curstate,INPUT_PULLUP);
pinMode(outpt,OUTPUT);
digitalWrite(outpt,HIGH);

setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}

void loop() {

if (!client.connected()) {
reconnect();
}
client.loop();



inpcurstate = digitalRead(curstate);
delay(50);
if((inpcurstate == LOW)&&(inpcurstate != inplaststate))

{
Serial.println("HI");
outs = digitalRead(outpt);
if(outs == HIGH)
{
digitalWrite(outpt,LOW);
Serial.println("ON");
client.publish("hmia", "1on");
}
else
{
digitalWrite(outpt,HIGH);
Serial.println("OFF");
client.publish("hmia", "0of");
}

}
inplaststate = inpcurstate;

}

I appreciate your help, and it solved when I change IwIP Variant from v2 Lower Memory to v1.4 Higher Bandwidth in IDE Tool Menu.

Thanx a lot