The code:
server:
#include <ESP8266WiFi.h>
const char* ssid = "my ssid"; //replace this with your WiFi network name
const char* password = "my passwd"; //replace this with your WiFi network password
WiFiServer server(12345);
int a;
void setup()
{
a = 0;
pinMode(D5, OUTPUT); // piezo spkr
delay(1000);
Serial.begin(115200);
delay(1000);
tone(D5, 1720);
Serial.flush(); //irrelevant whether this is here or not
Serial.print("Wifi.begin()");
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
tone(D5, 220);
Serial.println("success!");
Serial.print("Server IP Address is: ");
Serial.println(WiFi.localIP());
delay(1000);
noTone(D5);
server.begin();
}
int read_and_process_packet(WiFiClient *client)
{
char packet_type = client->read();
switch (packet_type)
{
case 'a': a--; Serial.println(a); break;
case 'q': a++; Serial.println(a); break;
case '-': return 0;
}
return 1;
}
void loop() {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
Serial.println("Connected to client");
while (client.connected())
{
if (!read_and_process_packet(&client)) break;
if (Serial.available())
{
char c = Serial.read();
client.write(c);
}
delay(50);
}
client.stop();
Serial.println("Client left");
}
}
delay(1);
}
client:
#include <ESP8266WiFi.h>
// Replace these with your WiFi network settings
const char* ssid = "my ssid"; //replace this with your WiFi network name
const char* password = "my passwd"; //replace this with your WiFi network password
int a;
WiFiClient client;
char server_ip[20];
void setup()
{
a = 0;
pinMode(D5, OUTPUT);
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.flush(); //irrelevant whether this is here or not
Serial.print("Enter server IP: ");
char *p = server_ip;
for (int i = 0; i < 4; i++)
{
int ip = read_number();
p = print_num(p, ip);
if (i < 3)
p = print_char(p, '.');
}
Serial.println();
tone(D5, 1720);
Serial.print("Wifi.begin()");