由一个属性设置导致的一系列问题排查

作者:郑楠

前言

  今天解决了一个历史遗留问题,真的是敲开心!涉及的的部分项目内容是:利用 Arduino Yun 作为上传介质将气象站传感器传回的数据经过处理之后上传至 PTC-ThingWorx 物联网云平台。
  但是经过我反复调试,都会遇到一个问题:在我切换成新的高精度传感器之后,数据上传出现断续的情况。而我Arduino程序里设置的延时是10秒一次上传。

Arduino 代码

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Bridge.h>
#include <HttpClient.h>
#include <Process.h>

/*
Copyright (c) 2015 PTC Inc. 
Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

This sketch demonstrate how to send data through REST Calls using linux processes
 on an Arduino Yún. 
 created 25 February 2015
 by Veronica MIHAI IoT Application Engineer at PTC (vmihai@ptc.com)
 */

#define DEBUG

// how offen will the data read from your sensor be sent to a ThingWorx Server 
int timeBetweenRefresh = 10000;
// the ThingWorx Server domain name to which you want to send data to 
String TWServer = "保密";
//Value of the port on which your ThingWorx Server is listening
// It's 80 if you are using http and 443 if you are using https
String TWPort="保密";
//The value for an Application Key used to login on the platform 
// without using the credentials
//To learn how to create it check the PDF : 2.How to Read Temperature and Humidity from DHT21 Sensor Using Rest Calls in C  for Arduino Yun
//from the Weather App with Arduino Yun tutorial
String appKey = "保密"; 
// this function is executed only once when you upload the sketch
// and it is responsible for initializing the variables you use throughout your program

SoftwareSerial mySerial(9, 10); // RX, TX
char                 databuffer[35];
double               temp;

void setup()
{
  Bridge.begin();
  Serial.begin(9600);
  mySerial.begin(9600);
}
void loop()
{ 
  getBuffer();                                                                      //Begin!
  Serial.print("Wind Direction: ");
  Serial.print(WindDirection());
  Serial.println("  ");
  Serial.print("Average Wind Speed (One Minute): ");
  Serial.print(WindSpeedAverage());
  Serial.println("m/s  ");
  Serial.print("Max Wind Speed (Five Minutes): ");
  Serial.print(WindSpeedMax());
  Serial.println("m/s");
  Serial.print("Rain Fall (One Hour): ");
  Serial.print(RainfallOneHour());
  Serial.println("mm  ");
  Serial.print("Rain Fall (24 Hour): ");
  Serial.print(RainfallOneDay());
  Serial.println("mm");
  float temp = Temperature();
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.println("C  ");
  float humi = Humidity();
  Serial.print("Humidity: ");
  Serial.print(humi);
  Serial.println("%  ");
  float pressure = BarPressure();
  Serial.print("Barometric Pressure: ");
  Serial.print(pressure);
  Serial.println("hPa");
  Serial.println("");
  Serial.println("");
  if(temp>0&&temp<100&&humi>0&&humi<100&&pressure>0){
    for(int i = 0; i < 35; i++){
     Serial.print(databuffer[i]);
    }
    Serial.println("");
    runShellCommand(temp,humi);
    delay(timeBetweenRefresh);
  }
}

void getBuffer()                                                                    //Get weather status data
{
  int index;
  for (index = 0;index < 35;index ++)
  {
    if(mySerial.available())
    {
      databuffer[index] = mySerial.read();
      if (databuffer[0] != 'c')
      {
        index = -1;
      }
    }
    else
    {
      index --;
    }
  }
}

int transCharToInt(char *_buffer,int _start,int _stop)                               //char to int)
{
  int _index;
  int result = 0;
  int num = _stop - _start + 1;
  int _temp[num];
  for (_index = _start;_index <= _stop;_index ++)
  {
    _temp[_index - _start] = _buffer[_index] - '0';
    result = 10*result + _temp[_index - _start];
  }
  return result;
}

int WindDirection()                                                                  //Wind Direction
{
  return transCharToInt(databuffer,1,3);
}

float WindSpeedAverage()                                                             //air Speed (1 minute)
{
  temp = 0.44704 * transCharToInt(databuffer,5,7);
  return temp;
}

float WindSpeedMax()                                                                 //Max air speed (5 minutes)
{
  temp = 0.44704 * transCharToInt(databuffer,9,11);
  return temp;
}

float Temperature()                                                                  //Temperature ("C")
{
  temp = (transCharToInt(databuffer,13,15) - 32.00) * 5.00 / 9.00;
  return temp;
}

float RainfallOneHour()                                                              //Rainfall (1 hour)
{
  temp = transCharToInt(databuffer,17,19) * 25.40 * 0.01;
  return temp;
}

float RainfallOneDay()                                                               //Rainfall (24 hours)
{
  temp = transCharToInt(databuffer,21,23) * 25.40 * 0.01;
  return temp;
}

int Humidity()                                                                       //Humidity
{
  return transCharToInt(databuffer,25,26);
}

float BarPressure()                                                                  //Barometric Pressure
{
  temp = transCharToInt(databuffer,28,32);
  return temp / 10.00;
}

void runShellCommand(float t, float h){

  //create a process variable that will send the two Shell commands 
  //for setting the temperature and humidity properties to Thingworx 

 // transform float value of temperature into a String so that it can be concatenated 
 // in the setTemperature command 

  char temperature[10];
  String tempAsString;
  dtostrf(t,1,2,temperature);
  tempAsString=String(temperature); 

  char humidity[10];
  String humidityAsString;
  dtostrf(h,1,2,humidity);
  humidityAsString=String(humidity);

  String setData("curl "
  "\"http://"+TWServer+":"+TWPort+"/Thingworx/Things/DHT21Thing/Properties/*?appKey="+appKey+"\" "
  "-X PUT "
  "-i -H \"Content-Type:application/json\" "
  "-H \"Accept:application/json\" "
  "-d '{\"Temperature\" :" + tempAsString + ", \"Humidity\" :" + humidityAsString + "}' "
  );
  Serial.println("Set Data: "+ setData);

 // Leverage Yun Linux (curl)
  Process process;

  // run the curl command with the REST call that was build in the setTemperature String variable
  process.runShellCommand(setData);

  mySerial.flush();
 // transform float value of humidity into a String so that it can be concatenated 
 // in the setHumidity command 
}
*/

实物图

ArduinoYun

ArduinoYun

气象站传感器转接板

气象站传感器转接板

平台数据

平台数据

  很明显,我们可以看出数据的间隔不是10秒。
  非常奇怪的是,在更换这套新的传感器系统之前,数据是可以照常更新的,而且速度也是10秒一次。
  从问题提出至今,上上下载我把这套硬软件系统各个部分都怀疑了一次。

1. 排查硬件问题

1.1 ArduinoYun

  直接利用Putty软件进入 ArduinoYun 的 Linux 系统使用 curl 工具指定数据上传。
  Putty上传
  很明显,数据马上就传上去了,非常及时。

1.2 传感器系统

  进入 ArduinoYun 的串口调试工具,发现传感器的数据正常回传,而且传感器上TX的灯频闪的速度也和spec中一致,因此传感器系统不存在问题。

  看来硬件上是没有问题了,那问题会不会出在软件上呢?

2. 排查软件问题

2.1 Arduino 代码

  通过分析上文的代码,我发现如果网络连接不上,进程会一直死在那里,虽然代码里对于每一个进程都单独创建了一个进程,但是这样累积下来会让系统爆掉。因此我怀疑是程序问题。我在 Github 上找到了一个同样利用 ArduinoYun 的 linux 系统的 curl 工具上传的代码。

Github样例

// Original:
// http://hypernephelist.com/2014/08/19/https_on_arduino_yun.html

// Libraries
#include <Process.h>

// Literals
// #define DEBUG

// Constants
const char *PARSE_API = "api.parse.com";
const char *PARSE_APP = "_YOUR_PARSE_APP_";
const char *PARSE_CLASS = "Temperature";
const char *PARSE_KEY = "_YOUR_PARSE_REST_KEY_";
const char *PARSE_VERSION = "1";

// Counter
int counter = 0;

// Leverage Yun Linux (curl)
Process process;

// Buffer for parameters
char buffer[80];

// Setup
void setup() 
{
  // Bridge communication
  Bridge.begin();

  // Console debugging
  // Wait for console
  #ifdef DEBUG
    Console.begin();  
    while( !Console ) {;}
  #endif
}

// Loop
void loop() 
{
  // Increment counter
  counter = counter + 1;

  // Put value in data store
  request( counter );
  wait();
  response();

  // Wait for next sample
  delay( 5000 );
}

// Send the data to Parse.com
void request( int value )
{
  // Curl request per Parse.com documentation
  /*
  curl -X POST \
  -H "X-Parse-Application-Id: APPLICATION_ID" \
  -H "X-Parse-REST-API-Key: REST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"reading":123}' \
  https://api.parse.com/1/classes/Temperature  
  */  

  // Build curl command line
  // Includes HTTPS support
  // POST
  // JSON
  process.begin( "curl" );
  process.addParameter( "-k" );
  process.addParameter( "-X" );
  process.addParameter( "POST" );
  process.addParameter( "-H" );
  process.addParameter( "Content-Type:application/json" );

  // Parse.com application
  process.addParameter( "-H" );
  sprintf( buffer, "X-Parse-Application-Id: %s", PARSE_APP );
  process.addParameter( buffer );

  // Parse.com key
  process.addParameter( "-H" );
  sprintf( buffer, "X-Parse-REST-API-Key: %s", PARSE_KEY );
  process.addParameter( buffer );  

  // JSON body
  process.addParameter( "-d" );
  sprintf( buffer, "{\"reading\": %d}", value );
  process.addParameter( buffer );

  // URI
  sprintf( buffer, "https://%s/%s/classes/%s", PARSE_API, PARSE_VERSION, PARSE_CLASS );
  process.addParameter( buffer );  

  // Run the command 
  // Synchronous
  process.run();
}

// Response from Parse.com
void response()
{
  bool print = true;
  char c;

  // While there is data to read
  while( process.available() ) 
  {
    // Get character
    c = process.read();

    // Debugging
    #ifdef DEBUG 
      // Print until newline
      if( c == '\n' )
      {
        print = false;
      }      

      // Not newline yet
      // Print to console
      if( print )
      {
        Console.print( c );
      }
    #endif
  }

  // Debugging
  #ifdef DEBUG
    // Newline for console
    Console.println();
  #endif
}

// Wait for a response from Parse.com
void wait()
{
  // Periodically check curl process
  while( !process.available() ) 
  {
    delay( 100 );
  }
}

  上面等待每一个进程结束才进行下一次查询,防止了进程过多爆掉的情况。改改上面的代码,再加上 curl 工具的 -m 超时设置,开始测试。但测试结果还是一样,数据没有上传上去。

解决

  最终结果用我们导师的一句话来说就是:成功是概率问题。
  昨天我在细数这套系统还有哪个地方没有找过茬的时候,下意识地点开了 ThingWorx 平台,突然一个想法冒了出来:是不是属性设置问题? 因为以前在看说明书的时候隐约有提到过平台自带数据纠错的功能。 点开属性设置的 “?” 后阅读了说明。咦,好像是那么一回事。不管了,死马当活马医吧。继续测试,我把属性的 “Data Change type” 改成了 “Always”。 啊~~成功啦。
  至于理由嘛,我就直接引用我在 ThingWorx Community 中的回复,E文不好求轻喷。  

Thanks for your help.
1. “a while” means “sometimes a half hour, sometimes five minutes”. I am not sure.
2. The 1Amper charger still displays the same behavior.
3. I’ve found the problem. In the Thing’s Properties, There is a attributes which called “Data Change Info”. Set the “Data Change type” to “Always”. Because my data always the same as before, and the default of the “Data Change type” is “Values 0”, so the same data change event can’t be fired.

最终测试

  两次都使用putty进linux系统实验
  
这里写图片描述
  这个是服务器最新的数据

这里写图片描述
  上传完毕 没做改变

这里写图片描述
  第二次上传 湿度多了个.1 马上就有数据了

后记

  我发现在 ArduinoYun 上的 curl 工具的 -m 参数貌似不怎么管用,放进去反而没数据了,我把这段删去了,现在这问题还未解决。就目前情况而言,删掉没出什么问题。

最终代码

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Bridge.h>
#include <HttpClient.h>
#include <Process.h>

/*
Copyright (c) 2015 PTC Inc. 
Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

This sketch demonstrate how to send data through REST Calls using linux processes
 on an Arduino Yún. 
 created 25 February 2015
 by Veronica MIHAI IoT Application Engineer at PTC (vmihai@ptc.com)
 */

#define DEBUG

// how offen will the data read from your sensor be sent to a ThingWorx Server 
int timeBetweenRefresh = 10000;
// the ThingWorx Server domain name to which you want to send data to 
String TWServer = "保密";
//Value of the port on which your ThingWorx Server is listening
// It's 80 if you are using http and 443 if you are using https
String TWPort="保密";
//The value for an Application Key used to login on the platform 
// without using the credentials
//To learn how to create it check the PDF : 2.How to Read Temperature and Humidity from DHT21 Sensor Using Rest Calls in C  for Arduino Yun
//from the Weather App with Arduino Yun tutorial
String appKey = "保密"; 
// this function is executed only once when you upload the sketch
// and it is responsible for initializing the variables you use throughout your program

SoftwareSerial mySerial(9, 10); // RX, TX
char                 databuffer[35];
double               temp;

void setup()
{
  Bridge.begin();
  Serial.begin(9600);
  mySerial.begin(9600);
}
void loop()
{ 
  getBuffer();                                                                      //Begin!
  Serial.print("Wind Direction: ");
  Serial.print(WindDirection());
  Serial.println("  ");
  Serial.print("Average Wind Speed (One Minute): ");
  Serial.print(WindSpeedAverage());
  Serial.println("m/s  ");
  Serial.print("Max Wind Speed (Five Minutes): ");
  Serial.print(WindSpeedMax());
  Serial.println("m/s");
  Serial.print("Rain Fall (One Hour): ");
  Serial.print(RainfallOneHour());
  Serial.println("mm  ");
  Serial.print("Rain Fall (24 Hour): ");
  Serial.print(RainfallOneDay());
  Serial.println("mm");
  float temp = Temperature();
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.println("C  ");
  float humi = Humidity();
  Serial.print("Humidity: ");
  Serial.print(humi);
  Serial.println("%  ");
  float pressure = BarPressure();
  Serial.print("Barometric Pressure: ");
  Serial.print(pressure);
  Serial.println("hPa");
  Serial.println("");
  Serial.println("");
  if(temp>0&&temp<100&&humi>0&&humi<100&&pressure>0){
    for(int i = 0; i < 35; i++){
     Serial.print(databuffer[i]);
    }
    Serial.println("");
    runShellCommand(temp,humi);
    delay(timeBetweenRefresh);
  }
}

void getBuffer()                                                                    //Get weather status data
{
  int index;
  for (index = 0;index < 35;index ++)
  {
    if(mySerial.available())
    {
      databuffer[index] = mySerial.read();
      if (databuffer[0] != 'c')
      {
        index = -1;
      }
    }
    else
    {
      index --;
    }
  }
}

int transCharToInt(char *_buffer,int _start,int _stop)                               //char to int)
{
  int _index;
  int result = 0;
  int num = _stop - _start + 1;
  int _temp[num];
  for (_index = _start;_index <= _stop;_index ++)
  {
    _temp[_index - _start] = _buffer[_index] - '0';
    result = 10*result + _temp[_index - _start];
  }
  return result;
}

int WindDirection()                                                                  //Wind Direction
{
  return transCharToInt(databuffer,1,3);
}

float WindSpeedAverage()                                                             //air Speed (1 minute)
{
  temp = 0.44704 * transCharToInt(databuffer,5,7);
  return temp;
}

float WindSpeedMax()                                                                 //Max air speed (5 minutes)
{
  temp = 0.44704 * transCharToInt(databuffer,9,11);
  return temp;
}

float Temperature()                                                                  //Temperature ("C")
{
  temp = (transCharToInt(databuffer,13,15) - 32.00) * 5.00 / 9.00;
  return temp;
}

float RainfallOneHour()                                                              //Rainfall (1 hour)
{
  temp = transCharToInt(databuffer,17,19) * 25.40 * 0.01;
  return temp;
}

float RainfallOneDay()                                                               //Rainfall (24 hours)
{
  temp = transCharToInt(databuffer,21,23) * 25.40 * 0.01;
  return temp;
}

int Humidity()                                                                       //Humidity
{
  return transCharToInt(databuffer,25,26);
}

float BarPressure()                                                                  //Barometric Pressure
{
  temp = transCharToInt(databuffer,28,32);
  return temp / 10.00;
}

void runShellCommand(float t, float h){

  //create a process variable that will send the two Shell commands 
  //for setting the temperature and humidity properties to Thingworx 

 // transform float value of temperature into a String so that it can be concatenated 
 // in the setTemperature command 

  char temperature[10];
  String tempAsString;
  dtostrf(t,1,2,temperature);
  tempAsString=String(temperature); 

  char humidity[10];
  String humidityAsString;
  dtostrf(h,1,2,humidity);
  humidityAsString=String(humidity);

  String setData("curl "
  "\"http://"+TWServer+":"+TWPort+"/Thingworx/Things/DHT21Thing/Properties/*?appKey="+appKey+"\" "
  "-X PUT "
  "-i -H \"Content-Type:application/json\" "
  "-H \"Accept:application/json\" "
  "-d '{\"Temperature\" :" + tempAsString + ", \"Humidity\" :" + humidityAsString + "}' "
  );
  Serial.println("Set Data: "+ setData);

 // Leverage Yun Linux (curl)
  Process process;

  // run the curl command with the REST call that was build in the setTemperature String variable
  //wait();
  process.runShellCommand(setData);
  //response();

  mySerial.flush();
 // transform float value of humidity into a String so that it can be concatenated 
 // in the setHumidity command 
}

/*
// Wait for a response from Parse.com
void wait()
{
  // Periodically check curl process
  while( !process.available() ) {
    delay( 100 );
  }
}

// Response from Parse.com
void response()
{
  bool print = true;
  char c;

  // While there is data to read
  while( process.available() ) 
  {
    // Get character
    c = process.read();

    // Debugging
    #ifdef DEBUG 
      // Print until newline
      if( c == '\n' )
      {
        print = false;
      }      

      // Not newline yet
      // Print to console
      if( print )
      {
        Console.print( c );
      }
    #endif
  }

  // Debugging
  #ifdef DEBUG
    // Newline for console
    Console.println();
  #endif
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值