*เก็บถาวร* = วิธีสร้างเว็บเซิร์ฟเวอร์การจัดการ IoT = สวัสดีทุกคน ฉันเป็นโปรแกรมเมอร์งานอดิเรกและต้องการคำแนะนำเล็กน้อย พูดให้สั้น: ฉันต้องการสร้างเว็บแอปเล็กๆ เพื่อควบคุมหลอดไฟ Philips Hue และอุปกรณ์ IoT อื่นๆ ที่บ้าน ฉันได้เล่นกับ Hue API ใน Python แล้วและมีสคริปต์บางตัวที่ใช้งานได้ดี ฉันยังมีสคริปต์บางตัวที่เรียกใช้คำสั่งต่อ SSH บนอุปกรณ์อื่น ในขั้นตอนต่อไปของฉัน ฉันต้องการสร้างเว็บแอปให้มี UI ที่ดูดี ในอดีตฉันยังสร้างแอป Android ขนาดเล็กด้วย ครั้งแรกใน Kotlin จากนั้นใน Flutter และ Firebase และฉันรู้สึกทึ่งมากที่ได้ UI กึ่งดูดีที่ทำงานใน Flutter ได้อย่างง่ายดาย ฉันรู้ว่า Flutter สามารถใช้เป็นเว็บแอปได้ แต่ฉันไม่คิดว่านี่เป็นเครื่องมือที่เหมาะสมสำหรับสิ่งนี้ ฉันได้ดู React และ Angular และทำงานผ่านคู่มือสำหรับผู้เริ่มต้น เมื่อเทียบกับ Flutter พวกเขาดูเหมือนต้องทำงานมากขึ้นเพื่อให้ดูดี แต่บางทีฉันแค่ต้องการเทมเพลตที่ดีกว่านี้ ในจินตนาการของฉัน ฉันต้องการเรียกใช้สคริปต์หนึ่งสคริปต์เพื่อรวบรวมสถานะปัจจุบัน สคริปต์เขียนลงในไฟล์ JSON และ UI แสดงกราฟหรือไดอะแกรมที่สวยงาม จากนั้นกดปุ่มและสิ่งนี้จะเขียนไฟล์อื่นและเริ่มสคริปต์ ซึ่งอ่านไฟล์และสมมติว่าเปลี่ยนสีของแสงตามนั้น ตอนนี้ฉันหลงทางเล็กน้อยกับสิ่งที่จะเริ่มพูดตามตรง จากสิ่งที่ฉันรวบรวมมา ฉันสามารถสร้าง front-end ด้วย Angular และอาจมี MongoDB เป็น "ระบบไฟล์"ของฉัน ซึ่งฉันสามารถจัดเก็บและโหลดสถานะของไฟหรือสคริปต์ปรับแต่งของฉันได้ ฉันคิดว่าฉันจะต้องมี API เพื่อเชื่อมต่อฐานข้อมูลกับ UI ของฉัน ฉันหวังว่าฉันจะพูดถูกบางส่วน :D คุณจะบอกว่าการเริ่มต้นด้วย JS/TS และสร้าง UI ใน Angular หรือ React เป็นทิศทางที่ถูกต้องหรือไม่? หรือนี่ผิดไปจากความตั้งใจของฉันอย่างสิ้นเชิง? ขอบคุณสำหรับคำแนะนำ! คุณมาถูกทางแล้ว สิ่งที่คุณขาดหายไปคือการรวมข้อมูลทั้งหมดเข้าด้วยกันเป็นข้อมูลจำเพาะสำหรับเซิร์ฟเวอร์ สิ่งที่ฉันจะทำในกรณีของคุณคือสร้างเซิร์ฟเวอร์ที่ให้บริการหน้าเว็บ หน้าเว็บจะส่งคำขอ HTTP กลับไปยัง REST API ที่เปิดเผยโดยเซิร์ฟเวอร์ เซิร์ฟเวอร์เป็นจุดเชื่อมต่อกลางระหว่างหน้าเว็บส่วนหน้าของคุณ สคริปต์ของคุณ ฐานข้อมูลของคุณ อย่าพยายามเชื่อมต่อจากหน้าเว็บไปยัง DB หรือเปลี่ยนสถานะไฟโดยไม่ต้องผ่านเซิร์ฟเวอร์ กำหนดปลายทาง API ของคุณ สมมติว่าคุณต้องการควบคุมไฟ บางทีคุณอาจต้อง: รับโทเค็นการรับรองความถูกต้อง [ HTTP GET/api/token with creds in header], returns token or 401 Get status of devices [ HTTP GET /api/devices with token in header], returns JSON status of all devices Get status of lights [ HTTP GET /api/devices/lights with token in header] returns JSON status of all lights Control a single light [ HTTP POST /api/light/ with token in header, payload specifying RGB], returns empty 200 on valid request, 422 otherwise And so forth. Once you have an API spec you can develop whatever you want to first; the server code for the API (Django is good if you're comfortable with Python), the front-end code, the server code for integrating your scripts or the DB. As long as the API is sufficiently specified then the parts will all talk to each other nicely. Separation of concerns makes it simple to build some bits, mock others up, modify and replace as needed A tip: don't write anything to files if you're just sending JSON data around. The server itself can *probably* just call all the script code directly with no need to pass files, and can definitely serve the JSON via HTTP to the web page for display purposes This sounds like a fun project. Can you tell me a little about your end goal with it? Maybe I’ll do something like it at my house. I guess what I’m wondering is aside from being a fun project, what (if any) controls does it give you over your devices that don’t come sort of “baked in” via remote controls or apps for example that may come with the device. Just would like to hear about your real world advantages of doing this == About Community == Members Online