Im Betrieb des esp8266 ist es oft störend und auch eine Sicherheitslücke wenn das WiFi Board im AP Modus arbeitet. Dies kann man durch einfügen der folgenden Codezeile abändern.
WiFi.softAPdisconnect(true);
Damit sollte das Melden als AccessPoint beendet sein.
Die Hausautomatisierung benötigt sehr viele Aktoren. In diesem Bereich gibt es sehr viele Produkte der großen Hersteller. Leider auch entsprechend teuer. Hier möchte ich ein Projekt vorstellen das über einen Arduino (WeMos D1) eine Relaiskarte ansteuert. zur Steuerung verwende ich das MQTT Protokoll. Natürlich alles per WiFi. Es wird nur noch die Versorgungsspannung und der Laststromkreis benötig.
5 Minuten Probeaufbau
Gesteuert wird nun das Relais A mit folgendem Befehl:
Ebenso einfach geht das Aus schalten des Relais
Relais B reagiert folglich auf
Das Relais ist nun im kompletten Heimnetzwerk erreichbar.
Mit dem nachfolgendem Sketch ist der WeMos D1 in der Lage Messdaten in eine mySQL Datenbank zu senden.
Das eigentliche schreiben der Daten erfolgt mit dem Aufrufen einer PHP Datei in Form eines GET Befehls.
<?php/*
Daten per GET in eine mySQL Datenbank schrieben
by Tobias Guggenberger
guggenberber.me
*/// POD Verbindung zur Datenbank$datenbank_name="NAME_DATENBANK";$datenbank_username="USER";$datenbank_passwort="PASS";// Datenbankverbindung aufbauen$pdo=new PDO('mysql:host=localhost;dbname='.$datenbank_name,$datenbank_username,$datenbank_passwort);// Neuen Datensatz schreiben$neuer_datensatz=array();$neuer_datensatz['WERT1']=time();$neuer_datensatz['WERT2']=$_GET['temperatur'];$neuer_datensatz['WERT3']=$_GET['sensor'];$statement=$pdo->prepare("INSERT INTO TABELLE (wert1, wert2, wert3) VALUES (:wert1, :wert2, :wert3)");$statement->execute($neuer_datensatz);?>
Der passende Sketch sieht so aus:
#include <ESP8266WiFi.h>constchar* ssid ="SSID";// SSIDconstchar* password ="PASS";// Passwordconstchar* host ="SERVER_DB";// IP ServePconstint port =80;// Portconstint watchdog =5000;// Schreibefruenzunsignedlong previousMillis = millis();void setup(){
Serial.begin(115200);
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());}void loop(){
delay(5000);unsignedlong currentMillis = millis();if( currentMillis - previousMillis > watchdog ){
previousMillis = currentMillis;
WiFiClient client;if(!client.connect(host, port)){
Serial.println("connection failed");return;}
String url ="/arduino/FILE_zum-SCHREIBEN.php?wert1=wer1&usw";
url += String(millis());
url +="&ip=";
url += WiFi.localIP().toString();// Envoi la requete au serveur - This will send the request to the server
client.print(String("GET ")+ url +" HTTP/1.1\r\n"+"Host: "+ host +"\r\n"+"Connection: close\r\n\r\n");unsignedlong timeout = millis();while(client.available()==0){if(millis()- timeout >5000){
Serial.println(">>> Client Timeout !");
client.stop();return;}}while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);}}}
Mit dem nachfolgendem Sketch betreiben Sie den Wemos DA ESP8266 im WiFi als Webserver.
In dem Sketch wird eine feste IP vergeben. Ich finde das immer angenehmer wie dynamische Vergabe.
#include <ESP8266WiFi.h>constchar* ssid ="DEINE_SSID";constchar* password ="DEIN_PASSWORT";int ledPin = D5;
WiFiServer server(80);void setup(){
Serial.begin(19200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
IPAddress ip(192, 168, 187, 52);// Feste IP vergeben
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress dns(192, 168, 1, 1);
WiFi.config(ip, dns, gateway, subnet);while(WiFi.status()!= WL_CONNECTED){
delay(500);
Serial.print(".");}
Serial.println("");
Serial.println("WiFi connected");// Start the server
server.begin();
Serial.println("Server started");// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");}void loop(){// Check if a client has connected
WiFiClient client = server.available();if(!client){return;}// Wait until the client sends some data
Serial.println("new client");while(!client.available()){
delay(1);}// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();// Match the requestint value = LOW;if(request.indexOf("/LED=ON")!=-1){
digitalWrite(ledPin, HIGH);
value = HIGH;}if(request.indexOf("/LED=OFF")!=-1){
digitalWrite(ledPin, LOW);
value = LOW;}// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");// do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Led pin is now: ");if(value == HIGH){
client.print("On");}else{
client.print("Off");}
client.println("<br><br>");
client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 5 ON<br>");
client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 5 OFF<br>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");}
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.