Code works only with serial monitor
Posted: Fri Aug 26, 2016 2:12 am
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:
Server:
Client:
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 all
is not working... I measured it with my voltmeter and sensor is working but somehow pin is not able to read its value...pirValue = digitalRead(pirPin);
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);
}