70 lines
1.8 KiB
C++
70 lines
1.8 KiB
C++
#include <WiFi.h>
|
|
const char* ssid = "yourssid";
|
|
const char* password = "12345678";
|
|
const char* host = "192.168.4.1";
|
|
WiFiClient client;
|
|
uint16_t port = 9100;
|
|
uint8_t temp;
|
|
uint8_t buffer[512];
|
|
void taskConnectHost(void *param);
|
|
void taskReceive(void *parm);
|
|
void setup()
|
|
{
|
|
Serial.begin(921600);
|
|
delay(10);
|
|
// We start by connecting to a WiFi network
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
xTaskCreate(taskConnectHost,"taskConnectHost",2048,NULL,1,NULL);
|
|
xTaskCreate(taskReceive,"taskReceive",2048,NULL,1,NULL);
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
|
|
}
|
|
////////////////////////////////////////////////////////////
|
|
//
|
|
void taskConnectHost(void *param){
|
|
Serial.println("taskConnectHost created...");
|
|
for(;;){
|
|
if(!client.connected()){
|
|
Serial.println("connecting to ");
|
|
Serial.printf("%s %d\n",host,port);
|
|
client.connect(host, port);
|
|
}
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
}
|
|
}
|
|
///////////////////////////////////////////
|
|
//
|
|
void taskReceive(void *parm){
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
Serial.println("taskReceive created...");
|
|
for(;;){
|
|
if (client.connected()) {
|
|
while (client.available() != 0) {
|
|
if (client.available()<=512){
|
|
int cnt = client.available();
|
|
client.read(buffer,cnt);
|
|
Serial.write(buffer,cnt);
|
|
}else{
|
|
client.read(buffer,512);
|
|
Serial.write(buffer,512);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |