Deva_Rishi:
The OP posted both sensor(client0 code and server code (as links to github rather than the preferred within </> code-tags) but they are both there.[/code]
Correct.
Still here is a copy-paste from github:
/* Create a WiFi access point and provide a web server on it. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include "server_config.h"
#ifndef APSSID
#define BLYNK_AUTH_CODE "aaaa"
#define APSSID "ESP-Pump-Server"
#define APPSK "1234567890"
#define SENSOR_ID "12345"
#endif
const int VERSION = 1;
// hw
const int RELAY_PIN = D4;
// software
HTTPClient http;
String sensor_host = "http://192.168.4.2";
// thresholds in cm; sensor value
const int SENSOR_FULL_THRESHOLD = 20;
const int SENSOR_LOW_THRESHOLD = 160;
int last_measurement = -1;
// default check is once per 15 minutes
const int SENSOR_POLL_DELAY = 900000;
// while pump is active check every second
const int ACTIVE_SENSOR_POLL_DELAY = 1000;
/* Set these to your desired credentials. */
const char *ssid = APSSID;
const char *password = APPSK;
int SERVER_STATE = 0;
const int STATE_IDLE = 0;
const int STATE_ERROR = 1;
const int STATE_SENSOR_CONNECTED = 2;
const int STATE_SENSOR_OK = 3;
const int STATE_RELAY_CLOSED = 4;
int get_state() {
return SERVER_STATE;
}
ESP8266WebServer server(80);
void state_on_registered() {
Serial.println("state_on_registered");
SERVER_STATE = STATE_SENSOR_CONNECTED;
}
void state_on_sensor_full() {
Serial.println("state_on_sensor_full");
SERVER_STATE = STATE_SENSOR_OK;
}
void state_on_relay_close() {
Serial.println("state_on_relay_close");
SERVER_STATE = STATE_RELAY_CLOSED;
}
void state_on_error() {
Serial.println("state_on_error");
SERVER_STATE = STATE_ERROR;
}
void handleRoot() {
server.send(200, "text/html", "ESP-Pump-Server");
}
void handleRegister() {
if (server.method() != HTTP_POST) {
server.send(405, "text/plain", "Method Not Allowed");
Serial.println("handleRegister wrong method");
} else {
String message = "";
if (server.arg("sensor_code") == SENSOR_ID) {
Serial.println("sensor_registered true");
if (server.hasArg("sensor_host")) {
sensor_host = server.arg("sensor_host");
state_on_registered();
Serial.println("Sensor registered @ " + sensor_host);
}
}
// debug
Serial.println("handleRegister args:");
Serial.println(server.arg("sensor_code"));
Serial.println(SENSOR_ID);
Serial.println(server.arg("sensor_host"));
Serial.println(message);
if (get_state() == STATE_SENSOR_CONNECTED) {
server.send(200, "text/html", "Registered");
} else {
server.send(401, "text/html", "Wrong arguments");
}
}
}
void readSensorData() {
String URL = sensor_host + "/sensor";
http.begin(URL);
Serial.println("readSensorData @ " + URL);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("readSensorData GET: " + payload);
check_measurement(payload.toInt());
} else {
state_on_error();
}
http.end();
// #TODO replace with proper non-blocking delay
delay(ACTIVE_SENSOR_POLL_DELAY);
}
void check_measurement(int tmp_last_measurement) {
if (tmp_last_measurement >= 0) {
last_measurement = tmp_last_measurement;
// normal operation
if (get_state() == STATE_RELAY_CLOSED) {
// relay closed, waiting till the tank reaches FULL
if (last_measurement <= SENSOR_FULL_THRESHOLD) {
state_on_sensor_full();
}
} else {
// relay open, just watching the tank
if (last_measurement >= SENSOR_LOW_THRESHOLD) {
state_on_relay_close();
}
}
} else {
state_on_error();
}
}
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/register", handleRegister);
server.begin();
Serial.println("HTTP server started");
WiFi.hostname("relay-server");
}
void loop() {
server.handleClient();
/*
* State Loop
*
* If registered
* If relay open then poll sensor with timer REGULAR
* If SENSOR_LOW then close relay
* If SENSOR_OK then open relay
* poll sensor with timer ACTIVE
*
*/
readSensorData();
if (get_state() >= STATE_SENSOR_CONNECTED) {
// readSensorData();
}
digitalWrite(RELAY_PIN, get_state() == STATE_RELAY_CLOSED);
}
So it seems that you can connect but your request is denied, have you tried adding a handleNotFound() callback ?
I tried, but to no effect.
This is not a 404 error.
SteveMann:
Just out of curiosity, if all you are doing is to poll a sensor, why not simply use MQTT for sensor data transfer?
I didn't know about it, and I'm familiar with REST.
Deva_Rishi:
I don't think he is trying to do that, but maybe didn't update the sketch he posted between switching. Anyway, just humor me, can you put both of them in station mode and get the client to connect to the server?
Tried to do it yesterday, but nothing woked at all, even the things that used to be stable before.
Starting to assume that there is a hardware issue with one of my boards.