The use of the ESP8266 in the world of IoT

User avatar
By ESP TOAD
#80210 /********* CURRENT PROJECT WORKING 2018 2019 *********
* I have 1 temperature sensor only reading as one but all the rest works
* This works on my mobile connect to your network then use the ip you supply DEVICE IS ON FIXED IP
* https://bitbucket.org/xoseperez/fauxmoesp
* http://www.instructables.com/id/Smart-L ... azon-Echo/
* https://randomnerdtutorials.com/esp8266-web-server/
* https://github.com/esp8266/Arduino/blob ... 266_peri.h
https://www.w3schools.com/htmL/tryit.as ... input_text
http://www.instructables.com/id/How-to- ... 6-01-pins/


Wireing to new pcb all relays are on by default so reverse on off
temperature sensor is on D4
Relay on D0 is 16
Relay on D1 is Led 1
Relay on D2 is Led 2
Relay on D1
CHANGE THE TEXT ON THE WEB PAGE TO YOUR REQUIREMENTS WITH CARE
This works 100% with TWO led connected to port / pin 4 & 5
Temperature reading from port 0 (ONE PROBE ONLY)
ds18S20

*/



#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// DallasTemperature wire is plugged into port 0 on Arduino WiFi esp8266 Board
#define ONE_WIRE_BUS 0 // sensor on 0
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire ds18x20[] = { 0, 15 };// what is the 0,15 for??
const int oneWireCount = sizeof(ds18x20)/sizeof(OneWire);

OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DallasTemperature sensor[oneWireCount];


//===================================================================
MDNSResponder mdns;
ESP8266WebServer server(80);
int* temperature[5] ;// was float
int t;
int i;
// Replace with your network credentials
const char* ssid = "YOUR NETWORK";
const char* password = "YOUR PASS WORD";
String thisString = "";
String webPage = "";
String TEMP ="";
int gpio2_pin = 2;
int gpio4_pin = 4;
int gpio5_pin = 5;

int gpio12_pin = 12;
int gpio13_pin = 13;
int gpio14_pin = 14;
int gpio15_pin = 15;
int gpio16_pin = 16;
//int Temperature = 20;//dummy to start builds the web page
//int oldTemperature = 21;//dummy to start
//============================================================
void setup(void) {

// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature Multiple Bus Control Library Simple Demo");
Serial.print("============Ready with ");
Serial.print(oneWireCount);
Serial.println(" Sensors================");

// Start up the library on all defined bus-wires
DeviceAddress deviceAddress;
for ( i = 0; i < oneWireCount; i++) {;
sensor[i].setOneWire(&ds18x20[i]);
sensor[i].begin();
if (sensor[i].getAddress(deviceAddress, 0)) sensor[i].setResolution(deviceAddress, 12);
}
// 0 is sensor SEE NOTE ABOVE
//1&3 6 to 11 appear to be reserved crashes system when used
pinMode(gpio2_pin, OUTPUT);
pinMode(gpio4_pin, OUTPUT);
pinMode(gpio5_pin, OUTPUT);
pinMode(gpio12_pin, OUTPUT);
pinMode(gpio13_pin, OUTPUT);
pinMode(gpio14_pin, OUTPUT);
pinMode(gpio15_pin, OUTPUT);
pinMode(gpio16_pin, OUTPUT);

/*
*
working from here

*/
Serial.begin(74800);//allows us to see all texts Was Serial.begin(115200);
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IPAddress ip(192, 168, 1, 200); // CHANGE 1 and 200 TO WHAT YOU WANT then connect by typing this to //your phone while phone connected to your local network
IPAddress gateway(192, 168, 1, 254); // works fine leave it
IPAddress subnet(255, 255, 255, 0);// works fine leave it
WiFi.config(ip, gateway, subnet);
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WiFi.begin(ssid, password);
Serial.println("");

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

if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}

server.on("/", []() {
server.send(200, "text/html", webPage);
});
server.on("/socket1On", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio5_pin, HIGH);
delay(1000);
});
server.on("/socket1Off", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio5_pin, LOW);
delay(1000);
});
server.on("/socket2On", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio4_pin, HIGH);
delay(1000);
});
server.on("/socket2Off", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio4_pin, LOW);
delay(1000);
});
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
server.on("/socket16On", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio6_pin, HIGH);
delay(1000);
});
server.on("/socket16Off", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio6_pin, LOW);
delay(1000);
});

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

void loop(void) {
scanprobes();
rebuild(); //currently unconditional
//if (oldTemperature != Temperature) rebuild();// conditonal update of web
//oldTemperature = Temperature;// prevetts update of web until a change in temperature
server.handleClient();
delay (500);

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void rebuild() {
webPage = "";
webPage += "<!DOCTYPE html><html><head><style>";
webPage += "div { text-align: center;background-color: lightgrey;width: 600px;border: 25px solid green;padding: 25px;margin: 25px;}";
webPage += "</style></head><body>";
webPage += "<div><h1>ESP8266 Web Server";//</h1></div></body></html>
webPage += "<p>Led #1 <a href=\"socket1On\">ON </button></a>&nbsp;<a href=\"socket1Off\">OFF</button></a></p>";
webPage += "<p>Led #2 <a href=\"socket2On\">ON </button></a>&nbsp;<a href=\"socket2Off\">OFF</button></a></p>";
webPage += "<p>Led #16 <a href=\"socket16On\">ON </button></a>&nbsp;<a href=\"socket16Off\">OFF</button></a></p>";

webPage += (thisString);
}




void scanprobes(){
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
for ( i = 0; i < oneWireCount; i++) {
sensor[i].requestTemperatures();
}
Serial.println("DONE");
// Read probes and build string for web page
delay(1000);
thisString = "";
for ( i = 0; i < oneWireCount; i++); {
t = sensor[i].getTempCByIndex(0);// read each probe
thisString += String(t); // convert to string
thisString += "&#176 C "; // add text degree symbol
thisString += " sensor No ";
thisString += String(i);
Serial.print("Temperature for the sensor "); //for debug see output
Serial.print(i);
Serial.print(" is ");
Serial.println (thisString) ; //back to loop count
}
}