Post topics, source code that relate to the Arduino Platform

User avatar
By crood58
#62449 Hello All,

I'm working on a project using the Adafruit Huzzah ESP 8266 Feather. What I'm trying to is connecting to a wifi access point that sends commands to my model train control system. This AP reads the commands sent from wifi and then puts them into the command system.

I have been trying to get this figured out for a few days now and can't seem to get it to work. I originally thought the AP need UDP packets, but I was wrong. Testing last night with a known ip address and port. I was able to send and receive commands back with Packet Sender using TCP.

I have tried to setup the ESP8266 as just a station and use as a client and also as a server to send the commands, but it always tell me no client. Anyone have an idea how can get this to work? I scoured the internet and everything I find doesn't seem to do it.

If at all possible I would like to try to connect to the AP with SSID and not have to worry about any ip or port.

Here is code that I developed in my trail and error.

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>


/* WIFI PARAMETERS */
const char* ssid = "********";
const int serverPort = 9999;

WiFiClient LCSclient;
WiFiServer server(serverPort);

/* CONSTANT LCS COMMANDS */
const byte SOP = 0xD1; //Start of Packet Command
const byte CMD = 0x25; //Command - Switch Base Table
const byte PF = 0x02; //Read Process Flag Command
const byte EOP = 0xDF; //End of Packet Command

/* PROGRAM DETERMINED LCS COMMANDS & VARIABLES */
byte TMCC_ID; //Used to convert TMCC id from intger form to byte
byte SUM = 0; //The sum of the HEX string 
byte XS; //The checksum byte

/* RECIEVED COMMANDS */

/* TEST VARIABLES */
int id = 1;
int cycle = 1;

void setup() {
  // Begin Serial Monitor for Debugging
  Serial.begin(115200);

  //Clear Serial Monitor
  Serial.println("");
  Serial.println("");
  Serial.println("");

  //Start WiFi
  Serial.print("Connecting to ");
  WiFi.begin(ssid);

 
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.print("IP Address: ");
  Serial.print(WiFi.localIP());
  Serial.println("");
  Serial.println("");
 
  //Start TCP Server
  server.begin();

}

void loop() {

   
    //WRITE COMMANDS TO LCS WIFI
      if(cycle == 1) {
        //Convert TMCC ID to byte
        TMCC_ID = byte(id);

        //Calculate checksum
        SUM = CMD + TMCC_ID + PF;
        XS = (0-SUM);

        //Send commands
        server.write(SOP);
        server.write(CMD);
        server.write(TMCC_ID);
        server.write(PF);
        server.write(XS);
        server.write(EOP);

        //Change cycle from 1
        cycle++;
      }
     
  LCSclient = server.available();

  // SEE IF THE CLIENT IS AVAILABLE & SEND COMMANDS
  if(LCSclient) {
    if(LCSclient.connected()) {
      Serial.println("Connected to Client");

      //See if there is anything to read
      if(LCSclient.available() > 0) {
        Serial.println("THERE ARE COMMANDS IN THE BUFFER");
        Serial.println("--------------------------------");

        while(LCSclient.available()) {
          Serial.write(LCSclient.read());
        }

        Serial.println("");
        Serial.println("");
        Serial.println("");
      }

      else {
        Serial.println("There is nothing in the buffer");
      }
    }
  }

  else {
    Serial.println("No client connection");
  }
}


Thanks in advance for the help.

Chris
User avatar
By rudy
#62456
Code: Select allIf at all possible I would like to try to connect to the AP with SSID and not have to worry about any ip or port.


Without a known IP or port it is like sending a letter in the mail but without an send to location. Where should it be sent to?
User avatar
By crood58
#62458
rudy wrote:
Code: Select allIf at all possible I would like to try to connect to the AP with SSID and not have to worry about any ip or port.


Without a known IP or port it is like sending a letter in the mail but without an send to location. Where should it be sent to?


Good point. I have tried with the IP address and port, but no luck. See I thought the AP gives the IP address to the client to transmit on she. You call WiFi.begin()

I'm trying so many different things I think I confused myself.....lol
User avatar
By rudy
#62459 I did a little searching and found this.

http://ogrforum.ogaugerr.com/topic/connecting-phone-to-lcs-wifi

The IP Address when the WiFi box is in "Join Network" mode is 192.168.254.18. Some how there needs to be a mechanism within the router that allows a device running the Bluetrain App to search for the assigned IP address. I am not a router expert but there seems to be a connectivity process using NAT or DMZ. Maybe NOT. I don't know...


Since you were not clear on what you are trying to connect to I don't know if this is applicable. As far as the port number, if it is connecting to a web page then start with a port number of 80.