: wtorek 24 mar 2015, 22:45
Nowy układ WiFi:emw3162
http://hackaday.com/2015/03/24/emw3162- ... attention/
http://hackaday.com/2015/03/24/emw3162- ... attention/
Kod: Zaznacz cały
#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTPIN 2 // definicja pinu w którym podpięty jest DHT22
#define DHTTYPE DHT22 // definicja modelu czujnika AM2302
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "coś"; //SSID WiFI
const char* password = "coś"; //hasło WiFi
String readStrings = "";
int pin1 = 14; // GPIO 14 (D5)
int pin2 = 12; // GPIO 12 (D6)
int pin3 = 13; // GPIO 13 (D7)
int pin4 = 15; // GPIO 15 (D8)
WiFiServer server(82); //port nasłuchu
IPAddress ip(192, 168, 1, 50); //IP
IPAddress gateway(192,168,1,1); //brama
IPAddress subnet(255,255,255,0); //maska podsieci
void setup() {
Serial.begin(115200); // prędkość portu szeregowego
delay(10);
dht.begin();
pinMode(pin1, OUTPUT); //pin1 ustawiony jako wyjście
pinMode(pin2, OUTPUT); //pin2 ustawiony jako wyjście
pinMode(pin3, OUTPUT); //pin3 ustawiony jako wyjście
pinMode(pin4, OUTPUT); //pin4 ustawiony jako wyjście
// Połączenie z siecią WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(ip,gateway,subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start serwera wwww
server.begin();
Serial.println("Server started");
// Pokaż adres IP przez port szeregowy
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
WiFiClient client = server.available();
if (client) {
Serial.println("Yes client");
while (client.connected()) {
if (client.available()) {
char c = client.read();
// Odczyt temperatury oraz wilgotności
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Odczyt temperatury w stopniach Celciusz
float t = dht.readTemperature();
// Odczyt temperatury w stopniach Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Sprawdzenie odczytu i ewenualne ponowienie pomiaru
if (readStrings.length() < 100) {
readStrings += c;
}
if (c == '\n') {
Serial.println(readStrings);
client.print("HTTP/1.1 200 OK\r\n"); //wyślij nową stronę
client.print("Content-Type: text/html\r\n\r\n");
client.print("<!DOCTYPE HTML>\r\n");
client.print("<HTML>\r\n");//html tag
client.print("<HEAD>\r\n"); //
// client.print("<meta http-equiv='refresh' content='10'/>\r\n");
client.print("<meta name='apple-mobile-web-app-capable' content='yes' />\r\n");
client.print("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />\r\n");
//client.print("<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-2" /> />\r\n");
client.print("<link rel='stylesheet' type='text/css' href='http://relay.paranoja.us/style.css' />\r\n");
client.print("<TITLE>Home Automation</TITLE>\r\n");
client.print("</HEAD>\r\n");
client.print("<BODY>\r\n");
client.print("<H1>Inteligentny dom by artur_n</H1>\r\n");
client.print("<hr />\r\n");
client.print("<br />\r\n");
client.print("<H2>Build Using Nodemcuv1 Arduino</H2>\r\n");
client.print("<br />\r\n");
client.print("<p><b>Brama garazowa Agatka</b></p><br />\r\n");
client.print("<a href=\"/?button1on\"><font color = \"green\">Brama nr_1 </font></a>\r\n"); //przycisk nr 1 część w komentarzu, przycisk działa na zasadzie włącz i wyłącz czas załączenia 0,2s
// client.print("<a href=\"/?button1off\"><font color = \"red\">OFF </font></a><br />\r\n");
client.print("<br />\r\n");
/* if (digitalRead(14))
{
client.print("Relay 1 is ON");
client.print("<br />\r\n");
}
else
{
client.print("Relay 1 is OFF");
client.print("<br />\r\n");
}*/
client.print("<p><b>Relay 2</b></p>");
client.print("<br />\r\n");
client.print("<a href=\"/?button2on\"><font color = \"green\">ON </font></a>\r\n"); //przycisk nr 2
client.print("<a href=\"/?button2off\"><font color = \"red\">OFF </font></a><br />\r\n");
client.print("<br />\r\n");
/* if (digitalRead(12))
{
client.print("Relay 2 is ON");
client.print("<br />\r\n");
}
else
{
client.print("Relay 2 is OFF");
client.print("<br />\r\n");
}*/
client.print("<p><b>Relay 3</b></p>");
client.print("<br />\r\n");
client.print("<a href=\"/?button3on\"><font color = \"green\">ON </font></a>\r\n"); //przycisk nr 3
client.print("<a href=\"/?button3off\"><font color = \"red\">OFF </font></a><br />\r\n");
client.print("<br />\r\n");
/* if (digitalRead(13))
{
client.print("Relay 3 is ON");
client.print("<br />\r\n");
}
else
{
client.print("Relay 3 is OFF");
client.print("<br />\r\n");
}*/
client.print("<p><b>Relay 4</b></p>");
client.print("<br />\r\n");
client.print("<a href=\"/?button4on\"><font color = \"green\">ON </font></a>\r\n"); //przycisk nr 4
client.print("<a href=\"/?button4off\"><font color = \"red\">OFF </font></a><br />\r\n");
client.print("<br />\r\n");
/* if (digitalRead(15))
{
client.print("Relay 4 is ON");
client.print("<br />\r\n");
}
else
{
client.print("Relay 4 is OFF");
client.print("<br />\r\n");
} */
client.print("<p><b>All Relays</b></p>");
client.print("<br />\r\n");
client.print("<br />\r\n");
client.print("<a href=\"/?buttonallon\"><font color = \"green\">ALL ON </font></a>\r\n"); //wszystkie przyciski
client.print("<a href=\"/?buttonalloff\"><font color = \"red\">ALL OFF </font></a><br />\r\n");
client.print("<br />\r\n");
client.print("<hr />\r\n");
// Wskaźnik temperatury w F
float hif = dht.computeHeatIndex(f, h);
// Wskaźnik temp. w C (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
client.print("<br />\r\n");
client.print("Wilgotnosc: "); //wolgotność powietrza DHT22
client.print(h);
client.print(" %\t");
client.print("<br />\r\n");
client.print("<br />\r\n");
client.print("Temperatura: "); //temperatura DHT22
client.print(t);
client.print(" *C, ");
client.print(f);
client.print(" *F\t");
client.print("<br />\r\n");
client.print("<br />\r\n");
client.print("Heat index: ");
client.print(hic);
client.print(" *C, ");
client.print(hif);
client.println(" *F");
client.print("<br />\r\n");
client.print("</BODY>\r\n");
client.print("</HTML>\n");
delay(1);
client.stop(); //czytaj opis przycisku przy www
if (readStrings.indexOf("?button1on") > 0) {
Serial.println("1 on");
digitalWrite(pin1, HIGH);
delay (75) ; //odczekaj
digitalWrite(pin1, LOW);
}
if (readStrings.indexOf("?button1off") > 0) {
Serial.println("1 off");
digitalWrite(pin1, HIGH);
}
if (readStrings.indexOf("?button2on") > 0) {
Serial.println("2 on");
digitalWrite(pin2, LOW);
}
if (readStrings.indexOf("?button2off") > 0) {
Serial.println("2 off");
digitalWrite(pin2, HIGH);
}
if (readStrings.indexOf("?button3on") > 0) {
digitalWrite(pin3, LOW);
Serial.println("3 on");
}
if (readStrings.indexOf("?button3off") > 0) {
digitalWrite(pin3, HIGH);
Serial.println("3 off");
}
if (readStrings.indexOf("?button4on") > 0) {
digitalWrite(pin4, LOW);
Serial.println("4 on");
}
if (readStrings.indexOf("?button4off") > 0) {
digitalWrite(pin4, HIGH);
Serial.println("4 off");
}
if (readStrings.indexOf("?buttonallon") > 0) {
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
}
if (readStrings.indexOf("?buttonalloff") > 0) {
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
}
readStrings = "";
}//if(c == '\n')
}//if(client.available())
}//while(client.connected())
}//if(client)
}//void loop()
Kod: Zaznacz cały
#include <ESP8266WiFi.h>
#include "DHT.h"
Kod: Zaznacz cały
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
const char* ssid = "........";
const char* password = "........";
ESP8266WebServer server(80);
const char* www_username = "admin";
const char* www_password = "esp8266";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if(WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Connect Failed! Rebooting...");
delay(1000);
ESP.restart();
}
ArduinoOTA.begin();
server.on("/", [](){
if(!server.authenticate(www_username, www_password))
return server.requestAuthentication();
server.send(200, "text/plain", "Login OK");
});
server.begin();
Serial.print("Open http://");
Serial.print(WiFi.localIP());
Serial.println("/ in your browser to see it working");
}
void loop() {
ArduinoOTA.handle();
server.handleClient();
}