2020-03-20 esp01s做的繼電器控制

基于esp8266 2.5.2 給出的范例WiFiManualWebServer,稍做修改現(xiàn)實中文,并符合自己的控制邏輯.

全文代碼如下:

/* This sketch demonstrates how to set up a simple HTTP-like server. The server will set a GPIO pin depending on the request http://server_ip/gpio/0 will set the GPIO2 low, http://server_ip/gpio/1 will set the GPIO2 high server_ip is the IP address of the ESP8266 module, will be printed to Serial when the module is connected.*/#include#ifndef STASSID#define STASSID "your-ssid" //修改為你自己的無線網(wǎng)絡ssid#define STAPSK "your-password" //修改為你自己的無線密碼#endif#define LED_BUILTIN 0const char* ssid = STASSID;const char* password = STAPSK;// Create an instance of the server// specify the port to listen on as an argumentWiFiServer server(80);void setup() { Serial.begin(115200); // prepare LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, 0); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print(F("Connecting to ")); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } Serial.println(); Serial.println(F("WiFi connected")); // Start the server server.begin(); Serial.println(F("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; } Serial.println(F("new client")); client.setTimeout(5000); // default is 1000 // Read the first line of the request String req = client.readStringUntil('\r\n'); Serial.println(F("request: ")); Serial.println(req); // Match the request int val; if (req.indexOf(F("/gpio/0")) != -1) { val = 0; } else if (req.indexOf(F("/gpio/1")) != -1) { val = 1; } else { Serial.println(F("invalid request")); val = digitalRead(LED_BUILTIN); } // Set LED according to the request digitalWrite(LED_BUILTIN, val); // read/ignore the rest of the request // do not client.flush(): it is for output only, see below while (client.available()) { // byte by byte is not very efficient client.read(); //Serial.println(client.read()); } // Send the response to the client // it is OK for multiple small client.print/write, // because nagle algorithm will group them into one single packet client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html;charset=utf-8\r\n\r\n\r\n\r\n 模塊狀態(tài)是: "));Serial.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html;charset=utf-8\r\n\r\n\r\n\r\n 模塊狀態(tài)是: ")); client.print((val) ? F("關") : F("開"));Serial.print((val) ? F("關") : F("開")); client.print(F("

點擊打開點擊打開點擊關閉點擊關閉返回")); Serial.print (F("'>返回"));? //client.print(F("/gpio/0'>點擊關閉"));? // The client will actually be *flushed* then disconnected? // when the function returns and 'client' object is destroyed (out-of-scope)? // flush = ensure written data are received by the other side? Serial.println(F("Disconnecting from client"));}


學習本部分代碼注意使用rom來減少ram消耗,值得學習.

關鍵詞:client.read();??string .indexOf(f(""));? client.print(F(""))

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蝌箍,一起剝皮案震驚了整個濱河市黍檩,隨后出現(xiàn)的幾起案子噩峦,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機馁启,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來芍秆,“玉大人惯疙,你說我怎么就攤上這事⊙叮” “怎么了霉颠?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長荆虱。 經(jīng)常有香客問我蒿偎,道長朽们,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任诉位,我火速辦了婚禮骑脱,結果婚禮上,老公的妹妹穿的比我還像新娘苍糠。我一直安慰自己叁丧,他們只是感情好,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布岳瞭。 她就那樣靜靜地躺著拥娄,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瞳筏。 梳的紋絲不亂的頭發(fā)上稚瘾,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天,我揣著相機與錄音乏矾,去河邊找鬼孟抗。 笑死,一個胖子當著我的面吹牛钻心,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播铅协,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼捷沸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了狐史?” 一聲冷哼從身側響起痒给,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎骏全,沒想到半個月后苍柏,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡姜贡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年试吁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片楼咳。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡熄捍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出母怜,到底是詐尸還是另有隱情余耽,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布苹熏,位于F島的核電站碟贾,受9級特大地震影響币喧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜袱耽,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一粱锐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扛邑,春花似錦怜浅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至沥阳,卻和暖如春跨琳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背桐罕。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工脉让, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人功炮。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓溅潜,卻偏偏與公主長得像,于是被迫代替她去往敵國和親薪伏。 傳聞我的和親對象是個殘疾皇子滚澜,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容