พอดี อ.ให้โจทย์มาในการ วัดค่าความชื้นแล้วส่งจากตัว esp8266 ที่เป็น client ไปยัง esp8266 Server เพื่อไปสั่งให้รีเลย์ทำงาน แต่ตอนนี้ติดเรื่องการรับส่งค่า แล้วนำค่าที่ได้ไปใช้ครับ
ต้องการให้รับส่งกับแบบไหน
ส่งผ่าน wifi น่ะครับ เชื่อมกันสองตัว รับส่งค่ากันแล้วนำไปใช้ ตอนนี้ไม่รู้ต้องใช้ฟังชั่นไหนในการรับค่าความชื้นแล้วนำไปใช้
ทำได้ครับ
ตัวอย่างการอ่านค่า ข้อมูลด้วย esp8266 แล้วส่งค่าไปให้ esp8266 อีกตัว ผ่าน WiFi
การตั้งค่า
ตัวอย่างการต่อ
โค้ตตัวส่ง และอ่านค่า
#include <ESP8266WiFi.h>
#include <U8g2lib.h>
//U8g2 Constructor List - https://github.com/olikraus/u8g2/wiki/u8g2setupcpp#introduction
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4);
const char *ssid = "poopssid";
const char *password = "pingu4prez";
const int analogInPin = 0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the potentiometer
int outputValue = 0; // value sent to server
void setup() {
Serial.begin(115200);
delay(10);
// Explicitly set the ESP8266 to be a WiFi-client
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
u8g2.begin();
u8g2.setFont(u8g2_font_logisoso62_tn);
u8g2.setFontMode(0); // enable transparent mode, which is faster
}
void loop() {
// read the analog in value:
sensorValue = analogRead(A0);
// map to range. The pot goes from about 3 to 1023. This makes the sent value be between 0 and 999 to fit on the OLED
outputValue = map(sensorValue, 3, 1023, 0, 999);
char intToPrint[5];
itoa(outputValue, intToPrint, 10); //integer to string conversion for OLED library
u8g2.firstPage();
u8g2.drawUTF8(0, 64, intToPrint);
u8g2.nextPage();
// Use WiFiClient class to create TCP connections
WiFiClient client;
const char * host = "192.168.4.1";
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request. Something like /data/?sensor_reading=123
String url = "/data/";
url += "?sensor_reading=";
url += intToPrint;
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
delay(500);
}
โค้ตตัวรับ
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <U8g2lib.h>
//U8g2 Constructor List - https://github.com/olikraus/u8g2/wiki/u8g2setupcpp#introduction
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4);
const char *ssid = "poopssid";
const char *password = "pingu4prez";
ESP8266WebServer server(80);
void handleSentVar() {
if (server.hasArg("sensor_reading")) { // this is the variable sent from the client
int readingInt = server.arg("sensor_reading").toInt();
char readingToPrint[5];
itoa(readingInt, readingToPrint, 10); //integer to string conversion for OLED library
u8g2.firstPage();
u8g2.drawUTF8(0, 64, readingToPrint);
u8g2.nextPage();
server.send(200, "text/html", "Data received");
}
}
void setup() {
delay(1000);
u8g2.begin();
u8g2.setFont(u8g2_font_logisoso62_tn);
u8g2.setFontMode(0); // enable transparent mode, which is faster
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
server.begin();
}
void loop() {
server.handleClient();
}
ที่มา