------------------------------------------------------------------------------------------------------------------------------
交叉编译器:arm-linux-gcc-4.5.4
Linux内核版本:Linux-3.0
主机操作系统:Centos 6.5
开发板:FL2440
温度传感器:ds18b20
注:此程序的客户端是在装有ds18b20模块并有ds18b20驱动的系统上跑的,本人写的ds18b20的驱动
------------------------------------------------------------------------------------------------------------------------------
以下操作是建立在你有sqlite3数据库的情况下,sqlite3数据库的安装参考链接:http://www.linuxidc.com/Linux/2012-07/66854.htm
注:数据库安装好之后会在/home下自动创建一个sqlite-3.6.18的用户,其中保存了数据库sqlite3所有的执行文件,头文件还有库函数。
以下程序实现的功能是:客户端读取温度传感器的温度以及当时的时间,并向服务器发送数据。若连接服务器失败则保存数据到数据库。等待客户端可以连接上服务器的时候把保存的数据发送给服务器。
一、客户端代码
1 /********************************************************************************* 2 * Copyright: (C) 2016 2013dianxin_3 3 * All rights reserved. 4 * 5 * Filename: mclient.c 6 * Description: This file 7 * 8 * Version: 1.0.0(08/16/2016) 9 * Author: xiaohexiansheng <1392195453@qq.com> 10 * ChangeLog: 1, Release initial version on "08/16/2016 07:16:44 PM" 11 * 12 ********************************************************************************/ 13 #include <sys/types.h> 14 #include <sys/ioctl.h> 15 #include <termios.h> 16 #include <sys/stat.h> 17 #include <fcntl.h> 18 #include <sys/time.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #include <errno.h> 23 #include <sys/types.h> 24 #include <sys/socket.h> 25 #include <netinet/in.h> 26 #include <sqlite3.h> 27 28 #define MAXLINE 4096 29 char get_sql[4096]; 30 /******************************************************************************** 31 * Description: 32 * Input Args: 33 * Output Args: 34 * Return Value: 35 ********************************************************************************/ 36 double get_tem(void) //获取温度的函数 37 { 38 int fd; 39 double result = 0; 40 unsigned char buff[2]; 41 unsigned short temp = 0; 42 int flag = 0; 43 44 if ((fd=open("/dev/ds18b20",O_RDWR | O_NDELAY | O_NOCTTY)) < 0) 45 { 46 perror("open device ds18b20 failed. "); 47 exit(1); 48 } 49 50 printf("open device ds18b20 success. "); 51 52 read(fd, buff, sizeof(buff)); 53 temp=((unsigned short)buff[1])<<8; 54 temp|=(unsigned short)buff[0]; 55 result=0.0625*((double)temp); 56 close(fd); 57 printf("temperature is %4f ", result); 58 return result; 59 } 60 61 int save_tem(char *s_temp) //将数据保存到数据库 62 { 63 char sql[128]; 64 sqlite3 *db; 65 FILE *fd; 66 67 sqlite3_open("temperature.db", &db); 68 memset(sql, '