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

Moderator: igrr

User avatar
By drekthral
#53827 Hello,

My project idea: 2x esp8266, one as server, one as client. Client have attached motion sensor hc-sr501. Client connect to server and then telling him if he detected motion or not.

Problem: Everything is fine but client ESP must be connected to PC and also serial monitor must be opened. When i close serial monitor it seems like
Code: Select allpirValue = digitalRead(pirPin);
is not working... I measured it with my voltmeter and sensor is working but somehow pin is not able to read its value...

Code:

Server:
Code: Select all#include <ESP8266WiFi.h>
#include<WiFiClient.h>
#include<ESP8266WebServer.h>`

const char *ssid = "esp-motion";
const char *password = "123456789";

ESP8266WebServer server(80);

void handle_led1(){
  digitalWrite(2, 1);
  server.send(200, "text/plain", String("LED off "));
}

void handle_led0(){
  digitalWrite(2, 0);
  server.send(200, "text/plain", String("LED on "));
}

void setup() {
  delay(1000);
  Serial.begin(115200);
  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);

  Serial.println();
  Serial.print("Configuring access point...");
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();
  Serial.println("HTTP server started");
  server.on("/led1",handle_led1);
  server.on("/led0",handle_led0);
}

void loop() {
  server.handleClient();
}


Client:
Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid     = "esp-motion";
const char* password = "123456789";
const char* host = "http://192.168.4.1";

int pirPin = 0;
int pirValue;

HTTPClient http;

void rptaSrv(int httpCode) {
  if (httpCode == 200) {
    String payload = http.getString();
    Serial.println(payload);
  } else {
    Serial.print("[HTTP] GET... failed, no connection or no HTTP server\n");
  }
}

void setup() {

  pinMode(pirPin, INPUT);
  pinMode(2, OUTPUT);
 
  Serial.begin(115200);
  delay(10);
  Serial.println("client-server");
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

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

  delay(50);
}

void loop() {
  pirValue = digitalRead(pirPin);
 
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin(ssid, password);
    delay(500);
  } else {

    Serial.println("[HTTP] begin...");
   
    if(pirValue == 1) {
      http.begin("192.168.4.1", 80, "/led0");
      int httpCode1 = http.GET();
      rptaSrv(httpCode1);
      digitalWrite(2, 0);
    }
    else{
      http.begin("192.168.4.1", 80, "/led1");
      int httpCode2 = http.GET();
      rptaSrv(httpCode2);
      digitalWrite(2, 1);
    }

  }
  delay(1000);
}
User avatar
By michaeltandy
#53833 TLDR: Move your PIR off GPIO0 and onto GPIO 4, 5, 12, 13, or 14.

Lucky for you, I had a similar problem and had to solve it last week.

I'm guessing you're using a module you can plug directly into your computer's USB port, and that it lets you program the module without manually pressing a program or reset button. These circuits work by using the serial port out-of-band signalling to pull the reset pin down, resetting the chip, then pulling GPIO 0 down to cause the bootloader to enter programming mode. The circuit often looks something like this:

Image

Unfortunately a side-effect of this circuit is you get slightly different behaviour when DTR and RTS are both high to when they're both low. For the module I use, when they're both high they act like an extra ~6k pullup resistor on RST and GPIO0, in parallel with the ~10k pullup resistor my board provides.

For the module I'm using, if I plug my module into a PC and don't access it, DTR and RTS are both low; if I run the Arduino serial console DTR and RTS are both low; but when I close the Arduino serial console DTR and RTS are both left high; and if I power my module from a 5v usb power bank or a separate 3.3v power supply, DTR and RTS are both left high.
User avatar
By drekthral
#53849
michaeltandy wrote:TLDR: Move your PIR off GPIO0 and onto GPIO 4, 5, 12, 13, or 14.

Lucky for you, I had a similar problem and had to solve it last week.

I'm guessing you're using a module you can plug directly into your computer's USB port, and that it lets you program the module without manually pressing a program or reset button. These circuits work by using the serial port out-of-band signalling to pull the reset pin down, resetting the chip, then pulling GPIO 0 down to cause the bootloader to enter programming mode. The circuit often looks something like this:

Image

Unfortunately a side-effect of this circuit is you get slightly different behaviour when DTR and RTS are both high to when they're both low. For the module I use, when they're both high they act like an extra ~6k pullup resistor on RST and GPIO0, in parallel with the ~10k pullup resistor my board provides.

For the module I'm using, if I plug my module into a PC and don't access it, DTR and RTS are both low; if I run the Arduino serial console DTR and RTS are both low; but when I close the Arduino serial console DTR and RTS are both left high; and if I power my module from a 5v usb power bank or a separate 3.3v power supply, DTR and RTS are both left high.


Changed pins and works 100% :) thanks !