iotcat 5 years ago
parent c91d0bdcbe
commit d07f8d0f58
  1. 1
      src/din/.vscode/.gitignore
  2. 48
      src/din/buz.h
  3. 115
      src/din/din.ino
  4. 83
      src/din/led.h
  5. 91
      src/din/swi.h
  6. 6
      src/gateway/gateway.ino
  7. 6
      src/gateway/lora-mqtt.h
  8. 6
      src/gateway/lora-socket.h
  9. 6
      src/gateway/stringVec.h
  10. 6
      src/liv/led.h
  11. 6
      src/liv/liv.ino
  12. 6
      src/livb/buz.h
  13. 6
      src/livb/led.h
  14. 6
      src/livb/livb.ino

@ -0,0 +1 @@
./bin/

@ -0,0 +1,48 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:58:28
* @Last Modified by:
* @Last Modified time: 2019-08-20 10:07:51
*/
#ifndef __BUZ_H__
#define __BUZ_H__
class Buz{
public:
Buz(const int& pin){
this->_pin = pin;
};
~Buz(){};
void ini() const{
pinMode(this->_pin, OUTPUT);
};
void loop() const{
if(millis() < this->_toT){
analogWrite(this->_pin, (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,115 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:57:58
* @Last Modified by:
* @Last Modified time: 2019-08-20 10:04:04
*/
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include "led.h"
#include "buz.h"
#include "swi.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);
Swi swi(D8, D9);
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);
swi.on("on", swiOn);
swi.on("off", swiOff);
buz.ini();
swi.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);
client.publish("hass/snsr/livd/test", String(analogRead(A0)).c_str());
Serial.println(String(analogRead(A0)).c_str());
}
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 swiOn(){
Serial.println("onononon");
}
void swiOff(){
Serial.println("offoffoff");
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
buz.loop();
swi.loop();
}

@ -0,0 +1,83 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:58:21
* @Last Modified by:
* @Last Modified time: 2019-08-20 10:00:02
*/
#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,91 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 10:04:18
* @Last Modified by:
* @Last Modified time: 2019-08-20 10:40:35
*/
#ifndef __SWI_H__
#define __SWI_H__
class Swi{
public:
Swi(const int& pin0, const int& pin1){
_pin0 = pin0;
_pin1 = pin1;
};
~Swi(){};
static inline void ini() {
pinMode(_pin0, INPUT);
pinMode(_pin1, OUTPUT);
_fState = state();
};
static inline void begin() {
ini();
};
static inline void loop(){
const bool sta = state();
if(sta != _fState){
if(sta == true){
if(_isDefOn) _on();
}else{
if(_isDefOff) _off();
}
if(_isDefToggle) _toggle();
_fState = sta;
}
}
static inline void on(const String& event, void (*f)(void)){
if(event == "on"){
_on = f;
_isDefOn = true;
}
if(event == "off"){
_off = f;
_isDefOff = true;
}
if(event == "toggle"){
_toggle = f;
_isDefToggle = true;
}
}
static const bool state(const unsigned int& total = 600, const unsigned int& threshole = 500){
unsigned int cnt = 0;
for(unsigned int i = 0; i < total; i ++){
digitalWrite(_pin1, i % 2);
if(digitalRead(_pin0) == i%2){
cnt ++;
}
}
if(cnt >= threshole){
return true;
}else{
return false;
}
}
private:
static unsigned short _pin0, _pin1;
static bool _fState;
static bool _isDefOn, _isDefOff, _isDefToggle;
static void (*_on)(void);
static void (*_off)(void);
static void (*_toggle)(void);
};
unsigned short Swi::_pin0, Swi::_pin1;
bool Swi::_fState, Swi::_isDefOn = false, Swi::_isDefOff = false, Swi::_isDefToggle = false;
void (*Swi::_on)(void);
void (*Swi::_off)(void);
void (*Swi::_toggle)(void);
#endif //__SWI_H__

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:58:56
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:58:56
*/
//#include <ArduinoJson.h>
#include <SPI.h>
#include <LoRa.h>

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:58:50
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:58:50
*/
#ifndef __LORA_MATT_H__
#define __LORA_MATT_H__

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:58:47
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:59:57
*/
#ifndef __LORA_SOCKET_H__
#define __LORA_SOCKET_H__

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:58:43
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:59:58
*/
#ifndef __STRINGVEC__
#define __STRINGVEC__

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:59:16
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:59:53
*/
#ifndef __LED_H__
#define __LED_H__

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:59:13
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:59:55
*/
#include <PubSubClient.h>
#include <ESP8266WiFi.h>

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:59:24
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:59:24
*/
#ifndef __BUZ_H__
#define __BUZ_H__

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:59:20
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:59:50
*/
#ifndef __LED_H__
#define __LED_H__

@ -1,3 +1,9 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-08-20 09:59:27
* @Last Modified by:
* @Last Modified time: 2019-08-20 09:59:27
*/
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <EEPROM.h>

Loading…
Cancel
Save