极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19864|回复: 3

基于应用的简单框架(sht,PPD42ns,ad,以太网),后续陆续补充传感器

[复制链接]
发表于 2013-2-9 12:29:54 | 显示全部楼层 |阅读模式
本帖最后由 laoliu1982 于 2013-3-3 10:14 编辑

说明:代码由@grissiom 提供

代码已经更新到 https://github.com/lewei50/leweiclient  可能与这里的不一致,以github上面为准


鉴于现在的所谓的物联网应用需要了解的知识相对复杂,一般爱好者不好上手,产生了做一个基于应用的简单框架的想法。利用arduino的硬件平台,把所有的传感器的读取,以太网数据的上传都进行了封装。

使用方法
1 导入库



实现代码如下:包含sht,AD PPD,后续会陆续增加传感器以及把反向控制加入到框架中

  1. #include <LeweiClient.h>
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <Wire.h> //BH1750 IIC Mode

  5. uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

  6. #define LW_USERKEY "xxxxxxxxxxxxxxxxxxxxxxxx111"
  7. #define LW_GATEWAY "01"

  8. //delay between updates
  9. #define POST_INTERVAL (10*1000)

  10. LeWeiClient *lwc;

  11. const unsigned long postingInterval = 10*1000;

  12. LeWeiAnalogSensor the_UVSensor("UV", "UV_sensor", "UVsensor", 0);
  13. //LeWeiAnalogSensor the_MoistureSensor("MS", "Moisture_sensor", "MoistureSensor", 1);

  14. DHT11 the_dht11(2);
  15. LeWeiDHTHumSensor the_hum("HM", "Hum_sensor", "HumSensor", &the_dht11);
  16. LeWeiDHTTemprSensor the_tempr("TEMP", "Temp_sensor", "TempSensor", &the_dht11);

  17. LeWeiPPDSensor the_PM1Sensor("PM1", "PM_sensor", "PM1sensor", 8);

  18. LeWeiBH17xxSensor the_bh1750("BH", "light_sensor", "BH1750", 0x23);

  19. void setup() {
  20.     Serial.begin(9600);
  21.     if (Ethernet.begin(mac) == 0)
  22.     {
  23.         Serial.print("Failed to configure Ethernet using DHCP\r\n");
  24.     }
  25.     else
  26.     {
  27.         Serial.print("Ethernet configuration OK\r\n");
  28.     }

  29.     the_dht11.init();
  30.     the_bh1750.init();

  31.     // hope no exception here
  32.     lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY);

  33.     lwc->registerSensor(the_PM1Sensor);
  34.     lwc->registerSensor(the_UVSensor);
  35.     //lwc->registerSensor(the_MoistureSensor);
  36.     lwc->registerSensor(the_hum);
  37.     lwc->registerSensor(the_tempr);
  38.     lwc->registerSensor(the_bh1750);
  39. }

  40. void loop() {
  41.     if (lwc) {
  42.         Serial.print("*** start data collection\r\n");
  43.         lwc->scanDevices();

  44.         Serial.print("*** start data send\r\n");
  45.         lwc->send();

  46. /*
  47. *        Serial.print("*** start log send\r\n");
  48. *        lwc->sendLog("I'm alive.");
  49. *
  50. */
  51.         delay(POST_INTERVAL);
  52.     }
  53. }

复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2013-2-10 15:47:01 | 显示全部楼层
代码封装的很好,结构简介 逻辑清楚 很好的例子 多谢楼主
回复 支持 反对

使用道具 举报

发表于 2013-2-10 21:30:56 | 显示全部楼层
谢谢楼主,很需要这个!
回复 支持 反对

使用道具 举报

发表于 2015-12-19 11:24:59 | 显示全部楼层
不错的代码,写的很好
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-7 13:56 , Processed in 0.083316 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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