Skip to content

Instantly share code, notes, and snippets.

@tosiara
Last active January 4, 2019 19:38
Show Gist options
  • Save tosiara/d2e7d6b300c1e64e25b834d43b1f8311 to your computer and use it in GitHub Desktop.
Save tosiara/d2e7d6b300c1e64e25b834d43b1f8311 to your computer and use it in GitHub Desktop.
NodeMCU DS18B20 Client
#include <ESP8266WiFi.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 5 // nodemcu D1
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const char* ssid = "mynetwork";
const char* password = "myappassword";
int refresh = 3;
String html = "<meta http-equiv=\"refresh\" content=\"" + String(refresh) + "\" />";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
sensors.begin();
}
void loop()
{
sensors.requestTemperatures();
double celsius = sensors.getTempCByIndex(0);
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.println(" Celsius, ");
WiFiClient client = server.available();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println(html + "<h1>" + String(celsius) + "</h1>");
delay(refresh * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment