- packet size : 5 bytes
- [0] : command
- [1] : value
- [2] : reserved
- [3] : reserved
- [4] : reserved
Server (Arduino)
#include <ESP8266WiFi.h>
typedef struct _protocol {
char cmd;
char val;
char etc1;
char etc2;
char etc3;
} Protocol;
const char AP_SSID[] = "Makecube Drone";
const char AP_PASS[] = "12345678";
const int SERVER_PORT = 7777;
WiFiServer server(SERVER_PORT);
WiFiClient client;
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP(AP_SSID, AP_PASS);
server.begin();
Serial.println("*** Connection Info ***");
Serial.print("SSID : "); Serial.println(AP_SSID);
Serial.print("PASS : "); Serial.println(AP_PASS);
Serial.print("IP : "); Serial.println(WiFi.localIP());
Serial.print("PORT : "); Serial.println(SERVER_PORT);
Serial.println("+- Server Ready -+");
}
void loop()
{
if (!client) {
client = server.available();
if(client) {
Serial.println("Client Connected");
}
return;
}
if(client.available() ) {
Protocol command;
command.cmd = client.read();
command.val = client.read();
command.etc1 = client.read();
command.etc2 = client.read();
command.etc3 = client.read();
doControl(command);
}
}
void doControl(Protocol cmd) {
Serial.print("Control : ");
Serial.print(cmd.cmd);
Serial.print(":");
Serial.println((int)cmd.val);
if(cmd.cmd == 'q' && cmd.val == 0) {
client.stop();
Serial.println("Disconnected");
return;
}
}
Client (PC : Java)
package in.makecube.drone.controller;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
Controller controller = new Controller();
controller.setServer("192.168.4.1", 7777);
controller.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
String command = br.readLine();
if ("q".equals(command))
break;
controller.send(command);
} catch (Exception e) {
e.printStackTrace();
}
}
controller.disconnect();
}
}
package in.makecube.drone.controller;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Controller {
private String serverIP;
private int port;
Socket socket;
InputStream in;
OutputStream out;
byte[] chunk;
public Controller() {
chunk = new byte[5];
}
public void setServer(String address, int port) {
serverIP = address;
this.port = port;
}
public void connect() {
try {
socket = new Socket(serverIP, port);
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (Exception e) {
e.printStackTrace();
}
}
public void disconnect() {
send("q0");
try {
if (in != null)
in.close();
if (out != null)
out.close();
if (socket != null)
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void send(String command) {
if (socket == null || out == null)
return;
if (command == null || command.trim().length() == 0)
return;
try {
chunk[0] = (byte) command.charAt(0);
try {
chunk[1] = (byte) Integer.parseInt(command.substring(1));
} catch (Exception e) {
chunk[1] = (byte) 0;
}
chunk[2] = (byte) 0;
chunk[3] = (byte) 0;
chunk[4] = (byte) 0;
out.write(chunk);
} catch (Exception e) {
e.printStackTrace();
}
}
}