diff --git a/.drafts/.1/conn_master/conn_master.ino b/.drafts/.1/conn_master/conn_master.ino new file mode 100644 index 0000000..0b7cf9d --- /dev/null +++ b/.drafts/.1/conn_master/conn_master.ino @@ -0,0 +1,106 @@ +// =================================================================== +// Auth: alex +// File: conn.ino +// Revn: 07-18-2023 1.0 +// Func: connect to QOTD service via TCP +// +// TODO: add gps +// =================================================================== +// CHANGE LOG +// ------------------------------------------------------------------- +//*07-18-2023: ac copied from https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino +// +// =================================================================== + + +#include + +#ifndef STASSID +#define STASSID "your-ssid" +#define STAPSK "your-password" +#endif + +const char* ssid = STASSID; +const char* password = STAPSK; + +// need byte array for ip's +//const unsigned char host[4] = { xyz, xyz, xyz, xyz }; +const char* host = "192.168.1.120"; +const uint16_t port = 1202; + +void setup() { + Serial.begin(115200); + + // 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.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +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(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; +} \ No newline at end of file