-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp8266_wemosd1_kuchnia.ino
211 lines (172 loc) · 5.5 KB
/
esp8266_wemosd1_kuchnia.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//********* MARCIN PRZYBYSZ www.vsx.pl **************
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ArduinoJson.h>
#include <Bounce2.h>
#define domoticz_in "domoticz/in"
#define LED_BUILTIN 2
#define BUTTON_PIN D2
// Connect to the WiFi
const char* ssid = "";
const char* password = "";
const char* mqtt_server = "";
WiFiClient espClient;
PubSubClient client(espClient);
IPAddress ip(192, 168, X, X); // where xx is the desired IP Address
IPAddress gateway(192, 168, X, X); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 8000;
const byte ledPin = 0; // Pin with LED on Adafruit Huzzah
Bounce debouncer = Bounce();
void callback(char* topic, byte* payload, unsigned int length) {
StaticJsonBuffer<200> jsonBuffer;
char receivedChar[length];
//Serial.print("Message arrived [");
//Serial.print(topic);
//Serial.print("] ");
for (int i=0;i<length;i++) {
receivedChar[i] = (char)payload[i];
//Serial.print(receivedChar);
}
JsonObject& root = jsonBuffer.parseObject(receivedChar);
int idx = root["idx"];
int nvalue = root["nvalue"];
String svalue = root["svalue1"];
obsluga(idx,nvalue,svalue);
}
void obsluga(int idx, int nvalue, String svalue)
{
Serial.println("IDX: " + String(idx) + " nval.: ");
//Serial.print(nvalue + " svalue: ");
// Serial.println(svalue);
if (idx == 85) {
Serial.println("IDX 85 - sprawdzam nvalue: " + String(nvalue) + " / " + svalue);
if (nvalue==1) { digitalWrite(D7,LOW); Serial.println("zalaczam d7"); }
if (nvalue==0) { digitalWrite(D7,HIGH); Serial.println("wylaczam d7"); }
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266 Client")) {
Serial.println("connected");
// ... and subscribe to topic
client.subscribe("domoticz/out");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// displays at startup the Sketch running in the Arduino
void display_Running_Sketch (void){
String the_path = __FILE__;
int slash_loc = the_path.lastIndexOf('/');
String the_cpp_name = the_path.substring(slash_loc+1);
int dot_loc = the_cpp_name.lastIndexOf('.');
String the_sketchname = the_cpp_name.substring(0, dot_loc);
Serial.print("\nArduino is running Sketch: ");
Serial.println(the_sketchname);
Serial.print("Compiled on: ");
Serial.print(__DATE__);
Serial.print(" at ");
Serial.print(__TIME__);
Serial.print("\n");
}
void podlaczWifi(){
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
Serial.println("Uruchomiono. / podlaczam sie do wifi /");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("WIFI OK!");
Serial.println("");
Serial.print("Podlaczono do: ");
Serial.println(ssid);
Serial.print("Adres IP: ");
Serial.println(WiFi.localIP());
}
void setup()
{
Serial.begin(9600);
display_Running_Sketch();
podlaczWifi();
// pin setup
pinMode(LED_BUILTIN,OUTPUT); //////////////////////////////////////////////////////// pin mode
pinMode(D3, INPUT_PULLUP);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(D7, OUTPUT);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
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();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(D7,LOW);
debouncer.attach(BUTTON_PIN,INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncer.interval(25); // Use a debounce interval of 25 milliseconds
}
void publikuj(int idx){
Serial.println("Publikuje toggle przelacznika");
String message;
//{"command": "switchlight", "idx": 2450, "switchcmd": "On" }
message+="{\"command\": \"switchlight\", \"idx\": ";
message+=String(idx);
message+=", \"switchcmd\" : \"Toggle\"}";
if ( client.publish(domoticz_in, (char*) message.c_str())) {
Serial.println("Opublikowano: " + message);
}
}
void loop()
{
debouncer.update();
ArduinoOTA.handle();
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
//publikuj(0,0);
digitalWrite(LED_BUILTIN,LOW);
delay(200);
digitalWrite(LED_BUILTIN,HIGH);
}
if ( debouncer.fell() ) publikuj(85);
delay(2);
}