Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This sketch sends a message to a TCP server
- *
- */
- #include <WiFi.h>
- #include <WiFiMulti.h>
- WiFiMulti WiFiMulti;
- IPAddress server;
- String str_hostname = "BatLOG_" + String(random(1,2000));
- void setup()
- {
- Serial.begin(115200);
- delay(10);
- // We start by connecting to a WiFi network
- WiFiMulti.addAP("Batlog", "12345678");
- Serial.println();
- Serial.println();
- Serial.println("Waiting for WiFi... ");
- while(WiFiMulti.run() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.print("Hostname: ");
- Serial.println(str_hostname);
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.println("Server address: ");
- Serial.println(WiFi.gatewayIP());
- server = WiFi.gatewayIP();
- Serial.println("");
- Serial.println("");
- Serial.println("");
- Serial.println("Starting Loop....");
- Serial.println("");
- Serial.println("");
- delay(500);
- }
- void loop()
- {
- const uint16_t port = 2000;
- // const char * host = "192.168.1.10"; // ip or dns
- IPAddress host = WiFi.gatewayIP(); // ip or dns
- Serial.print("Connecting to ");
- Serial.println(host);
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- if (!client.connect(host, port)) {
- Serial.println("Connection failed.");
- Serial.println("Waiting 5 seconds before retrying...");
- delay(5000);
- return;
- }
- // This will send a request to the server
- //uncomment this line to send an arbitrary string to the server
- //client.print("Send this data to the server");
- //uncomment this line to send a basic document request to the server
- client.print(random(1,2000));
- //Serial.println("Closing connection.");
- // client.stop();
- Serial.println("Waiting 5 seconds before restarting...");
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement