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.
#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