找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1258|回复: 0
打印 上一主题 下一主题
收起左侧

PM2.5源码

[复制链接]
跳转到指定楼层
楼主
ID:279688 发表于 2018-2-6 17:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <SoftwareSerial.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI
#define  mySerial Serial1
uint16_t pm25;
uint16_t pm10;
void pmRead() {
  uint8_t data[] = {0xAA, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x67, 0xBB};
  mySerial.write(data, 9);
  delay(100);
  for (int i = 0; i < 9; i++) {
    if (mySerial.available()) {
      data[i] = mySerial.read ();

    }
  }
  if (data[0] == 0xAA && data[8] == 0xBB) {
    pm25 = data[4] * 256 + data[5];
    pm10 = data[2] * 256 + data[3];


  }
}


void pmOn() {

  uint8_t data[] = {0xAA, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x66, 0xBB};
  mySerial.write(data, 9);
  delay(1000);


}

void pmOff() {
  uint8_t data[] = {0xAA, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x68, 0xBB};
  mySerial.write(data, 9);
}


void draw(void) {
  // graphic commands to redraw the complete screen should be placed here
  u8g.setFont(u8g_font_unifont);
  u8g.setPrintPos(0, 20);
  u8g.print("PM2.5 = ");
  u8g.print(pm25);

  u8g.setPrintPos(0, 60);
  u8g.print("PM10 = ");
  u8g.print(pm10);
}


void setup() {
  // put your setup code here, to run once:
  mySerial.begin(9600);
  Serial.begin(115200);
  pmOn();

delay(50);

}
void loop() {
  // put your main code here, to run repeatedly:
  pmRead();
  u8g.firstPage();
  do {
    draw();
  } while ( u8g.nextPage() );
  delay(50);
}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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