Skip to content

Commit

Permalink
Added some sketches for webservers on ESP8266 to provide: DHT tempera…
Browse files Browse the repository at this point in the history
…ture/humidity, 1-Wire DS81B20 temperature, PulseCounter for meters
  • Loading branch information
gianfrdp committed Mar 9, 2016
1 parent de57940 commit b47ba1a
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 0 deletions.
110 changes: 110 additions & 0 deletions ESP8266/Arduino/DHTServer/DHTServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* DHTServer - ESP8266 Webserver with a DHT sensor as an input
Based on ESP8266Webserver, DHTexample, and BlinkWithoutDelay (thank you)
Version 1.0 5/3/2014 Version 1.0 Mike Barela for Adafruit Industries
Version 1.1 15/02/2016 Version 1.1 Gianfranco Di Prinzio
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#define DHTTYPE DHT22

#define DHTPIN D2

const char* ssid = "myssid";
const char* password = "mypassword";

// the IP address for the shield:
IPAddress ip (192, 168, 2, 33);
IPAddress dns (192, 168, 2, 1);
IPAddress gateway (192, 168, 2, 1);
IPAddress subnet (255, 255, 255, 0);

void gettemperature();

ESP8266WebServer server(80);

// Initialize DHT sensor
// NOTE: For working with a faster than ATmega328p 16 MHz Arduino chip, like an ESP8266,
// you need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold. It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value. The default for a 16mhz AVR is a value of 6. For an
// Arduino Due that runs at 84mhz a value of 30 works.
// This is for the ESP8266 processor on ESP-01
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266

float humidity, temp_c; // Values read from sensor
String webString=""; // String to display
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor

void handle_root() {
gettemperature(); // read sensor
webString="{\"humidity\":" + String(humidity) + ",\"temperature\":" + String(temp_c) + "}";
server.send(200, "application/json", webString); // send to someones browser when asked
delay(100);
}

void setup(void)
{
// You can open the Arduino IDE Serial Monitor window to see what the code is doing
Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable
dht.begin(); // initialize temperature sensor

// Uncomment this row to use static IP
WiFi.config(ip, dns, gateway, subnet);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("DHT Weather Reading Server");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.on("/", handle_root);

server.begin();
Serial.println("HTTP server started");
}

void loop(void)
{
server.handleClient();
}

void gettemperature() {
// Wait at least 2 seconds seconds between measurements.
// if the difference between the current time and last time you read
// the sensor is bigger than the interval you set, read the sensor
// Works better than delay for things happening elsewhere also
unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval) {
// save the last time you read the sensor
previousMillis = currentMillis;
Serial.println("Time since start = " + String((int)(currentMillis/1000)) + " sec");

// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
humidity = dht.readHumidity(); // Read humidity (percent)
temp_c = dht.readTemperature(false); // Read temperature as Celsius
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temp_c)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println("DHT Humidity:" + String(humidity) + "%, Temperature:" + String(temp_c) + " C");
}
}
97 changes: 97 additions & 0 deletions ESP8266/Arduino/DS18B20Server/DS18B20Server.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* DS18B20Server - ESP8266 Webserver with a DS18B20 1-Wire sensor as an input
Based on ESP8266Webserver, DHTexample, and BlinkWithoutDelay (thank you)
Version 1.0 5/3/2014 Version 1.0 Mike Barela for Adafruit Industries
Version 1.1 15/02/2016 Version 1.1 Gianfranco Di Prinzio
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>

const char* ssid = "myssid";
const char* password = "mypassword";

// the IP address for the shield:
IPAddress ip (192, 168, 2, 34);
IPAddress dns (192, 168, 2, 1);
IPAddress gateway (192, 168, 2, 1);

IPAddress subnet (255, 255, 255, 0);

/*
IPAddress ip (192, 168, 15, 103);
IPAddress dns (192, 168, 15, 1);
IPAddress gateway (192, 168, 15, 1);
*/

void gettemperature();

ESP8266WebServer server(80);

//initialize 1-wire bus
#define ONE_WIRE_BUS D2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

float temp_c; // Values read from sensor
String webString=""; // String to display

void handle_root() {
gettemperature(); // read sensor
webString="{\"temperature\":" + String(temp_c) + "}";
server.send(200, "application/json", webString); // send to someones browser when asked
delay(100);
}

void setup(void)
{
// You can open the Arduino IDE Serial Monitor window to see what the code is doing
Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable

// Uncomment this row to use static IP
WiFi.config(ip, dns, gateway, subnet);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("DS18B20 Weather Reading Server");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.on("/", handle_root);

server.begin();
Serial.println("HTTP server started");
}

void loop(void)
{
server.handleClient();
}

void gettemperature() {
unsigned long currentMillis = millis();

do {
sensors.requestTemperatures(); // Send the command to get temperatures
temp_c = sensors.getTempCByIndex(0);
} while (temp_c == 85.0 || temp_c == (-127.0));

Serial.println("Time since start (1) = " + String((int)(currentMillis/1000)) + " sec");
currentMillis = millis();
Serial.println("Time since start (2) = " + String((int)(currentMillis/1000)) + " sec");

Serial.println("DS18B20 Temperature:" + String(temp_c) + " C");
}
113 changes: 113 additions & 0 deletions ESP8266/Arduino/PulseServer/PulseServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* PulseServer - ESP8266 Webserver with a pulse counter as an input
Based on ESP8266Webserver, DHTexample, and BlinkWithoutDelay (thank you)
Version 1.0 5/3/2014 Version 1.0 Mike Barela for Adafruit Industries
Version 1.1 29/02/2016 Version 1.1 Gianfranco Di Prinzio
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

#define PIN D2

const char* ssid = "myssid";
const char* password = "mypassword";


// the IP address for the shield:
IPAddress ip (192, 168, 3, 21);
IPAddress dns (192, 168, 3, 1);
IPAddress gateway (192, 168, 3, 1);
IPAddress subnet (255, 255, 255, 0);


void getCounter();


ESP8266WebServer server(80);

float counter = 0, read_counter; // Values read from sensor
unsigned long seconds;
String webString=""; // String to display
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor

void handle_root() {
getCounter(); // read sensor
webString="{\"counter\":" + String(read_counter) + ",\"seconds\":" + String(seconds) + "}";
server.send(200, "application/json", webString); // send to someones browser when asked
delay(100);
}

void manageCounter() {
// Increase counter
counter++;
}

void setup(void)
{
// You can open the Arduino IDE Serial Monitor window to see what the code is doing
Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable
//pinMode(PIN, INPUT);
pinMode(PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN), manageCounter, FALLING);

// Uncomment this row to use static IP
WiFi.config(ip, dns, gateway, subnet);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Pulse Counter Server");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.on("/", handle_root);

server.begin();
Serial.println("HTTP server started");
}

void loop(void)
{
server.handleClient();
}

void getCounter() {
// Wait at least 2 seconds seconds between measurements.
// if the difference between the current time and last time you read
// the sensor is bigger than the interval you set, read the sensor
// Works better than delay for things happening elsewhere also
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// save the last time you read the sensor

seconds = (currentMillis - previousMillis)/1000;
Serial.println("Time since start = " + String((int)(currentMillis/1000)) + " sec");
Serial.println("Time since last read = " + String(seconds) + " sec");
previousMillis = currentMillis;


read_counter = counter; // Read counter
counter = 0; // reset counter
// Check if any reads failed and exit early (to try again).
if (isnan(read_counter)) {
Serial.println("Failed to read from counter!");
return;
}
Serial.println("Counter: " + String(read_counter));
}
}
3 changes: 3 additions & 0 deletions ESP8266/NodeMCU/DHTServer/credentials.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Credentials
SSID = "myssid
PASSWORD = "mypassword"
30 changes: 30 additions & 0 deletions ESP8266/NodeMCU/DHTServer/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--load credentials for WI-FI
dofile("credentials.lua")

function startup()
if file.open("init.lua") == nil then
print("init.lua deleted")
else
print("Running")
file.close("init.lua")
-- Run main program
dofile("myprogram.lua")
end
end

--init.lua
print("set up wifi mode")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,PASSWORD)
--here SSID and PassWord should be modified according your wireless router
wifi.sta.connect()
-- Staic IP
wifi.sta.setip({ip="192.168.2.33",netmask="255.255.255.0",gateway="192.168.2.1"})
print("ESP8266 mode is: " .. wifi.getmode())
print("Network: " .. SSID)
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is "..wifi.sta.getip())
--print("You have 5 seconds to abort Startup")
--print("Waiting...")
--tmr.alarm(0,5000,0,startup)
startup()
Loading

0 comments on commit b47ba1a

Please sign in to comment.