diff --git a/src/gateway/.vscode/.gitignore b/src/gateway/.vscode/.gitignore new file mode 100644 index 0000000..31ea3ff --- /dev/null +++ b/src/gateway/.vscode/.gitignore @@ -0,0 +1 @@ +./bin/ \ No newline at end of file diff --git a/src/livb/.vscode/.gitignore b/src/livb/.vscode/.gitignore new file mode 100644 index 0000000..31ea3ff --- /dev/null +++ b/src/livb/.vscode/.gitignore @@ -0,0 +1 @@ +./bin/ \ No newline at end of file diff --git a/src/livb/buz.h b/src/livb/buz.h new file mode 100644 index 0000000..783bdd2 --- /dev/null +++ b/src/livb/buz.h @@ -0,0 +1,43 @@ +#ifndef __BUZ_H__ +#define __BUZ_H__ + +class Buz{ + public: + Buz(const int& pin){ + this->_pin = pin; + }; + ~Buz(){}; + + void ini(){ + pinMode(this->_pin, OUTPUT); + }; + + void loop(){ + if(millis() < this->_toT){ + analogWrite(this->_pin, (int)((float)((this->_toT - millis()) % 3000) * 255 / 3000)); + Serial.print(((int)((float)((this->_toT - millis()) % 3000) * 255 / 3000))); + }else{ + analogWrite(this->_pin, 0); + } + }; + + inline void on(const int& t = 12*60*1000){ + this->_toT = millis() + t; + } + + inline void off(){ + this->_toT = 0; + } + + inline void once(const int& times = 4){ + this->_toT = millis() + 3000 * times; + } + + private: + static unsigned int _toT; + unsigned int _pin; +}; + +unsigned int Buz::_toT = 0; + +#endif //__BUZ_H__ diff --git a/src/livb/led.h b/src/livb/led.h new file mode 100644 index 0000000..79170b9 --- /dev/null +++ b/src/livb/led.h @@ -0,0 +1,77 @@ +#ifndef __LED_H__ +#define __LED_H__ + +class LED { + public: + + LED(const int& pin){ + this->_pin = pin; + this->_mode = "null"; + }; + ~LED(){}; + + inline void on(){ + digitalWrite(this->_pin, HIGH); + this->_mode = "null"; + } + + inline void off(){ + digitalWrite(this->_pin, LOW); + this->_mode = "null"; + } + + inline const bool toggle(){ + digitalWrite(this->_pin, !digitalRead(this->_pin)); + this->_mode = "null"; + return digitalRead(this->_pin); + } + + inline void ini() const{ + pinMode(this->_pin, OUTPUT); + } + + inline void loop() const{ + if(this->_mode == "null"){ + return; + } + + if(this->_mode == "star"){ + if(millis() % this->_period < this->_period / 2){ + digitalWrite(this->_pin, HIGH); + }else{ + digitalWrite(this->_pin, LOW); + } + } + + if(this->_mode == "breath"){ + int val; + if(millis() % this->_period < this->_period / 2){ + val = millis() % this->_period; + val = (float)val * 255 / (this->_period / 2); + }else{ + val = this->_period - millis() % this->_period; + val = (float)val * 255 / (this->_period / 2); + } + analogWrite(this->_pin, val); + } + + } + + void star(const unsigned int& period = 2000){ + this->_mode = "star"; + this->_period = period; + } + + void breath(const unsigned int& period = 3000){ + this->_mode = "breath"; + this->_period = period; + } + + private: + unsigned short _pin; + String _mode; + unsigned int _period; + +}; + +#endif //__LED_H__ \ No newline at end of file diff --git a/src/livb/livb.ino b/src/livb/livb.ino new file mode 100644 index 0000000..8ee5694 --- /dev/null +++ b/src/livb/livb.ino @@ -0,0 +1,88 @@ +#include +#include + +#include "led.h" +#include "buz.h" + +const char* ssid = "yimian-iot"; +const char* password = "1234567890."; +const char* mqtt_server = "192.168.3.4"; // change this to the mqtt server + +const char* topicIn = "hass/ctl/liv/#"; // change this to the outgoing messages +const String clientId = "liv"; + + +WiFiClient espClient; +PubSubClient client(espClient); + +Buz buz(D13); + +void setup() { + Serial.begin(115200); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + client.setServer(mqtt_server, 1883); // default port, change it... + client.setCallback(callback); + + buz.ini(); +} + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + String s = ""; + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + s += (char)payload[i]; + } + Serial.println(""); + if(s=="1"){ + buz.once(); + }else{ + buz.off(); + } + client.publish("hass/snsr/liv/light", s.c_str(), 1); +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + + // Attempt to connect + if (client.connect(clientId.c_str())) { + Serial.println("connected"); + // Once connected, publish an announcement... + // client.publish(topicOut, "Hello from the Gateway!"); + // ... and resubscribe + client.subscribe(topicIn); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 3 seconds before retrying + delay(3000); + } + } +} + +void loop() { + if (!client.connected()) { + reconnect(); + } + client.loop(); + + buz.loop(); +} + diff --git a/src/livb/switch.h b/src/livb/switch.h new file mode 100644 index 0000000..e69de29