mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-27 21:16:50 +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:
parent
a1e59e9c01
commit
d17ffc2874
@ -1,9 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
This sketch sends data via HTTP GET requests to data.sparkfun.com service.
|
This sketch establishes a TCP connection to a "quote of the day" service.
|
||||||
|
It sends a "hello" message, and then prints received data.
|
||||||
You need to get streamId and privateKey at data.sparkfun.com and paste them
|
|
||||||
below. Or just customize this script to talk to other HTTP servers.
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
@ -11,9 +8,8 @@
|
|||||||
const char* ssid = "your-ssid";
|
const char* ssid = "your-ssid";
|
||||||
const char* password = "your-password";
|
const char* password = "your-password";
|
||||||
|
|
||||||
const char* host = "data.sparkfun.com";
|
const char* host = "djxmmx.net";
|
||||||
const char* streamId = "....................";
|
const uint16_t port = 17;
|
||||||
const char* privateKey = "....................";
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@ -43,54 +39,44 @@ void setup() {
|
|||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
}
|
}
|
||||||
|
|
||||||
int value = 0;
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
delay(5000);
|
|
||||||
++value;
|
|
||||||
|
|
||||||
Serial.print("connecting to ");
|
Serial.print("connecting to ");
|
||||||
Serial.println(host);
|
Serial.print(host);
|
||||||
|
Serial.print(':');
|
||||||
|
Serial.println(port);
|
||||||
|
|
||||||
// Use WiFiClient class to create TCP connections
|
// Use WiFiClient class to create TCP connections
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
const int httpPort = 80;
|
if (!client.connect(host, port)) {
|
||||||
if (!client.connect(host, httpPort)) {
|
|
||||||
Serial.println("connection failed");
|
Serial.println("connection failed");
|
||||||
|
delay(5000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We now create a URI for the request
|
// This will send a string to the server
|
||||||
String url = "/input/";
|
Serial.println("sending data to server");
|
||||||
url += streamId;
|
client.println("hello from ESP8266");
|
||||||
url += "?private_key=";
|
|
||||||
url += privateKey;
|
|
||||||
url += "&value=";
|
|
||||||
url += value;
|
|
||||||
|
|
||||||
Serial.print("Requesting URL: ");
|
|
||||||
Serial.println(url);
|
|
||||||
|
|
||||||
// This will send the request to the server
|
|
||||||
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
|
|
||||||
"Host: " + host + "\r\n" +
|
|
||||||
"Connection: close\r\n\r\n");
|
|
||||||
unsigned long timeout = millis();
|
unsigned long timeout = millis();
|
||||||
while (client.available() == 0) {
|
while (client.available() == 0) {
|
||||||
if (millis() - timeout > 5000) {
|
if (millis() - timeout > 5000) {
|
||||||
Serial.println(">>> Client Timeout !");
|
Serial.println(">>> Client Timeout !");
|
||||||
client.stop();
|
client.stop();
|
||||||
|
delay(60000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read all the lines of the reply from server and print them to Serial
|
// Read all the lines of the reply from server and print them to Serial
|
||||||
while (client.available() || client.connected()) {
|
Serial.println("receiving from remote server");
|
||||||
String line = client.readStringUntil('\r');
|
while (client.available()) {
|
||||||
Serial.print(line);
|
char ch = static_cast<char>(client.read());
|
||||||
|
Serial.print(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the connection
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("closing connection");
|
Serial.println("closing connection");
|
||||||
}
|
client.stop();
|
||||||
|
|
||||||
|
delay(300000); // execute once every 5 minutes, don't flood remote service
|
||||||
|
}
|
||||||
|
@ -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 <ESP8266WiFi.h>
|
||||||
#include <ESP8266WiFiMulti.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;
|
ESP8266WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
@ -14,7 +21,7 @@ void setup() {
|
|||||||
|
|
||||||
// We start by connecting to a WiFi network
|
// We start by connecting to a WiFi network
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFiMulti.addAP("SSID", "passpasspass");
|
WiFiMulti.addAP(ssid, password);
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println();
|
Serial.println();
|
||||||
@ -35,13 +42,10 @@ void setup() {
|
|||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
const uint16_t port = 80;
|
|
||||||
const char * host = "192.168.1.1"; // ip or dns
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Serial.print("connecting to ");
|
Serial.print("connecting to ");
|
||||||
Serial.println(host);
|
Serial.print(host);
|
||||||
|
Serial.print(':');
|
||||||
|
Serial.println(port);
|
||||||
|
|
||||||
// Use WiFiClient class to create TCP connections
|
// Use WiFiClient class to create TCP connections
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
@ -54,9 +58,10 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This will send the request to the server
|
// 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
|
//read back one line from server
|
||||||
|
Serial.println("receiving from remote server");
|
||||||
String line = client.readStringUntil('\r');
|
String line = client.readStringUntil('\r');
|
||||||
Serial.println(line);
|
Serial.println(line);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user