Chat freely about anything...

User avatar
By hortplus
#73933 I have a WiFi repeater going and need to send a command to the unit to tell it to sleep. For example "sleep 60" will put the device into deep sleep for 60 seconds, thus saving battery power. However i'm struggling with how to send a command. The unit is called MyAP, 192.168.4.1 and needs to communicate through port 7777. I have seen that there is a telnet type server available but i am struggling to get it going. Does anyone have any experience with getting it working. Or am i going about this the wrong way?

Thanks in advance for any help you can give.
User avatar
By martin_g
#73935 This Arduino code sends a command to the repeater and prints its result (if any)

Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "your-ssid";
const char* password = "your-password";
const char* host = "192.168.4.1";
const char* command = "sleep 10";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  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() {
  delay(5000);

  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 7777;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  client.print(String("\r\n") + String(command) + String("\r\n"));
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    char line[1024];
    int len = client.read((uint8_t*)line, sizeof(line-1));
    line[len] = '\0';
    Serial.print(String(line));
  }
 
  Serial.println();
  Serial.println("closing connection");
}