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