BrickUp API Service for Docker version.

Hight_Temp.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. High_Temp.cpp
  3. 2014 Copyright (c) Seeed Technology Inc. All right reserved.
  4. Loovee
  5. 2013-4-14
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <Arduino.h>
  19. #include "High_Temp.h"
  20. const float VOL_OFFSET = 350; // offset voltage, mv
  21. const float AMP_AV = 54.16; // Av of amplifier
  22. const float Var_VtoT_K[3][10] =
  23. {
  24. {0, 2.5173462e1, -1.1662878, -1.0833638, -8.9773540/1e1, -3.7342377/1e1,
  25. -8.6632643/1e2, -1.0450598/1e2, -5.1920577/1e4},
  26. {0, 2.508355e1, 7.860106/1e2, -2.503131/1e1, 8.315270/1e2,
  27. -1.228034/1e2, 9.804036/1e4, -4.413030/1e5, 1.057734/1e6, -1.052755/1e8},
  28. {-1.318058e2, 4.830222e1, -1.646031, 5.464731/1e2, -9.650715/1e4,
  29. 8.802193/1e6, -3.110810/1e8}
  30. };
  31. HighTemp::HighTemp(int _pinTmp, int _pinThmc)
  32. {
  33. pinRoomTmp = _pinTmp;
  34. pinThmc = _pinThmc;
  35. }
  36. void HighTemp::begin()
  37. {
  38. tempRoom = getRoomTmp();
  39. Serial.print("tempRoom = ");
  40. Serial.println(tempRoom);
  41. delay(10);
  42. Serial.print("pinRoomTmp = ");Serial.println(pinRoomTmp);
  43. delay(10);
  44. Serial.print("pinThmc = ");Serial.println(pinThmc);
  45. }
  46. float HighTemp::getThmc()
  47. {
  48. float vol = getThmcVol();
  49. tempThmc = K_VtoT(vol) + tempRoom;
  50. return tempThmc;
  51. }
  52. int HighTemp::getAnalog(int pin)
  53. {
  54. long sum = 0;
  55. for(int i=0; i<32; i++)
  56. {
  57. sum += analogRead(pin);
  58. }
  59. return ((sum>>5)); // 3.3V supply
  60. }
  61. float HighTemp::getRoomTmp()
  62. {
  63. int a = getAnalog(pinRoomTmp)*50/33; // 3.3V supply
  64. float resistance=(float)(1023-a)*10000/a; // get the resistance of the sensor;
  65. float temperature=1/(log(resistance/10000)/3975+1/298.15)-273.15; // convert to temperature via datasheet ;
  66. Serial.print("a = ");Serial.println(a);
  67. Serial.print("resistance = ");Serial.println(resistance);
  68. // Serial.print("temperature = ");Serial.println(temperature);
  69. tempRoom = temperature;
  70. return temperature;
  71. }
  72. float HighTemp::getThmcVol() // get voltage of thmc in mV
  73. {
  74. float vout = (float)getAnalog(pinThmc)/1023.0*5.0*1000;
  75. float vin = (vout - VOL_OFFSET)/AMP_AV;
  76. return (vin);
  77. }
  78. float HighTemp::K_VtoT(float mV)
  79. {
  80. int i = 0;
  81. float value = 0;
  82. if(mV >= -6.478 && mV < 0)
  83. {
  84. value = Var_VtoT_K[0][8];
  85. for(i = 8; i >0; i--)
  86. value = mV * value + Var_VtoT_K[0][i-1];
  87. }
  88. else if(mV >= 0 && mV < 20.644)
  89. {
  90. value = Var_VtoT_K[1][9];
  91. for(i = 9; i >0; i--)
  92. {
  93. value = mV * value + Var_VtoT_K[1][i-1];
  94. }
  95. }
  96. else if(mV >= 20.644 && mV <= 54.900)
  97. {
  98. value = Var_VtoT_K[2][6];
  99. for(i = 6; i >0; i--)
  100. value = mV * value + Var_VtoT_K[2][i-1];
  101. }
  102. return value;
  103. }