| #include <NeoPixelBus.h> |
| #define pixelCount 1 |
| #define colorSaturation 128 |
| NeoPixelBus strip = NeoPixelBus(pixelCount, 5); |
|
|
| RgbColor Sumcolor = RgbColor(255); //กำหนดค่าสี RGB |
| RgbColor Low = RgbColor(0, 0, 0); |
|
|
| // Libraries |
| #include <ESP8266WiFi.h> |
| #include <PubSubClient.h> |
| #include <WiFiHelper.h> |
| |
| // Credentials |
| String deviceId = "ID"; // * set your device id (will be the MQTT client username) |
| String deviceSecret = "SECRET"; // * set your device secret (will be the MQTT client password) |
| String clientId = "TYPE"; // * set a random string (max 23 chars, will be the MQTT client id) |
|
|
| // WiFi name & password |
| const char* ssid = "xxxx"; |
| const char* pass = "xxxxxx"; |
| |
| // Sketch logic |
| // See more: http://lelylan.github.io/types-dashboard-ng/#/ |
| char* payloadOn = "{\"properties\":[{\"id\":\"518be88300045e0610000008\",\"value\":\"on\"}]}"; |
| char* payloadOff = "{\"properties\":[{\"id\":\"518be88300045e0610000008\",\"value\":\"off\"}]}"; |
|
|
| // Topics |
| String outTopic = "devices/" + deviceId + "/set"; // * MQTT channel where physical updates are published |
| String inTopic = "devices/" + deviceId + "/get"; // * MQTT channel where lelylan updates are received |
|
|
| // MQTT server address |
| IPAddress server( You IP Server); |
| |
| // WiFi Client |
| WiFiHelper *wifi; |
| |
| // MQTT |
| PubSubClient client(server); |
|
|
| // Pins |
| int outPin = 5; // Gerora |
| int inPin = 4; // PIR |
| |
| // Logic |
| int state = HIGH; // current state of the output pin |
| int reading; // current reading from the input pin |
| int previous = LOW; // previous reading from the input pin |
| long time = 0; // the last time the output pin was toggled |
| long debounce = 200; // the debounce time, increase if the output flickers |
|
|
| int currentSwitch = HIGH; |
| int lastSwitch = HIGH; |
|
|
| // Callback |
| void callback(const MQTT::Publish& pub) { |
| |
| // Copy the payload content into a char* |
| char* json; |
| json = (char*) malloc(pub.payload_len() + 1); |
| memcpy(json, pub.payload(), pub.payload_len()); |
| json[pub.payload_len()] = '\0'; |
| |
| // Update the physical status and confirm the executed update |
| // boolean currentSwitch; |
| if (String(payloadOn) == String(json)) { |
| Serial.println("[LELYLAN] Led turned on"); |
| lelylanPublish("on"); |
| currentSwitch = HIGH; |
| } else { |
| Serial.println("[LELYLAN] Led turned off"); |
| lelylanPublish("off"); |
| currentSwitch = LOW; |
| } |
| |
| digitalWrite(outPin, currentSwitch); |
| free(json); |
| } |
|
|
| void init_wifi() |
| { |
| wifi = new WiFiHelper(ssid, pass); |
|
|
| wifi->on_connecting([](const char* message) |
| { |
| Serial.println (message); |
| }); |
|
|
| wifi->on_connected([](const char* message) |
| { |
| Serial.println (message); |
| Serial.println("IP address: "); |
| Serial.println(WiFi.localIP()); |
| }); |
|
|
| wifi->on_disconnected([](const char* message) |
| { |
| Serial.println (message); |
| }); |
| |
| wifi->begin(); |
| } |
| |
| void setup() { |
| |
| pinMode(4,INPUT); |
| Serial.begin(115200); |
| delay(50); |
| strip.Begin(); |
| strip.Show(); |
|
|
| |
| init_wifi(); |
| // Set callback |
| client.set_callback(callback); |
| |
| // MQTT server connection |
| lelylanConnection(); |
| pinMode(outPin, OUTPUT); // Gerara pin setup |
| pinMode(inPin, INPUT_PULLUP); |
| } |
| |
| void loop() { |
| // Keep connection open |
| lelylanConnection(); |
|
|
| if( digitalRead(inPin) == HIGH ) { |
| currentSwitch = HIGH; |
| Serial.println("ooooooooooooooooooooooooooooooooooo"); |
| strip.SetPixelColor(0,Sumcolor); |
| strip.Show(); |
| lelylanPublish("on"); //ให้Gerora no |
| |
| } |
| else { |
| currentSwitch = LOW; |
| Serial.println("ooooooooooooo"); |
| strip.SetPixelColor(0,Low); |
| strip.Show(); |
| lelylanPublish("off"); //ให้Gerora off |
| } |
| |
| } |
| |
| /* MQTT server connection */ |
| void lelylanConnection() { |
| // add reconnection logics |
| if (!client.connected()) { |
| // connection to MQTT server |
| if (client.connect(MQTT::Connect(clientId).set_auth(deviceId, deviceSecret))) { |
| Serial.println("[PHYSICAL] Successfully connected with MQTT"); |
| lelylanSubscribe(); // topic subscription |
| } |
| } |
| client.loop(); |
| } |
| |
| /* MQTT publish */ |
| void lelylanPublish(char* value) { |
| if (value == "on") |
| client.publish(outTopic, payloadOn); // light on |
| else |
| client.publish(outTopic, payloadOff); // light off |
| } |
| |
| /* MQTT subscribe */ |
| void lelylanSubscribe() { |
| client.subscribe(inTopic); |
| } |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น