A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By Sheisthedark
#71891 Hi, guys,

I have a project with 3 esp 8266 nodeMCU v1. This is Alarm System. 1 module is a server and 2 modules are clients. To clients i join PIR sensor and they send the data to server. I want is for the server to show the number clients connects to him. If i turn on 1 client - server show me "Clients is - 1" etc. If for example 2 clients and i turn off 1 i want something that would show the correct amount. When i take this code viewtopic.php?f=32&t=5669 alternate (if i turn on show 2, turn off - turn on show me 3 and so on). Please help me :) I can donate :)
[#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Устанавливаем дисплей

extern "C" {
#include<user_interface.h>
}
int status = WL_IDLE_STATUS;
boolean alreadyConnected[3] ;
// MAXIMUM NUMBER OF CLIENTS

byte ledPin = 15;
char ssid[] = "DWORCOWA12/8"; // Мой логин к Wi-Fi
char pass[] = "dworcowa13102012"; // Пароль WiFi
int knopka;
bool Protection_Status = false; // Состояниие охраны (Отключена)


WiFiServer server(80);
WiFiClient TClient[3];

IPAddress ip(192,168,1,18); // IP address of the server
IPAddress gateway(192,168,1,1); // gateway of your network
IPAddress subnet(255,255,255,0);

unsigned long uidDec, uidDecTemp;
#define SS_PIN 4
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN); //



void setup() {
Serial.begin(115200); // only for debug
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
server.begin();
Wire.begin(2, 9);

digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
if (WiFi.status() == WL_CONNECTED) // проверка подключения
{
lcd.begin();
lcd.backlight();
lcd.println("Connected Access!"); //- если подключился к WiFi - Access
przywitanie(); // функция екрана приветствия

}
else
{
lcd.begin();
lcd.backlight();
lcd.println("Connecting to WiFi!");// это почему-то не высвечивает екран..
return;
}
return;
}

void loop() {

WiFiClient TClient = server.available();
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
wlaczenieSystemu(); // инициализация рфид метки. Надание метке номмера UID
// Вот так попробуй
if(uidDec == 3773714048) // если метка равна значению
{Protection_Status = !Protection_Status;
uidDec = 0;}
if (Protection_Status == true)
{//sprawdzenieCzujnika();
test();
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
String value = TClient.readStringUntil('\r'); // для считывания данных с клиента
TClient.flush();
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Stan czujki: " + value);// Екран - Состояние датчика + значение
}
else
{przywitanie(); // функция экрана приветствия
}
TClient.stop();
}
void wlaczenieSystemu() //инициализация РФИД метки
{
SPI.begin();
mfrc522.PCD_Init();
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;}
uidDec = 0;
for (byte i = 0; i < mfrc522.uid.size; i++)
{uidDecTemp = mfrc522.uid.uidByte[i];
uidDec = uidDec * 256 + uidDecTemp;}
}
void sprawdzenieCzujnika() // функция проверки клиента
{
WiFiClient TClient = server.available();
if (!TClient.connected()) {
lcd.print("Cz. odlaczona!"); // клиент одключен
TClient.stop();
}

else if (TClient.available())
{
lcd.print("Czujka jest");
TClient.stop();
}
}
void test()
{
unsigned int i;
for (i= 1; i<3; i++)
{
// wait for a new client:
if (!TClient[i])
{
TClient[i] = server.available();
}
else
{
if (TClient[i].status() == CLOSED)
{
TClient[i].stop();
Serial.print("\nConnection closed on client: " );
Serial.println(i);
Serial.println();
alreadyConnected[i] = false;}
else
{
if(!alreadyConnected[i]) // when the client sends the first byte, say hello:

{
// clean out the input buffer:
TClient[i].flush();
Serial.print("\nNew client: ");
TClient[i].println("Hello, client !");
Serial.println(i);
alreadyConnected[i] = true;
}
if (TClient[i].available())
{
// read the bytes incoming from the client:
String data = TClient[i].readString();
// echo the bytes back to the client:
TClient[i].print(data);
// echo the bytes to the server as well:
Serial.println(data);
}
}
}
}
}
void przywitanie() // Функция приветствия
{
Wire.begin(2, 9);
lcd.begin();
lcd.backlight();// Включаем подсветку дисплея
lcd.setCursor(0,0);
lcd.print("Hello user !");
Serial.print("Hello user !" "\n");
lcd.setCursor(0, 1);
lcd.print("System waiting for card ...");
}
//====================================================================================

void client_status() { // Это тестировал количество клиентов а также их мак адрес , IP и тд

unsigned char number_client;
struct station_info *stat_info;

struct ip_addr *IPaddress;
IPAddress address;
int i=1;

number_client= wifi_softap_get_station_num();
stat_info = wifi_softap_get_station_info();

Serial.print(" Total connected_client are = ");
Serial.println(number_client);
lcd.print("Czujek jest: " + String(number_client));
while (stat_info != NULL) {

IPaddress = &stat_info->ip;
address = IPaddress->addr;

Serial.print("client= ");

Serial.print(i);
Serial.print(" ip adress is = ");
Serial.print((address));
Serial.print(" with mac adress is = ");

Serial.print(stat_info->bssid[0],HEX);
Serial.print(stat_info->bssid[1],HEX);
Serial.print(stat_info->bssid[2],HEX);
Serial.print(stat_info->bssid[3],HEX);
Serial.print(stat_info->bssid[4],HEX);
Serial.print(stat_info->bssid[5],HEX);

stat_info = STAILQ_NEXT(stat_info, next);
i++;
Serial.println();

}
delay(500);
}]