

Code: Select all
/*
* NodeMCU/ESP8266 act as Client (WIFI-STA) and simplest Web Server
* to control GPIO (on-board LED)
* Connect to WIFI "nazwa sieci wifi", password = "haslo do wifi"
* check ip adress, and open browser
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define LoadPin 2// GPIO2
const char *ssid = "nazwa sieci wifi"; // wpisujemy własne dane
const char *password = "haslo do wifi"; // wpisujemy własne dane
int stateLED = LOW;
const byte miBufferON[] = {0xA0, 0x01, 0x01, 0xA2};
const byte miBufferOFF[] = {0xA0, 0x01, 0x00, 0xA1};
ESP8266WebServer server(80);
void handleRoot() {
response();
}
void handleLedOn() {
stateLED = LOW;
digitalWrite(LoadPin, stateLED);
Serial.write(miBufferON, sizeof(miBufferON));
response();
}
void handleLedOff() {
stateLED = HIGH;
Serial.write(miBufferOFF, sizeof(miBufferOFF));
digitalWrite(LoadPin, stateLED);
response();
}
const String HtmlHtml = "<html><head>"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /></head>";
const String HtmlHtmlClose = "</html>";
const String HtmlTitle = "<h1>RELAY</h1><br/>\n"; //wpisujemy własną nazwę przekaźnika
const String HtmlLedStateLow = "<big>is now <b>ON</b></big><br/>\n";
const String HtmlLedStateHigh = "<big>is now <b>OFF</b></big><br/>\n";
const String HtmlButtons =
"<a href=\"LEDOn\"><button style=\"display: block; width: 100%;\">ON</button></a><br/>"
"<a href=\"LEDOff\"><button style=\"display: block; width: 100%;\">OFF</button></a><br/>";
void response(){
String htmlRes = HtmlHtml + HtmlTitle;
if(stateLED == LOW){
htmlRes += HtmlLedStateLow;
}else{
htmlRes += HtmlLedStateHigh;
}
htmlRes += HtmlButtons;
htmlRes += HtmlHtmlClose;
server.send(200, "text/html", htmlRes);
}
void setup() {
Serial.begin(9600); // Start the Serial communication to send messages to the computer
delay(1000);
Serial.println('\n');
WiFi.mode(WIFI_STA);
delay(50000); //wait 50sec
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
server.on("/", handleRoot);
server.on("/LEDOn", handleLedOn);
server.on("/LEDOff", handleLedOff);
server.begin();
Serial.println('\n');
Serial.println("HTTP server beginned");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
pinMode(LoadPin, OUTPUT);
digitalWrite(LoadPin, stateLED);
}
void loop() {
server.handleClient();
}
Nie wiem jak w supli jest to rozwiązane, lecz ŁATWEesp ma opcję ustawienia: Communication - Serial Server w zakładce devices.const byte miBufferON[] = {0xA0, 0x01, 0x01, 0xA2};
const byte miBufferOFF[] = {0xA0, 0x01, 0x00, 0xA1};
Code: Select all
echo -e '\xA0\x01\x00\xA1' | nc <adresIP-ESP8266> <port serial servera> -w 1
oraz
echo -e '\xA0\x01\x01\xA2' | nc <adresIP-ESP8266> <port serial servera> -w 1