请选择 进入手机版 | 继续访问电脑版
凡是官网购买产品,均可以获得积分,(积分可以用来下载技术资料)还可以获得精致技术支持。零知实验室可提供全套电子方案定制服务,了解详情请咨询客服。           
查看: 3057|回复: 4

零知开源-室内温湿度监控系统1-本地进行数据显示

[复制链接]
发表于 2018-7-4 14:05:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
1、我们首先需要获取温湿度数据,我们将温湿度模块和零知开发板按照下面电路连接:
2.png
在零知开发工具中新建项目并编写如下代码:
[C++] 纯文本查看 复制代码
/**
*    文件: 室内温湿度监控系统.ino      by 零知实验室([url=http://www.lingzhilab.com]www.lingzhilab.com[/url])
*    -- 零知开源,让电子制作变得更简单! --
*    时间: 2018/07/04 11:01
*    说明: 使用零知开发板获取室内温湿度数据经过wifi上传到APP显示
**/

#include "HTU3X.h" //加入SHT3X的库

//定义连接模块的各个引脚


//SHT3X实例
HTU3X myHumidity;
//存放获取的温湿度数据
float sht3x_humd, sht3x_temp;

//获取sht3x的数据
void SHT3X_get_sample(float *humd, float *temp);

void setup() {
    //打开串口,方便在串口中显示
        Serial.begin(9600);
        Serial.println("program start!");

        //开启SHT3X
        myHumidity.begin();
}

void loop() {
    
        SHT3X_get_sample(&sht3x_humd, &sht3x_temp);
        
        delay(500);
}

void SHT3X_get_sample(float *humd, float *temp)
{
        myHumidity.readTempAndHumi(temp, humd);
        
        //在串口中显示
        Serial.print(" 温度:");
        Serial.print(*temp, 1);
        Serial.print(" °C");
        Serial.print(" 湿度:"); 
        Serial.print(*humd, 1);
        Serial.print("%");
        Serial.println();
}


我们把代码编译后上传到开发板中验证一下是否是正确的结果,上传后打开串口监视器得到如下结果:
145014sflkl78tylptfyp9.png
2、现在我们已经得到正确的温湿度数据了,下面我们把数据显示到OLED模块上,按照我们之前零知快速入门系列的OLED模块使用中进行连接硬件(这里我们的A5,A4两个接口被SHT3X占用了,所以我们把OLED的换成2,3脚),然后我们编写如下代码:

[C++] 纯文本查看 复制代码
/**
*    文件: 室内温湿度监控系统.ino      by 零知实验室([url=http://www.lingzhilab.com]www.lingzhilab.com[/url])
*    -- 零知开源,让电子制作变得更简单! --
*    时间: 2018/07/04 11:01
*    说明: 使用零知开发板获取室内温湿度数据经过wifi上传到APP显示
**/

#include "HTU3X.h" //加入SHT3X的库
#include "oled_demo.h"//OLED的库

//定义连接OLED模块的各个引脚
#define OLED_DC 1
#define OLED_RST 0
#define OLED_SCL 2
#define OLED_SDA 3

//OLED需要显示的内容
unsigned char  oled_str_head[] =                    
{           
0x00,0x00,0x80,0x80,0x80,0x80,0xC0,0xFC,0xF8,0xC0,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x41,0x39,0x1F,0x1F,0x0F,0x0F,0x1F,0x1F,0x71,0x01,0x00,0x00,0x00,/*"★"*/ 
0x00,0x00,0x80,0x80,0x80,0x80,0x60,0x1C,0x38,0x40,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x41,0x3E,0x22,0x10,0x08,0x08,0x10,0x2E,0x72,0x01,0x00,0x00,0x00,/*"☆"*/

0x00,0x00,0xC0,0x30,0x90,0xF0,0x54,0xFC,0x92,0x7A,0x08,0x08,0x28,0x10,0x00,0x00,
0x00,0x00,0x10,0x08,0x04,0x1A,0x09,0x36,0x6A,0x19,0x02,0x04,0x08,0x08,0x00,0x00,/*"零",0*/

0x00,0x00,0x00,0x00,0x3C,0xA0,0x50,0x30,0x00,0x80,0x00,0x80,0x80,0x40,0x80,0x00,
0x00,0x00,0x04,0x22,0x13,0x0E,0x09,0x09,0x01,0x03,0x0D,0x08,0x08,0x07,0x00,0x00,/*"知",1*/

0x00,0x00,0x00,0x00,0x80,0xE8,0x48,0x28,0x18,0x1C,0xE4,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x02,0x03,0x22,0x1E,0x01,0x01,0x01,0x01,0x7F,0x01,0x01,0x00,0x00,0x00,/*"开",2*/

0x00,0x00,0x40,0x84,0x18,0x08,0xC0,0xB8,0x48,0x34,0xC4,0xA4,0xE0,0x00,0x00,0x00,
0x00,0x00,0x1F,0x7E,0x1A,0x06,0x31,0x13,0x25,0x43,0x3F,0x0B,0x10,0x30,0x00,0x00,/*"源",3*/

0x00,0x00,0x80,0x80,0x80,0x80,0x60,0x1C,0x38,0x40,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x41,0x3E,0x22,0x10,0x08,0x08,0x10,0x2E,0x72,0x01,0x00,0x00,0x00,/*"☆"*/        
0x00,0x00,0x80,0x80,0x80,0x80,0xC0,0xFC,0xF8,0xC0,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x41,0x39,0x1F,0x1F,0x0F,0x0F,0x1F,0x1F,0x71,0x01,0x00,0x00,0x00,/*"★"*/ 

};

uint8_t oled_str_webaddr[] = "www.lingzhilab.com";

unsigned char oled_str_hutemp[] = 
{
0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00,/*"温",0*/

0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F,0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00,/*"湿",1*/

0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00,
0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00,/*"度",2*/

0x06,0x09,0x09,0xE6,0xF8,0x0C,0x04,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00,
0x00,0x00,0x00,0x07,0x1F,0x30,0x20,0x40,0x40,0x40,0x40,0x40,0x20,0x10,0x00,0x00,/*"℃",0*/
};

//SHT3X实例化
HTU3X myHumidity;
//存放获取的温湿度数据
float sht3x_humd, sht3x_temp;

//oled实例化
OLED_DEMO myoled(OLED_SDA, OLED_SCL, OLED_DC, OLED_RST);

//获取sht3x的数据
void SHT3X_get_sample(float *humd, float *temp);

//在OLED显示温湿度数据
void oled_show_sample(float humd, float temp);

void setup() {
    //打开串口,方便在串口中显示
        Serial.begin(9600);
        Serial.println("program start!");

        //开启SHT3X
        myHumidity.begin();
        
        //打开OLED
        myoled.begin();
        
        //显示标题"零知开源"
        for(int i=0; i<8; i++){
                myoled.print_p16x16(i, 0, oled_str_head);//显示在第0行的第0-8列,这里一共可以显示4行
        }
        //显示网址[url=http://www.lingzhilab.com]www.lingzhilab.com[/url]
        myoled.print_p6x8(16,7,oled_str_webaddr);
        
}

void loop() {
    
        //获取温湿度
        SHT3X_get_sample(&sht3x_humd, &sht3x_temp);
        
        //数据显示到OLED
        oled_show_sample(sht3x_humd, sht3x_temp);
        
        
        delay(500);
}

void SHT3X_get_sample(float *humd, float *temp)
{
        myHumidity.readTempAndHumi(temp, humd);
        
        //在串口中显示
        Serial.print(" 温度:");
        Serial.print(*temp, 1);
        Serial.print(" °C");
        Serial.print(" 湿度:"); 
        Serial.print(*humd, 1);
        Serial.print("%");
        Serial.println();
}

//我们的温湿度都是1位小数,并且都是 xx.x 的格式
//这个函数把一个1位的小数转换成字符串数组,便于显示,其中最高位放在第一个下标中
//例如:32.1 存放到数组中为:{'3','2','1'};
void float_to_int_array(float f, unsigned char *arr)
{
        unsigned int intdata = f*10;//转换为整数
        
        //分别取出每一位
        unsigned char d0 = intdata%10;
        unsigned char d1 = (intdata%100)/10;
        unsigned char d2 = intdata/100;
        
        //转换为数字的ASCII码
        arr[0] = d2 + '0';
        arr[1] = d1 + '0';
        arr[2] = d0 + '0';
}

void oled_show_sample(float humd, float temp)
{        
        //湿度信息显示,这里主要是位置需要调整
        unsigned char arr_str_hum[3];
        unsigned char arr_str_tem[3];
        float_to_int_array(humd, arr_str_hum);
        unsigned char str1[2] = {arr_str_hum[0], '\0'};
        unsigned char str2[2] = {arr_str_hum[1], '\0'};
        unsigned char str3[2] = {arr_str_hum[2], '\0'};
        unsigned char str4[2] = ".";
        unsigned char str5[2] = ":";
        unsigned char str6[2] = "%";
        myoled.print_p8x16(50, 4, str5);
        myoled.print_p8x16(60, 4, str1);
        myoled.print_p8x16(70, 4, str2);
        myoled.print_p8x16(78, 4, str4);
        myoled.print_p8x16(86, 4, str3);
        myoled.print_p8x16(100, 4, str6);
        myoled.print_p16x16(1, 2, oled_str_hutemp, 1);
        myoled.print_p16x16(2, 2, oled_str_hutemp, 2);
        
        //温度信息
        float_to_int_array(temp, arr_str_tem);
        unsigned char str7[2] = {arr_str_tem[0], '\0'};
        unsigned char str8[2] = {arr_str_tem[1], '\0'};
        unsigned char str9[2] = {arr_str_tem[2], '\0'};
        myoled.print_p8x16(50, 2, str5);
        myoled.print_p8x16(60, 2, str7);
        myoled.print_p8x16(70, 2, str8);
        myoled.print_p8x16(78, 2, str4);
        myoled.print_p8x16(86, 2, str9);

        myoled.print_p16x16(1, 1, oled_str_hutemp, 0);
        myoled.print_p16x16(2, 1, oled_str_hutemp, 2);
        myoled.print_p16x16(6, 1, oled_str_hutemp, 3);
}


把上面代码进行编译后上传到开发板中,可以看到OLED已经可以显示温湿度的信息了:
5.jpg

本次我们完成了本地的显示,我们接下来把数据经过wifi传输到远程进行显示,本次的完整代码工程见附件(复制到软件库的project目录后直接打开工程即可)。

室内温湿度监控系统-v1.zip

8.44 KB, 下载次数: 25, 下载积分: 积分 -5

回复

使用道具 举报

发表于 2018-7-17 00:12:45 | 显示全部楼层
程序注释很详细
回复

使用道具 举报

发表于 2018-7-17 06:58:14 | 显示全部楼层
使用起来方便快捷,谢谢分享。
回复

使用道具 举报

发表于 2020-1-1 01:05:59 | 显示全部楼层
怎么查看库里面的底层代码?
回复

使用道具 举报

 楼主| 发表于 2020-1-2 09:35:25 | 显示全部楼层
18318202383 发表于 2020-1-1 01:05
怎么查看库里面的底层代码?

直接看源码文件,都是开放代码的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版| 小黑屋| 零知实验室 ( 粤ICP备17040594号-3 )

GMT+8, 2020-2-25 20:23 , Processed in 0.062902 second(s), 18 queries .

快速回复 返回顶部 返回列表