You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
4.6 KiB

6 years ago
/* *****************************************************************
6 years ago
* this is the code for wIoT TEST purpose
6 years ago
*
* *****************************************************************/
6 years ago
// MQTT auth code
#define MQTT_AUTH "2cf87de895ee"
6 years ago
// Set as WIFI mode
6 years ago
#define BLINKER_WIFI
6 years ago
#define BLINKER_PRINT Serial
6 years ago
6 years ago
// Include Blinker lib
6 years ago
#include <Blinker.h>
6 years ago
// Correspond Pins to Tag
#define lightCtl D5 //Control the light
#define swiIn D4 // Pins for Check the switch state :: signal In
#define swiOut D8 // Pins for Check the switch state :: signal Out
6 years ago
6 years ago
// Parameter Define
#define SWI_TRY_TIMES 100
#define SWI_OK_TIMES 77
6 years ago
6 years ago
// Set wifi and MQTT config
6 years ago
char auth[] = ;
6 years ago
char ssid[] = "yimian-iot";
char pswd[] = "1234567890.";
// load module
6 years ago
BlinkerButton wIoT("wIoT");
6 years ago
BlinkerButton lightCtlBtn("btn-light");
6 years ago
6 years ago
6 years ago
// declare global var
int swiStatus=0;
6 years ago
6 years ago
/******** Custom Functions *********/
6 years ago
6 years ago
/*** LightCtl Functions ***/
// function for control light :: 0(shutdown),1(open),2(switch)
int light_ctl(int cmd)
6 years ago
{
6 years ago
if(cmd == 0)
{
digitalWrite(lightCtl, HIGH);
if(digitalRead(lightCtl) == HIGH) {BLINKER_LOG("Run Funtion light_ctl :: light Shutdown");update_light_btn();return 1;}
else {BLINKER_LOG("ERROR in Funtion light_ctl :: when light Shutdown");return 0;}
}
if(cmd == 1)
{
digitalWrite(lightCtl, LOW);
if(digitalRead(lightCtl) == LOW) {BLINKER_LOG("Run Funtion light_ctl :: light Open");update_light_btn();return 1;}
else {BLINKER_LOG("ERROR in Funtion light_ctl :: when light Open");return 0;}
}
if(cmd == 2)
{
int lightStatus = digitalRead(lightCtl);
digitalWrite(lightCtl, !lightStatus);
if(digitalRead(lightCtl) != lightStatus) {BLINKER_LOG("Run Funtion light_ctl :: light Switch");update_light_btn();return 1;}
else {BLINKER_LOG("ERROR in Funtion light_ctl :: when light Switch");return 0;}
}
return 0;
6 years ago
}
6 years ago
//function for get light info :: ::return 0(shutdown),1(open)
int get_light_status()
6 years ago
{
6 years ago
if(digitalRead(lightCtl) == HIGH) return 0;
else if (digitalRead(lightCtl == LOW)) return 1;
else return -1;
}
6 years ago
6 years ago
// function for dealing with light error
int light_err()
{
BLINKER_LOG("ERROR with LIGHTCTL!!!");
}
6 years ago
6 years ago
// function for update app button state
void update_light_btn()
6 years ago
{
6 years ago
if(digitalRead(lightCtl) == LOW)
{
lightCtlBtn.icon("fas fa-lightbulb");
lightCtlBtn.color("#00CD00");
lightCtlBtn.text("戳我关灯~","关灯啦~");
lightCtlBtn.print("on");
}
else
{
lightCtlBtn.icon("far fa-lightbulb");
lightCtlBtn.color("#FF0000");
lightCtlBtn.text("戳我开灯~","戳我开灯~");
lightCtlBtn.print("off");
}
}
6 years ago
6 years ago
/*** Swi Functions ***/
// function for judging swi state :: ::return 0(off),1(on),-1(error)
int get_swi_status()
{
int swiCount = 0;
int swiEff = 0;
for(swiCount = 0; swiCount < SWI_TRY_TIMES; swiCount++)
{
digitalWrite(swiOut, HIGH);
if(digitalRead(swiIn) == HIGH) swiEff++;
digitalWrite(swiOut, LOW);
if(digitalRead(swiIn) == LOW) swiEff++;
swiEff--;
}
BLINKER_LOG("Parameter in get_swi_status :: swiEff = ",swiEff);
if(swiEff > SWI_OK_TIMES) return 1;
else return 0;
}
/******** Blinker Attached Function *********/
//
void lightCtlBtn_callback(const String & state)
{
BLINKER_LOG("lightCtlBtn :: get button state: ", state);
6 years ago
6 years ago
if (state == BLINKER_CMD_BUTTON_TAP) {
BLINKER_LOG("Button tap!");
if(!light_ctl(2)) light_err();
}
else if (state == BLINKER_CMD_ON) {
BLINKER_LOG("Toggle on!");
if(!light_ctl(1)) light_err();
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("Toggle off!");
if(!light_ctl(0)) light_err();
}
}
6 years ago
6 years ago
/******* Heartbeat Function ********/
// Heartbeat for Blinker app
void heartbeat_app()
{
update_light_btn();
}
// Heartbeat for wIoT
void heartbeat(const String & state)
{
Blinker.print("li","jj");
}
6 years ago
/******* Arduino Setup Funstion *******/
void setup()
6 years ago
{
6 years ago
// Serial ini
6 years ago
Serial.begin(115200);
6 years ago
BLINKER_DEBUG.stream(Serial);
6 years ago
6 years ago
// Pins state declare
pinMode(lightCtl, OUTPUT);
pinMode(swiIn, INPUT);
pinMode(swiOut, OUTPUT);
6 years ago
6 years ago
// Pins state ini
digitalWrite(lightCtl, HIGH);
6 years ago
6 years ago
// swi ini
swiStatus = get_swi_status();
// Blinker ini
Blinker.begin(auth, ssid, pswd);
// Blinker attached Functions
6 years ago
wIoT.attach(heartbeat);
6 years ago
lightCtlBtn.attach(lightCtlBtn_callback);
6 years ago
// Blinker attached Heartbeat
Blinker.attachHeartbeat(heartbeat_app);
6 years ago
}
6 years ago
6 years ago
/******** Arduino Main loop Function********/
6 years ago
void loop() {
6 years ago
// Active Blinker
6 years ago
Blinker.run();
6 years ago
if(swiStatus != get_swi_status()) {light_ctl(2);swiStatus = get_swi_status();}
6 years ago
}