I Need example for WIFI+ TIMER INTERRUPT
Posted: Fri Jun 30, 2017 11:25 pm
I Need example for WIFI+ TIMER INTERRUPT. Any one can help me?
-->
Open Community Forum for ESP8266, Come share Arduino and IoT (Internet of Things)
https://www.esp8266.com/
void loop() {
if (WiFi.status() != WL_CONNECTED) { // WiFi not connected, try to fix it:
WiFi.mode(WIFI_STA); // start setup WiFi
WiFi.config(ip_sensor, ip_gateway, ip_subnet); // set fix WiFi config
delay(10);
WiFi.begin(WiFi_SSID, WiFi_PW);
for ( int i = 0; i < 300; i++) { // try to connect to WiFi for max 30s
if (WiFi.status() == WL_CONNECTED) {break;}
delay(100);
}
WiFiRestart = true;
}
if (WiFi.status() != WL_CONNECTED) { // if WiFi still failed, then
delay(5000); // wait 5 sec and reboot
ESP.restart();
}
if (!mqttClient.connected() || WiFiRestart) { // start MQTT connection if not connected
mqttClient.set_server(MQTT_Broker, 8883); // config MQTT Server
mqttClient.connect(MQTT::Connect(Hostname).set_auth(MQTT_User, MQTT_PW));
mqttClient.loop();
delay(1000);
if (!mqttClient.connected()) { // if MQTT still not connected, then
delay(5000); // wait 5 sec and reboot
ESP.restart();
} else { // MQTT connection succeeded, therefore
mqttClient.set_callback(command_callback); // setup callback routine for commands from broker
mqttClient.subscribe(cmd_topic); // subscribe to command topic
mqttClient.publish(input_topic, Version); // publish SW version as a (re-)connect info
mqttClient.loop();
delay(100); // and get the messages out of the door before continuing
WiFiRestart = false;
}
} // at this point WiFi and MQTT are up and running
//------
// more code here for measurements and MQTT communication
// deleted since I think it is not relevant for the discussion here
//------
ArduinoOTA.handle(); // start section with regular household functions
mqttClient.loop();
yield(); // end
}