WiFi风扇控制开发

1,风扇开发概述
L9110风扇模块介绍
在这里插入图片描述
L9110芯片特性:
- 控制和驱动电机
- 两通道推挽式功率放大专用集成电路期件
- 电压范围:2.5v-12v
- 每通道具有800mA连续电流输出能力
- 输出具有正转,反转,高阻和刹车 四种状态

引脚定义:
在这里插入图片描述
在这里插入图片描述
控制电路:
在这里插入图片描述
引出端波形:
在这里插入图片描述
接线:

2,风扇开发接线
风扇GND-------开发板GND
VCC-------5V
IA---------PA15
IB---------PA0

3,风扇产品创建
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4,风扇代码编写
创建工程:aos create project fenshan -b mk3080 -t linkkit_demo -d temp
工程目录下link_example_solo.c中加入以下代码:

#include "aos/hal/gpio.h"
#define  GPIO_CLOCKW  1   //PA15
#define  GPIO_ACLOKW   6   //PA0
gpio_dev_t  clockWiseIO;
gpio_dev_t  antiClockWiseIO;


static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{
    int res = 0;
    cJSON *root ,*prop;
    root = cJSON_Parse(request);
    if(root == NULL){
        return res;
    }
    prop = cJSON_GetObjectItem(root,"powerstate");
    if(prop!=NULL && cJSON_IsNumber(prop)){
        if(prop->valueint == 1){
                fanPowerStatus = 1;
                hal_gpio_output_high(&clockWiseIO);
                hal_gpio_output_low(&antiClockWiseIO);
                printf("power on\n");
        }else if(prop->valueint == 0){
                fanPowerStatus = 0;
                hal_gpio_output_low(&clockWiseIO);
                hal_gpio_output_low(&antiClockWiseIO);
                printf("power off\n");
        }
    }

    prop = cJSON_GetObjectItem(root,"direction");
    if(prop!=NULL && cJSON_IsNumber(prop)){
        if(fanPowerStatus == 1){
                if(prop->valueint == 1){
                	hal_gpio_output_low(&clockWiseIO);
                	hal_gpio_output_high(&antiClockWiseIO);
                    printf("direction 1\n");
                 }else if(prop->valueint == 0){
                 	hal_gpio_output_high(&clockWiseIO);
                	hal_gpio_output_low(&antiClockWiseIO);
                    printf("direction 0\n");
                 }
        }else{
        		hal_gpio_output_high(&clockWiseIO);
                hal_gpio_output_high(&antiClockWiseIO);
         }
        
    }

//linkkit_main()函数前定义驱动风扇转动的GPIO
void initGPIO()
{
	clockWiseIO.port = GPIO_CLOCKW;
	antiClockWiseIO.port = GPIO_ACLOKW;

	clockWiseIO.config = OUTPUT_PUSH_PULL;
	antiClockWiseIO.config = OUTPUT_PUSH_PULL;

	hal_gpio_init(&clockWiseIO);
	hal_gpio_init(&antiClockWiseIO);

	sys_jtag_off();
	
	hal_gpio_output_high(&clockWiseIO);
	hal_gpio_output_high(&antiClockWiseIO);
}


aos make menuconfig 进行配置(cJSON library勾选上),aos make编译,烧录,手机app/天猫精灵语音配网,putty端查看打印信息,控制风扇的正转反转,停止灯属性。

5,风扇IO温度控制演示
link_example_solo.c中添加:

#include "aos/hal/gpio.h"
#define  GPIO_CLOCKW  1   //PA15
#define  GPIO_ACLOKW   6  //PA0

#define G18b20IO  0   //PA14
gpio_dev_t GPIO_18b20;
float temp;
gpio_dev_t  clockWiseIO;
gpio_dev_t  antiClockWiseIO;

#define CLKWISE 1 //正转
#define ANT_CLKWISE 2 //反转

int fanPowerStatus = 0;
int  fanDirection = 0;
void initForInput()
{
   GPIO_18b20.config=INPUT_PULL_UP;
   if(GPIO_18b20io.priv){
   		free(GPIO_18b20io.priv);
   		GPIO_18b20io.priv = NULL;
   	}
   hal_gpio_init(&GPIO_18b20);
}

void initForOutput()
{
   GPIO_18b20.config = OUTPUT_PUSH_PULL;
   hal_gpio_init(&GPIO_18b20);
}
//初始化
void init_18b20()
{
   int inputValue=0;
   hal_gpio_output_high(&GPIO_18b20);
   DelayUs(100);
   hal_gpio_output_low(&GPIO_18b20);
   DelayUs(750);
   hal_gpio_output_high(&GPIO_18B20);
   initForInput();
   DelayUs(50);
   hal_gpio_input_get(&GPIO_18b20,inputValue);

   if(inputValue){
        printf("18b20 init failed\n");
        return -1;
   }
 initForOutput();
   hal_gpio_output_high(&GPIO_18b20);
   DelayUs(15);
   retrun ;

}
//写时序
void write_18b20_byte(unsigned char data)
{
   int i;
   for(i=0;i<8;i++){
        hal_gpio_output_low(&GPIO_18B20);
        if(data&0x01){    //write 1 to 18b20
            DelayUs(10);
            hal_gpio_output_high(&GPIO_18b20);
            DelayUs(45);
        }else{
            DelayUs(60);
            hal_gpio_output_high(&GPIO_18b20);
        }
        DelayUs(10);
        data>>=1;
   }
}
//读时序
unsigned char read_18b20_byte()
{
   int i;
	int inputValue;
   int reByte=0;

   for(i=0;i<8;i++){
        hal_gpio_output_low(&GPIO_18b20);
        initForInput();
        DelayUs(5);

        hal_gpio_input_get(&GPIO_18b20,&inputValue);
        reByte = reByte>>1;
        if(inputValue){
           reByte |= 0x80;
        }

        initForOutput();
        hal_gpio_output_high(&GPIO_18b20);

        DelayUs(50);
   }
   retrun  reByte;
}
//从18b20温度传感器读温度值到暂存寄存器中
unsigned short readTempFrom18b20()
{
   int ret;
   unsigned char tempH;
   unsigned char tempL;
   unsigned short tmep;
   ret = init_18b20();
   if(ret<0){
        return -1;
   }

   write_18b20_byte(0xCC);
   write_18b20_byte(0x44);

   DelayUs(2000);

   write_18b20_byte(0xCC);
   write_18b20_byte(0xbe);
   DelayUs(500);

   tempL= read_18b20_byte();
   tempH = read_18b20_byte();

   temp = tempH*256 + tempL;
   printf("read th=%d,tl=%d,temp=%d\n",tempH,tempL,temp);
   return temp;

}

void user_post_property(void)
{
    static int cnt = 0;
    int res = 0;
    static int tempLowsetFlag = 0;
    char property_payload[30] = {0};
   // HAL_Snprintf(property_payload, sizeof(property_payload), "{\"Counter\": %d}", cnt++);
	temp = readTempFrom18b20();
	temp = temp/16;
	printf("read temp=%f\n",temp);
	sprintf(property_payload,"{\"temperature\":%f}",temp);
	if(temp>30){
		if(fanPowerStatus == 1){
			if(fanDirection = CLKWISE){
				hal_gpio_output_high(&clockWiseIO);
        		hal_gpio_output_low(&antiClockWiseIO);
			}else if(fanDirection == ANT_CLKWISE){
				hal_gpio_output_low(&clockWiseIO);
        		hal_gpio_output_high(&antiClockWiseIO);
			}		
		}
		tempLowsetFlag = 0;
    }else{
    		if(tempLowsetFlag == 0){
    			hal_gpio_output_high(&clockWiseIO);
       			hal_gpio_output_high(&antiClockWiseIO);
    			tempLowsetFlag = 1;
			}
		}
	}
    res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
                             (unsigned char *)property_payload, strlen(property_payload));

    EXAMPLE_TRACE("Post Property Message ID: %d", res);
}


static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{
    int res = 0;
    cJSON *root ,*prop;
    root = cJSON_Parse(request);
    if(root == NULL){
        return res;
    }
    prop = cJSON_GetObjectItem(root,"powerstate");
    if(prop!=NULL && cJSON_IsNumber(prop)){
        if(prop->valueint == 1){
                fanPowerStatus = 1;
                hal_gpio_output_high(&clockWiseIO);
                hal_gpio_output_low(&antiClockWiseIO);
                printf("power on\n");
        }else if(prop->valueint == 0){
                fanPowerStatus = 0;
                hal_gpio_output_low(&clockWiseIO);
                hal_gpio_output_low(&antiClockWiseIO);
                printf("power off\n");
        }
    }

    prop = cJSON_GetObjectItem(root,"direction");
    if(prop!=NULL && cJSON_IsNumber(prop)){
        if(fanPowerStatus == 1){
                if(prop->valueint == 1){
                	hal_gpio_output_low(&clockWiseIO);
                	hal_gpio_output_high(&antiClockWiseIO);
                    printf("direction 1\n");
                    fanDirection = CLKWISE;
                 }else if(prop->valueint == 0){
                 	hal_gpio_output_high(&clockWiseIO);
                	hal_gpio_output_low(&antiClockWiseIO);
                	fanDirection = ANT_CLKWISE;
                    printf("direction 0\n");
                 }
        }else{
        		hal_gpio_output_high(&clockWiseIO);
                hal_gpio_output_high(&antiClockWiseIO);
         }
        
    }

//linkkit_main()函数前定义驱动风扇转动的GPIO
void initGPIO()
{
	clockWiseIO.port = GPIO_CLOCKW;
	antiClockWiseIO.port = GPIO_ACLOKW;

	clockWiseIO.config = OUTPUT_PUSH_PULL;
	antiClockWiseIO.config = OUTPUT_PUSH_PULL;

	hal_gpio_init(&clockWiseIO);
	hal_gpio_init(&antiClockWiseIO);

	sys_jtag_off();
	
	hal_gpio_output_high(&clockWiseIO);
	hal_gpio_output_high(&antiClockWiseIO);
}

app_entry.c中添加:

initGPIO();    //初始化风扇的gpio
initGPIO18b20();  //初始化18b20的gpio
aos_loop_run();
  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
早市向来以卖菜者居多,但总有一些年长者将自家闲置废旧物品拿来早市摆摊售卖,一来可以打发早起的这段时光,二来可以享受二次创业给自己带来的乐趣,当然,也给喜欢物美价廉、充满好奇心、喜欢瞎折腾的人创造淘宝机会,这个是什么,CUP风扇,2块钱买回家来研究一番。 CPU风扇是四线制,一般情况下,红黑分别为正负,接上12V电压,转速超快,发出嗡嗡嚎叫声,说明风扇还可以用,但还有黄蓝两根线不知是什么线,于是万事不解百度一下找到了答案,其中蓝线为PWM调速线,做了一下试验,将蓝线接地,风扇立即减慢直至停止,如果接上脉宽调制信号,就可以调速了,酷暑难耐,如果做一个电风扇,无级变速加摆头,让夏天不再炎热,岂不美哉! 原理介绍: 接入电源后,红色指示灯亮,打开风扇控制开关S1,风扇开始转动,打开摆头控制开关S2,风扇开始摆动。 脉宽调制电路功能说明:采用NE555控制芯片,如上图,上电后,电流通过1K电阻、IN5819二极管、可调电阻对0.1微法电容进行充电,通过可调电阻IN5819、1K电阻进行放电,构成占空比可调的方波发生器,用于控制风扇转速。 转向驱动电路功能说明:仍然以NE555控制芯片为核心,如上图,2脚和6脚的解法构成双稳态触发器,当按下2脚的开关时,3脚输出高电平,7脚通过三极管s8050输出低电平,当按下6脚开关时,高低电平相反,变为高低电平两路输出, 通过L9110驱动电机,控制电机左右旋转。 演示视频:
好的,下面是一个简单的 Arduino 代码示例,用于控制智能风扇并连接到 MQTT 服务器: ```c++ #include <ESP8266WiFi.h> #include <PubSubClient.h> // WiFi 认证信息 const char* ssid = "YourSSID"; const char* password = "YourWifiPassword"; // MQTT 服务器信息 const char* mqtt_server = "mqtt.server.com"; const int mqtt_port = 1883; const char* mqtt_user = "mqtt_username"; const char* mqtt_password = "mqtt_password"; // MQTT 订阅主题 const char* mqtt_topic = "home/fan"; // Arduino 引脚定义 const int fanPin = D1; // MQTT 客户端 WiFiClient espClient; PubSubClient client(espClient); void setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); 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()); } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) { Serial.println("connected"); client.subscribe(mqtt_topic); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, mqtt_port); pinMode(fanPin, OUTPUT); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message received ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); // 解析 MQTT 消息并控制风扇 String message = String((char*)payload); if (message == "on") { digitalWrite(fanPin, HIGH); } else if (message == "off") { digitalWrite(fanPin, LOW); } } ``` 需要注意的一些事项: 1. 请先确保你已经安装了 ESP8266WiFi 和 PubSubClient 库。 2. 请修改代码中的 WiFi 认证信息、MQTT 服务器信息和引脚定义。 3. 代码中的 `mqtt_topic` 是该 MQTT 客户端订阅的主题。你需要确保服务器发布的消息与该主题匹配。 4. `callback` 函数是 MQTT 客户端接收到消息后的处理函数。你需要在其中解析 MQTT 消息并控制风扇。 希望这个例子能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值