pasang

ESP8266 Web Server

Example Sketch: AP Web Server

ESP8266 Tidak hanya dapat terhubung ke jaringan WiFi dan berinteraksi dengan internet, tetapi juga dapat membuat jaringan sendiri, yang memungkinkan perangkat lain untuk terhubung langsung pada ESP8266. Contoh ini menunjukkan bagaimana untuk mengubah ESP8266 menjadi titik akses (AP), dan melayani sampai halaman web untuk setiap klien yang terhubung.
Copy dan paste kode dari bawah, atau download
language:c
#include <ESP8266WiFi.h>
//////////////////////
//////////////////////
// WiFi Definitions //
/////////////////////
const char WiFiAPPSK[] = "sparkfun"; // Pin Definitions //
const int LED_PIN = 5; // Thing's onboard, green LED
/////////////////////
const int DIGITAL_PIN = 12; // Digital pin to be read
const int ANALOG_PIN = A0; // The only analog pin on the Thing
server.begin();
WiFiServer server(80); void setup() { initHardware(); setupWiFi(); }
WiFiClient client = server.available();
void loop() { // Check if a client has connected if (!client) { return; }
Serial.println(req);
// Read the first line of the request String req = client.readStringUntil('\r'); client.flush();
// request type (read/set) and value if set.
// Match the request int val = -1; // We'll use 'val' to keep track of both the if (req.indexOf("/led/0") != -1)
else if (req.indexOf("/read") != -1)
val = 0; // Will write LED low else if (req.indexOf("/led/1") != -1) val = 1; // Will write LED high val = -2; // Will print pin reads
digitalWrite(LED_PIN, val);
// Otherwise request will be invalid. We'll say as much in HTML // Set GPIO5 according to the request if (val >= 0) client.flush(); // Prepare the response. Start with the common header:
s += "LED is now ";
String s = "HTTP/1.1 200 OK\r\n"; s += "Content-Type: text/html\r\n\r\n"; s += "<!DOCTYPE HTML>\r\n<html>\r\n"; // If we're setting the LED, print out a message saying we did if (val >= 0) { s += (val)?"on":"off"; }
s += String(digitalRead(DIGITAL_PIN));
else if (val == -2) { // If we're reading pins, print out those values: s += "Analog Pin = "; s += String(analogRead(ANALOG_PIN)); s += "<br>"; // Go to the next line. s += "Digital Pin 12 = "; } else {
// when the function returns and 'client' object is detroyed
s += "Invalid Request.<br> Try /led/1, /led/0, or /read."; } s += "</html>\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected } void setupWiFi() { WiFi.mode(WIFI_AP);
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
// Do a little work to get a unique-ish name. Append the // last two bytes of the MAC (HEX'd) to "Thing-": uint8_t mac[WL_MAC_ADDR_LENGTH]; WiFi.softAPmacAddress(mac); String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) + macID.toUpperCase(); String AP_NameString = "ESP8266 Thing " + macID;
pinMode(LED_PIN, OUTPUT);
char AP_NameChar[AP_NameString.length() + 1]; memset(AP_NameChar, 0, AP_NameString.length() + 1); for (int i=0; i<AP_NameString.length(); i++) AP_NameChar[i] = AP_NameString.charAt(i); WiFi.softAP(AP_NameChar, WiFiAPPSK); } void initHardware() { Serial.begin(115200); pinMode(DIGITAL_PIN, INPUT_PULLUP); digitalWrite(LED_PIN, LOW);
}
// Don't need to set ANALOG_PIN as input,
// that's all it can be.
Setelah meng-upload sketsa ini, menemukan perangkat lain yang dapat Anda terhubung ke jaringan WiFi - ponsel, laptop, dll Carilah jaringan disebut "Thing-XXXX", di mana XXXX adalah 2 byte terakhir dari alamat MAC Thing.

sketsa menetapkan password jaringan untuk "sparkfun". Setelah tersambung ke jaringanThing’s AP network, memuat browser dan arahkan ke 192.168.4.1/read. Hal yang harus melayani sampai halaman web yang menunjukkan Anda ADC dan digital pin 12 :

The Thing serving up a web page
Setelah itu, berikan 192.168.4.1/led/0 dan 192.168.4.1/led/1 

Share this:

Posting Komentar

 
Copyright © Nandan Hunter. Designed by OddThemes | Distributed By Gooyaabi Templates