#include "ESP8266.h" #include <SoftwareSerial.h>
#define SSID "와이파이 이름" #define PASSWORD "비밀번호" #define HOST_PORT 23 /* 포트번호*/ #define LED 8 /* LED GPIO*/ SoftwareSerial esp(2, 3); /* RX:D2, TX:D3 */ ESP8266 wifi(esp, 9600);
void setup(void) { pinMode(LED, OUTPUT);
Serial.begin(9600); Serial.print("setup begin\r\n"); Serial.print("FW Version:"); Serial.println(wifi.getVersion().c_str());
if (wifi.joinAP(SSID, PASSWORD)) { Serial.print("Join AP success\r\n"); Serial.print("IP: "); Serial.println(wifi.getLocalIP().c_str()); } else { Serial.print("Join AP failure\r\n"); } if (wifi.enableMUX()) { Serial.print("multiple ok\r\n"); } else { Serial.print("multiple err\r\n"); } if (wifi.(HOST_PORT)) { Serial.print("start tcp server ok\r\n"); } else { Serial.print("start tcp server err\r\n"); } if (wifi.setTCPServerTimeout(10)) { Serial.print("set tcp server timout 10 seconds\r\n"); } else { Serial.print("set tcp server timout err\r\n"); } Serial.print("setup end\r\n"); }
void loop(void) { uint8_t buffer[128] = {0}; uint8_t mux_id; uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 10000);
if (len > 0){
Serial.print("Received:["); for (uint32_t i = 0; i < len-1; i++) { Serial.print((char)buffer[i]); } Serial.print("]\r\n");
char cmd = buffer[0]; int ledStatus = digitalRead(LED);
switch (cmd) { case '0': if (ledStatus == HIGH) { digitalWrite(LED, LOW); sprintf(buffer, "LED is off.\n"); wifi.send(mux_id, buffer, strlen(buffer)); } else { sprintf(buffer, "LED is already off.\n"); wifi.send(mux_id, buffer, strlen(buffer)); } break; case '1': if (ledStatus == LOW) { digitalWrite(LED, HIGH); sprintf(buffer, "LED is on.\n"); wifi.send(mux_id, buffer, strlen(buffer)); } else { sprintf(buffer, "LED is already on.\n"); wifi.send(mux_id, buffer, strlen(buffer)); } break; default: break; } } } |