Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By nuesome
#47623 /*this is the client code i am running/ it has only about a 1 sec. latency to make it to esp#1/webserver so i guess thats not bad. But it keeps connecting and disconnecting to the server after message is sent. Also, I see the websocket server and client examples on arduino ide but I am new to programming and not sure how to program the payload code. In other words how to say "if pir high turn on pinX at server/esp1
* This sketch sends a message to a TCP server
*
*/

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

ESP8266WiFiMulti WiFiMulti;
int ledPin = 3;
int inputPin = 2;
int pirState = LOW;
int val = 0;


void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
// We start by connecting to a WiFi network
WiFiMulti.addAP("JP", "passpasspass");

Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");

while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

delay(500);
}


void loop() {
const uint16_t port = 80;
const char * host = "192.168.1.241"; // ip or dns
// val = digitalRead (inputPin);


Serial.print("connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;

if (!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;

}
val = digitalRead (inputPin);
if (val == HIGH){
// This will send the request to the server
client.print("/gpio/0"); //this is command to turn led on at the webserver
digitalWrite (ledPin, HIGH);}//this is led on this esp
else {client.print("/gpio/1");//this is command to turn led off at webserver
digitalWrite (ledPin, LOW);}
//read back one line from server
String line = client.readStringUntil('\r');
client.println(line);

// Serial.println("closing connection");
// client.stop();

// Serial.println("wait 5 sec...");
// delay(5000);
}
[code][/code]
User avatar
By eduperez
#47645 I do not see the problem with opening a new connection each time the PIR detects some movement, you do not seem to have any tight schedule if you wait for five seconds between checks.

On the other hand, you seem to be opening a new connection irregardless of the PIR status, why don't you put the "client.connect" code inside the "if (val == HIGH)" condition?

On the other hand, you could use the schema at viewtopic.php?f=11&t=4458 to wake up the client when the PIR detects some movement.