LMS行政E化暨班級社群教職員工數位歷程檔線上手冊Login
NodeMCU
by 王文良 2019-10-29 06:36:59, Reply(0), Views(61)

ESP8266: Getting Started


app inventor & nodeMCU by json

通過 ESP8266 搭建簡單的物聯網項目


ESP8266+aREST框架+App Inventor製作(『遠程』監測)溫濕度計
https://kknews.cc/zh-tw/other/q4kyy2b.html

由aREST控制感測器


Control Your Raspberry Pi From Anywhere Using aREST

ESPlorer — Integrated Development Environment (IDE) for ESP8266 developers

NodeMCU系列文件

https://esp8266.ru/esplorer/

nodeMCU & jQuery(調整LED亮度)

nodeMCU & jQuery (讀取溫度)

esp8266 websocket javascript example with css style button using arduino ide - NodeMcu


example code:

#include <ESP8266WiFi.h> #include <WebSocketsServer.h> #include <ESP8266WebServer.h>
const char* ssid="1509"; const char* password="apower99";
int LED=2; int websocketMillis=50; WebServer server(80); WebSocketsServer webSocket=WebSocketsServer(88); String webSite,JSONtxt; boolean LEDonoff=true;
const char webSiteCont[] PROGMEM = R"=====( <!DOCTYPE HTML> <HTML> <META name="viewport" content="width=device-width, initial-scale=1"> <SCRIPT> InitWebSocket() function InitWebSocket() { websock = new WebSocket("ws://"+window.location.hostname+":88/"); websock.onmessage=function(evt) { JSONobj = JSON.parse(evt.data); document.getElementById('btn').innerHTML = JSONobj.LEDonoff; if(JSONobj.LEDonoff == 'ON') { document.getElementById('btn').style.background='#FFA200'; document.getElementById('btn').style["boxShadow"]="0px 0px 0px 0px #FFA200"; }else{ document.getElementById('btn').style.background='#111111'; document.getElementById('btn').style["boxShadow"]="0px 0px 0px 8px #111111"; } } } function button(){ btn = 'LEDonoff=ON'; if(document.getElementById('btn').innerHTML === 'ON') { btn = 'LEDonoff=OFF'; } websock.send(btn); } </SCRIPT>
<style> #btn{ display: inline-block; text-decoration:none; background: #8cd460; color: rgba(255,255,255, 0.80); font: 100px arial, sans-serif; font-weight: bold; line-height: 320px; width: 320px; height: 320px; border-radius: 50%; margin:30%; margin-top:60px; text-align: center; vertical-align: middle; overflow: hidden; box-shadow: 0px 0px 0px 8px #8cd460; border: solid 2px rgba(255,255,255,0.47); transition: 0.4s; } </style>
<BODY> <a href="#" id="btn" ONCLICK='button()'></a> </BODY> </HTML> )====="; void WebSite(){ server.send(200,"text/html",webSiteCont); } void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t welength) { String payloadString = (const char *)payload; Serial.print("payloadString= "); Serial.println(payloadString); if(type == WStype_TEXT) { byte seperator=payloadString.indexOf('='); String var = payloadString.substring(0,seperator); Serial.print("var= "); Serial.println(var); String val = payloadString.substring(seperator+1); Serial.print(" "); if(var == "LEDonoff"){ LEDonoff = false; if(val == "ON"){ LEDonoff = true; } } } } void setup() { Serial.begin(115200); pinMode(LED,OUTPUT); ESP8266WiFi.begin(ssid,password); while(ESP8266WiFi.status() != WL_CONNECTED)
{ Serial.println(":"); delay(500); } ESP8266WiFi.mode(ESP8266WIFI_STA);
Serial.println("Start Esp"); Serial.println(ESP8266WiFi.localIP());
server.on("/",WebSite); server.begin(); webSocket.begin(); webSocket.onEvent(webSocketEvent); } void loop() { webSocket.loop(); server.handleClient(); if(LEDonoff == true) { digitalWrite(LED,HIGH); }else{ digitalWrite(LED,LOW); } String LEDswitch = "OFF"; if(LEDonoff == true){ LEDswitch = "ON"; } JSONtxt ="{\"LEDonoff\":\""+LEDswitch+"\"}"; webSocket.broadcastTXT(JSONtxt); }
Reply