Permalink
Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upArduino/libraries/ESP8266WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino
Go to file* update examples * fix serial<->tcp example, use STASSID instead of SSID (name collision) * fix HTTPSRequest.ino * update AxTLS HTTPS examples, update AxTLS API to deprecated * fixes * fixes + fix astyle (no preproc directives) + restyling script * fix HTTPClient library * fixes * common.sh: do not reload arduino when already present (for locally CI testing) * common.sh: do not reload ArduinoJson when already present (for locally CI testing) * fix * fix * fix deprecated example * fix WiFiHTTPSServer.ino * reduce footprint * wipfix * fix led builtin * fix example * finished updating APSSID on all examples * style * restyle examples * helper to run CI test locally * local CI runner more verbose * +const * deprecation deprecation * deprecation * Update NTPClient.ino const char[] => const char * * Update interactive.ino const char[] => const char *
/* | |
This sketch sends a string to a TCP server, and prints a one-line response. | |
You must run a TCP server in your local network. | |
For example, on Linux you can use this command: nc -v -l 3000 | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#ifndef STASSID | |
#define STASSID "your-ssid" | |
#define STAPSK "your-password" | |
#endif | |
const char* ssid = STASSID; | |
const char* password = STAPSK; | |
const char* host = "192.168.1.1"; | |
const uint16_t port = 3000; | |
ESP8266WiFiMulti WiFiMulti; | |
void setup() { | |
Serial.begin(115200); | |
// We start by connecting to a WiFi network | |
WiFi.mode(WIFI_STA); | |
WiFiMulti.addAP(ssid, password); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Wait for WiFi... "); | |
while (WiFiMulti.run() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
delay(500); | |
} | |
void loop() { | |
Serial.print("connecting to "); | |
Serial.print(host); | |
Serial.print(':'); | |
Serial.println(port); | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
if (!client.connect(host, port)) { | |
Serial.println("connection failed"); | |
Serial.println("wait 5 sec..."); | |
delay(5000); | |
return; | |
} | |
// This will send the request to the server | |
client.println("hello from ESP8266"); | |
//read back one line from server | |
Serial.println("receiving from remote server"); | |
String line = client.readStringUntil('\r'); | |
Serial.println(line); | |
Serial.println("closing connection"); | |
client.stop(); | |
Serial.println("wait 5 sec..."); | |
delay(5000); | |
} | |