mirror of https://github.com/IoTcat/sola.git
parent
cb2d2afa91
commit
bcb0a343f0
6 changed files with 210 additions and 0 deletions
@ -0,0 +1 @@ |
||||
./bin/ |
@ -0,0 +1 @@ |
||||
./bin/ |
@ -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__
|
@ -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__
|
@ -0,0 +1,88 @@ |
||||
#include <PubSubClient.h> |
||||
#include <ESP8266WiFi.h> |
||||
|
||||
#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(); |
||||
} |
||||
|
Loading…
Reference in new issue