|
本帖最后由 laoliu1982 于 2013-3-3 10:14 编辑
说明:代码由@grissiom 提供
代码已经更新到 https://github.com/lewei50/leweiclient 可能与这里的不一致,以github上面为准
鉴于现在的所谓的物联网应用需要了解的知识相对复杂,一般爱好者不好上手,产生了做一个基于应用的简单框架的想法。利用arduino的硬件平台,把所有的传感器的读取,以太网数据的上传都进行了封装。
使用方法
1 导入库
实现代码如下:包含sht,AD PPD,后续会陆续增加传感器以及把反向控制加入到框架中
- #include <LeweiClient.h>
- #include <SPI.h>
- #include <Ethernet.h>
- #include <Wire.h> //BH1750 IIC Mode
- uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- #define LW_USERKEY "xxxxxxxxxxxxxxxxxxxxxxxx111"
- #define LW_GATEWAY "01"
- //delay between updates
- #define POST_INTERVAL (10*1000)
- LeWeiClient *lwc;
- const unsigned long postingInterval = 10*1000;
- LeWeiAnalogSensor the_UVSensor("UV", "UV_sensor", "UVsensor", 0);
- //LeWeiAnalogSensor the_MoistureSensor("MS", "Moisture_sensor", "MoistureSensor", 1);
- DHT11 the_dht11(2);
- LeWeiDHTHumSensor the_hum("HM", "Hum_sensor", "HumSensor", &the_dht11);
- LeWeiDHTTemprSensor the_tempr("TEMP", "Temp_sensor", "TempSensor", &the_dht11);
- LeWeiPPDSensor the_PM1Sensor("PM1", "PM_sensor", "PM1sensor", 8);
- LeWeiBH17xxSensor the_bh1750("BH", "light_sensor", "BH1750", 0x23);
- void setup() {
- Serial.begin(9600);
- if (Ethernet.begin(mac) == 0)
- {
- Serial.print("Failed to configure Ethernet using DHCP\r\n");
- }
- else
- {
- Serial.print("Ethernet configuration OK\r\n");
- }
- the_dht11.init();
- the_bh1750.init();
- // hope no exception here
- lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY);
- lwc->registerSensor(the_PM1Sensor);
- lwc->registerSensor(the_UVSensor);
- //lwc->registerSensor(the_MoistureSensor);
- lwc->registerSensor(the_hum);
- lwc->registerSensor(the_tempr);
- lwc->registerSensor(the_bh1750);
- }
- void loop() {
- if (lwc) {
- Serial.print("*** start data collection\r\n");
- lwc->scanDevices();
- Serial.print("*** start data send\r\n");
- lwc->send();
- /*
- * Serial.print("*** start log send\r\n");
- * lwc->sendLog("I'm alive.");
- *
- */
- delay(POST_INTERVAL);
- }
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|