|
1 | 1 | /* |
2 | | - This sketch sends data via HTTP GET requests to data.sparkfun.com service. |
3 | | -
|
4 | | - You need to get streamId and privateKey at data.sparkfun.com and paste them |
5 | | - below. Or just customize this script to talk to other HTTP servers. |
6 | | -
|
| 2 | + This sketch establishes a TCP connection to a "quote of the day" service. |
| 3 | + It sends a "hello" message, and then prints received data. |
7 | 4 | */ |
8 | 5 |
|
9 | 6 | #include <ESP8266WiFi.h> |
10 | 7 |
|
11 | 8 | const char* ssid = "your-ssid"; |
12 | 9 | const char* password = "your-password"; |
13 | 10 |
|
14 | | -const char* host = "data.sparkfun.com"; |
15 | | -const char* streamId = "...................."; |
16 | | -const char* privateKey = "...................."; |
| 11 | +const char* host = "djxmmx.net"; |
| 12 | +const uint16_t port = 17; |
17 | 13 |
|
18 | 14 | void setup() { |
19 | 15 | Serial.begin(115200); |
@@ -43,54 +39,44 @@ void setup() { |
43 | 39 | Serial.println(WiFi.localIP()); |
44 | 40 | } |
45 | 41 |
|
46 | | -int value = 0; |
47 | | - |
48 | 42 | void loop() { |
49 | | - delay(5000); |
50 | | - ++value; |
51 | | - |
52 | 43 | Serial.print("connecting to "); |
53 | | - Serial.println(host); |
| 44 | + Serial.print(host); |
| 45 | + Serial.print(':'); |
| 46 | + Serial.println(port); |
54 | 47 |
|
55 | 48 | // Use WiFiClient class to create TCP connections |
56 | 49 | WiFiClient client; |
57 | | - const int httpPort = 80; |
58 | | - if (!client.connect(host, httpPort)) { |
| 50 | + if (!client.connect(host, port)) { |
59 | 51 | Serial.println("connection failed"); |
| 52 | + delay(5000); |
60 | 53 | return; |
61 | 54 | } |
62 | 55 |
|
63 | | - // We now create a URI for the request |
64 | | - String url = "/input/"; |
65 | | - url += streamId; |
66 | | - url += "?private_key="; |
67 | | - url += privateKey; |
68 | | - url += "&value="; |
69 | | - url += value; |
70 | | - |
71 | | - Serial.print("Requesting URL: "); |
72 | | - Serial.println(url); |
73 | | - |
74 | | - // This will send the request to the server |
75 | | - client.print(String("GET ") + url + " HTTP/1.1\r\n" + |
76 | | - "Host: " + host + "\r\n" + |
77 | | - "Connection: close\r\n\r\n"); |
| 56 | + // This will send a string to the server |
| 57 | + Serial.println("sending data to server"); |
| 58 | + client.println("hello from ESP8266"); |
78 | 59 | unsigned long timeout = millis(); |
79 | 60 | while (client.available() == 0) { |
80 | 61 | if (millis() - timeout > 5000) { |
81 | 62 | Serial.println(">>> Client Timeout !"); |
82 | 63 | client.stop(); |
| 64 | + delay(60000); |
83 | 65 | return; |
84 | 66 | } |
85 | 67 | } |
86 | 68 |
|
87 | 69 | // Read all the lines of the reply from server and print them to Serial |
88 | | - while (client.available() || client.connected()) { |
89 | | - String line = client.readStringUntil('\r'); |
90 | | - Serial.print(line); |
| 70 | + Serial.println("receiving from remote server"); |
| 71 | + while (client.available()) { |
| 72 | + char ch = static_cast<char>(client.read()); |
| 73 | + Serial.print(ch); |
91 | 74 | } |
92 | 75 |
|
| 76 | + // Close the connection |
93 | 77 | Serial.println(); |
94 | 78 | Serial.println("closing connection"); |
95 | | -} |
| 79 | + client.stop(); |
96 | 80 |
|
| 81 | + delay(300000); // execute once every 5 minutes, don't flood remote service |
| 82 | +} |
0 commit comments