-
Notifications
You must be signed in to change notification settings - Fork 0
/
tykin_ohjaus2.ino
159 lines (133 loc) · 3.62 KB
/
tykin_ohjaus2.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
/*Ohjelma Excelvan LED-videoprojektorin etäkäynnistystä/sammutusta varten
*/
const int buttonPin = D5; // the number of the pushbutton pin (tykin poweri-nappi)
const int ledPin = D6; // tykin vihreä ledi (1.85V)
int lastLedState = LOW; //debouncea varten
int ledState = LOW; // variable for reading the LED status
int powerUP = 0; //tykin käynnistystila
int powerDOWN = 0; //tykin sammutustila
int powerON = 0; //power-tila
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 1000;
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "settings.h"
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback);
pinMode(ledPin, INPUT);
pinMode(buttonPin, INPUT);
}
void setup_wifi() {
delay (10);
Serial.println();
Serial.print("Connecting ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP adress:");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
String message;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
message = message + (char)payload[i];
}
Serial.println(message);
if ((message == "on") and (powerON == 0)) {
powerUP = 1;
}
if ((message == "off") and (powerON == 1)) {
powerDOWN = 1;
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASSWORD)) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe(MQTT_TOPIC);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
int reading = digitalRead(ledPin); //power-tila luetaan siniseltä lediltä
if (reading != lastLedState) { //debounce välkkyvää lediä varten
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != ledState) {
ledState = reading;
if (ledState == HIGH) {
powerON = 1;
}
else powerON = 0;
}
}
lastLedState = reading;
//tykin käynnistys
if ((powerON == 0) and (powerUP == 1)) {
pinMode (buttonPin, OUTPUT);
digitalWrite (buttonPin, LOW);
delay(1000);
digitalWrite (buttonPin, HIGH);
delay(500);
pinMode (buttonPin, INPUT);
powerUP = 0;
delay(4000);
}
//tykin sammutus
if ((powerON == 1) and (powerDOWN == 1)) {
pinMode (buttonPin, OUTPUT);
digitalWrite (buttonPin, LOW);
delay(1000);
digitalWrite (buttonPin, HIGH);
pinMode (buttonPin, INPUT);
powerDOWN = 0;
delay(4000);
powerON = 0;
}
/*
Serial.println("reading");
Serial.println(reading);
Serial.println("ledState");
Serial.println(ledState);
Serial.println("powerON");
Serial.println(powerON);
Serial.println("powerUP");
Serial.println(powerUP);
Serial.println("powerDOWN");
Serial.println(powerDOWN);
delay(10);
*/
}