Hi, I'm wanna creat a small project followed by this tutorial (https://www.instructables.com/ESP8266-and-Python-Communication-ForNoobs/?fbclid=IwAR2afNnKZRuwAgqIrsxzDo5gjv5LuxUzj3UwpuXdCYmIezHTofWaGIgy-sM), which can potray in this pic below.
**Brief description: esp collected data from sensors. Then, esp connects to a router and creates a localhost server and starts to waiting a request. Everytime a client (pc) (connected to same router) sends a request to that localhost, esp runs the desired code and then returns the result as an http request.
**The code is work well, but I want to add more rule: Only 1 pc can request to localhost and get the data from esp (Ex, we have 2 pc: pc1 and pc2 connect to the router, but only pc1 can get the data from localhost, if pc2 try to go to the localhost, esp won't send the data).
I try to do that by get the MAC address of client, which send a request to localhost and check if it similar with pc1 MAC address, if not, esp will not returns the data. But I dont know how to check the MAC address of client (the pc) on ESP8266 in STA mode. Can anyone help me? Thank you so much for your help
**My code so far:
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
WiFiServer server(80); //port 80
WiFiClient client;
String path;
void start(String ssid, String pass) {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP: ");
Serial.println(WiFi.localIP());
server.begin();
}
bool isReq = false;
void CheckReq() {
client = server.available();
if (!client) {
return;
}
Serial.println("");
Serial.print("FIND NEW REQUEST - ");
while (client.connected() && !client.available()) {
delay(1);
}
String req = client.readStringUntil('\r');
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Requested Path: ");
Serial.println(req);
path = req;
isReq = true;
client.flush();
}
void waitNewReq () {
do {
CheckReq();
}
while (!isReq);
isReq = false;
}
void returnStr(String data1, String data2) {
String s1;
s1 = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s1 += data1;
s1 += " ";
s1 += data2;
client.print(s1);
}
void returnData(float data1, int data2) {
returnStr(String(data1), String(data2));
}
String getPath() {
return path;
}
#include <ESP8266WiFi.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include "func.h"
const char* ssid = "ssid";
const char* pass = "pas*******";
const int TEMP_PIN = 4;
const int LIGHT_PIN = 14;
OneWire oneWire(TEMP_PIN);
DallasTemperature sensors(&oneWire);
void setup () {
Serial.begin(115200);
start(ssid,pass);
pinMode(LIGHT_PIN, INPUT);
}
void loop() {
sensors.requestTemperatures();
Serial.print("Temp: ");
Serial.println(sensors.getTempCByIndex(0));
Serial.print("Light: ");
Serial.println(digitalRead(LIGHT_PIN));
waitNewReq();
returnData(sensors.getTempCByIndex(0),digitalRead(LIGHT_PIN));
}
