Facebook
From Whipped Macaque, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 224
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <WiFiClient.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6.  
  7.  
  8. //------------------------------------------
  9. //DS18B20
  10. #define ONE_WIRE_BUS D3 //Pin to which is attached a temperature sensor
  11. #define ONE_WIRE_MAX_DEV 15 //The maximum number of devices
  12.  
  13. const int led = 2;
  14.  
  15. void handleLED();
  16.  
  17. OneWire oneWire(ONE_WIRE_BUS);
  18. DallasTemperature DS18B20(&oneWire);
  19. int numberOfDevices; //Number of temperature devices found
  20. DeviceAddress devAddr[ONE_WIRE_MAX_DEV];  //An array device temperature sensors
  21. float tempDev[ONE_WIRE_MAX_DEV]; //Saving the last measurement of temperature
  22. float tempDevLast[ONE_WIRE_MAX_DEV]; //Previous temperature measurement
  23. long lastTemp; //The last measurement
  24. const int durationTemp = 5000; //The frequency of temperature measurement
  25.  
  26. //------------------------------------------
  27. //WIFI
  28. const char* ssid = "esp";
  29. const char* password = "haslo8266";
  30.  
  31. //------------------------------------------
  32. //HTTP
  33. ESP8266WebServer server(80);
  34.  
  35. //------------------------------------------
  36. //Convert device id to String
  37. String GetAddressToString(DeviceAddress deviceAddress){
  38.   String str = "";
  39.   for (uint8_t i = 0; i < 8; i++){
  40.     if( deviceAddress[i] < 16 ) str += String(0, HEX);
  41.     str += String(deviceAddress[i], HEX);
  42.   }
  43.   return str;
  44. }
  45.  
  46. //Setting the temperature sensor
  47. void SetupDS18B20(){
  48.   DS18B20.begin();
  49.  
  50.   Serial.print("Parasite power is: ");
  51.   if( DS18B20.isParasitePowerMode() ){
  52.     Serial.println("ON");
  53.   }else{
  54.     Serial.println("OFF");
  55.   }
  56.  
  57.   numberOfDevices = DS18B20.getDeviceCount();
  58.   Serial.print( "Device count: " );
  59.   Serial.println( numberOfDevices );
  60.  
  61.   lastTemp = millis();
  62.   DS18B20.requestTemperatures();
  63.  
  64.   // Loop through each device, print out address
  65.   for(int i=0;i<numberOfDevices; i++){
  66.     // Search the wire for address
  67.     if( DS18B20.getAddress(devAddr[i], i) ){
  68.       //devAddr[i] = tempDeviceAddress;
  69.       Serial.print("Found device ");
  70.       Serial.print(i, DEC);
  71.       Serial.print(" with address: " + GetAddressToString(devAddr[i]));
  72.       Serial.println();
  73.     }else{
  74.       Serial.print("Found ghost device at ");
  75.       Serial.print(i, DEC);
  76.       Serial.print(" but could not detect address. Check power and cabling");
  77.     }
  78.  
  79.     //Get resolution of DS18b20
  80.     Serial.print("Resolution: ");
  81.     Serial.print(DS18B20.getResolution( devAddr[i] ));
  82.     Serial.println();
  83.  
  84.     //Read temperature from DS18b20
  85.     float tempC = DS18B20.getTempC( devAddr[i] );
  86.     Serial.print("Temp C: ");
  87.     Serial.println(tempC);
  88.   }
  89. }
  90.  
  91. //Loop measuring the temperature
  92. void TempLoop(long now){
  93.   if( now - lastTemp > durationTemp ){ //Take a measurement at a fixed time (durationTemp = 5000ms, 5s)
  94.     for(int i=0; i<numberOfDevices; i++){
  95.       float tempC = DS18B20.getTempC( devAddr[i] ); //Measuring temperature in Celsius
  96.       tempDev[i] = tempC; //Save the measured value to the array
  97.     }
  98.     DS18B20.setWaitForConversion(false); //No waiting for measurement
  99.     DS18B20.requestTemperatures(); //Initiate the temperature measurement
  100.     lastTemp = millis();  //Remember the last time measurement
  101.   }
  102. }
  103.  
  104. //------------------------------------------
  105. void HandleRoot(){
  106.   String message = "Number of devices: ";
  107.   message += numberOfDevices;
  108.   message += "rn<br>";
  109.   char temperatureString[6];
  110.  
  111.   message += "<table border='1'>rn";
  112.   message += "<tr><td>Device id</td><td>Temperature</td></tr>rn";
  113.   message += "<button type="button" onclick="alert('Hello world!')">Leds on!</button>";
  114.   //<form action="http://google.com">
  115.    //<input type="submit" value="Go to Google" /> </form>
  116.   message += "<form action="/LED" method="POST"><input type="submit" value="Toggle LED"></form>";
  117.   for(int i=0;i<numberOfDevices;i++){
  118.     dtostrf(tempDev[i], 2, 2, temperatureString);
  119.     Serial.print( "Sending temperature: " );
  120.     Serial.println( temperatureString );
  121.  
  122.     message += "<tr><td>";
  123.     message += GetAddressToString( devAddr[i] );
  124.     message += "</td>rn";
  125.     message += "<td>";
  126.     message += temperatureString;
  127.     message += "</td></tr>rn";
  128.     message += "rn";
  129.   }
  130.   message += "</table>rn";
  131.  
  132.   server.send(200, "text/html", message );
  133. }
  134.  
  135. void HandleNotFound(){
  136.   String message = "File Not Foundnn";
  137.   message += "URI: ";
  138.   message += server.uri();
  139.   message += "nMethod: ";
  140.   message += (server.method() == HTTP_GET)?"GET":"POST";
  141.   message += "nArguments: ";
  142.   message += server.args();
  143.   message += "n";
  144.   for (uint8_t i=0; i<server.args(); i++){
  145.     message += " " + server.argName(i) + ": " + server.arg(i) + "n";
  146.   }
  147.   server.send(404, "text/html", message);
  148. }
  149.  
  150.  
  151. //------------------------------------------
  152. void setup() {
  153.   //Setup Serial port speed
  154.   Serial.begin(115200);
  155.  
  156.   //Setup WIFI
  157.   WiFi.begin(ssid, password);
  158.   Serial.println("");
  159.  
  160.   //Wait for WIFI connection
  161.   while (WiFi.status() != WL_CONNECTED) {
  162.     delay(500);
  163.     Serial.print(".");
  164.   }
  165.   Serial.println("");
  166.   Serial.print("Connected to ");
  167.   Serial.println(ssid);
  168.   Serial.print("IP address: ");
  169.   Serial.println(WiFi.localIP());
  170.  
  171.   server.on("/", HandleRoot);
  172.   server.on("/LEDon", handleLEDOn);
  173.   server.on("/LEDoff", handleLEDOff);
  174.  
  175.  
  176.   server.onNotFound( HandleNotFound );
  177.   server.begin();
  178.   Serial.println("HTTP server started at ip " + WiFi.localIP().toString() );
  179.  
  180.   //Setup DS18b20 temperature sensor
  181.   SetupDS18B20();
  182.  
  183. }
  184.  
  185. void loop() {
  186.   long t = millis();
  187.  
  188.   server.handleClient();
  189.   TempLoop( t );
  190. }
  191. void handleLEDOn() {                          // If a POST request is made to URI /LED
  192.   digitalWrite(led,!digitalRead(led));      // Change the state of the LED
  193.   server.sendHeader("Location","/");        // Add a header to respond with a new location for the browser to go to the home page again
  194.   server.send(303);                         // Send it back to the browser with an HTTP status 303 (See Other) to redirect
  195. }
  196.  
  197.  
  198.