วันอาทิตย์ที่ 21 ตุลาคม พ.ศ. 2561

ESP8266 web server Home IOT




ESP8266 


Use CH340G repalce the CP2102.
Open-source, Interactive, Programmable, Low cost, Simple, Smart, WI-FI enabled
FOR Arduino like hardware IO
Advanced API for hardware IO, which can dramatically reduce the redundant work for configuring and manipulating hardware.
Code like FOR arduino, but interactively in Lua script.
Nodejs style network API
Event-driven API for network applicaitons,
which faciliates developers writing code running on a 5mm*5mm sized MCU in Nodejs style.
Greatly speed up your IOT application developing process.
Lowest cost WI-FI
Less than $3 WI-FI MCU ESP8266 integrated and esay to prototyping development kit.
We provide the best platform for IOT application development at the lowest cost.
ขั้นตอนการลงโปรแกรม และ ติดตั้ง
1.        โหลดIDE เวอร์ชั่น 1.6.5 ถ้าตัวอื่นก็ลองดูได้ครับผมใช้ตัวนี้ โหลดจาก   https://www.arduino.cc/en/Main/Software

เลื่อนลงมาอีกนิดจะมีให้เลือกเวอร์ชั่น
Previous Release

การติดตั้ง Board ESP8266IDE Sketch ที่แนะนำคือ เวอร์ชั่น 1.6.5



File >Preferences>


http://arduino.esp8266.com/stable/package_esp8266com_index.json






ขั้นตอนการตรวจสอบว่าบอร์ดติดตั้งหรือยัง



MAP I/O




ต่อวงจรสำหรับ โรเจคนี้




Please Donate for long life blog
ขอเชิญร่วมบริจาค เพื่อสนับสนุนทุนการทำโครงงานต่อไป



Code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

MDNSResponder mdns;

// Replace with your network credentials
const char* ssid = "santi";
const char* password = "santi12345678";

ESP8266WebServer server(80);

String webPage = "";

Int  gpio16_pin = D0;  
int  gpio5_pin = D1;
int  gpio4_pin = D2;
int  gpio0_pin = D3;
void setup(void){
  webPage += "<h1>A-Arduino Farm</h1><p>pump#1 <a href=\"socket1On\"><button>ON</button></a>&nbsp;<a href=\"socket1Off\"><button>OFF</button></a></p>";
  webPage += "<p>Pump#2 <a href=\"socket2On\"><button>ON</button></a>&nbsp;<a href=\"socket2Off\"><button>OFF</button></a></p>";
  webPage += "<p>Pump#3 <a href=\"socket3On\"><button>ON</button></a>&nbsp;<a href=\"socket3Off\"><button>OFF</button></a></p>";
  webPage += "<p>Pump#4 <a href=\"socket4On\"><button>ON</button></a>&nbsp;<a href=\"socket4Off\"><button>OFF</button></a></p>";
 
  // preparing GPIOs
  pinMode(gpio16_pin, OUTPUT);
  digitalWrite(gpio16_pin, HIGH);
 
  pinMode(gpio5_pin, OUTPUT);
  digitalWrite(gpio5_pin, HIGH);

  pinMode(gpio4_pin, OUTPUT);
  digitalWrite(gpio4_pin, HIGH);

  pinMode(gpio0_pin, OUTPUT);
  digitalWrite(gpio0_pin, HIGH);
 
  delay(1000);
  Serial.begin(115200);
  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(gpio16_pin, LOW);
    delay(1000);
  });
  server.on("/socket1Off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio16_pin, HIGH);
    delay(1000);
  });
  server.on("/socket2On", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio5_pin, LOW);
    delay(1000);
  });
  server.on("/socket2Off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio5_pin, HIGH);
    delay(1000);
  });


//////////////////////

server.on("/socket3On", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio4_pin, LOW);
    delay(1000);
  });
  server.on("/socket3Off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio4_pin, HIGH);
    delay(1000);
  });
  server.on("/socket4On", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio0_pin, LOW);
    delay(1000);
  });
  server.on("/socket4Off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio0_pin, HIGH);
    delay(1000);
  });

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

void loop(void){
  server.handleClient();
}































ไม่มีความคิดเห็น:

แสดงความคิดเห็น