C语言基于Socket和SQLite的智能家居状态监测与建议系统

本文介绍一个简单的智能家居状态监测与建议系统,使用C语言编写,结合Socket编程实现客户端与服务器端通信,并利用SQLite数据库进行数据存储和查询。

服务器端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <sqlite3.h>

#define MAX_BUFFER_SIZE 1024

typedef struct {
    int uid;
    char device_name[10];
    char device_state[10];
    char value[10];
    char mode[10];
} Status;

typedef struct {
    int sockfd;
    struct sockaddr_in client_addr;
    socklen_t client_addr_len;
    sqlite3 *db;
} ServerContext;

void handleClientRequest(ServerContext *context) {
    char buffer[MAX_BUFFER_SIZE];
    int userid;

    // 接收客户端发送的userid
    ssize_t recvSize = recv(context->sockfd, &userid, sizeof(int), 0);
    if (recvSize == -1) {
        perror('userid接受失败\nrecv');
        return;
    }
    printf('客户端已连接\n');
    printf('userid:%d\n', userid);

    // 查询数据库获取设备状态信息
    char sql[100];
    snprintf(sql, sizeof(sql), 'SELECT * FROM Status WHERE uid = %d', userid);
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(context->db, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, '无法执行的语句: %s\n', sqlite3_errmsg(context->db));
        return;
    }

    Status status;
    memset(&status, 0, sizeof(Status)); // 清空status结构体
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        status.uid = sqlite3_column_int(stmt, 1);
        strncpy(status.device_name, sqlite3_column_text(stmt, 2), sizeof(status.device_name) - 1);
        strncpy(status.device_state, sqlite3_column_text(stmt, 3), sizeof(status.device_state) - 1);
        strncpy(status.value, sqlite3_column_text(stmt, 4), sizeof(status.value) - 1);
        strncpy(status.mode, sqlite3_column_text(stmt, 5), sizeof(status.mode) - 1);
    }
  
    if (sqlite3_column_count(stmt) == 0) {
        fprintf(stderr, '查询结果为空\n');
        return;
    }
    sqlite3_finalize(stmt);

    if (strlen(status.device_name) == 0) {
        perror('接收设备状态信息失败');
        return;
    }

    // 分析设备状态并生成建议
    char suggestion[MAX_BUFFER_SIZE];
    if (strcmp(status.device_name, '空调') == 0) {
        int temperature = atoi(status.value);
        if (temperature < 24) {
            snprintf(suggestion, sizeof(suggestion), '空调温度过低,建议提高温度至26℃');
        } else {
            snprintf(suggestion, sizeof(suggestion), '空调温度正常');
        }
    } else if (strcmp(status.device_name, '加湿器') == 0) {
        int humidity = atoi(status.value);
        if (humidity < 40 || humidity > 70) {
            snprintf(suggestion, sizeof(suggestion), '加湿器湿度过高或过低,建议调整加湿器湿度');
        } else {
            snprintf(suggestion, sizeof(suggestion), '加湿器湿度正常');
        }
    } else {
        snprintf(suggestion, sizeof(suggestion), '设备状态未知');
    }

    // 向客户端发送建议
    ssize_t sendSize = send(context->sockfd, suggestion, strlen(suggestion), 0);
    if (sendSize == -1) {
        perror('发送失败\nsend');
        return;
    }
    printf('%s\n',suggestion);
}

int main() {
    printf('正在连接中...\n');
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        perror('连接失败\nsocket');
        return 1;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(12345);

    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror('连接失败\nbind');
        close(sockfd);
        return 1;
    }

    if (listen(sockfd, 5) == -1) {
        perror('连接失败\nlisten');
        close(sockfd);
        return 1;
    }

    sqlite3 *db;
    int rc = sqlite3_open('/mnt/g/Qt/Client/Smarthome_Client/database/database.db', &db);
    if (rc != SQLITE_OK) {
        fprintf(stderr, '数据库打开失败: %s\n', sqlite3_errmsg(db));
        return 1;
    }

    ServerContext context;
    context.sockfd = sockfd;
    context.client_addr_len = sizeof(context.client_addr);
    context.db = db;

    fd_set readfds;
    int maxfd = sockfd;

    while (1) {
        FD_ZERO(&readfds);
        FD_SET(sockfd, &readfds);

        int activity = select(maxfd + 1, &readfds, NULL, NULL, NULL);
        if (activity == -1) {
            perror('select');
            break;
        }

        if (FD_ISSET(sockfd, &readfds)) {
            int clientSockfd = accept(sockfd, (struct sockaddr *)&context.client_addr, &context.client_addr_len);
            if (clientSockfd == -1) {
                perror('套接字接收失败\naccept');
                break;
            }

            context.sockfd = clientSockfd;
            handleClientRequest(&context);

            close(clientSockfd);
        }
    }
    sqlite3_close(db);
    close(sockfd);
    printf('服务器已关闭\n');

    return 0;
}

客户端代码

#include 'procession.h'
#include 'ui_procession.h'
#include <QAbstractSocket>
#include <QDebug>

Procession::Procession(int userid,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Procession),
    userid(userid)
{
    ui->setupUi(this);
    processionWidget();
    m_socket = new QTcpSocket(this);
    //connect(ui->connectBtn, &QPushButton::clicked, this, &Procession::on_connectBtn_clicked);
    connect(m_socket, &QTcpSocket::readyRead, this, &Procession::readyRead);
    connect(m_socket, &QTcpSocket::connected, this, &Procession::connected);
    connect(m_socket, &QTcpSocket::disconnected,this,&Procession::disconnected);
    connect(m_socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error), this, &Procession::displayError);
}

Procession::~Procession()
{
    delete ui;
}

void Procession::processionWidget()
{
    setWindowTitle('服务器通信');
    setAutoFillBackground(true);
    QPalette palette=this->palette();
    QPixmap pixmap(':user/image/image/net.jpg');
    palette.setBrush(QPalette::Window, QBrush(pixmap));
    setPalette(palette);
    setFixedSize(600,400);
}


void Procession::connected()
{
    ui->message->append('连接成功');
    // 发送userid给服务器
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out << qint32(this->userid); // 使用qint32类型发送userid
    if (m_socket->write(block) == -1) {
        ui->message->append('发送userid失败');
        m_socket->close();
    }
}

void Procession::disconnected()
{
    ui->message->append('连接失败');
    m_socket->close();
}

void Procession::readyRead()
{
    QByteArray data = m_socket->readAll();
    if (data.isEmpty()) {
        ui->message->append('读取的数据为空');
        return;
    }
    // 解析服务器返回的数据
    QDataStream in(&data, QIODevice::ReadOnly);
    QString suggestion;
    in >> suggestion; // 使用重载的 >> 运算符来读取数据
    if (in.status() != QDataStream::Ok) {
        ui->message->append('解析服务器返回的数据失败');
        return;
    }
    ui->message->append(suggestion);
    m_socket->close();
}

void Procession::displayError(QAbstractSocket::SocketError error)
{
    QString errorMessage;
    switch (error) {
    case QAbstractSocket::ConnectionRefusedError:
        errorMessage = '连接被拒绝';
        break;
    case QAbstractSocket::RemoteHostClosedError:
        errorMessage = '远程主机关闭';
        break;
    case QAbstractSocket::HostNotFoundError:
        errorMessage = '未找到主机';
        break;
    case QAbstractSocket::SocketTimeoutError:
        errorMessage = '连接超时';
        break;
    case QAbstractSocket::NetworkError:
        errorMessage = '网络错误';
        break;
    default:
        errorMessage = '未知错误';
        break;
    }
    ui->message->append('连接失败:' + errorMessage);
}

void Procession::on_connectBtn_clicked()
{
    if (m_socket->state() == QAbstractSocket::ConnectedState) {
            m_socket->disconnectFromHost(); // 先关闭套接字
            qDebug()<<'1';
        }

    ui->message->append('已经连接上服务器');
    QString ip = ui->IP->text();
    QString port = ui->port->text();

    if (ip.isEmpty() || port.isEmpty()) {
        ui->message->clear();
        ui->message->append('请输入有效的IP地址和端口号');
        return;
    }

    ui->message->clear();
    ui->message->append('正在连接中...');
    m_socket->connectToHost(ip, static_cast<quint16>(ui->port->text().toInt()));
}

代码说明

  • 服务器端使用C语言编写,监听指定端口,接收客户端连接请求。
  • 客户端使用Qt框架编写,连接服务器并发送userid。
  • 服务器端接收到userid后,查询SQLite数据库获取对应用户的设备状态信息。
  • 服务器端根据设备状态信息生成建议,并发送回客户端。
  • 客户端接收服务器端发送的建议并显示。

问题解决

  • 客户端发送的userid是123,但服务器接收到的是2063597568:
    • 问题出在客户端发送userid的代码上面,需要使用qint32类型发送userid。
    • 在服务器端的handleClientRequest()函数中,接收userid的代码也需要进行相应的修改,使用int类型接收userid。

总结

本文介绍了一个简单的智能家居状态监测与建议系统,展示了如何使用C语言、Socket编程和SQLite数据库实现基本的网络通信、数据存储和状态分析功能。开发者可以根据实际需求,扩展系统功能,例如添加更多类型的设备、实现更复杂的控制逻辑等。

C语言基于Socket和SQLite的智能家居状态监测与建议系统

原文地址: http://www.cveoy.top/t/topic/fCuv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录