|
| 1 | +/* |
| 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. |
| 4 | +*/ |
| 5 | + |
| 6 | +#include <W6100lwIP.h> |
| 7 | + |
| 8 | +const char* host = "djxmmx.net"; |
| 9 | +const uint16_t port = 17; |
| 10 | + |
| 11 | +Wiznet6100lwIP eth(1 /* chip select */); |
| 12 | + |
| 13 | +void setup() { |
| 14 | + // Set up SPI pinout to match your HW |
| 15 | + SPI.setRX(0); |
| 16 | + SPI.setCS(1); |
| 17 | + SPI.setSCK(2); |
| 18 | + SPI.setTX(3); |
| 19 | + |
| 20 | + Serial.begin(115200); |
| 21 | + delay(5000); |
| 22 | + Serial.println(); |
| 23 | + Serial.println(); |
| 24 | + Serial.println("Starting Ethernet port"); |
| 25 | + |
| 26 | + // Start the Ethernet port |
| 27 | + if (!eth.begin()) { |
| 28 | + Serial.println("No wired Ethernet hardware detected. Check pinouts, wiring."); |
| 29 | + while (1) { |
| 30 | + delay(1000); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + while (!eth.connected()) { |
| 35 | + Serial.print("."); |
| 36 | + delay(500); |
| 37 | + } |
| 38 | + |
| 39 | + Serial.println(""); |
| 40 | + Serial.println("Ethernet connected"); |
| 41 | + Serial.println("IP address: "); |
| 42 | + Serial.println(eth.localIP()); |
| 43 | +} |
| 44 | + |
| 45 | +void loop() { |
| 46 | + static bool wait = false; |
| 47 | + |
| 48 | + Serial.print("connecting to "); |
| 49 | + Serial.print(host); |
| 50 | + Serial.print(':'); |
| 51 | + Serial.println(port); |
| 52 | + |
| 53 | + // Use WiFiClient class to create TCP connections |
| 54 | + WiFiClient client; |
| 55 | + if (!client.connect(host, port)) { |
| 56 | + Serial.println("connection failed"); |
| 57 | + delay(5000); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // This will send a string to the server |
| 62 | + Serial.println("sending data to server"); |
| 63 | + if (client.connected()) { |
| 64 | + client.println("hello from RP2040"); |
| 65 | + } |
| 66 | + |
| 67 | + // wait for data to be available |
| 68 | + unsigned long timeout = millis(); |
| 69 | + while (client.available() == 0) { |
| 70 | + if (millis() - timeout > 5000) { |
| 71 | + Serial.println(">>> Client Timeout !"); |
| 72 | + client.stop(); |
| 73 | + delay(60000); |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Read all the lines of the reply from server and print them to Serial |
| 79 | + Serial.println("receiving from remote server"); |
| 80 | + // not testing 'client.connected()' since we do not need to send data here |
| 81 | + while (client.available()) { |
| 82 | + char ch = static_cast<char>(client.read()); |
| 83 | + Serial.print(ch); |
| 84 | + } |
| 85 | + |
| 86 | + // Close the connection |
| 87 | + Serial.println(); |
| 88 | + Serial.println("closing connection"); |
| 89 | + client.stop(); |
| 90 | + |
| 91 | + if (wait) { |
| 92 | + delay(300000); // execute once every 5 minutes, don't flood remote service |
| 93 | + } |
| 94 | + wait = true; |
| 95 | +} |
0 commit comments