ESP8266 1 / 12E + meteo BME - mcworkshop

MC        WORKSHOP
Vai ai contenuti

Menu principale:

ESP8266 1 / 12E + meteo BME

Projects
ESP 8266 1 / ESP 8266 12E / ESP 8266 12E V0.1 / meteorological station BME 280
MOD. STA / MOD. AP

GPIO2 output control with ESP8266 based on CH340G. Connection with USB
Controllo dell'uscita GPIO2 tramite ESP8266 basato su CH340G. Connessione con USB.

First part wifi

             


              Pin out
                   
clic to enlarge

Driver

Scaricate i driver del CH340 a questo indirizzo: http://www.wch.cn/downloads/CH341SER_EXE.html

IDE configuration

In the "Additional URLs for the Card Manager:" field enter the address: http://arduino.esp8266.com/stable/package_esp8266com_index.json
In "strmenti", "gestore schede" aggiungete questo indirizzo:  http://arduino.esp8266.com/stable/package_esp8266com_index.json




clic to enlarge

Tools, card, card manager
Aggiungete la libreria "esp8266 by esp8266 Community"


clic to enlarge


clic to enlarge

Programming



1 Short-circuit the GND and GPIO0 pins.
2 Insert the device into a USB port.
3 Open the Arduino IDE and load the following sketch.

1 Cortocircuitare i pin GND e GPIO0.
2 Inserite il dispositivo a una porta USB.
3 Aprite l'IDE di Arduino e caricate il seguente sketch.




clic to enlarge
Sketch

With this sketch, for the control of the LED, you will use a web page.
Con questo sketch, per il controllo del LED,  utilizzerete una pagina web.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
ESP8266WebServer server(80);
const int util = 0;
const int led = 2;
void handleRoot() {
 digitalWrite(util, 1);
 server.send(200, "text/plain", "ESP8266 STATUS: OK");
 digitalWrite(util, 0);
}
void handleNotFound(){
 digitalWrite(util, 1);
 String message = "File not found\n\n";
 message += "URI: ";
 message += server.uri();
 message += "\nMethod: ";
 message += (server.method() == HTTP_GET)?"GET":"POST";
 message += "\nArguments: ";
 message += server.args();
 message += "\n";
 for (uint8_t i=0; i<server.args(); i++){
   message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
 }
 server.send(404, "text/plain", message);
 digitalWrite(util, 0);
}
void setup(void){
 pinMode(2, OUTPUT);
 pinMode(util, OUTPUT);
 digitalWrite(util, 0);
 Serial.begin(115200);
 Serial.println("");
 Serial.print("ESP8266 ON");
 Serial.print("192.168.4.1");
 server.on("/", handleRoot);
 server.on("/inline", [](){
   server.send(200, "text/plain", "OK");
 });
 server.on("/on", [](){
   digitalWrite(led, 1);
 server.send(200, "text/plain", "LED ON");
 });
 server.on("/off", [](){
   digitalWrite(led, 0);
 server.send(200, "text/plain", "LED OFF");
 });
 server.onNotFound(handleNotFound);
 server.begin();
 Serial.println("Server HTTP ON");
}
void loop(void){
 server.handleClient();
}
After loading the sketch

1 Disconnect GPIO0 from GND.
2 From the phone enter in "connections" and look for the ESP.
3 Connect.
4 Call up the page: http://192.168.4.1
5 Activate the GPIO2 by typing http://192.168.4.1/on
6 Deactivate the GPIO2 by typing http://192.168.4.1/off

1 Disconnettere GPIO0 da GND.
2 Dal telefono entrate in connessioni e cercate l'ESP.
3 Connettetevi.
4 Richiamate la pagina:  http://192.168.4.1
5 Attivate la GPIO2 digitando  http://192.168.4.1/on
6 Disattivate la GPIO2 digitando  http://192.168.4.1/off

Power supply ext


clic to enlarge

Second part wifi network

Sketch

Download Library BlynkSimpleEsp8266:
Upload this Sketch
Caricate questo Sketch

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxx..."; //token by mail - The APP will send you a code by email - La APP vi inviera per mail un codice che incollerete al posto delle x
char ssid[] = "xxxx..."; // name network - Invent a name of your choice - Inventate un nome a vostro piacimento
char pass[] = "xxxx..."; //pw network - Invent a password of your choice - Inventate una password a vostro piacimento

void setup()
{
 Serial.begin(9600);
 Blynk.begin(auth, ssid, pass);
}
void loop()
{
 Blynk.run();
}

App
Download this APP on your smartphone and configure it with a button
Scaricate questa APP sullo smartphone e configuratela con un pulsante




Now you are ready to control the GPIO2 output with your smartphone
Adesso siete pronti a comandare con lo smartphone l'uscita GPIO2


clic to enlarge

clc to enlarge
clic to enlarge


Third nodeMCU part

The ESP8266 that connects to an existing WiFi network is called Station (STA). In STA mode ESP8266 gets IP from wireless router to which it is connected. With this IP address, it can set up a web server and deliver web pages to all connected devices under existing WiFi network. The ESP8266 that creates its own WiFi network and acts as a hub for one or more stations is called Access Point (AP). In AP mode ESP8266 creates a new WiFi network and sets SSID and IP address to it. With this IP address, it can deliver web pages to all connected devices under its own network.
L'ESP8266 che si collega a una rete WiFi esistente si chiama Station (STA). In modalità STA ESP8266 ottiene l'IP dal router a cui è connesso. Con questo indirizzo IP, è possibile configurare un server Web. L'ESP8266 che crea la propria rete WiFi e funge da hub per una o più stazioni è denominato Access Point (AP). In modalità AP l'ESP8266 crea una nuova rete WiFi.

ESP8266 12E V0.1
ESP8266 nodeMCU as HTTP Server using WiFi Station (STA) mode

Sketch
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "xxxxxxx-xxxxxxx";
const char* password = "xxxxxxxxxxx";
ESP8266WebServer server(80);
uint8_t LED1pin = D0;
bool LED1status = LOW;
uint8_t LED2pin = D1;
bool LED2status = LOW;
void setup() {
 Serial.begin(115200);
 delay(100);
 pinMode(LED1pin, OUTPUT);
 pinMode(LED2pin, OUTPUT);
 Serial.println("Connecting to ");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 delay(1000);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected..!");
 Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
 server.on("/", handle_OnConnect);
 server.on("/led1on", handle_led1on);
 server.on("/led1off", handle_led1off);
 server.on("/led2on", handle_led2on);
 server.on("/led2off", handle_led2off);
 server.onNotFound(handle_NotFound);
 server.begin();
 Serial.println("HTTP server started");
}
void loop() {
 server.handleClient();
 if(LED1status)
 {digitalWrite(LED1pin, HIGH);}
 else
 {digitalWrite(LED1pin, LOW);}
 
 if(LED2status)
 {digitalWrite(LED2pin, HIGH);}
 else
 {digitalWrite(LED2pin, LOW);}
}
void handle_OnConnect() {
 LED1status = LOW;
 LED2status = LOW;
 Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF");
 server.send(200, "text/html", SendHTML(LED1status,LED2status));
}
void handle_led1on() {
 LED1status = HIGH;
 Serial.println("GPIO7 Status: ON");
 server.send(200, "text/html", SendHTML(true,LED2status));
}
void handle_led1off() {
 LED1status = LOW;
 Serial.println("GPIO7 Status: OFF");
 server.send(200, "text/html", SendHTML(false,LED2status));
}
void handle_led2on() {
 LED2status = HIGH;
 Serial.println("GPIO6 Status: ON");
 server.send(200, "text/html", SendHTML(LED1status,true));
}
void handle_led2off() {
 LED2status = LOW;
 Serial.println("GPIO6 Status: OFF");
 server.send(200, "text/html", SendHTML(LED1status,false));
}
void handle_NotFound(){
 server.send(404, "text/plain", "Not found");
}
String SendHTML(uint8_t led1stat,uint8_t led2stat){
 String ptr = "<!DOCTYPE html> <html>\n";
 ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
 ptr +="<title>LED Control</title>\n";
 ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
 ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
 ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
 ptr +=".button-on {background-color: #1abc9c;}\n";
 ptr +=".button-on:active {background-color: #16a085;}\n";
 ptr +=".button-off {background-color: #34495e;}\n";
 ptr +=".button-off:active {background-color: #2c3e50;}\n";
 ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
 ptr +="</style>\n";
 ptr +="</head>\n";
 ptr +="<body>\n";
 ptr +="<h1>ESP8266 Web Server</h1>\n";
   ptr +="<h3>Using Station(STA) Mode</h3>\n";
 
  if(led1stat)
 {ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
 else
 {ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}
 if(led2stat)
 {ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
 else
 {ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}
 ptr +="</body>\n";
 ptr +="</html>\n";
 return ptr;
}




Open your browser. Enter the IP address




Meteorological station - BME280 -
Mod network: AP

VIN BME TO 3.3V ESP
GND BME TO GND ESP
SCL BME TO D1 ESP
SDA BME T0 D2 ESP

Con Arduino Uno
- volendo collegare il sensore BM ad Arduino -
SCL A5
SDA A4

Blu: numero dei pin. Rosso: uscite / ingressi
Blue: number of pins. Red: outputs / inputs
Attenzione ai collegamenti. Non ho trovato il documento originale della casa costruttrice.
Pay attention to the connections. I did not find the original document of the manufacturer.
Quando chiamate la pagina web è possibile leggere "not found", oppure PW errata; insistete!
When you call the web page you can read "not found", or PW incorrect; insist!
Sketch


//Weather Station whit ESP8266 nodeMCU and BME280 sensor
//Maggio 2019 by M.C.
//This example code is in the public domain.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME280_ADDRESS (0x76)
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
float temperature, humidity, pressure, altitude;
const char* ssid = "xxxxxxxxxxxx";
const char* password = "xxxxxxxxxxx";
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
void setup() {
 Serial.begin(115200);
 bme.begin(0x76);
 Serial.println("Connecting to ");
 Serial.println(ssid);
 WiFi.softAP(ssid, password);
 WiFi.softAPConfig(local_ip, gateway, subnet);
 delay(100);
 server.begin();
 Serial.println("HTTP server started");
}
void loop() {
 server.handleClient();
 temperature = bme.readTemperature();
 humidity = bme.readHumidity();
 pressure = bme.readPressure() / 100.0F;
 altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
 server.send(200, "text/html", SendHTML(temperature, humidity, pressure, altitude));
}
void handle_NotFound() {
 server.send(404, "text/plain", "Not found");
}
String SendHTML(float temperature, float humidity, float pressure, float altitude) {
 String ptr = "<!DOCTYPE html> <html>\n";
 ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
 ptr += "<title>xxxxxxxxxxxxxxx</title>\n";
 ptr += "<style>html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center;}\n";
 ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
 ptr += "p {font-size: 18px;color: #444444;margin-bottom: 10px;}\n";
 ptr += "</style>\n";
 ptr += "<script>\n";
 ptr += "setInterval(loadDoc,1000);\n";
 ptr += "function loadDoc() {\n";
 ptr += "var xhttp = new XMLHttpRequest();\n";
 ptr += "xhttp.onreadystatechange = function() {\n";
 ptr += "if (this.readyState == 4 && this.status == 200) {\n";
 ptr += "document.body.innerHTML =this.responseText}\n";
 ptr += "};\n";
 ptr += "xhttp.open(\"GET\", \"/\", true);\n";
 ptr += "xhttp.send();\n";
 ptr += "}\n";
 ptr += "</script>\n";
 ptr += "</head>\n";
 ptr += "<body>\n";
 ptr += "<div id=\"webpage\">\n";
 ptr += "<h1>xxxxxxxxxxxxx</h1>\n";
 ptr += "<p>Temperature: ";
 ptr += temperature;
 ptr += "&deg;C</p>";
 ptr += "<p>Humidity: ";
 ptr += humidity;
 ptr += "UR %</p>";
 ptr += "<p>Pressure: ";
 ptr += pressure;
 ptr += "hPa</p>";
 ptr += "<p>Altitude: ";
 ptr += altitude;
 ptr += "m s.l.m.</p>";
 ptr += "</div>\n";
 ptr += "</body>\n";
 ptr += "</html>\n";
 return ptr;
}
Html
<!DOCTYPE html> <html>
<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">
<title>weather station</title>
<style>html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center;}
body{margin-top: 50px;} h1 {color: #0789bf;margin: 50px auto 30px;}
p {font-size: 18px;color: #0789bf;margin-bottom: 10px;}
</style>
</head>
<body>
<div id=\"webpage\">
<h1>Weather station</h1>
<p>Temperature:
&deg;C</p>
<p>Humidity:
UR
</p>
<p>Pressure:
hPa</p>
<p>Altitude:
m s.l.m.
</p>
</div>
</body>
</html>

Java script

Put this script just before closing the </ head> tag if you want to refresh the page automatically.
Metti questo script appena prima di chiudere il tag </ head> se vuoi aggiornare la pagina automaticamente.
With this simple method in HTML I had problems. meta http-equiv = "refresh" content = "2"
Con questo semplice metodo in HTML ho avuto dei problemi. meta http-equiv="refresh" content="2"
ptr +="<script>\n";
ptr +="setInterval(loadDoc,1000);\n";
ptr +="function loadDoc() {\n";
ptr +="var xhttp = new XMLHttpRequest();\n";
ptr +="xhttp.onreadystatechange = function() {\n";
ptr +="if (this.readyState == 4 && this.status == 200) {\n";
ptr +="document.body.innerHTML =this.responseText}\n";
ptr +="};\n";
ptr +="xhttp.open(\"GET\", \"/\", true);\n";
ptr +="xhttp.send();\n";
ptr +="}\n";
ptr +="</script>\n";



clic to enlarge



Codice completo
code complete

velocità di upload 115200
115200 upload speed

righe di codice personalizzabili:
customizable code lines:

ptr += "<title>MCW Meteo</title>\n";
ptr += "p {font-size: 16px;color: #444444;margin-bottom: 10px;}\n";
ptr += "<h1>MCW</h1>\n";
ptr += "<p>t: ";
ptr += " &deg;C</p>";
ptr += "<p>UR: ";
ptr += " %</p>";
ptr += "<p>hPa: ";
ptr += "<p>m s.l.m. : ";



//Weather Station whit ESP8266 nodeMCU and BME280 sensor
//Maggio 2019 by M.C. REV agosto 2019
//This example code is in the public domain.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME280_ADDRESS (0x76)
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
float temperature, humidity, pressure, altitude;
const char* ssid = "MCW";
const char* password = "12345678";
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
void setup() {
 Serial.begin(115200);
 bme.begin(0x76);
 Serial.println("Connecting to ");
 Serial.println(ssid);
 WiFi.softAP(ssid, password);
 WiFi.softAPConfig(local_ip, gateway, subnet);
 delay(100);
 server.begin();
 Serial.println("HTTP server started");
}
void loop() {
 server.handleClient();
 temperature = bme.readTemperature();
 humidity = bme.readHumidity();
 pressure = bme.readPressure() / 100.0F;
 altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
 server.send(200, "text/html", SendHTML(temperature, humidity, pressure, altitude));
}
void handle_NotFound() {
 server.send(404, "text/plain", "Not found");
}
String SendHTML(float temperature, float humidity, float pressure, float altitude) {
 String ptr = "<!DOCTYPE html> <html>\n";
 ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
 ptr += "<title>MCW Meteo</title>\n";
 ptr += "<style>html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center;}\n";
 ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
 ptr += "p {font-size: 16px;color: #444444;margin-bottom: 10px;}\n";
 ptr += "</style>\n";
 ptr += "<script>\n";
 ptr += "setInterval(loadDoc,1000);\n";
 ptr += "function loadDoc() {\n";
 ptr += "var xhttp = new XMLHttpRequest();\n";
 ptr += "xhttp.onreadystatechange = function() {\n";
 ptr += "if (this.readyState == 4 && this.status == 200) {\n";
 ptr += "document.body.innerHTML =this.responseText}\n";
 ptr += "};\n";
 ptr += "xhttp.open(\"GET\", \"/\", true);\n";
 ptr += "xhttp.send();\n";
 ptr += "}\n";
 ptr += "</script>\n";
 ptr += "</head>\n";
 ptr += "<body>\n";
 ptr += "<div id=\"webpage\">\n";
 ptr += "<h1>MCW</h1>\n";
 ptr += "<p>t: ";
 ptr += temperature;
 ptr += " &deg;C</p>";
 ptr += "<p>UR: ";
 ptr += humidity;
 ptr += " %</p>";
 ptr += "<p>hPa: ";
 ptr += pressure;
 ptr += "</p>";
 ptr += "<p>m s.l.m. : ";
 ptr += altitude;
 ptr += "</p>";
 ptr += "</div>\n";
 ptr += "</body>\n";
 ptr += "</html>\n";
 return ptr;
}
 
Copyright 2015. All rights reserved.
Torna ai contenuti | Torna al menu