#include
#include
#include
#define LED D9
long debouncdDelay = 500;
long lastDebounceTime = 0;
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, D6, D5, U8X8_PIN_NONE);
#define AP_SSID "vivoX20A"
#define AP_PASSWORD "12345678"
#define DebugBegin(baud_rate) Serial.begin(baud_rate)
#define DebugPrintln(message) Serial.println(message)
#define DebugPrint(message) Serial.print(message)
IPAddress local_IP(192, 168, 4, 22);
IPAddress gateway(192, 168, 4, 9);
IPAddress subnet(255, 255, 255, 0);
const uint16_t port = 1234;
const char * host = "192.168.43.1";
uint8_t TX_Buffter[10] = {0xAA, 0X55, 0x12, 0x54, 0x85,0x12, 0x54, 0x85,0x0d,0x0a};
WiFiClient client;
uint8_t mills_val = 10;
uint8_t Rx_size = 0;
uint8_t RX_buffer[50] = {0};
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
digitalWrite(D5, 0);
digitalWrite(D6, 0);
u8g2.begin();
u8g2.enableUTF8Print();
DebugBegin(115200);
DebugPrint("**************************Setting STA*************************" );
DebugPrint("/n");
WiFi.mode(WIFI_STA);
WiFi.begin(AP_SSID, AP_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
DebugPrint(".");
DebugPrint(WiFi.status());
}
if (WiFi.status() == WL_CONNECTED)
{
DebugPrintln(String("Connected, mac address: ") + WiFi.macAddress().c_str());
DebugPrintln(String("Connected, IP address: ") + WiFi.localIP().toString());
DebugPrintln(String("Subnet mask: ") + WiFi.subnetMask().toString());
DebugPrintln(String("Gataway IP: ") + WiFi.gatewayIP().toString());
DebugPrintln(String("Default hostname: ") + WiFi.hostname());
WiFi.hostname("Station_host_1");
DebugPrintln(String("New hostname: ") + WiFi.hostname());
DebugPrintln(String("SSID: ") + WiFi.SSID());
DebugPrintln(String("psk: ") + WiFi.psk());
DebugPrintln(String("BSSID: ") + WiFi.BSSIDstr());
DebugPrintln(String("RSSI: ") + WiFi.RSSI() + " dBm");
}
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_5x8_tf);
u8g2.setFontDirection(0);
u8g2.drawStr(0, 8, " AP_SSID:");
u8g2.drawStr(50, 8, AP_SSID);
u8g2.drawStr(0, 18, " AP_PSW:");
u8g2.drawStr(50, 18, AP_PASSWORD);
} while ( u8g2.nextPage() );
ESP.wdtEnable(5000);
}
void loop() {
ESP.wdtFeed();
if (millis() - lastDebounceTime > debouncdDelay)
{
mills_val++;
if (mills_val > 10)
{
mills_val = 0;
if (!client.connected())
{
if (!client.connect(host, port))
{
DebugPrintln("connection fail");
}
else
{
DebugPrintln("connection ok");
}
}
}
lastDebounceTime = millis();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_5x8_tf);
u8g2.setFontDirection(0);
u8g2.drawStr(0, 8, " AP_SSID:");
u8g2.drawStr(50, 8, AP_SSID);
u8g2.drawStr(0, 18, " AP_PSW:");
u8g2.drawStr(50, 18, AP_PASSWORD);
if (WiFi.status() == WL_CONNECTED)
{
u8g2.drawStr(0, 28, " RSSI:");
u8g2.setCursor(50, 28);
u8g2.print( WiFi.RSSI());
}
else
{
u8g2.clearBuffer();
u8g2.drawStr(0, 28, "WL_DISCONNECTED");
u8g2.sendBuffer();
}
} while ( u8g2.nextPage() );
digitalWrite(D9, !digitalRead(D9));
}
if (client.available())
{
Rx_size = client.available();
client.read(RX_buffer, Rx_size);
Serial.write(RX_buffer, Rx_size);
client.flush();
if ((RX_buffer[Rx_size-1] == 0x0A)&& (RX_buffer[Rx_size-2] == 0x0D))
{
client.write(TX_Buffter,10);
}
}
}