Juraj:
why web? why HTTP? plain socket communication will do it
OK. In the mean time this has been adequately dealt with. I'll just add that if the OP chooses to integrate the devices in an existing WLAN it is (a) very simple (without even making a new WLAN connection) to test the web server part using a standard PC browser or smartphone browser and (b) he can enhance the system later to, say, serve up a web page (for a standard browser client) which indicates the current status of the door he is monitoring, or the status of the dht22 temperature/humidity sensor mentioned in the OP.
Of course there are other ways of solving the problem, but using a well known method ensures that the OP can easily get support if something goes wrong.
It looks like i have started something here. The switch attached to the shed door (client) can be either mains or battery ( i have come into possesion of lots of 5v phone chargers so power not a problem). I do have WIFI local router ( i was told to connect through this as better range than just the esp).
SO using get and post requests to send the status of the door is the way to go, sorry i am new to the wifi things and thats where i am falling down. I have used a Uno and set up everthing on it. The door switch when switched operates the servo and the DHT sends the temp to a display so i have it working but all in one place.
I have looked at the wificlient and server examples but still cant figure out how to post the signal switch output? and then then get the signal on the second esp!
Fathead:
It looks like i have started something here. The switch attached to the shed door (client) can be either mains or battery ( i have come into possesion of lots of 5v phone chargers so power not a problem). I do have WIFI local router ( i was told to connect through this as better range than just the esp).
SO using get and post requests to send the status of the door is the way to go, sorry i am new to the wifi things and thats where i am falling down. I have used a Uno and set up everthing on it. The door switch when switched operates the servo and the DHT sends the temp to a display so i have it working but all in one place.
I have looked at the wificlient and server examples but still cant figure out how to post the signal switch output? and then then get the signal on the second esp!
wouldn't it be better to use simple RF communication?
Just been looking at rf but i will then need to buy more stuff i already have 3 esp devices so i wanted to put them to use well maybe do something else with them thanks for all the help
You want one ESP8266 which is physically wired to a door sensor and a DHT22 temp/humidity sensor.
You want another ESP8266 which is physically wired to a servo and display.
If that is the case, then I would still suggest the "web" solution in post #13 and further, I would suggest integrating the ESP8266s in your existing wireless lan if the location of the 'door' ESP8266 and the 'servo' ESP8266 are within range of this.
Your door sensor/DHT ESP8266 would periodically send HTTP GET command of the format http://192.168.1.20/myRoot?door=1&temp=19&humidity=55. Your server would receive these, parse out the data, and act on it accordingly i.e. displaying the data and operating the servo.
Of course there are other ways of doing this. It is a feature of this forum that some threads catalyse intense discussions about valid alternative solutions and their various merits, sometimes in some considerable technical depth. Often, with the risk that the subject of the original post gets almost forgotten in all this.
The RF solution, which would also be valid, requires two radio devices. If you've already got ESP8266, that is an additional reason for staying with the ESP in this case.
Yes that is correct one wsp8266 wired to the magnetic reed switch for the door and a dht22, this can either be mains (5v) or battery.
The other esp8266 hard wired to a lcd or oled and a servo (with a pointer).
I'll look into post #13 and see what i can come up with, I have managed to connect both devices to my network so halfway there.
I have wriiten a code for both client and server they have complied but i havent tried them out yet, im soldering the boards will try by the end of the day. I dont fully understand the wifi but getting closer. The first part i have included the dht but the second part i havent still waiting for the oled to turn up.
Server
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <DHT.h> // DHT22
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#define DHTPIN D3
#define DHTTYPE DHT22
const int REED_PIN = D2; // Pin connected to reed switch
byte ledPin = 2;
char ssid[] = "*********"; // SSID of your home WiFi
char password[] = "*******"; // password of your home WiFi
WiFiServer server(80);
IPAddress ip(192, 168, 1, 80); // 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
DHT dht(DHTPIN, DHTTYPE);
unsigned long DHTtimer = 0;
float hum;
float temp;
unsigned long clientTimer = 0;
void setup() {
pinMode(REED_PIN, INPUT_PULLUP);
//Serial.begin(115200); // only for debug
WiFi.config(ip, gateway, subnet); // forces to use the fix IP
WiFi.begin(ssid, password); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
dht.begin();
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());
pinMode(ledPin, OUTPUT);
server.begin(); // starts the server
server_start(0); // starts the WiFi server
delay(2000);
}
void loop () {
int proximity = digitalRead(REED_PIN); // Read the state of the switch
if (proximity == LOW) // If the pin reads low, the switch is closed.
{
Serial.println ("Door Closed");
}
else
{
Serial.println("Door Open");
}
if (millis() > DHTtimer + 2000) {
hum = dht.readHumidity(); // reads the DHT for humidity
temp = dht.readTemperature(); // reads the DHT for temperature
if (isnan(hum) || isnan(temp)) {
return;
} else {
DHTtimer = millis();
}
}
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
String request = client.readStringUntil('\r'); // receives the message from the client
client.flush();
//client.println(String(temp, 1)); // sends the temperature to the client
//client.println(String(hum, 1)); // sends the humidity to the client
client.println(String(REED_PIN)); // sends the status of the reed switch
digitalWrite(ledPin, HIGH);
}
client.stop(); // terminates the connection with the client
clientTimer = millis();
}
if (millis() - clientTimer > 30000) { // stops and restarts the WiFi server after 30 sec
WiFi.disconnect(); // idle time
delay(500);
server_start(1);
}
}
void server_start(byte restart) {
if (restart) {
Serial.print("Server restart");
} else {
Serial.print("Server start");
}
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
server.begin();
delay(500);
clientTimer = millis();
}
client
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <Servo.h>
Servo myservo;
int pos = 0; // variable to store the servo position
IPAddress server(192,168,1,80); // fix IP of the server
WiFiClient client;
char ssid[] = "*********";
char pass[] = "*********";
unsigned long askTimer = 0;
String REED_PIN;
void setup() {
myservo.attach(D4);
delay(2000);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
// Serial.print(".");
delay(500);
}
}
void loop() {
if (millis() - askTimer > 2340) { // time between two connection to the server
client.connect(server, 80); // connects to the server
client.println("Hello server!\r"); // trigger message to the server, its value is scrapped
REED_PIN = client.readStringUntil('\r'); // received the server's answer
if ((REED_PIN) == LOW)
{
myservo.write(90);
Serial.print("Chicken's are OUT");
}
else
{
myservo.write(0);
Serial.print("Chicken's are IN");
}
}
client.flush();
askTimer = millis();
}
this is my second attempt if you see a problem or i have got it completly wrong please let me know, trial by error i guess
In the example above, door=1 is the current status of the door switch. 1 to indicate open. door=0 would indicate closed.
The format is called “query string” and can be parsed on the receiving web server using standard methods.
Thanks for the help it is most helpful but i dont think this is for me i can't get my head around the wifi issues and everybody keeps saying its easy to do, i have looked at so many examples with no luck