极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12184|回复: 2

Arduino+W5100+8个BS18B20传感器做的温度检测数据上传到乐联网存在的问题

[复制链接]
发表于 2016-4-8 23:53:05 | 显示全部楼层 |阅读模式
我做了一个室内温度检测系统(检测室内的8个地点),因此用了Arduino+W5100+8个BS18B20温度传感器,并且把数据通过网线传到乐联网,使之能够看到实时数据。但是这个过程中存在一些没能解决的问题,希望能够得到大家的指点以解决问题。




先看例程:
/*
   lewei50 open platform sensor client
   This code is in the public domain.
  */

#include <LeweiClient.h>
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2   //使用Digital 2端口,其它端口也可以
OneWire  oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensor(&oneWire);

#define LW_USERKEY "6efb239e954c493a89436279171dc62a"
#define LW_GATEWAY "01"
int temp01=0;
  int temp02=0;
   int temp03=0;
    int temp04=0;
     int temp05=0;
      int temp06=0;
       int temp07=0;
        int temp08=0;

//delay between updates
#define POST_INTERVAL (30*1000)


//IPAddress ip(192,168,1, 15);
//IPAddress mydns(8,8,8,8);
//IPAddress gw(192,168,1,1);
//IPAddress subnet(255,255,255,0);

LeWeiClient *lwc;


void setup() {
   // start serial port:
   Serial.begin(9600);
   // hope no exception here
    tempSensor.begin();
   lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY);
   //lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY,ip,mydns,gw,subnet);
   
}

void loop(void) {
   // read the analog sensor:
   //int sensorReading = analogRead(A0);  

  // if there's incoming data from the net connection.
   // send it out the serial port.  This is for debugging
   // purposes only:

  if (lwc) {
   
    tempSensor.requestTemperatures(); // 发送命令获取温度
    temp01=tempSensor.getTempCByIndex(1);
    temp02=tempSensor.getTempCByIndex(2);
    temp03=tempSensor.getTempCByIndex(3);
    temp04=tempSensor.getTempCByIndex(4);
    temp05=tempSensor.getTempCByIndex(5);
    temp06=tempSensor.getTempCByIndex(6);
    temp07=tempSensor.getTempCByIndex(7);
    temp08=tempSensor.getTempCByIndex(8);
    //t1,t2.. must using the same name setting on web server.
    lwc->append("01",temp01);
    Serial.print(";");
    lwc->append("02", temp02);
    Serial.print(";");
    lwc->append("03", temp03);
    Serial.print(";");
    lwc->append("04", temp04);
    Serial.print(";");
    lwc->append("05", temp05);
    Serial.print(";");
    lwc->append("06", temp06);
    Serial.print(";");
    lwc->append("07", temp07);
    Serial.print(";");
    lwc->append("08",temp08);
    Serial.println();
    lwc->send();
    //Grammar changed by Wei&Anonymous ;)

   delay(1000);
   }
}

问题一:  8个传感器的温度值都能够在乐联网显示,但是显示的值都是整数的,怎么样才能够显示两位小数的温度值呢?是不是这个例程有问题?(做其他的实验, DS18B20传感器的数据都是两位小数的)


问题二:8个DS18B20传感器在乐联网上显示的值与传感器无法一一对应,不能区分哪一个值是哪一个传感器的,这个问题怎么解决呢??
问题三:窗口为什么是这样显示的?

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2016-4-9 01:50:54 | 显示全部楼层
int temp01=0;
  int temp02=0;
   int temp03=0;
    int temp04=0;
     int temp05=0;
      int temp06=0;
       int temp07=0;
        int temp08=0;
你的变量是整形。。。你让它如何是浮点数?
另外,一般的变量定义 放在函数之外,变量有一个作用域的概念,你这样是局部变量,程序运行会反复的分配空间,释放空间,请知悉!
回复 支持 反对

使用道具 举报

发表于 2016-4-9 01:57:26 | 显示全部楼层
本帖最后由 darkorigin 于 2016-4-9 01:59 编辑

还有个优化方案,用浮点数组来存储温度值
定义部分: float temp[7];


采集部分:  for(int i=1;i<=8;i++) temp=tempSensor.getTempCByIndex(i);

输出部分
for(int i=1;i<=8;i++)lwc->append(i,temp);
这样会看着简单很多,代码冗余容易造成编译出来的程序变大,降低效率,同时占用单片机宝贵的程序存储器。
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-5-20 13:16 , Processed in 0.069616 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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