Created
May 13, 2016 05:39
-
-
Save prasertsakd/d2915b3a6270bb1f1f1ca84d8623e639 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* This sketch shows the WiFi event usage | |
* | |
* In this example you can receive and process below events. | |
* Refer to ESP8266WiFiType.h | |
* | |
typedef enum { | |
WIFI_EVENT_STAMODE_CONNECTED = 0, | |
WIFI_EVENT_STAMODE_DISCONNECTED, | |
WIFI_EVENT_STAMODE_AUTHMODE_CHANGE, | |
WIFI_EVENT_STAMODE_GOT_IP, | |
WIFI_EVENT_STAMODE_DHCP_TIMEOUT, | |
WIFI_EVENT_SOFTAPMODE_STACONNECTED, | |
WIFI_EVENT_SOFTAPMODE_STADISCONNECTED, | |
WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED, | |
WIFI_EVENT_MAX | |
} WiFiEvent_t; | |
* | |
* hardcopyworld.com | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <ArduinoOTA.h> | |
#include <ESP8266mDNS.h> | |
// | |
const char *ssid = "fatfamily3"; | |
const char *password = "212224236"; | |
//IPAddress ip = {192,168,1,255}; | |
//// | |
//IPAddress local_ip = {192,168,1,50}; | |
//IPAddress gateway = {192,168,1,100}; | |
//IPAddress subnet = {255,255,255,0}; | |
int ledState = LOW; | |
//const int ledPin = 16; // the number of the LED pin | |
unsigned long previousMillis = 0; | |
unsigned int interval; | |
boolean connectWifi; | |
void WiFiEvent(WiFiEvent_t event); | |
void ota_init(); | |
void setup() { | |
Serial.begin(115200); | |
// Serial.println("hello"); | |
// delete old config | |
WiFi.disconnect(true); | |
WiFi.mode(WIFI_STA); | |
pinMode(LED_BUILTIN, OUTPUT); | |
delay(500); | |
WiFi.onEvent(WiFiEvent); | |
WiFi.begin(ssid, password); | |
// WiFi.config(local_ip, gateway, subnet); | |
Serial.println(); | |
Serial.println(); | |
Serial.println("Wait for WiFi... "); | |
ota_init(); | |
} | |
void loop() { | |
ArduinoOTA.handle(); | |
unsigned long currentMillis = millis(); | |
if(currentMillis - previousMillis >= interval) { | |
previousMillis = currentMillis; | |
if (ledState == LOW) | |
ledState = HIGH; // Note that this switches the LED *off* | |
else | |
ledState = LOW; // Note that this switches the LED *on* | |
digitalWrite(LED_BUILTIN, ledState); | |
} | |
} | |
void ota_init() { | |
// Port defaults to 8266 | |
// ArduinoOTA.setPort(8266); | |
// Hostname defaults to esp8266-[ChipID] | |
//ArduinoOTA.setHostname("my-esp8266"); | |
// No authentication by default | |
// ArduinoOTA.setPassword((const char *)"123"); | |
ArduinoOTA.onStart([]() { | |
Serial.println("Start"); | |
}); | |
ArduinoOTA.onEnd([]() { | |
Serial.println("\nEnd"); | |
}); | |
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | |
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); | |
}); | |
ArduinoOTA.onError([](ota_error_t error) { | |
Serial.printf("Error[%u]: ", error); | |
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); | |
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); | |
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); | |
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); | |
else if (error == OTA_END_ERROR) Serial.println("End Failed"); | |
}); | |
ArduinoOTA.begin(); | |
} | |
void WiFiEvent(WiFiEvent_t event) { | |
Serial.printf("[WiFi-event] event: %d\n", event); | |
switch(event) { | |
case WIFI_EVENT_STAMODE_CONNECTED: | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
break; | |
case WIFI_EVENT_STAMODE_GOT_IP: | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
interval = 500; | |
connectWifi = true; | |
break; | |
case WIFI_EVENT_STAMODE_DISCONNECTED: | |
Serial.println("WiFi lost connection"); | |
interval = 50; | |
connectWifi = false; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment