趁最近空闲点,有点时间,先把任务完成。实现传感器的数据互通。最终效果见顶部的视频。
最开始定的任务是包含温湿度数据的,但是OK527N开发板上并不带温湿度传感器,所以就用了cpu的温度作为参考,目前无法检测外部环境温度和湿度数据。
可以参考之前写的文章,验证了ADC功能,led功能,wifi功能、can功能、gpio功能、蜂鸣器功能、usb等设备。这里只挑选了一部分比较直观的功能在上位机中显示。
上位机界面:
程序见下面,主要是创建线程,创建tcp service,监听tcp client的连接,连接上后发送温度信息给客户端,客户端可以下发控制命令控制板子上的器件如led,蜂鸣器等。
有时间再研究下linux的can发送和接收。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<termios.h>
#include<errno.h>
#define PORT 8080
#define BUFFER_SIZE 1024
#define MAXLINE 256
extern int wifi_Connect(void);
static int stop = 0;
pthread_t read_id, write_id;
int server_fd, new_socket;
void beep_onoff(int sta)
{
if(sta == 1)
{
system("echo 8 > /sys/class/pwm/pwmchip0/export");
system("echo 10000000 > /sys/class/pwm/pwmchip0/pwm8/period");
system("echo 5000000 > /sys/class/pwm/pwmchip0/pwm8/duty_cycle");
system("echo 1 > /sys/class/pwm/pwmchip0/pwm8/enable");
}
else if(sta == 0)
{
system("echo 0 > /sys/class/pwm/pwmchip0/pwm8/enable");
system("echo 8 > /sys/class/pwm/pwmchip0/unexport");
}
}
void led_onoff(int sta)
{
if(sta == 1)
{
system("echo none > /sys/class/leds/heartbeat/trigger");
system("echo 1 > /sys/class/leds/heartbeat/brightness");
}
else if(sta == 0)
{
system("echo none > /sys/class/leds/heartbeat/trigger");
system("echo 0 > /sys/class/leds/heartbeat/brightness");
}
else if(sta == 2)
{
system("echo heartbeat > /sys/class/leds/heartbeat/trigger");
}
}
int read_CpuTemp(void)
{
int fd;
char buffer[1024];
int bytesRead;
fd = open("/sys/class/thermal/thermal_zone0/temp", O_RDONLY);
if (fd == -1) {
perror("Error opening file");
return EXIT_FAILURE;
}
bytesRead = read(fd, buffer, sizeof(buffer) - 1);
if (bytesRead == -1) {
perror("Error reading file");
close(fd);
return EXIT_FAILURE;
}
printf("%d bytes read)\n", bytesRead);
buffer[bytesRead] = '\0';
int temp = atoi(buffer);
return temp;
}
static void quit(int sig)
{
(void)sig;
stop = 1;
close(new_socket);
close(server_fd);
_exit(0);
}
void signal_pipe(int signo)
{
pthread_kill(read_id, SIGQUIT);
pthread_join(read_id, NULL);
close(new_socket);
printf("read pthread out \n");
pthread_exit(0);
}
void pthread_out(int signo)
{
pthread_exit(0);
}
void* read_func(void* arg)
{
char readbuff[MAXLINE];
int n = 0;
int fd;
fd = *(int*)arg;
memset(&readbuff,0,sizeof(readbuff));
signal(SIGQUIT,pthread_out);
while(1)
{
n = recv(fd, readbuff, MAXLINE, 0);
if(n > 0)
{
if(readbuff[0] == 0xA0 && readbuff[1] == 0x5A)
{
if(readbuff[2] == 0x33)
{
beep_onoff(readbuff[3]);
printf("beep sta:%d\n", readbuff[3]);
}
else if(readbuff[2] == 0x34)
{
led_onoff(readbuff[3]);
printf("led sta:%d\n", readbuff[3]);
}
}
}
};
}
void* write_func(void* arg)
{
char writebuff[MAXLINE];
int fd;
int cpu_Temp;
fd = *(int*)arg;
memset(&writebuff,0,sizeof(writebuff));
signal(SIGPIPE,signal_pipe);
while(1)
{
sleep(1);
cpu_Temp = read_CpuTemp();
printf("temp: %d\n", cpu_Temp);
send(fd, &cpu_Temp, sizeof(int),0);
}
}
int main(int argc, char *argv[])
{
int ret = 0;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char *hello = "Hello from server";
int cpu_Temp = 0;
ret = wifi_Connect();
if(ret != 0)
{
printf("Failed to connect to WiFi\n");
return -1;
}
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if(bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
printf("bind failed");
exit(EXIT_FAILURE);
}
if(listen(server_fd, 3) < 0)
{
printf("listen error");
exit(EXIT_FAILURE);
}
printf("creat tcp server succeeded\n");
signal(SIGINT, quit);
while(1)
{
if((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
{
printf("accept errror");
exit(EXIT_FAILURE);
}
printf("%d connect succeeded\n", new_socket);
if(pthread_create(&read_id,NULL,read_func,&new_socket))
{
printf("pthread_create read_func err\n");
}
if(pthread_create(&write_id,NULL,write_func,&new_socket))
{
printf("pthread_create write_func err\n");
}
pthread_join(write_id,NULL);
printf("write pthread out \n");
send(new_socket, hello, sizeof(hello), 0);
printf("Hello message sent to client\n");
send(new_socket, &cpu_Temp, sizeof(int), 0);
sleep(1);
}
return 0;
}