ESP8266 ESP-01 as both client and server - also ESP power question

I have a project involving two PIR sensors each connected to an ESP8266 ESP-01. One will be a client and will send its status to the other ESP and that ESP will act as both server and client in order to collect that status and then send info to ThingSpeak.

Through a lot of trial and error with combining info from multiple examples that I've found online, I've been able to get this to work, but I'm not 100% sure what all of my code is doing. I was wondering if someone would mind taking a look at specifically the server and client related parts to see if there were things that I should be doing differently.

Also, I had been running into false positive issues with the HC-SR501. When I was troubleshooting that issue, both ESP's were set up as client only and were each communicating to ThingSpeak independently. I found two methods that solved the issue for me:

  • Use a larger breadboard and place the sensor far away from the ESP.
  • Keep the smaller breadboard, but program the ESP to use less power.

However, the false positives have returned, and as far as I can tell right now it's only happening on the one that is both server and client. I'm wondering if there is something in the code that is overriding the power management code that's in setup.

#include <SPI.h>
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"

//------- WI-FI details ----------//
char ssid[] = "xxxxxxxxxxx"; //SSID here
char pass[] = "xxxxxxxxxxx"; // Passowrd here
//-----------------------------//

//----------- Channel details ----------------//
unsigned long Channel_ID = xxxxxxxxxx; // Your Channel ID
const char * myWriteAPIKey = "xxxxxxxxxx"; //Your write API key
//-------------------------------------------//

WiFiServer server(80); 
WiFiClient client;  

IPAddress ip(192, 168, 4, 147);            // 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

#define SENSOR 2

unsigned long timeNow = 0;
unsigned long speakTimer = 0;
unsigned long sensorTimer = 0;
unsigned long instance = 0;
int powerValue = 10;
int sensorVal = 0;
int serverVal = 0;
int sendingVal = 0;
int writeSuccess = 0;
String request = "";

void setup() {
  Serial.begin(115200);
  wifi_set_phy_mode(PHY_MODE_11G);
  system_phy_set_max_tpw(powerValue);
  pinMode(SENSOR, INPUT);
  internetServer();   
}

void loop() {
  internetServer();
  timeNow = millis();
  if (timeNow - sensorTimer > 500) {
    sensorVal = digitalRead(SENSOR);
    Serial.println(sensorVal);
    if (sensorVal == 1) {
      serverVal = 1;
    }
    sensorTimer = millis();
  }
  WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {
      Serial.println(".");
      request = client.readStringUntil('\r');    // receives the message from the client
      Serial.print("From client: ");
      Serial.println(request);
      client.flush();
      client.println("Data received!\r"); //
    }
    client.stop();
  }
  if (request == "1" || serverVal == 1) {
    sendingVal = 1;
  }
  if (timeNow - speakTimer > 15100) {
    internetClient();
  }
}

void internetServer() {
  if (WiFi.status() != WL_CONNECTED) {
    while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      WiFi.config(ip, gateway, subnet);
      WiFi.begin(ssid, pass);
      delay(5000); 
    }
    server.begin();                         // starts the server
    Serial.println("Connected to wifi");
    Serial.print("Status: "); Serial.println(WiFi.status());  // some parameters from the network
    Serial.print("IP: ");     Serial.println(WiFi.localIP());
    Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
    Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
    Serial.print("SSID: "); Serial.println(WiFi.SSID());
    Serial.print("Signal: "); Serial.println(WiFi.RSSI());
    Serial.print("Networks: "); Serial.println(WiFi.scanNetworks());
  }
}

void internetClient() {
  WiFiClient client;
  ThingSpeak.begin(client);
  upload();
}

void upload() {
  ThingSpeak.setField(1,(String)sendingVal);
  ThingSpeak.setField(2,(String)instance);
  writeSuccess = ThingSpeak.writeFields(Channel_ID, myWriteAPIKey);
  while (writeSuccess == -301) {
    ThingSpeak.setField(1,(String)sendingVal);
    ThingSpeak.setField(2,(String)instance);
    writeSuccess = ThingSpeak.writeFields(Channel_ID, myWriteAPIKey);
    Serial.println(writeSuccess);
  }
  speakTimer = millis();
  sendingVal = 0;
  serverVal = 0;
  instance++;
}