Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
understanding networks - code for tcp client controller
/*
This sketch is based on Tom Igoe's modified version of the EspWiFiTelnetClient sketch.
note: the header file "settings.h" is not included in this repository.
Add a new tab in Arduino, call it "settings.h", and Include the following variables:
char ssid[] = "ssid"; // your network SSID
char password[] = "password"; // your network password
char host[] = "192.168.0.2"; // the IP address of the device running the server
*/
#include <ESP8266WiFi.h>
#include "settings.h"
#include <Encoder.h>
WiFiClient socket; // variable for the socket connection
const int port = 8080; // port for the socket connection
//button to log back in
const int loginButton = 3;
int loginButtonStatePrev;
//status LED
int statusLED = 13;
//encoders & previous encoder positions
Encoder upDownEnc(4, 5);
long upDownEncPrevPos = -999;
Encoder leftRightEnc(14, 12);
long leftRightEncPrevPos = -999;
//delay between reading input
int encoderDelay = 2;
//debounce for button
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(9600);
Serial.setTimeout(10);
socket.setTimeout(10);
pinMode(statusLED, OUTPUT);
pinMode(loginButton, INPUT_PULLUP);
WiFi.begin(ssid, password);
Serial.print("Connecting to "); // connect to access point
Serial.println(ssid);
// wait for connection to network access point:
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// when connected to access point, acknowledge it:
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); // print device's IP address
// login();
}
void loop() {
// Read the reply from server and print them to serial port:
while (socket.available()) {
Serial.print("Got something");
String input = socket.readString();
Serial.print(input);
}
//log into the game if login button is pressed
//log out if the button is pressed again
int loginButtonState = digitalRead(loginButton);
if (loginButtonState == LOW && loginButtonState != loginButtonStatePrev) {
Serial.println("button pressed");
if (socket.connected() == false) {
Serial.println("logging in");
login();
} else {
Serial.println("logging out");
socket.print('x');
}
}
loginButtonStatePrev = loginButtonState;
// LED indicator that we are connected
digitalWrite(statusLED, socket.connected());
//up-down movement encoder
long upDownEncNewPos = upDownEnc.read();
//if the encoder position changes
if (upDownEncNewPos != upDownEncPrevPos) {
//and it is increasing, move up
if (upDownEncNewPos > upDownEncPrevPos && upDownEncNewPos % 4 == 0) {
socket.print('u');
Serial.println("up");
delay(encoderDelay);
//and it is decreasing, move down
} else if (upDownEncNewPos < upDownEncPrevPos && upDownEncNewPos % 4 == 0) {
socket.print('d');
Serial.println("down");
delay(encoderDelay);
}
upDownEncPrevPos = upDownEncNewPos;
}
//left-right movement encoder
long leftRightEncNewPos = leftRightEnc.read();
//if the encoder position changes
if (leftRightEncNewPos != leftRightEncPrevPos) {
//and it is increasing, move right
if (leftRightEncNewPos > leftRightEncPrevPos && leftRightEncNewPos % 4 == 0) {
socket.print('r');
Serial.println("right");
delay(encoderDelay);
//and it is decreasing, move left
} else if (leftRightEncNewPos < leftRightEncPrevPos && leftRightEncNewPos % 4 == 0) {
socket.print('l');
Serial.println("left");
}
leftRightEncPrevPos = leftRightEncNewPos;
}
}
boolean login() {
socket.connect(host, port); // attempt to connect
while (!socket.connected()) { // While not connected, try again
delay(1000);
if (socket.connected()) { // if you connected,
socket.println("hello"); // say hello to the server
} else {
// if not connected, try again:
Serial.println("connection failed, trying again");
socket.connect(host, port);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.