Created
August 1, 2021 08:13
-
-
Save arihantdaga/68b78139ed403a6082c178898a3d6209 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const char *wifiSSID = "MY_WIFI"; | |
const char *wifiPassword = "MY_PASS"; | |
#define MQTT_HOST "mybroker" | |
#deinfe MQTT_PORT 1883 | |
#include <ESP8266WiFi.h> | |
WiFiClient wifiClient; | |
#include <AsyncMqttClient.h> | |
AsyncMqttClient mqttClient; | |
void onMqttConnect(bool sessionPresent) { | |
Serial.print(millis()); | |
Serial.println(" Connected to MQTT."); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
WiFi.config(IPAddress(192,168,1,11), IPAddress(192,168,1,1), IPAddress(255, 255, 255, 0)); | |
uint8_t bssid[] = {0xEE, 0x59, 0x65, 0x25, 0xC9, 0xE3}; | |
uint8_t channel = 1; | |
Serial.println(F("Initialising Wifi...")); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(wifiSSID, wifiPassword,channel, bssid,true); | |
WiFi.persistent(true); | |
WiFi.setAutoConnect(true); | |
WiFi.setAutoReconnect(true); | |
if (WiFi.waitForConnectResult() != WL_CONNECTED) | |
{ | |
Serial.println(F("Connection Failed!")); //################## IT is not called. | |
// Change settings now. | |
WiFi.disconnect(); | |
WiFi.begin(wifiSSID, wifiPassword); | |
WiFi.persistent(true); | |
WiFi.setAutoConnect(true); | |
WiFi.setAutoReconnect(true); | |
if (WiFi.waitForConnectResult() != WL_CONNECTED){ | |
Serial.println(F("Connection Failed Again!")); | |
} | |
} | |
Serial.println(millis()); | |
Serial.println(WiFi.localIP().toString()); | |
Serial.println(WiFi.macAddress()); | |
Serial.println(WiFi.gatewayIP().toString()); | |
Serial.println(WiFi.dnsIP().toString()); | |
Serial.println(WiFi.subnetMask().toString()); | |
Serial.println(WiFi.channel()); | |
Serial.println(WiFi.BSSIDstr()); | |
mqttClient.onConnect(onMqttConnect); | |
mqttClient.setServer(MQTT_HOST, MQTT_PORT); | |
mqttClient.setCredentials("MY_USER_ID", "MY_PASS"); | |
mqttClient.connect(); | |
} | |
void loop() | |
{ | |
delay(10000); | |
ESP.restart(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment