Advertisement
sxiii

8-thermistors static arduino table

Mar 21st, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.58 KB | None | 0 0
  1.  
  2. #include <OneWire.h>
  3. #include <SPI.h>
  4. #include <Ethernet.h>
  5. OneWire  ds(9);  // (a 4.7K resistor is necessary)
  6.  
  7. // KNOWN ROMs (IDs of Thermistors) //
  8. const char* therm1 = "28ff988b8141cd"; const char* therm2 = "28ffac6d68142bc"; const char* therm3 = "28ffe6746814243"; const char* therm4 = "28ffd96f681424";
  9. const char* therm5 = "28ff734b814175"; const char* therm6 = "28ff7bb66814378"; const char* therm7 = "28ff8758691438"; const char* therm8 = "28ff57bb81418";
  10. const char* therms[] = {therm1, therm2, therm3, therm4, therm5, therm6, therm7, therm8};
  11. char rel1; char rel2; char rel3; char rel4; char rel5; char rel6; char rel7; char rel8;
  12. char rels[] = {rel1, rel2, rel3, rel4, rel5, rel6, rel7, rel8};
  13. int o; int z;
  14.  
  15. // Enter a MAC address and IP address for your controller below.
  16. // The IP address will be dependent on your local network:
  17. byte mac[] = {
  18.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  19. };
  20. IPAddress ip(192, 168, 1, 20);
  21.  
  22. // Initialize the Ethernet server library with the IP address and port you want to use (port 80 is default for HTTP):
  23. EthernetServer server(80);
  24.  
  25. // RELAY LOGIC & TEMPERATURE
  26. char on = LOW; char off = HIGH;
  27. int celmax = 25.00;
  28.  
  29. void setup() {
  30.   pinMode(32, OUTPUT);    pinMode(33, OUTPUT);    pinMode(34, OUTPUT);    pinMode(35, OUTPUT);    
  31.   pinMode(36, OUTPUT);    pinMode(37, OUTPUT);    pinMode(38, OUTPUT);    pinMode(39, OUTPUT);
  32.  
  33.   // Open serial communications and wait for port to open:
  34.   Serial.begin(9600);
  35.   while (!Serial) {
  36.     ; // wait for serial port to connect. Needed for native USB port only
  37.   }
  38.  
  39.   // start the Ethernet connection and the server:
  40.   Ethernet.begin(mac, ip);
  41.   server.begin();
  42. //  Serial.print("server is at ");
  43.   Serial.println(Ethernet.localIP());
  44. }
  45.  
  46.  
  47. void loop() {
  48.  
  49.   byte i;  byte present = 0;  byte type_s;  byte data[12];  byte addr[8];  float celsius;  
  50.   String aa;  String ab;  String xx;  String check;
  51.  
  52.   // listen for incoming clients
  53.   EthernetClient client = server.available();
  54.   if (client) {
  55.     Serial.println("new client");
  56.     // an http request ends with a blank line
  57.     boolean currentLineIsBlank = true;
  58.     while (client.connected()) {
  59.       if (client.available()) {
  60.         char c = client.read();
  61.         Serial.write(c);
  62.         // if you've gotten to the end of the line (newline character) and the line is blank, the http request has ended, so you reply
  63.         if (c == '\n' && currentLineIsBlank) {
  64.           // send a standard http response header
  65.           client.println("HTTP/1.1 200 OK");
  66.           client.println("Content-Type: text/html");
  67.           client.println("Connection: close");  // the connection will be closed after completion of the response
  68.           client.println("Refresh: 10");  // refresh the page automatically every 5 sec
  69.           client.println();
  70.           client.println("<!DOCTYPE HTML>");
  71.           client.println("<html><head><style>.bold {font-weight: bold;}</style></head>");
  72.  
  73. client.println("<table border=1><tr class=bold><td>#</td><td>Chip</td><td>Data</td><td>CRC</td><td>Temperature</td><td>ROM</td><td>State</td></tr>");
  74.          
  75. for (o = 0; o < 8; o++) {
  76.  
  77. aa = "<tr><td>T";
  78. aa += o;
  79. aa += "</td><td>";
  80.  
  81.   if ( !ds.search(addr)) {
  82.  //   Serial.println("No more addresses.");
  83.  //   Serial.println();
  84.     ds.reset_search(); delay(0); return;  }
  85.  
  86.   if (OneWire::crc8(addr, 7) != addr[7]) { aa += ("CRC is not valid!"); return; }
  87.  
  88.   // the first ROM byte indicates which chip
  89.   switch (addr[0]) {
  90.     case 0x10:
  91.       aa += "DS18S20"; type_s = 1; break; // or old DS1820
  92.     case 0x28:
  93.       aa += "DS18B20"; type_s = 0; break;
  94.     case 0x22:
  95.       aa += "DS1822"; type_s = 0; break;
  96.     default:
  97.       aa += "Device is not a DS18x20 family device"; return;
  98.   }
  99.   aa += "</td><td>";
  100.   ds.reset();
  101.   ds.select(addr);
  102.   ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  103.   // delay(750);     // maybe 750ms is enough, maybe not - we might do a ds.depower() here, but the reset will take care of it.
  104.  
  105.   present = ds.reset();
  106.   ds.select(addr);    
  107.   ds.write(0xBE);         // Read Scratchpad
  108.   aa += String(present, HEX);
  109. //  aa += "</td><td>";
  110.  
  111.   //Serial.print(" ");
  112.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  113.     data[i] = ds.read();
  114.     aa += String(data[i], HEX);
  115.    // Serial.print(" ");
  116.   }
  117.  // aa += ", CRC = ";
  118.    aa += "</td><td>";
  119.   aa += String(OneWire::crc8(data, 8), HEX);
  120.   aa += "</td><td>";
  121.  
  122.    int16_t raw = (data[1] << 8) | data[0];
  123.   if (type_s) {
  124.     raw = raw << 3; // 9 bit resolution default
  125.     if (data[7] == 0x10) {
  126.       // "count remain" gives full 12 bit resolution
  127.       raw = (raw & 0xFFF0) + 12 - data[6];
  128.     }
  129.   } else {
  130.     byte cfg = (data[4] & 0x60);
  131.     // at lower res, the low bits are undefined, so let's zero them
  132.     if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  133.     else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  134.     else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  135.     //// default is 12 bit resolution, 750 ms conversion time
  136.   }
  137.   celsius = (float)raw / 16.0;
  138.  
  139. //  aa += (", Temperature = ");
  140.   aa += (celsius);
  141.   aa += "</td><td>";
  142.  
  143.   delay(0);
  144.   check = "";
  145. //  aa += "ROM = ";
  146.   for( i = 0; i < 8; i++) {
  147.       check += String(addr[i], HEX);
  148.   }
  149.        aa += check;
  150.        aa += "</td><td>";
  151.  
  152.   if (check == therms[o])  { if (celsius >= celmax) { rels[o] = on; aa += "on"; } else { rels[o] = off; aa += "off"; } }
  153.   digitalWrite(o+32, rels[o]);
  154.   client.print(aa);
  155.   client.println("</td></tr>");
  156. }
  157.  
  158.   client.println("</table></html>");
  159.           break;
  160.         }
  161.         if (c == '\n') { currentLineIsBlank = true; } else if (c != '\r') { currentLineIsBlank = false; }
  162.       }
  163.     }
  164.  
  165.     delay(1);
  166.     client.stop();
  167.   }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement