当前位置:   article > 正文

基于树莓派的智能家居系统_树莓派硬件画图

树莓派硬件画图

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


概述

  • 使用树莓派、Arduino、语音模块、红外模块实现对家用空调进行控制
  • 树莓派与Arduino进行TCP协议通信
  • 树莓派与语音模块,Arduino与红外模块都进行串口通信

一、硬件组成

在这里插入图片描述
图1.树莓派

在这里插入图片描述
图2.Arduino

在这里插入图片描述

图3.语音模块
在这里插入图片描述
图4.红外模块

二、软件

在这里插入图片描述

图1.Arduino开发环境
在这里插入图片描述
图2.树莓派系统
在这里插入图片描述

图3.串口调试助手

三.获取空调遥控器红外编码

1、将红外模块与TTL串口相连接,并插入电脑USB口(注意管脚接线正确)

在这里插入图片描述
将需要被解码的空调遥控器按键对着红外模块按下,即在串口上返回所需要的解码数据(注意波特率,这里默认为115200)。
在这里插入图片描述
获取到红外编码后,将编码全部复制,以十六进制发送,红外模块对着空调,查看空调是否有反应。

四.树莓派与Arduino进行TCP协议通信

(1) 将Arduino做为服务端

#include <ESP8266WiFi.h>

char ssid[] = "1004";               /* wifi账号*/
char passwd[] = "13609618951";      /* 密码*/
int port = 8888;                    /* 端口号*/
WiFiServer server(port);            /* 设置服务器端口*/

const byte open_data[] = {      /* 空调红外开码*/
0xFE, 0xFD, 0x03, 0x85, 0x01, 0xC3, 0x40, 0x39, 0x3F, 0x3B, 0x3D, 0x39, 0x3F, 0xAA, 0x3D, 0x3B, 0x3D, 0x39, 0x3F, 
0xAA, 0x3C, 0x39, 0x3F, 0xAA, 0x3D, 0x39, 0x3F, 0x3C, 0x3C, 0xAA, 0x3F, 0x37, 0x3F, 0x39, 0x41, 0x37, 0x3F, 0x39, 
0x3F, 0x3C, 0x3C, 0x39, 0x3F, 0x39, 0x3F, 0x3C, 0x3D, 0x38, 0x40, 0x38, 0x40, 0x3B, 0x3D, 0x39, 0x3F, 0x39, 0x3F,
0x39, 0x3F, 0x39, 0x3F, 0x39, 0x3F, 0xAA, 0x3F, 0x37,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 0x3C, 
0x3D, 0x3B, 0x3D, 0x3B, 0x3D, 0x3B, 0x3D, 0x39, 0x3F, 0x39, 0x3F, 0x39, 0x40, 0x38, 0x40, 0x39, 0x3F, 0x3B, 0x3D, 
0x39, 0x3F, 0x39, 0x3F, 0x3B, 0x3D, 0xAA, 0x3D, 0xAA, 0x3C, 0x3C, 0x3C, 0x39, 0x3F, 0xAA, 0x3D, 0xAA, 0x3C, 0x39, 0x40, 0xA9, 0x3D, 0xFF 
};

const byte close_data[] = {     /* 空调红外关码*/
0xFE, 0xFD, 0x03, 0x85, 0x01, 0xC1, 0x41, 0x37, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 
0xA7, 0x40, 0x36, 0x42, 0xA7, 0x3F, 0x37, 0x41, 0x37, 0x41, 0xA8, 0x3F, 0x36, 0x42, 0x37, 0x41, 0x37, 0x41, 0x37, 
0x41, 0x37, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 0x42, 0x36, 0x42, 
0x36, 0x41, 0x37, 0x42, 0x36, 0x42, 0xA7, 0x3F, 0x37, 0x42, 0xA7, 0x3F, 0x37, 0x41, 0x37, 0x41, 0xA8, 0x3F, 0x36, 
0x42, 0x07, 0xD3, 0x41, 0x37, 0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0x37, 
0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0x37, 0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 0x42, 
0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0x37, 0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 
0x42, 0x36, 0x42, 0xA7, 0x40, 0xA7, 0x3F, 0x37, 0x41, 0x37, 0x42, 0x0F, 0xA8, 0x03, 0x85, 0x01, 0xC1, 0x42, 0x36, 
0x42, 0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0x37, 0x41, 0x36, 0x42, 0xA8, 0x3F, 0x36, 0x42, 0xA7, 0x3F, 0x37, 0x42, 
0x36, 0x42, 0xA7, 0x3F, 0x37, 0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0x37, 
0x41, 0x37, 0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0xA8, 0x3F, 
0xA7, 0x3F, 0xA8, 0x3F, 0x36, 0x42, 0x36, 0x42, 0xA7, 0x3F, 0x37, 0x42, 0x07, 0xD2, 0x41, 0x37, 0x41, 0x37, 0x42, 
0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0x37, 0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 
0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0x37, 0x41, 0x37, 0x41, 0x37, 0x41, 0x37, 0x42, 0x36, 0x42, 
0x36, 0x42, 0x36, 0x42, 0x36, 0x42, 0xA7, 0x40, 0xA7, 0x3F, 0x37, 0x41, 0x37, 0x41, 0xA8, 0x3F, 0xA7, 0x40, 0x36, 0x42, 0x36, 0x42, 0xFF 
};


void initWifi()     /* 建立Wifi网络模块*/
{
    WiFi.mode(WIFI_STA);            /* 设置为STA模式*/
    WiFi.begin(ssid, passwd);       /*输入wifi账号和密码*/

    while(WiFi.status() != WL_CONNECTED){
        Serial.print(".");
        delay(500);
    }

    Serial.println(WiFi.localIP());		/* 打印IP地址*/
    delay(500);

}

void setup()
{
    Serial.begin(115200);       /* 设置波特率*/
    initWifi();                 /* 初始化网络*/

}

void loop()
{
    char cmd;
    WiFiClient client = server.available();     /* 设置为服务端*/
    server.begin();     /* 服务端初始化*/
    

    while(client.connected()){          /* 检测是否连接*/
        while(client.available() > 0){  /* 如果串口中有数据*/

            cmd = client.read();        /* 将串口中的数据传给cmd*/
            //Serial.println(cmd);      /* 打印cmd*/

            if(cmd == '1'){
             // Serial.println("1111111111"); 
                Serial.write(open_data, (sizeof(open_data) / sizeof(open_data[0])));
            }
            if(cmd == '2'){
             // Serial.println("22222222");
                Serial.write(close_data, (sizeof(close_data) / sizeof(close_data[0])));
            }
        }
    }

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

(2) 树莓派作为客户端

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<wiringSerial.h>
#include<unistd.h>
#include<string.h>
#include<wiringPi.h>

int main(int argc, char *argv[])
{
    int fd;
    int c_fd;
    int ret;
    char cmd[128] = {'\0'};
    char a = '1';
    char b = '2';

    if(argc != 3){
        printf("input params error!\n");
        exit(0);
    }

    wiringPiSetup();        /* 硬件初始化,使用wiringPi引脚*/
    fd = serialOpen("/dev/ttyAMA0", 9600);      /* 打开串口*/
    if(fd == -1) {
        perror("open failed");
        exit(-1);
    }

    /* 创建网络套接字*/
    c_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(c_fd == -1){
        perror("socket failed!\n");
        exit(-1);
    }

    /* 建立连接*/
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));

    addr.sin_family = AF_INET;                  /* 设置IP协议*/
    addr.sin_port = htons(atoi(argv[2]));       /* 传递端口号,从网络(大端)到主机(小端)*/
    inet_aton(argv[1], &addr.sin_addr);         /* 传递IP地址*/

    ret = connect(c_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
    if(ret == -1) {
        perror("connect wemos failed!\n");
        exit(-1);
    }else{
        printf("connected successfully\n");
    }

    while(1){
       
        read(fd, cmd, sizeof(cmd));     /* 读取语音模块发送的指令*/
        
        if(strstr(cmd,"open")){
            printf("accept open\n");
            write(c_fd, &a, sizeof(char));
        }

        if(strstr(cmd,"close")){
            printf("accept close\n");
            write(c_fd, &b, sizeof(char));
        }
        
        printf("cmd:%s",cmd);
		memset(cmd, '\0', sizeof(cmd) / sizeof(char));
        serialFlush(fd);                /* 刷新缓冲区数据*/
        delay(200);
    }

    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号