首先c语言中的一切函数库在wemos中都可以直接调用,比如下面的strstr函数和memset函数
可以根据参数的类型来进行定义变量,然后再进行类型的转化。
#include<ESP8266WiFi.h>
char *ssid="who lian who shabi";
char *passward="QWEasd147852369";
const uint16_t port = 8080;
const char * host = "192.168.1.214"; // ip
WiFiClient client;
void initWifiSta()
{
WiFi.mode(WIFI_STA); //设置STA模式
WiFi.begin(ssid, passward); //连接网络
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
}
Serial.print("WiFi connected, local IP address:");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
initWifiSta();
if (!client.connect(host, port)) {
while(1){ Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);}
return;
}
else
{
Serial.println("connect to tcp server success.");
Serial.println("Send this data to tcp server");
client.println(String("hello tcp server"));
}
}
void loop() {
String recv_data = client.readStringUntil('\r');
Serial.println(recv_data);
uint8_t cmd[]={'\0'};
while(1){
if(client.available()>0){
client.read(cmd,12);
if(strstr((char *)cmd,"kaiktiao")){
Serial.println((char *)cmd);
}
memset(cmd,'\0',12);
client.flush();
}
}
if (0 == recv_data.compareTo("exit"))
{
Serial.println("closing connection");
client.stop();
}
delay(200);
}