|
我用esp32做一个智能家居系统,但是到光敏电阻控制步进电机部分就出问题了,单独的拿出这部分来可以正常运行,但是放在一起就不对了,麻烦大家帮忙看看。
#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR
#define BLINKER_MIOT_MULTI_OUTLET
//#define BLINKER_MIOT_LIGHT //这里用灯光模式来代替小爱语音,可以多路控制。
#define DHTPIN 23
#define ruin 14
#define DHTTYPE DHT11 // DHT 11
#define PersonSensor 22
/*WS2812B的控制*/
#define NUM_LEDS 30 // LED灯珠数量
#define LED_DT 21 // Arduino输出控制信号引脚
#define LED_TYPE WS2812 // LED灯带型号
#define COLOR_ORDER GRB
#define IN1 16 //直流电机
#define IN2 4
#define GMCG 36 //光敏传感器
#define GMCGAO 15
#include <Stepper.h>
#include <Stepper2.h>
#include <Arduino.h>
#include <analogWrite.h>
#include <U8g2lib.h>
#include <DHT.h>
#include <Servo.h>
#include <Ticker.h>
#include <Blinker.h>
#include "FastLED.h"
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,32,33,U8X8_PIN_NONE);
char auth[] = "c164e9e4b4b6";
char ssid[] = "YYDSOK";
char pswd[] = "ABAB414AAA";
int t;
int h;
Servo myServo;
Servo mywindow;
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
BlinkerText Text1("textc");
BlinkerText Text2("textv");
BlinkerSlider Slider1("ran-t6y");
BlinkerSlider Slider2("ran-g4r");
BlinkerSlider Slider3("ran-wka");
BlinkerButton Button1("btn-c25");//门
BlinkerButton Button2("btn-mrx");//窗
BlinkerButton Button3("btn-wen");//温馨模式
BlinkerButton Button4("btn-len");//白冷模式
BlinkerButton Button5("btn-abc");//电风扇
BlinkerButton Button6("btn-123");//窗帘
BlinkerRGB Deng1("col-va2");
DHT dht(DHTPIN, DHTTYPE);
CRGB leds[NUM_LEDS];//建立光带
//CRGB leds[NUM_LEDS];//建立光带
float humi_read = 0, temp_read = 0;
uint8_t max_bright = 128; //LED亮度的控制
uint8_t beginHue;
uint8_t val;
uint8_t value_Light;
bool oState[5] = { false };
int8_t colorR, colorG, colorB, colorW;
/*步进电机*/
int pinOut[4] = { 25, 26, 27, 14};
const int rpm = 15; // 步进电机转速0~15
const int turnsNumber = 1; //一整圈
Stepper2 myStepper(pinOut);
Ticker buttonTicker;
/*声明*/
void MYwindow();
void WS2812FW();
void dis_play();
void DHT11_Inital();
void YU_Inital();
void GOOP();
//小爱同学语音部分
void miotPowerState(const String & state, uint8_t num)
{
BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
if (state == BLINKER_CMD_ON) {
if (num==0){
}
else if(num==1){
myServo.attach(13);
myServo.write(180);
Blinker.delay(1000);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
myServo.detach();
}
else if(num==2){
mywindow.attach(12);
mywindow.write(180);
Blinker.delay(1000);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
mywindow.detach();
}
BlinkerMIOT.powerState("on", num);
BlinkerMIOT.print();
oState[num] = true;
}
else if (state == BLINKER_CMD_OFF) {
BlinkerMIOT.powerState("off", num);
BlinkerMIOT.print();
oState[num] = false;
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
oState[o_num] = false;
}
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
else if(num==1){
myServo.attach(13);
myServo.write(0);
Blinker.delay(1000);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
myServo.detach();
}
else if(num==2){
mywindow.attach(12);
mywindow.write(0);
Blinker.delay(1000);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
mywindow.detach();
}
}
}
/*void miotPowerState(const String & state) //门电机
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myServo.attach(32);
myServo.write(180);
Blinker.delay(1000);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
myServo.detach();
}
else if (state == BLINKER_CMD_OFF) {
myServo.attach(32);
myServo.write(0);
Blinker.delay(1000);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
myServo.detach();
}
}
*/
/*void miotPowerState1(const String & state) //门电机
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myServo1.attach(23);
myServo1.write(180);
Blinker.delay(1000);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
myServo1.detach();
}
else if (state == BLINKER_CMD_OFF) {
myServo1.attach(23);
myServo1.write(0);
Blinker.delay(1000);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
myServo1.detach();
}
}
*/
void miotQuery(int32_t queryCode) //小爱同学语音命令反馈
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
int humi_read_int=humi_read; //去掉湿度浮点
BlinkerMIOT.humi(humi_read_int); //小爱接收湿度
BlinkerMIOT.temp(temp_read); //小爱接收温度
BlinkerMIOT.print();
}
/*void miotMode(uint8_t mode) //小爱同学的多路控制。具体模式点灯科技
{
BLINKER_LOG("need set mode: ", mode);
if (mode == BLINKER_CMD_MIOT_DAY) {
if(LED0_Flag)
{
myServo.attach(32);
myServo.write(180);
Blinker.delay(1000);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
myServo.detach();
}
else
{
myServo.attach(32);
myServo.write(0);
Blinker.delay(1000);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
myServo.detach();
}
}
else if (mode == BLINKER_CMD_MIOT_NIGHT) {
LED1_Flag = !LED1_Flag;
if(LED1_Flag)
{
myServo1.attach(23);
myServo1.write(180);
Blinker.delay(1000);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
myServo1.detach();
}
else
{
myServo1.attach(23);
myServo1.write(0);
Blinker.delay(1000);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
myServo1.detach();
}
}
else if (mode == BLINKER_CMD_MIOT_COLOR) {
LED2_Flag = !LED2_Flag;
if(LED2_Flag)
{
}
else
{
}
}
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
LED3_Flag = !LED3_Flag;
if(LED3_Flag)
{
}
else
{
}
}
else if (mode == BLINKER_CMD_MIOT_TV) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_READING) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
// Your mode function
}
// wsMode = mode;
BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
}
*/
void button1_callback(const String & state) //控制是开门与关门
{
BLINKER_LOG("get button state: ", state);
if(state == "on")
{
myServo.attach(13);
Button1.print("on");
myServo.write(110);
Blinker.delay(3000);
myServo.detach();
}
else if(state == "off")
{
myServo.attach(13);
Button1.print("off");
myServo.write(0);
Button1.text("关门");
Blinker.delay(3000);
myServo.detach();
}
}
void button2_callback(const String & state) //控制窗户的开与关
{
BLINKER_LOG("get button state: ", state);
if(state == "on")
{
mywindow.attach(12);
Button2.print("on");
mywindow.write(110);
Blinker.delay(3000);
mywindow.detach();
}
else if(state == "off")
{
mywindow.attach(12);
Button2.print("off");
mywindow.write(0);
Button2.text("关窗");
Blinker.delay(3000);
mywindow.detach();
}
}
void button3_callback(const String & state) //控制灯温馨模式
{
BLINKER_LOG("get button state: ", state);
if(state == "on")
{
beginHue++;
fill_rainbow(leds, NUM_LEDS, beginHue, 9); //beginHue是起始的色彩数;9是相隔的色调差
FastLED.show();//展示
Button3.print("on");
}
else if(state == "off")
{
fill_solid(leds, 30, CRGB::Black);//白天
FastLED.show();
Button3.print("off");
}
}
void button4_callback(const String & state) //控制灯白冷模式
{
BLINKER_LOG("get button state: ", state);
if(state == "on")
{
fill_solid(leds, 30, CRGB::BlanchedAlmond);
FastLED.show();//展示
Button4.print("on");
}
else if(state == "off")
{
fill_solid(leds, 30, CRGB::Black);//白天
FastLED.show();
Button4.print("off");
}
}
void button5_callback(const String & state) //控制电风扇
{
BLINKER_LOG("get button state: ", state);
if(state == "on")
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
Button5.print("on");
}
else if(state == "off")
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
Button5.print("off");
}
}
void button6_callback(const String & state) //控制窗帘
{
BLINKER_LOG("get button state: ", state);
if(state == "on")
{
myStepper.setDirection(1); // counter-clock-wise方向
myStepper.turn();
myStepper.stop();
Button6.print("on");
}
else if(state == "off")
{
myStepper.setDirection(0); // clock-wise
myStepper.turn(); // one full turn
myStepper.stop();
Button6.print("off");
}
}
void heartbeat() //心跳包keep touch,一般用来传输数据给APP
{
DHT11_Inital();
YU_Inital();
}
//#雨滴窗户控制部分,雨滴到来,窗户关闭。
void YU_Inital()
{
digitalRead(ruin);
if(ruin == 1)
{
mywindow.attach(12);
mywindow.write(110);
delay(1000);
mywindow.detach();
}
else if(ruin == 0)
{
mywindow.attach(12);
mywindow.write(0);//关窗
delay(1000);
mywindow.detach();
}
}
//光敏窗帘 就是这部分,我单独运行时光敏电阻感受到光,步进电机就运行了,但是综合放在一起就不起作用,不知道怎么改?麻烦大佬帮看看,
有偿的也可以,希望大家帮帮我,
void GOOP()
{
if(digitalRead(GMCG) == 0&&analogRead(GMCGAO)< 300)
{
myStepper.setDirection(1); // counter-clock-wise方向
myStepper.turn();
myStepper.stop();
while(digitalRead(GMCG) == 0&&analogRead(GMCGAO)< 300);
delay(500);
}
else if(digitalRead(GMCG) == 1&&analogRead(GMCGAO)> 3000)
{
myStepper.setDirection(0); // clock-wise
myStepper.turn(); // one full turn
myStepper.stop();
while(digitalRead(GMCG) == 1&&analogRead(GMCGAO)> 3000);
delay(500);
}
}
//门舵机控制#开门
void MyServo()
{
if(digitalRead(PersonSensor)==1)
{
myServo.write(180);
}
else if(digitalRead(PersonSensor)==0)
{
myServo.write(0);
Blinker.delay(400);
}
}
//#温湿度OLED数据显示子函数
void dis_play()
{
u8g2.firstPage(); // OLED首页
do
{
u8g2.setFont(u8g2_font_ncenB10_tr); //设置字体
u8g2.drawStr(25, 26, "Temp:"); //第26行25列显示“Temp:”
u8g2.setCursor(75,26);//位置
u8g2.print(t);
u8g2.drawStr(90, 26, "°C");
u8g2.setFont(u8g2_font_ncenB10_tr); //设置字体
u8g2.drawStr(25, 60, "Humi:"); //第60行25列显示“Humi:”
u8g2.setCursor(75,60); //第60行70列显示湿度数据
u8g2.print(h);
u8g2.drawStr(90, 60, "%");
} while (u8g2.nextPage()); //执行配置
}
//#温度传感模块
void DHT11_Inital()
{
h = dht.readHumidity();
t = dht.readTemperature();
HUMI.print(humi_read);
TEMP.print(temp_read);
if (isnan(h) || isnan(t))
{
BLINKER_LOG("Failed to read from DHT sensor!");
}
else
{
BLINKER_LOG("Humidity: ", h, " %");
BLINKER_LOG("Temperature: ", t, " *C");
humi_read = h;
temp_read = t;
}
Blinker.delay(2000);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
u8g2.begin();
Blinker.begin(auth, ssid, pswd);
Blinker.attachHeartbeat(heartbeat);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
Button6.attach(button6_callback);
BlinkerMIOT.attachQuery(miotQuery);//反馈的注册回调函数
//BlinkerMIOT.attachPowerState(miotPowerState);//电源的注册回调
BlinkerMIOT.attachPowerState(miotPowerState);
// BlinkerMIOT.attachMode(miotMode);
//BlinkerMIOT.attachMode(miotMode);//多模式语音的回调函数
dht.begin();
myServo.attach(13);//门舵机初始化
mywindow.attach(12);//门舵机初始化
myServo.write(0);
mywindow.write(0);
pinMode(ruin,INPUT);
pinMode(PersonSensor,INPUT);
pinMode(IN1,OUTPUT);//直流电机
pinMode(IN2,OUTPUT);
WiFi.begin(ssid, pswd);
pinMode(GMCG,INPUT); //这个是光敏电阻的DO和AO
pinMode(GMCGAO,INPUT);
myStepper.setSpeed(rpm);
LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // 初始化光带
FastLED.setBrightness(max_bright); //设置光带的亮度
//LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // 初始化光带
//FastLED.setBrightness(max_bright); //设置光带的亮度
int counter = 0;
while (WiFi.status() != WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
Serial.print(".");
}
}
void loop()
{
Blinker.run();
DHT11_Inital();
dis_play();
YU_Inital();
GOOP(); 光敏窗帘函数
Serial.println(temp_read);
MyServo();
}
|
|