Xiao ESP32C3 (in arduino-esp32 framework) act as socket client, connect to HC-25 WiFi Module.
Last post show my first test HC-25 WiFi Module, with AT Command; act as AP, setup socket server. Here is my exercise using Xiao ESP32C3 (in arduino-esp32 framework) to implement the client side: act as STA, join HC-25 WiFi network, connect to HC-25 socket server, receive and echo back in upper case.
Exercise code:
XiaoESP32C3_TCPclient.ino
XiaoESP32C3_TCPclient_i2c_SSD1306.ino
Exercise code:
XiaoESP32C3_TCPclient.ino
/* A simple TCP client run on ESP32-C3,
* connect socket server run on HC-25 as AP.
*
* Have to set WAP in HC-25 using AT command:
* AT+WMODE=AP
* AT+WAP=myHC-25,password
*
* 192.168.4.1:8080 is the socket server on HC-25
*/
#include <WiFi.h>
// AP of HC-25
const char *ssid = "myHC-25";
const char *password = "password";
// server IP and port
const IPAddress serverIP(192,168,4,1);
uint16_t serverPort = 8080;
WiFiClient client;
void setup() {
delay(200);
Serial.begin(115200);
delay(200);
Serial.println("\n\r- Socket Client run on Xiao ESP32C3 (arduino-esp32) -");
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
delay(500);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected");
Serial.print("My IP Address:");
Serial.println(WiFi.localIP());
}
void loop() {
Serial.println("Connect to TCP Server: "
+ serverIP.toString() + ":"
+ String(serverPort));
if (client.connect(serverIP, serverPort))
{
Serial.println("TCP Server connected");
client.print("\nHello from Xiao ESP32C3 (arduino-esp32) at " + WiFi.localIP().toString() + "\n\r");
while (client.connected() || client.available())
{
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
line = line + "\n";
line.toUpperCase();
client.write(line.c_str());
}
}
Serial.println("Stop TCP conection");
client.stop();
}
else {
Serial.println("Fail to connect TCP Server");
client.stop();
}
delay(5000);
}
XiaoESP32C3_TCPclient_i2c_SSD1306.ino
/* A simple TCP client run on ESP32-C3,
* connect socket server run on HC-25 as AP.
* with display of I2C SSD1306 128x64 OLED
*
* Have to set WAP in HC-25 using AT command:
* AT+WMODE=AP
* AT+WAP=myHC-25,password
*
* 192.168.4.1:8080 is the socket server on HC-25
*/
#include <WiFi.h>
#include <U8g2lib.h>
//- For I2C SSD1306 -
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// AP of HC-25
const char *ssid = "myHC-25";
const char *password = "password";
// server IP and port
const IPAddress serverIP(192,168,4,1);
uint16_t serverPort = 8080;
WiFiClient client;
void displayStr(u8g2_uint_t x, u8g2_uint_t y, const char *s){
u8g2.clearBuffer();
u8g2.drawStr(x, y, s);
u8g2.sendBuffer();
}
void setup() {
delay(200);
Serial.begin(115200);
delay(200);
Serial.println("\n\r- Socket Client run on Xiao ESP32C3 (arduino-esp32) -");
Serial.println();
u8g2.begin();
u8g2.setFont(u8g2_font_helvB10_tf);
displayStr(0, 14, "- Client Start -");
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected");
displayStr(0, 14, "- WiFi Connected -");
Serial.print("My IP Address:");
Serial.println(WiFi.localIP());
}
void loop() {
Serial.println("Connect to TCP Server: "
+ serverIP.toString() + ":"
+ String(serverPort));
if (client.connect(serverIP, serverPort))
{
Serial.println("TCP Server connected");
displayStr(0, 14, "Socket Connected");
client.print("\nHello from Xiao ESP32C3 (arduino-esp32) at " + WiFi.localIP().toString() + "\n\r");
while (client.connected() || client.available())
{
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
displayStr(0, 20, line.c_str());
line = line + "\n";
line.toUpperCase();
client.write(line.c_str());
}
}
Serial.println("Stop TCP conection");
client.stop();
}
else {
Serial.println("Fail to connect TCP Server");
client.stop();
}
delay(5000);
}
For SSD1306 I2C OLED, please read Xiao ESP32C3 display on SSD1306 128x64 OLED (I2C & SPI) using U8g2 Library.
Comments
Post a Comment