These are chat archives for esp8266/Arduino

24th
Jan 2018
rodrigocirilo
@rodrigocirilo
Jan 24 2018 10:13

Alright, try this code. This should work out pretty well for you. If not, let me know. :)

#include <ESP8266WiFi.h>
  #define       LED0      2         // WIFI Module LED
//------------------------------------------------------------------------------------
String Message="";
//------------------------------------------------------------------------------------
// Authentication Variables
//------------------------------------------------------------------------------------
  char*         connect_to = "ssid";
  char*         pass = "password";
  IPAddress     qwertyServer(192,168,4,1);
  WiFiClient    qwertyClient;
//====================================================================================

  void setup()
  {        
    // Setting The Mode Of Pins ---------------------------------------------
    pinMode(LED0, OUTPUT);          // WIFI OnBoard LED Light
    digitalWrite(LED0, !LOW);       // Turn WiFi LED Off

    // Print Message Of I/O Setting Progress --------------------------------
    //Serial.println("\nI/O Pins Modes Set .... Done");

    // Starting To Connect --------------------------------------------------
    WiFi.mode(WIFI_STA);            // To Avoid Broadcasting An SSID
    WiFi.begin(connect_to, pass);          // The SSID That We Want To Connect To

    // Printing Message For User That Connetion Is On Process ---------------
    Serial.println("!--- Connecting To " + WiFi.SSID() + " ---!");

     // Setting The Serial Port ----------------------------------------------
    Serial.begin(9600);           // Computer Communication
    delay(500);
    Serial.print("\n");

    // WiFi Connectivity ----------------------------------------------------
    CheckConnectivity();            // Checking For Connection  
  }

//====================================================================================

  void loop()
  {

    if(WiFi.status() != WL_CONNECTED)
    {
        Serial.println("\nOops! Connection lost. Trying to reconnect " + String(WiFi.SSID()));
        Serial.println("");
        CheckConnectivity();
    }
    else if(qwertyClient.available() > 0)
    {
        ReadMessage();
        Serial.print(Message);

      qwertyClient.flush();
      }

  }
//====================================================================================

  void ReadMessage()
  {
      Message = "";
      Message = qwertyClient.readStringUntil('\r');
  }

//====================================================================================

  void CheckConnectivity()
  {
    while(WiFi.status() != WL_CONNECTED)
    {
      for(int i=0; i < 2; i++)
      {
        digitalWrite(LED0, !HIGH);
        delay(50);
        digitalWrite(LED0, !LOW);
        delay(550);
        Serial.print(".");
      }
      Serial.println("");
    }

  // Stop Blinking To Indicate Connected ----------------------------------
    digitalWrite(LED0, LOW);
    Serial.println("\n!-- Client Device Connected --!");
    Serial.print("\n\n");

    // Printing IP Address --------------------------------------------------
    Serial.println("Connected To      : " + String(WiFi.SSID()));
    Serial.println("Signal Strenght   : " + String(WiFi.RSSI()) + " dBm");
    Serial.print  ("Server IP Address : ");
    Serial.println(qwertyServer);
    Serial.print  ("Device IP Address : ");
    Serial.println(WiFi.localIP());

    // Conecting The Device As A Client -------------------------------------
    ClientRequest();

  }

//====================================================================================

  void ClientRequest()
  {
    // First Make Sure You Got Disconnected
    qwertyClient.stop();

    // If Sucessfully Connected Send Connection Message
    if(qwertyClient.connect(qwertyServer, 23))
    {
      delay(250);
      Serial.print("status\n");
      Serial.println    ("Connected");
      qwertyClient.println ("Connected");
    }
  }

This is the code I am currently using, with some simple modifications.
then I would need to change the IP of (qwertyServer) to a DDNS address, does a

Daleon L
@Allwaysawake
Jan 24 2018 15:56
For whom me...?