Advertisement
dragonflydiy

Arduino code for ESP8266 module and DS18B20 temp sensor

Oct 28th, 2015
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. // Arduino code for the ESP8266 wifi module driving a Dallas DS18B20 temperature sensor
  2. // Install the ESP8266 extension into the Arduino environment using information found here:
  3. // https://github.com/esp8266/Arduino
  4. //
  5. // The DallasTemperature module used in this code can be found here:
  6. // https://github.com/milesburton/Arduino-Temperature-Control-Library
  7. // Install into Arduino using Sketch/Include library/Add ZIP library
  8. //
  9. // To use, put your network name and password into the ssid and pass locations below
  10. //
  11. // John Ridley / DragonflyDIY.com
  12. // CC0 Creative Commons public domain dedication. No rights reserved, no attribution required.
  13. // October 28 2015
  14.  
  15. #include <OneWire.h>
  16. #include <DallasTemperature.h>
  17.  
  18. #include <ESP8266WiFi.h>
  19. #include <WiFiClient.h>
  20.  
  21. // ************************** define your network parameters here ******************
  22. char ssid[] = "moria";
  23. char pass[] = "NotMyRealPassword";
  24. IPAddress ipaddr(192,168,1,99);
  25. IPAddress ipgateway(192,168,1,1);
  26. IPAddress ipnetmask(255,255,255,0);
  27. #define serverport 80
  28. // *********************************************************************************
  29.  
  30. // the pin of the blue LED
  31. #define GPIO1 1
  32.  
  33. // The temp sensor is on GPIO2.  Set it up here
  34. #define DS18B20PIN 2
  35.  
  36. // create our web server
  37. WiFiServer server(serverport);
  38.  
  39. // create the object that interfaces to the temperature sensor
  40. OneWire ds(DS18B20PIN); // the data interface
  41. DallasTemperature sensors(&ds); // the temp sensor object that uses the data interface
  42.  
  43. // this runs once at power up only.
  44. void setup()
  45. {
  46.   // enable control of the blue LED and start with it off
  47.   pinMode(GPIO1, OUTPUT);
  48.   digitalWrite(GPIO1, 1); // 1 = off
  49.  
  50.   // check for the presence of the shield
  51.   if (WiFi.status() == WL_NO_SHIELD)
  52.   {
  53.     // if we don't have one, there's no point in continuing, just stop.
  54.     while(true);
  55.   }
  56.  
  57.   // attempt to connect to Wifi network:
  58.   int status = WiFi.begin(ssid, pass);
  59.   WiFi.config(ipaddr, ipgateway, ipnetmask);
  60.  
  61.   while ( status != WL_CONNECTED)
  62.   {
  63.     delay(3000);
  64.     status = WiFi.status();
  65.   }
  66.  
  67.   // start up the server and the temperature sensor object
  68.   server.begin();
  69.   sensors.begin();
  70.  
  71.   // signal that we're operational now.
  72.   digitalWrite(GPIO1, 0); // turn on the blue LED (0 = lit)
  73. }
  74.  
  75.  
  76. // this just runs forever.
  77. void loop()
  78. {
  79.   // listen for incoming clients
  80.   WiFiClient client = server.available();
  81.   if (client)
  82.   {
  83.     // we pretty much live in here now.
  84.     // an http request ends with a blank line
  85.     boolean currentLineIsBlank = true;
  86.    
  87.     while (client.connected())
  88.     {
  89.       // we're in here for the duration of a single connection
  90.       if (client.available())
  91.       {
  92.         // this server is real dumb. It doesn't care what you say in the request. It gets a request, it feeds you the temp.
  93.         char c = client.read();
  94.        
  95.         // if you've gotten to the end of the line (received a newline
  96.         // character) and the line is blank, the http request has ended,
  97.         // so you can send a reply
  98.         if (c == '\n' && currentLineIsBlank)
  99.         {
  100.           // send a standard http response header
  101.           client.println("HTTP/1.1 200 OK");
  102.           client.println("Content-Type: text/plain");
  103.           client.println("Connection: close");  // the connection will be closed after completion of the response
  104.           client.println(); // blank line indicates the headers are over, here comes the data!
  105.  
  106.           // send the actual "web page" here
  107.           sensors.requestTemperatures(); // Send the command to get temperatures
  108.           client.print("Temperature=");
  109.           client.print(sensors.getTempCByIndex(0));  
  110.           client.print(" celcius");
  111.           break; // and we're done. This breaks out of the connected() loop and ends the connection
  112.         }
  113.         if (c == '\n')
  114.         {
  115.           // you're starting a new line
  116.           currentLineIsBlank = true;
  117.         }
  118.         else if (c != '\r')
  119.         {
  120.           // you've gotten a character on the current line
  121.           currentLineIsBlank = false;
  122.         }
  123.       }
  124.     }
  125.     // give the web browser time to receive the data
  126.     delay(1);
  127.    
  128.     // close the connection:
  129.     client.stop();
  130.   }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement