C1sc0

max31865

Aug 24th, 2018 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. /***************************************************
  2. This is a library for the Adafruit PT100/P1000 RTD Sensor w/MAX31865
  3.  
  4. Designed specifically to work with the Adafruit RTD Sensor
  5. ----> https://www.adafruit.com/products/3328
  6.  
  7. This sensor uses SPI to communicate, 4 pins are required to
  8. interface
  9. Adafruit invests time and resources providing this open source code,
  10. please support Adafruit and open-source hardware by purchasing
  11. products from Adafruit!
  12.  
  13. Written by Limor Fried/Ladyada for Adafruit Industries.
  14. BSD license, all text above must be included in any redistribution
  15. ****************************************************/
  16.  
  17. #include <Adafruit_MAX31865.h>
  18. #include <ESP8266WebServer.h>
  19. #include <ESP8266WiFi.h>
  20.  
  21.  
  22. // Use software SPI: CS, DI, DO, CLK
  23. //NodeMCU pins connected to: D0, D1, D2, D3
  24. //Adafruit_MAX31865 max1 = Adafruit_MAX31865(16, 5, 4, 14);
  25. // use hardware SPI, just pass in the CS pin
  26. Adafruit_MAX31865 max1 = Adafruit_MAX31865(5);
  27. Adafruit_MAX31865 max2 = Adafruit_MAX31865(4);
  28. Adafruit_MAX31865 max3 = Adafruit_MAX31865(15);
  29. // The value of the Rref resistor. Use 430.0!
  30. #define RREF 400.0
  31. // WIFI constants
  32. const char* ssid = "WTK_KL";
  33. const char* password = "";
  34.  
  35. // Create an instance of the server
  36. // specify the port to listen on as an argument
  37. ESP8266WebServer server(80);
  38. float temp1;
  39. float temp2;
  40. float temp3;
  41.  
  42. void setup() {
  43. Serial.begin(115200);
  44. Serial.println("Adafruit MAX31865 PT100 Sensor Test!");
  45. max1.begin(MAX31865_2WIRE); // set to 2WIRE or 4WIRE as necessary
  46. max2.begin(MAX31865_2WIRE);
  47. max3.begin(MAX31865_2WIRE);
  48. delay(500);
  49. // Connect to WiFi network
  50. Serial.println();
  51. Serial.println();
  52. Serial.print("Connecting to ");
  53. Serial.println(ssid);
  54.  
  55. WiFi.begin(ssid, password);
  56.  
  57. while (WiFi.status() != WL_CONNECTED) {
  58. delay(500);
  59. Serial.print(".");
  60. }
  61. Serial.println("");
  62. Serial.println("WiFi connected");
  63. Serial.print("MAC: ");
  64. Serial.println(WiFi.macAddress());
  65. // Start the server
  66. server.begin();
  67. Serial.println("Server started");
  68.  
  69. // Print the IP address
  70. Serial.println(WiFi.localIP());
  71. server.on("/", []() {
  72. String responseData = "[";
  73. uint16_t rtd = max1.readRTD();
  74.  
  75. Serial.print("RTD value: "); Serial.println(rtd);
  76. float ratio = rtd;
  77. ratio /= 32768;
  78. //Serial.print("Ratio = "); Serial.println(ratio,8);
  79. //Serial.print("Resistance = "); Serial.println(RREF*ratio,8);
  80. //Serial.print("Temperature = ");
  81. temp1 = max1.temperature(100, RREF);
  82. Serial.println(temp1);
  83. HandleFault(max1);
  84. delay(500);
  85. rtd = max2.readRTD();
  86.  
  87.  
  88. ratio = rtd;
  89. ratio /= 32768;
  90.  
  91. temp2 = max2.temperature(100, RREF);
  92. Serial.println(temp2);
  93. HandleFault(max2);
  94. delay(500);
  95. rtd = max3.readRTD();
  96.  
  97.  
  98. ratio = rtd;
  99. ratio /= 32768;
  100. temp3 = max3.temperature(100, RREF);
  101. Serial.println(temp3);
  102. HandleFault(max3);
  103.  
  104. for(byte i = 0; i< 3; i++)
  105. {
  106. Serial.println(i);
  107. responseData += "{ \"sensor_id\": " + String(i) + ", \"temperature\": " + String(getTemp(i)) + "}";
  108. if(i < 2)
  109. {
  110. responseData += ",";
  111. }
  112.  
  113. }
  114. responseData += "]";
  115.  
  116. server.sendHeader("Access-Control-Max-Age", "10000");
  117. server.sendHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
  118. server.sendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  119. server.send(200, "application/json", responseData);
  120. });
  121.  
  122.  
  123.  
  124.  
  125. }
  126.  
  127. String getTemp(byte i)
  128. {
  129. switch(i)
  130. {
  131. case 0:
  132. return String(temp1);
  133. break;
  134. case 1:
  135. return String(temp2);
  136. break;
  137. case 2:
  138. return String(temp3);
  139. break;
  140. default:
  141. return "";
  142. break;
  143. }
  144. }
  145.  
  146. void HandleFault(Adafruit_MAX31865 max_)
  147. {
  148.  
  149. // Check and print any faults
  150. uint8_t fault = max_.readFault();
  151. if (fault) {
  152. Serial.print("Fault 0x"); Serial.println(fault, HEX);
  153. if (fault & MAX31865_FAULT_HIGHTHRESH) {
  154. Serial.println("RTD High Threshold");
  155. }
  156. if (fault & MAX31865_FAULT_LOWTHRESH) {
  157. Serial.println("RTD Low Threshold");
  158. }
  159. if (fault & MAX31865_FAULT_REFINLOW) {
  160. Serial.println("REFIN- > 0.85 x Bias");
  161. }
  162. if (fault & MAX31865_FAULT_REFINHIGH) {
  163. Serial.println("REFIN- < 0.85 x Bias - FORCE- open");
  164. }
  165. if (fault & MAX31865_FAULT_RTDINLOW) {
  166. Serial.println("RTDIN- < 0.85 x Bias - FORCE- open");
  167. }
  168. if (fault & MAX31865_FAULT_OVUV) {
  169. Serial.println("Under/Over voltage");
  170. }
  171. max_.clearFault();
  172. }
  173.  
  174.  
  175. }
  176.  
  177. void loop() {
  178.  
  179. server.handleClient();
  180.  
  181.  
  182. }
Add Comment
Please, Sign In to add comment