-
Notifications
You must be signed in to change notification settings - Fork 0
/
fauxmoESP_Alexa01.ino
151 lines (115 loc) · 3.9 KB
/
fauxmoESP_Alexa01.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#define WIFI_SSID "entropy"
#define WIFI_PASS "xxxxxx"
#define SERIAL_BAUDRATE 115200
#define LED 2
//#include <Wire.h>
//#include "font.h"
////#define offset 0x00 // SDD1306 // offset=0 for SSD1306 controller
//#define offset 0x02 // SH1106 // offset=2 for SH1106 controller
//#define OLED_address 0x3c // all the OLED's I have seen have this address
fauxmoESP fauxmo;
// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------
void wifiSetup() {
// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);
// Connect
//Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
//WiFi.begin(WIFI_SSID, WIFI_PASS);
// Wait
int wifiwait = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
wifiwait++;
digitalWrite(BUILTIN_LED, HIGH);
digitalWrite(5, HIGH);
delay(100);
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(5, LOW);
delay(100);
if (wifiwait > 100) {
wifiwait = 0;
WiFi.disconnect();
delay(100);
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
}
}
Serial.println();
// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
void setup() {
// Wire.begin(0, 2); // Initialize I2C and OLED Display
// init_OLED(); //
// reset_display();
// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println();
Serial.println();
Serial.println("fauxmoESP_Alexa01 070617");
pinMode(14, OUTPUT);
digitalWrite(14, 1); // D5
delay(500);
digitalWrite(14, 0); // D5
delay(500);
pinMode(12, OUTPUT);
digitalWrite(12, 1); // D6
delay(500);
digitalWrite(12, 0); // D6
delay(500);
pinMode(13, OUTPUT);
digitalWrite(13, 1); // D7
delay(500);
digitalWrite(13, 0); // D7
delay(500);
pinMode(15, OUTPUT);
digitalWrite(15, 1); // D8
delay(500);
digitalWrite(15, 0); // D8
delay(500);
// Wifi
wifiSetup();
// LED
pinMode(14, OUTPUT);
digitalWrite(14, 1); // D5
delay(500);
digitalWrite(14, 0); // D5
delay(500);
// Fauxmo
fauxmo.addDevice("light one");
fauxmo.addDevice("light two");
fauxmo.addDevice("light three");
fauxmo.addDevice("light four");
// fauxmoESP 2.0.0 has changed the callback signature to add the device_id, this WARRANTY
// it's easier to match devices to action without having to compare strings.
fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
//digitalWrite(12, state);
if ((strcmp(device_name, "light one") == 0)) {
digitalWrite(14, state); // D5
Serial.printf("Light one: switched");
}
if ((strcmp(device_name, "light two") == 0)) digitalWrite(12, state); // D6
if ((strcmp(device_name, "light three") == 0)) digitalWrite(13, state); // D7
if ((strcmp(device_name, "light four") == 0)) digitalWrite(15, state); // D8
});
}
void loop() {
// Since fauxmoESP 2.0 the library uses the "compatibility" mode by
// default, this means that it uses WiFiUdp class instead of AsyncUDP.
// The later requires the Arduino Core for ESP8266 staging version
// whilst the former works fine with current stable 2.3.0 version.
// But, since it's not "async" anymore we have to manually poll for UDP
// packets
fauxmo.handle();
static unsigned long last = millis();
if (millis() - last > 5000) {
last = millis();
//Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
}
}