본문 바로가기

IOT(Arduino+Inventor)

esp8266_LED_On_OFF

LED ON OFF를 제어합니다.

핀 D4이며, gpio2 입니다.

아래 Arduino Code를 업로드 합니다. 

Components를 합니다.

properties는 CSS입니다.

마음껏 꾸며, 보세요.

블럭코드

 

 

 

#include <ESP8266WiFi.h>

const char* ssid = "IOT999";
const char* password = "kbs48752";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
Serial.begin(9600);
delay(1500);

// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, LOW);

// Connect to WiFi network
Serial.println();
Serial.println();
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");

// Start the server
server.begin();
Serial.println("Server started");

// Print the IP address
Serial.println(WiFi.localIP());
}

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 req = client.readStringUntil('\r');
Serial.println(req);
client.flush();

// Match the request
int val;
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else {
Serial.println("invalid request");
client.stop();
return;
}

// Set GPIO2 according to the request
digitalWrite(2, val);

client.flush();

// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";

// Send the response to the client
client.println(s);
delay(1);
Serial.println("Client disonnected");

// The client will actually be disconnected 
// when the function returns and 'client' object is detroyed
}

 

'IOT(Arduino+Inventor)' 카테고리의 다른 글

ESP8266온도_흙 수분_LED  (1) 2023.03.16
ESP8266_AM2320_LED  (0) 2023.03.14
esp8266_led_On_off  (0) 2023.03.12
온도센서_AM2320_ECP8266_LED_E  (0) 2023.03.07
온도센서_AM2320_ESP8266_Inventor_D  (0) 2023.03.06