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

原创 2016年06月02日 09:13:21

作者

郑楠

前言

  今天解决了一个历史遗留问题,真的是敲开心!涉及的的部分项目内容是:利用 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
}
*/
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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

作者:郑楠前言  今天解决了一个历史遗留问题,真的是敲开心!涉及的的部分项目内容是:利用 Arduino Yun 作为上传介质将气象站传感器传回的数据经过处理之后上传至 PTC-ThingWorx 物...

一个C笔试题引出一系列的问题

一个C笔试题引出一系列的问题 下段代码的输出是什么?(这是我做过的一个C笔试题目,当时213了,没有给出完全正确的答案,现在来分析总结一下,防止下次再出现这种213的错误) int main(vo...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

由一个HADOOP_HOME and hadoop.home.dir are unset报错引起的window环境连接调试远程hadoop的一系列问题

这篇准确来说不是搭建hadoop开发环境,而是指windows10连接到centos下hadoop集群进行开发遇到的一系列问题,仅此而已

由一个HADOOP_HOME and hadoop.home.dir are unset报错引起的window环境连接调试远程hadoop的一系列问题,hadoop版本 2.8

一、结论 先说结论。最后问题解决了。终于能在windows的eclipse上通过执行wordcount类,然后将某个文档内容处理后,将结果传到远程服务器的hadoop的某个文件夹下了。 二、当时的...

解决Unity中的mUnityPlayer.quit()方法导致一系列问题

如果做过Unity并且与Android打过交道的人,相信被Unity某些特性弄得没有脾气,比如说有个mUnityPlayer.quit()方法,这个方法在UnityPlayerActivity类的on...

"把一个div标签,给它的属性设置position: fixed;bottom:0;固定在浏览器的最底部。 这个div标签在浏览器上下滚动的时候,会一直保持在屏幕最底部,这一点没有问题。但问题是当浏览

如果要接受从其他的窗口那里发过来的消息,就必须对窗口对象的message事件进行监控. window.addEventListener("message",function(){},false); ...

思考一个问题:如何将map里面的值,映射到一个对象里的属性,不通过手动方式设置!!!

待补充
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)