-
Notifications
You must be signed in to change notification settings - Fork 8
/
a20_wifi.ino
92 lines (77 loc) · 3.86 KB
/
a20_wifi.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*******************************************************************************
# #
# An alternative firmware for Xiaomi Desk Lamp (Yeelight) #
# #
# #
# Copyright (C) 2018 Tom Stöveken #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
********************************************************************************/
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
uint8_t buffer[10] = {1};
/******************************************************************************
Description.: prepare the WiFi settings, uses WiFi Manager Library
Input Value.: -
Return Value: -
******************************************************************************/
void setup_wifi() {
// switch off WiFi if it is disabled
if( configuration.disable_WiFi ) {
Log("Wifi is configured to be off");
WiFi.mode(WIFI_OFF);
return;
}
WiFiManager wifiManager;
wifiManager.autoConnect("Xiaomi Desk Lamp");
Log("WiFi connected, RSSI: "+ String(WiFi.RSSI()));
}
/******************************************************************************
Description.: no permanent task necessary
Input Value.: -
Return Value: -
******************************************************************************/
void loop_wifi() {
if( configuration.disable_WiFi ) return;
//because of having issues with an unresposive connection
//just meaningless data traffic will hopefully fix it
//a single UDP packet is send to the gateway IP
static unsigned long then = 0;
// if the WiFi config has been reset, there is no use to do that over and over again
// this variable assists in remembering if we already reset it
static bool reset_config = false;
// for using millis be aware of overflow every ~50 days
// but using substraction is "overflow-safe"
if( configuration.send_WLAN_keep_alive_packet && (millis()-then >= 30000) ) {
then = millis();
//UDP port 9 is supposed to discard packets or it will simply not return
//anything because no service is running at the gateway-IP,port9
Udp.beginPacket(WiFi.gatewayIP(), 9);
Udp.write(buffer, LENGTH_OF(buffer));
Udp.endPacket();
Log("WiFi kept busy, RSSI: "+ String(WiFi.RSSI()) +", Connected: "+ String(WiFi.isConnected()));
}
// delete all WiFi Manager settings, but only erase it once
if(state == RESET_CONFIGURATION && !reset_config) {
Log("Resetting WiFi Manager");
WiFiManager wifiManager;
wifiManager.resetSettings();
reset_config = true;
}
}