แก้ไขยังไงครับ
โค้ตนี้จะทำอะไรครับ
วางโค้ตเต็มให้ด้วยครับ
เมื่อกดปุ่ม button1 บนappinventor แล้วให้คำสั่งเซนเซอร์ทำงานครับผม
#define echoPin D7 // Echo Pin
#define trigPin D6 // Trigger Pin
#include <ESP8266WiFi.h>
const char* ssid = "Riemta"; // ชื่อ Access Point
const char* password = "0843472099"; //password Access Point
IPAddress local_ip = {192,168,1,8}; // IP address ของ Node MCU แก้ตามเหมาะสมของผู้ใช้
IPAddress gateway = {192,168,1,1}; // ตั้งค่าตรงตาม Router ใน Local network
IPAddress subnet = {255,255,255,0};
WiFiServer server(80); // http port
double readCmDistance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//Return calculate the distance (in cm) based on the speed of sound.
return pulseIn(echoPin, HIGH)/58.2;
}
void setup() {
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(115200);
delay(10);
// prepare GPIO5 กำหนดขา ควบคุมค่า Output ของ Node MCU
digitalWrite(trigPin, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(local_ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r'); // คำขอจะต้องเท่ากับ client
Serial.println(req); //แสดงคำขอ
client.flush(); // รอส่งข้อมูลออกไปทั้งหมด
// Match the request
int val; //ประกาศตัวแปร val
if (req.indexOf("/gpio/0") != -1) //ในกรณี req ขาของ GPIO0 เท่ากับ -1
val = 0; // ให้ค่า val =0
else if (req.indexOf("/gpio/1") != -1) // ถ้า คำขอ GPIO1 เท่ากับ -1
val = 1; //ให้ค่า val=1
else { // ในกรณีถ้าไม่ใช่ทั้งหมด
Serial.println("invalid request"); //แสดงผล invalid request
client.stop(); // ให้ไคลเอนท์หยุด
return; //ย้อนกลับไปฟังชั่นบน
}
// Set GPIO2 according to the request
digitalWrite(trigPin, val); //อันนี้ให้โหนดกำหนด GPIO ตามคำขอ (request)กำหนดขา 5 = val
client.flush(); // ฟังชั่น ที่รอตัวแปรเอาต์พุตทั้งหมดถูกส่งไป
// Prepare the response แสดงข้อความสถานะ
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now ";
s += (val)?"high":"low"; //แสดงข้อความสถานะ high หรือ low
s += "\n";
// Send the response to the client
if (val) s += "Distance = " + String(readCmDistance()) + " cm.\n";
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
นี้ครับผม
แก้ไขแล้วครับผม
แก้ไขแล้วเป็นยังไงครับ
ขอลิ้งค์ที่ใช้อ้างอิงหน่อยครับ ว่าทำตามตัวอย่างไหนมา
ขอดูหน้าแอพที่เราสร้างหน่อยครับัว่ามีอะไรบ้าง
ขอดูหน้าที่เป็น ui ด้วยครับ
ทำอะไรมาถึงขึ้นครับ
นั่งแก้โค้ดครับผม
ขอบคุณมากๆ ครับ