-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathOrginal code.ino
158 lines (124 loc) Β· 3.08 KB
/
Orginal code.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
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define D1 5 // GPIO5
#define D2 4 // GPIO5
#define D3 0 // GPIO5
#define D4 2 // GPIO5
Servo servo;
char auth[] = "aba9f7cdbbe3457ba06f40adf5ca757a";
char ssid[] = "friends hub";
char pass[] = "agetakaden";
String Request;
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "CreativeIoT"
#define AIO_KEY "84e007aaa0cf4e9790a637a2f23c6afc"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Subscribe homefeed = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/homefeed");
boolean MQTT_connect();
boolean MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return true;
} uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) {
mqtt.disconnect();
delay(2000);
retries--;
if (retries == 0) {
return false;
}
} return true;
}
void setup()
{
Serial.begin(9600);
Request = "";
Blynk.begin(auth, ssid, pass);
servo.attach(15); // NodeMCU D8 pin
WiFi.disconnect();
delay(3000);
Serial.println("Start IoT");
WiFi.begin("friends hub", "agetakaden");
while ((!(WiFi.status() == WL_CONNECTED))) {
delay(300);
Serial.print(".");
}
Serial.println("Wifi Connected");
Serial.println("Your IP is");
Serial.println((WiFi.localIP().toString()));
mqtt.subscribe(&homefeed);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
}
void loop()
{
Blynk.run();
if (MQTT_connect()) {
Adafruit_MQTT_Subscribe *subscription_name;
while ((subscription_name = mqtt.readSubscription(5000))) {
if (subscription_name == &homefeed) {
Request = ((char *)homefeed.lastread);
if (Request == "d1on") {
digitalWrite(D1, LOW);
Serial.println("Red light turn ON");
}
if (Request == "d1off") {
digitalWrite(D1, HIGH);
Serial.println("Red light turn OFF");
}
if (Request == "d2on") {
digitalWrite(D2, LOW);
Serial.println("Blue light turn ON");
}
if (Request == "d2off") {
digitalWrite(D2, HIGH);
Serial.println("Fan turn OFF");
}
if (Request == "d3on") {
digitalWrite(D3, LOW);
Serial.println("Blue light turn ON");
}
if (Request == "d3off") {
digitalWrite(D3, HIGH);
Serial.println("Blue light turn OFF");
}
if (Request == "d4on") {
digitalWrite(D4, LOW);
Serial.println("LED ON");
}
if (Request == "d4off") {
digitalWrite(D4, HIGH);
Serial.println("LED OFF");
}
}
}
}
}
BLYNK_WRITE(V1)
{
servo.write(45);
}
BLYNK_WRITE(V2)
{
servo.write(90);
}
BLYNK_WRITE(V3)
{
servo.write(135);
}
BLYNK_WRITE(V4)
{
servo.write(180);
}