Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arduino code for the ESP8266 wifi module driving a Dallas DS18B20 temperature sensor
- // Install the ESP8266 extension into the Arduino environment using information found here:
- // https://github.com/esp8266/Arduino
- //
- // The DallasTemperature module used in this code can be found here:
- // https://github.com/milesburton/Arduino-Temperature-Control-Library
- // Install into Arduino using Sketch/Include library/Add ZIP library
- //
- // To use, put your network name and password into the ssid and pass locations below
- //
- // John Ridley / DragonflyDIY.com
- // CC0 Creative Commons public domain dedication. No rights reserved, no attribution required.
- // October 28 2015
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- // ************************** define your network parameters here ******************
- char ssid[] = "moria";
- char pass[] = "NotMyRealPassword";
- IPAddress ipaddr(192,168,1,99);
- IPAddress ipgateway(192,168,1,1);
- IPAddress ipnetmask(255,255,255,0);
- #define serverport 80
- // *********************************************************************************
- // the pin of the blue LED
- #define GPIO1 1
- // The temp sensor is on GPIO2. Set it up here
- #define DS18B20PIN 2
- // create our web server
- WiFiServer server(serverport);
- // create the object that interfaces to the temperature sensor
- OneWire ds(DS18B20PIN); // the data interface
- DallasTemperature sensors(&ds); // the temp sensor object that uses the data interface
- // this runs once at power up only.
- void setup()
- {
- // enable control of the blue LED and start with it off
- pinMode(GPIO1, OUTPUT);
- digitalWrite(GPIO1, 1); // 1 = off
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD)
- {
- // if we don't have one, there's no point in continuing, just stop.
- while(true);
- }
- // attempt to connect to Wifi network:
- int status = WiFi.begin(ssid, pass);
- WiFi.config(ipaddr, ipgateway, ipnetmask);
- while ( status != WL_CONNECTED)
- {
- delay(3000);
- status = WiFi.status();
- }
- // start up the server and the temperature sensor object
- server.begin();
- sensors.begin();
- // signal that we're operational now.
- digitalWrite(GPIO1, 0); // turn on the blue LED (0 = lit)
- }
- // this just runs forever.
- void loop()
- {
- // listen for incoming clients
- WiFiClient client = server.available();
- if (client)
- {
- // we pretty much live in here now.
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- while (client.connected())
- {
- // we're in here for the duration of a single connection
- if (client.available())
- {
- // this server is real dumb. It doesn't care what you say in the request. It gets a request, it feeds you the temp.
- char c = client.read();
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- if (c == '\n' && currentLineIsBlank)
- {
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/plain");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println(); // blank line indicates the headers are over, here comes the data!
- // send the actual "web page" here
- sensors.requestTemperatures(); // Send the command to get temperatures
- client.print("Temperature=");
- client.print(sensors.getTempCByIndex(0));
- client.print(" celcius");
- break; // and we're done. This breaks out of the connected() loop and ends the connection
- }
- if (c == '\n')
- {
- // you're starting a new line
- currentLineIsBlank = true;
- }
- else if (c != '\r')
- {
- // you've gotten a character on the current line
- currentLineIsBlank = false;
- }
- }
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement