So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By sophi
#72545 HI-
I've followed a great tutorial here:https://www.instructables.com/id/WiFi-Communication-Between-Two-ESP8266-Based-MCU-T/ to make 2 ESPs talk to each other through my home router. That code works.

ESP1 (client) is connected to 4 switches, I'd like for when the switches are on for ESP1, for that information to be sent to ESP2 (server).
When a switch is on on ESP1, it should turn on a motor on ESP2.
How can I do this? The switches are called val12, val13, val14, val16 on ESP1.
My code is below.
Thanks in advance!

Code: Select all#This is the client code for ESP1 which is connected to 4 switches
#include <SPI.h>
#include <ESP8266WiFi.h>
int val12;
int val13;
int val14;
int val15;
int val16;
byte ledPin = 2;
char ssid[] = "xxx";             // SSID of your home WiFi
char pass[] = "xxx";            // password of your home WiFi

unsigned long askTimer = 0;

IPAddress server(192,168,1,23);       // the fix IP address of the server
WiFiClient client;

void setup() {
  Serial.begin(115200);               // only for debug
  WiFi.begin(ssid, pass);             // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(12, INPUT);
  pinMode(13, INPUT);
  pinMode(14, INPUT);
  pinMode(16, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop () {
  client.connect(server, 80);   // Connection to the server
  digitalWrite(2, LOW);         // to show the communication only (inverted logic)
  val12 = digitalRead(12);      // read the input pin
  val13 = digitalRead(13);      // read the input pin
  val14 = digitalRead(14);      // read the input pin
  val16 = digitalRead(16);      // read the input pin
  digitalWrite(0, val16); 
  Serial.println(".");
  client.println("Hello server! Are you sleeping?\r");  // sends the message to the server
  client.println(val16);
  String answer = client.readStringUntil('\r');   // receives the answer from the server
  Serial.println("from server: " + answer);
  client.flush();
  digitalWrite(ledPin, HIGH);
  delay(2000);                  // client will trigger the communication after two seconds
}


Code: Select all#This is the server code which is connected to another ESP
#include <SPI.h>
#include <ESP8266WiFi.h>

int ledPin = 2;
char ssid[] = "xxx";                      // SSID of your home WiFi
char pass[] = "xxx";               // password of your home WiFi
WiFiServer server(80);                   

IPAddress ip(192, 168, 1, 23);            // IP address of the server
IPAddress gateway(192,168,1,1);           // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network

void setup() {
  Serial.begin(115200);                   // only for debug
  WiFi.config(ip, gateway, subnet);       // forces to use the fix IP
  WiFi.begin(ssid, pass);                 // connects to the WiFi router
  Serial.println();
  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress());
  digitalWrite(ledPin, HIGH);
  delay(1500);
  digitalWrite(ledPin, LOW);
  delay(1500);
  digitalWrite(ledPin, HIGH);
  delay(1500);
  digitalWrite(ledPin, LOW);
  delay(1500);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  server.begin();                          // starts the server
  pinMode(ledPin, OUTPUT);
}

void loop () {
  WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {
      digitalWrite(ledPin, LOW);  // to show the communication only (inverted logic)
      Serial.println(".");
      String request = client.readStringUntil('\r');    // receives the message from the client
      Serial.print("From client: "); Serial.println(request);
      client.flush();
      client.println("Hi client! No, I am listening.\r"); // sends the answer to the client
      digitalWrite(ledPin, HIGH);
    }
    client.stop();                // tarminates the connection with the client
  }
}
User avatar
By sophi
#72618 This is solved.
Code: Select allCLIENT CODE SNIP
// Serial.println prints to the monitor, client.println sends the data
  Serial.println(val12);
  client.println(val12);      // 0


Code: Select allSERVER CODE SNIP
WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {     
       String request12 = client.readStringUntil('d');
       request12 = request12.charAt(0);
       delay(10);
       Serial.println("val12 is " + request12);