//Sever
//=======================================================//
#include <DHT.h> // DHT22
#include <SPI.h> // SD
#include <Wire.h> // I2C->OLED
#include <Adafruit_GFX.h> // OLED
#include <Adafruit_SSD1306.h> // OLED
#include <Fonts/FreeMonoBoldOblique18pt7b.h>
#include <Fonts/TomThumb.h>
#include <ESP8266WiFi.h>
//=======================================================//
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 oled(OLED_RESET);
#define DHTPIN 13 // DHT pin (D7=13)3
#define DHTTYPE DHT11
//=======================================================//
WiFiServer server(80); // launches the server
IPAddress ip(192, 168, 1, 80); // fix IP of the server
IPAddress gateway(192, 168, 1, 1); // WiFi router's IP
IPAddress subnet(255, 255, 255, 0);
DHT dht(DHTPIN, DHTTYPE);
//=======================================================//
char ssid[] = "xxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxx";
unsigned long DHTtimer = 0;
float h, t;
unsigned long clientTimer = 0;
int led = D6; // led pin
int tempset = 30; // the temperature to start the relay
//=======================================================//
void setup() {
// Serial.begin(115200); // only for debug
dht.begin();
Wire.begin(); // default SDA and SCL
pinMode(led, OUTPUT);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3D); // I2C address of my b&w OLED
oled.setTextColor(WHITE);
oled.setTextSize(1);
oled.clearDisplay();
oledre(0, 10, "Wemos DHT_server_02", 0);
oled.display();
server_start(0); // starts the WiFi server
delay(2000);
}
//=======================================================//
void loop() {
if (millis() > DHTtimer + 2000) {
h = dht.readHumidity();
t = dht.readTemperature(); // reads the DHT for temperature
if (isnan(h) || isnan(t)) {
return;
} else {
DHTtimer = millis();
}
}
if (t < tempset) { // if temp is lower than tempset
digitalWrite(led, LOW);
}
if (t > tempset) { // if temp is higher than tempset
digitalWrite(led, HIGH); // turn on led
}
WiFiClient client = server.available();
if (client) {
oled.invertDisplay(true);
String request = client.readStringUntil('\r'); // reads the message from the client
client.flush();
client.println(String(t, 1)); // sends the temperature to the client
client.print(":");
oled.invertDisplay(true);
client.stop(); // disconnects 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) {
oled.clearDisplay();
oledre(0, 10, "Server restart", 0);
oled.setCursor(0, 20);
} else {
oledre(0, 20, "Server start", 0);
oled.setCursor(0, 30);
}
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
oled.print(".");
oled.display();
delay(500);
}
server.begin();
delay(500);
clientTimer = millis();
}
//=======================================================//
void ipOledre(byte xc) {
oled.setFont(&TomThumb);
oled.setCursor(xc + 35, 63);
oled.setTextSize(1);
oled.print(WiFi.localIP());
}
//=======================================================//
void oledre(byte x, byte y, String text, byte font) {
Serial.println(text);
if (font) {
oled.setFont(&FreeMonoBoldOblique18pt7b);
} else {
oled.setFont(&TomThumb);
}
oled.setCursor(x, y);
oled.print(text);
}
***************************************************************************
========================================================
***************************************************************************
//Client
//=========================================================//
#include <SPI.h>
#include <Wire.h> // I2C->OLED
#include <Adafruit_GFX.h> // OLED
#include <Adafruit_SSD1306.h> // OLED
#include <Fonts/FreeMonoBoldOblique18pt7b.h>
#include <Fonts/TomThumb.h>
#include <ESP8266WiFi.h>
//=========================================================//
IPAddress server(192, 168, 1, 80); // fix IP of the server
WiFiClient client;
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 oled(OLED_RESET);
//=========================================================//
char ssid[] = "xxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxx";
byte xc = 1, yc = 50, dx = 1, dy = 1;
unsigned long askTimer = 0;
unsigned long oledTimer = 0;
String answer;
uint8_t pinD1 = 5; // I2C Bus SCL (clock)
uint8_t pinD2 = 4; // I2C Bus SDA (data)
int led = D6;
//=========================================================//
void setup() {
Wire.begin(pinD2, pinD1); // SDA, SCL
pinMode(led, OUTPUT);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // I2C address of my blue OLED
oled.setTextColor(WHITE);
oled.setTextSize(1);
oled.clearDisplay();
oledre(0, 10, "Wemos DHTkliens_011", 0);
oled.display();
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
//=========================================================//
void loop () {
if (millis() - askTimer > 2340) { // time between two connection to the server
client.connect(server, 80); // connects to the server
Serial.println(".");
client.println("Haliho szerver!\r"); // trigger message to the server, its value is scrapped
answer = client.readStringUntil('\r'); // received the server's answer
String answer = client.readStringUntil('\r'); // receives the answer from the sever
Serial.println("from server: " + answer);
client.flush();
askTimer = millis();
}
if (millis() - oledTimer > 50) { // timer to refresh the OLED
oled.clearDisplay();
oledre(xc, yc, answer + "C", 1);
oled.display();
oledTimer = millis();
}
}
//=========================================================//
void oledre(byte x, byte y, String szoveg, byte font) { // prints strings on the OLED
if (font) {
oled.setFont(&FreeMonoBoldOblique18pt7b);
} else {
oled.setFont(&TomThumb);
}
oled.setCursor(x, y);
oled.print(szoveg);
}
//=========================================================//
ตอนนี้ McuSever แสดง Alarm สถานะ LED เมื่ออุณหภูมิเกินกำหนดแล้ว จะส่งให้ Mcuclient แสดง Alarm สถานะ LED ทำยังไงครับ
//=======================================================//
#include <DHT.h> // DHT22
#include <SPI.h> // SD
#include <Wire.h> // I2C->OLED
#include <Adafruit_GFX.h> // OLED
#include <Adafruit_SSD1306.h> // OLED
#include <Fonts/FreeMonoBoldOblique18pt7b.h>
#include <Fonts/TomThumb.h>
#include <ESP8266WiFi.h>
//=======================================================//
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 oled(OLED_RESET);
#define DHTPIN 13 // DHT pin (D7=13)3
#define DHTTYPE DHT11
//=======================================================//
WiFiServer server(80); // launches the server
IPAddress ip(192, 168, 1, 80); // fix IP of the server
IPAddress gateway(192, 168, 1, 1); // WiFi router's IP
IPAddress subnet(255, 255, 255, 0);
DHT dht(DHTPIN, DHTTYPE);
//=======================================================//
char ssid[] = "xxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxx";
unsigned long DHTtimer = 0;
float h, t;
unsigned long clientTimer = 0;
int led = D6; // led pin
int tempset = 30; // the temperature to start the relay
//=======================================================//
void setup() {
// Serial.begin(115200); // only for debug
dht.begin();
Wire.begin(); // default SDA and SCL
pinMode(led, OUTPUT);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3D); // I2C address of my b&w OLED
oled.setTextColor(WHITE);
oled.setTextSize(1);
oled.clearDisplay();
oledre(0, 10, "Wemos DHT_server_02", 0);
oled.display();
server_start(0); // starts the WiFi server
delay(2000);
}
//=======================================================//
void loop() {
if (millis() > DHTtimer + 2000) {
h = dht.readHumidity();
t = dht.readTemperature(); // reads the DHT for temperature
if (isnan(h) || isnan(t)) {
return;
} else {
DHTtimer = millis();
}
}
if (t < tempset) { // if temp is lower than tempset
digitalWrite(led, LOW);
}
if (t > tempset) { // if temp is higher than tempset
digitalWrite(led, HIGH); // turn on led
}
WiFiClient client = server.available();
if (client) {
oled.invertDisplay(true);
String request = client.readStringUntil('\r'); // reads the message from the client
client.flush();
client.println(String(t, 1)); // sends the temperature to the client
client.print(":");
oled.invertDisplay(true);
client.stop(); // disconnects 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) {
oled.clearDisplay();
oledre(0, 10, "Server restart", 0);
oled.setCursor(0, 20);
} else {
oledre(0, 20, "Server start", 0);
oled.setCursor(0, 30);
}
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
oled.print(".");
oled.display();
delay(500);
}
server.begin();
delay(500);
clientTimer = millis();
}
//=======================================================//
void ipOledre(byte xc) {
oled.setFont(&TomThumb);
oled.setCursor(xc + 35, 63);
oled.setTextSize(1);
oled.print(WiFi.localIP());
}
//=======================================================//
void oledre(byte x, byte y, String text, byte font) {
Serial.println(text);
if (font) {
oled.setFont(&FreeMonoBoldOblique18pt7b);
} else {
oled.setFont(&TomThumb);
}
oled.setCursor(x, y);
oled.print(text);
}
***************************************************************************
========================================================
***************************************************************************
//Client
//=========================================================//
#include <SPI.h>
#include <Wire.h> // I2C->OLED
#include <Adafruit_GFX.h> // OLED
#include <Adafruit_SSD1306.h> // OLED
#include <Fonts/FreeMonoBoldOblique18pt7b.h>
#include <Fonts/TomThumb.h>
#include <ESP8266WiFi.h>
//=========================================================//
IPAddress server(192, 168, 1, 80); // fix IP of the server
WiFiClient client;
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 oled(OLED_RESET);
//=========================================================//
char ssid[] = "xxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxx";
byte xc = 1, yc = 50, dx = 1, dy = 1;
unsigned long askTimer = 0;
unsigned long oledTimer = 0;
String answer;
uint8_t pinD1 = 5; // I2C Bus SCL (clock)
uint8_t pinD2 = 4; // I2C Bus SDA (data)
int led = D6;
//=========================================================//
void setup() {
Wire.begin(pinD2, pinD1); // SDA, SCL
pinMode(led, OUTPUT);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // I2C address of my blue OLED
oled.setTextColor(WHITE);
oled.setTextSize(1);
oled.clearDisplay();
oledre(0, 10, "Wemos DHTkliens_011", 0);
oled.display();
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
//=========================================================//
void loop () {
if (millis() - askTimer > 2340) { // time between two connection to the server
client.connect(server, 80); // connects to the server
Serial.println(".");
client.println("Haliho szerver!\r"); // trigger message to the server, its value is scrapped
answer = client.readStringUntil('\r'); // received the server's answer
String answer = client.readStringUntil('\r'); // receives the answer from the sever
Serial.println("from server: " + answer);
client.flush();
askTimer = millis();
}
if (millis() - oledTimer > 50) { // timer to refresh the OLED
oled.clearDisplay();
oledre(xc, yc, answer + "C", 1);
oled.display();
oledTimer = millis();
}
}
//=========================================================//
void oledre(byte x, byte y, String szoveg, byte font) { // prints strings on the OLED
if (font) {
oled.setFont(&FreeMonoBoldOblique18pt7b);
} else {
oled.setFont(&TomThumb);
}
oled.setCursor(x, y);
oled.print(szoveg);
}
//=========================================================//