Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This sketch establishes a TCP connection to a "quote of the day" service.
- It sends a "hello" message, and then prints received data.
- */
- #include <ESP8266WiFi.h>
- #ifndef STASSID
- #define STASSID "ssid"
- #define STAPSK "password"
- #endif
- const char* ssid = STASSID;
- const char* password = STAPSK;
- const char* host = "djxmmx.net";
- const uint16_t port = 17;
- void setup() {
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
- would try to act as both a client and an access-point and could cause
- network-issues with your other WiFi-devices on your WiFi-network. */
- WiFi.mode(WIFI_STA);
- WiFi.hostname("NodeMCU");
- //IPAddress ip(172, 16, 2, 2);
- //IPAddress dns(172, 16, 0, 1);
- //IPAddress gw(172, 16, 0, 1);
- //IPAddress sn(255, 255, 240, 0);
- //WiFi.config(ip, dns, gw, sn);
- WiFi.begin(ssid, password);
- int i = 1;
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print("Versuch: ");
- Serial.print(i);
- Serial.print(" // ");
- Serial.print("WiFi.status(): ");
- Serial.print(WiFi.status());
- Serial.print(" // ");
- Serial.print("wifi_station_get_connect_status(): ");
- Serial.println(wifi_station_get_connect_status());
- //WiFi.printDiag(Serial);
- i++;
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.println("");
- WiFi.printDiag(Serial);
- Serial.println("");
- }
- void loop() {
- static bool wait = false;
- 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");
- delay(5000);
- return;
- }
- // This will send a string to the server
- Serial.println("sending data to server");
- if (client.connected()) {
- client.println("hello from ESP8266");
- }
- // wait for data to be available
- unsigned long timeout = millis();
- while (client.available() == 0) {
- if (millis() - timeout > 5000) {
- Serial.println(">>> Client Timeout !");
- client.stop();
- delay(60000);
- return;
- }
- }
- // Read all the lines of the reply from server and print them to Serial
- Serial.println("receiving from remote server");
- // not testing 'client.connected()' since we do not need to send data here
- while (client.available()) {
- char ch = static_cast<char>(client.read());
- Serial.print(ch);
- }
- // Close the connection
- Serial.println();
- Serial.println("closing connection");
- client.stop();
- if (wait) {
- delay(300000); // execute once every 5 minutes, don't flood remote service
- }
- wait = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement