Hallo,
da ich die Frage schon in einem anderen Thread gestellt hatte und der Admin mich darauf hingewiesen hatte , dass dies ein neues Thema wäre und andere vielleicht die gleichen Fragen hätten poste ich hier.
Ausgangssituation:
Der Mega 2560 (China, Elegoo) mit einem LAN-Shield W5100 hat bereits ein Webserver am laufen.
Verbindung zum LAN besteht, funktioniert.
Auf der Webseite befinden sich 3 Buttons (AN, AUS und Submit).
Mit den Buttons wird der Ausgang 6 des Mega geschalten, sobald der Ausgang gesetzt ist sieht man auf der Seite den Zustand Ein und umgekehrt.
Auf dem NodeMCU V3 Lolin wurde ein BME280 über I2C gekoppelt.
Auch hier läuft ein Webserver auf dem die Werte des BME 280(HTTP) abgebildet werden.
[code
/*********
Original Sketch von
Rui Santos
Complete project details at https://randomnerdtutorials.com
Anpassungen der HTML Übersicht
Änderung der Zeichen in Grad
Anpassung des Textes der Messungen in Deutsch
Anpassung der Librarys, da nicht für NodeMCU geeignet
*********/
// Load Wi-Fi library
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//uncomment the following lines if you're using SPI
#include <SPI.h>
/#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5/
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
// Replace with your network credentials
const char* ssid = "Kernweb3";
const char* password = "*********";
// Webserver Name einrichten
const char* dns_name = "NodeMCU";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
void setup() {
Serial.begin(9600);
Serial.println("NodeMCU gestartet");
bool status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
//status = bme.begin();
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (!MDNS.begin(dns_name)) {
Serial.println("DNS gestartet, erreichbar unter: ");
Serial.println("http://" + String(dns_name) + ".local/");
}
server.begin();
Serial.println("Webserver gestartet.");
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("");
client.println("<meta name="viewport" content="width=device-width, initial-scale=1">");
client.println("<link rel="icon" href="data:,">");
// CSS to style the table
client.println("body { text-align: center; font-family: "Trebuchet MS", Arial;}");
client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
client.println("tr:hover { background-color: #bcbcbc; }");
client.println("td { border: none; padding: 12px; }");
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
// Web Page Heading
client.println("
Earnie
");client.println("
Node MCU V3 mit BME280
");client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
/code]
Zu meinem Problem:
Ich würde gerne die Werte des BME280 auf den Mega bringen über das Netzwerk, um anschließend den Ausgang 6 über einen Max Temperaturwert zu schalten.
Earnie_8266_BME280.txt (6.41 KB)
LED_ueber_Web_Earnie.txt (4.22 KB)
| Messungen | Werte |
|---|---|
| Temp. in Celsius | <span class="sensor">"); client.println(bme.readTemperature()); client.println(" °C |
| Temp. in Fahrenheit | <span class="sensor">"); client.println(1.8 * bme.readTemperature() + 32); client.println(" °F |
| Luftdruck | <span class="sensor">"); client.println(bme.readPressure() / 100.0F); client.println(" hPa |
| Höhe berechnet | <span class="sensor">"); client.println(bme.readAltitude(SEALEVELPRESSURE_HPA)); client.println(" m |
| Luftfeuchte | <span class="sensor">"); client.println(bme.readHumidity()); client.println(" % |