mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
WiFi: improve WiFiClient(Basic) examples (#5197)
WiFiClient no longer depends on now-defunct data.sparkfun.com service, but uses a TCP "quote of the day" service instead. fixes #4088
This commit is contained in:
@ -1,11 +1,18 @@
|
||||
/*
|
||||
This sketch sends a message to a TCP server
|
||||
|
||||
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>
|
||||
|
||||
const char* ssid = "your-ssid";
|
||||
const char* password = "your-password";
|
||||
|
||||
const char* host = "192.168.1.1";
|
||||
const uint16_t port = 3000;
|
||||
|
||||
ESP8266WiFiMulti WiFiMulti;
|
||||
|
||||
void setup() {
|
||||
@ -14,7 +21,7 @@ void setup() {
|
||||
|
||||
// We start by connecting to a WiFi network
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFiMulti.addAP("SSID", "passpasspass");
|
||||
WiFiMulti.addAP(ssid, password);
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
@ -35,13 +42,10 @@ void setup() {
|
||||
|
||||
|
||||
void loop() {
|
||||
const uint16_t port = 80;
|
||||
const char * host = "192.168.1.1"; // ip or dns
|
||||
|
||||
|
||||
|
||||
Serial.print("connecting to ");
|
||||
Serial.println(host);
|
||||
Serial.print(host);
|
||||
Serial.print(':');
|
||||
Serial.println(port);
|
||||
|
||||
// Use WiFiClient class to create TCP connections
|
||||
WiFiClient client;
|
||||
@ -54,9 +58,10 @@ void loop() {
|
||||
}
|
||||
|
||||
// This will send the request to the server
|
||||
client.println("Send this data to 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);
|
||||
|
||||
|
Reference in New Issue
Block a user