第一篇真正的文章:nodeMCU esp8266自學(xué)筆記

法克啊城榛,本來(lái)我的底線是要自己建網(wǎng)站的。态兴。狠持。各種原因沒(méi)有成功只好在簡(jiǎn)書(shū)上寫了。瞻润。喘垂。

這篇文章是宗旨是怎么用arduino IDE來(lái)給ESP8266寫程序。ESP8266是我最近發(fā)現(xiàn)的很黑科技的一個(gè)東西绍撞。首先正勒,他繼承了WIFI功能,本身也有GPIO之類的傻铣,這些以前的模塊都能做到到是也沒(méi)有什么了章贞,但是價(jià)格非常之便宜,淘寶只需要25塊錢非洲,比普通的Arduino還要便宜一些鸭限。而最diao的是他只要安裝一個(gè)插件,就可以直接用Arduino IDE寫程序怪蔑,就像是給Arduino寫程序一樣里覆。很多Arduino的庫(kù)也可以直接使用。

首先缆瓣,幾乎所有的部分都是來(lái)自于 Adafruit的網(wǎng)站

恩喧枷,然后需要把Arduino升級(jí)到1.6.4以上。

這里開(kāi)始可能電腦要翻墻弓坞,但是如果的電信的網(wǎng)絡(luò)好像也可以不翻


選擇這里preferences偏好設(shè)置

在那個(gè)文本框輸入http://arduino.esp8266.com/stable/package_esp8266com_index.json

選擇boards manager

搜索esp8266隧甚,然后點(diǎn)擊安裝

之后對(duì)話框下面會(huì)有一個(gè)進(jìn)度調(diào),要下載大約6m的文件渡冻,但是速度很慢戚扳。

之后就可以把我的代碼拷進(jìn)來(lái),整體界面和processing很像


點(diǎn)擊工具族吻,然后按照我的選擇帽借,端口選擇最下面的一個(gè)

然后就可以像給Arduino寫程序一樣寫了。

燒寫

這時(shí)候要拔掉控制板的220v插頭超歌。
燒寫程序的時(shí)候要先把控制板查到usb口上砍艾,然后在插口旁邊有兩個(gè)按鈕,先按住標(biāo)有flash的按鈕巍举,然后按住標(biāo)有reset的按鈕脆荷,板子上的藍(lán)燈會(huì)閃爍一下,然后點(diǎn)擊arduino上的upload或者下載按鈕。

UPDATE:
做了一個(gè)閃電云蜓谋,然后拿回來(lái)以后不知道為什么就不能用了梦皮,直接接引腳是有用的,用usb供電就不行桃焕,用萬(wàn)用表量了一下VCC有一個(gè)限流電阻好像是斷了剑肯。用杜邦線短接以后就行了。

然后不知道為什么現(xiàn)在燒程序不用想以前那樣按按鈕了观堂。退子。。

然后這里是天氣云(只能實(shí)現(xiàn)一個(gè)天氣類型的展示)的代碼

    /*
     *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
     *
     *  You need to get streamId and privateKey at data.sparkfun.com and paste them
     *  below. Or just customize this script to talk to other HTTP servers.
     *
     */

    #include <ESP8266WiFi.h>
    #include <Adafruit_NeoPixel.h>






    #define SENSOR_PIN   15
    #define PIXEL_PIN    5
    #define PIXEL_COUNT  60
    #define LED_IN       4




    Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);


    const char* ssid     = "X imlab";
    const char* password = "";

    const char* host = "query.yahooapis.com";





    void setup() {
      Serial.begin(115200);
      delay(10);


      pinMode(SENSOR_PIN, INPUT);


      strip.begin();
      strip.show(); // Initialize all pixels to 'off'


      // We start by connecting to a WiFi network

      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);

      
      
      WiFi.begin(ssid, password);


      pinMode(LED_IN, OUTPUT);

    }


    void loop() {
      if(digitalRead(SENSOR_PIN) == HIGH){
        digitalWrite(LED_IN,  LOW);
        delay(100);
        digitalWrite(LED_IN, HIGH);
        

        if(WiFi.status() != WL_CONNECTED){
          rainbowCycle(5);
          colorWipe(strip.Color(0,0,0), 0);
          Serial.println("connecting to WiFi");
          
        } else {
          Serial.print("connected to ");
          Serial.print(ssid);
          Serial.println(", and now show the weather");
          digitalWrite(LED_IN,  LOW);
          delay(100);
          digitalWrite(LED_IN, HIGH);

          int code_now = getWeatherCode();
          while(code_now == -1){
            code_now = getWeatherCode();

          }
          Serial.print("weather code is");
          Serial.println(code_now);
          displayWeather(code_now);
          
        }

      delay(6000);


        
      }else{
        delay(200);
      }


    }


    int getWeatherCode(){

      Serial.print("connecting to ");
      Serial.println(host);
      
      // Use WiFiClient class to create TCP connections
      WiFiClient client;
      const int httpPort = 80;
      if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return -1;
      }
      
      // We now create a URI for the request
      String url = "/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.placefinder%20where%20text%3D\"shenzhen\")%20and%20u%3D\"c\"%0A&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
      
      Serial.print("Requesting URL: ");
      Serial.println(url);
      
      // 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");
      delay(150);
      
      // Read all the lines of the reply from server and print them to Serial
      String str = "";
      while(client.available()){
        String line = client.readStringUntil('\r');
        str += line;
      }

      //Serial.println(str);

      //get the code of weather now see here: https://developer.yahoo.com/weather/documentation.html
      int weather_code_begin = str.indexOf("condition\"")+20;
      int weather_code_end   = str.indexOf("\"", weather_code_begin);
    //  Serial.print("begin at ");
    //  Serial.println(weather_code_begin);
    //  Serial.print("end with");
    //  Serial.println(weather_code_end);
      if(weather_code_end == -1){
          return -1;
      }
      int weather_code       = str.substring(weather_code_begin,weather_code_end).toInt();
      Serial.print("code = ");
      Serial.println(weather_code);

      return weather_code;



    }




    void displayWeather(int code_now){
      switch (code_now) {
          case 25:      
            // code
            for(int i=0; i<255; i++){
              colorWipe(strip.Color(0,0,i), 0);
              delay(20);
            }
            delay(1000);
            break;
          case 33:
            //fair (night)
            colorWipe(strip.Color(73, 147, 255),3);
            break;
          case 34:
            //fair (day)
            colorWipe(strip.Color(24,216,243),10);
            colorWipe(strip.Color(0, 0, 0), 0);
            colorWipe(strip.Color(24,216,243),10);

            break;
          case 36:
            // hot
            for(int i=0; i<255; i++){
              colorWipe(strip.Color(i,0,0), 0);
              delay(20);
            }
            delay(1000);        
            break;
      }
      


      colorWipe(strip.Color(0, 0, 0), 0);





    }


    void colorWipe_short(uint16_t be, uint16_t en, uint32_t c, uint8_t wait){
      for (uint16_t i = be; i < en; i++) {
        strip.setPixelColor(i, c);
        strip.show();
        delay(wait);
      }

    }

    // Fill the dots one after the other with a color
    void colorWipe(uint32_t c, uint8_t wait) {
      for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
        strip.show();
        delay(wait);
      }
    }

    void rainbow(uint8_t wait) {
      uint16_t i, j;

      for (j = 0; j < 256; j++) {
        for (i = 0; i < strip.numPixels(); i++) {
          strip.setPixelColor(i, Wheel((i + j) & 255));
        }
        strip.show();
        delay(wait);
      }
    }


    // Input a value 0 to 255 to get a color value.
    // The colours are a transition r - g - b - back to r.
    uint32_t Wheel(byte WheelPos) {
      WheelPos = 255 - WheelPos;
      if (WheelPos < 85) {
        return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
      }
      if (WheelPos < 170) {
        WheelPos -= 85;
        return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
      }
      WheelPos -= 170;
      return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
    }


    // Slightly different, this makes the rainbow equally distributed throughout
    void rainbowCycle(uint8_t wait) {
      uint16_t i, j;

      for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
        for(i=0; i< strip.numPixels(); i++) {
          strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
        }
        strip.show();
        delay(wait);
      }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末型将,一起剝皮案震驚了整個(gè)濱河市寂祥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌七兜,老刑警劉巖丸凭,帶你破解...
    沈念sama閱讀 216,843評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異腕铸,居然都是意外死亡惜犀,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門狠裹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)虽界,“玉大人,你說(shuō)我怎么就攤上這事涛菠±蛴” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,187評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵俗冻,是天一觀的道長(zhǎng)礁叔。 經(jīng)常有香客問(wèn)我,道長(zhǎng)迄薄,這世上最難降的妖魔是什么琅关? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,264評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮讥蔽,結(jié)果婚禮上涣易,老公的妹妹穿的比我還像新娘。我一直安慰自己冶伞,他們只是感情好新症,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,289評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著碰缔,像睡著了一般账劲。 火紅的嫁衣襯著肌膚如雪戳护。 梳的紋絲不亂的頭發(fā)上金抡,一...
    開(kāi)封第一講書(shū)人閱讀 51,231評(píng)論 1 299
  • 那天瀑焦,我揣著相機(jī)與錄音,去河邊找鬼梗肝。 笑死榛瓮,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的巫击。 我是一名探鬼主播禀晓,決...
    沈念sama閱讀 40,116評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼坝锰!你這毒婦竟也來(lái)了粹懒?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,945評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤顷级,失蹤者是張志新(化名)和其女友劉穎凫乖,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體弓颈,經(jīng)...
    沈念sama閱讀 45,367評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡帽芽,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,581評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了翔冀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片导街。...
    茶點(diǎn)故事閱讀 39,754評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖纤子,靈堂內(nèi)的尸體忽然破棺而出搬瑰,到底是詐尸還是另有隱情,我是刑警寧澤控硼,帶...
    沈念sama閱讀 35,458評(píng)論 5 344
  • 正文 年R本政府宣布跌捆,位于F島的核電站,受9級(jí)特大地震影響象颖,放射性物質(zhì)發(fā)生泄漏佩厚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,068評(píng)論 3 327
  • 文/蒙蒙 一说订、第九天 我趴在偏房一處隱蔽的房頂上張望抄瓦。 院中可真熱鬧,春花似錦陶冷、人聲如沸钙姊。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,692評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)煞额。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間膊毁,已是汗流浹背胀莹。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,842評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留婚温,地道東北人描焰。 一個(gè)月前我還...
    沈念sama閱讀 47,797評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像栅螟,于是被迫代替她去往敵國(guó)和親荆秦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,654評(píng)論 2 354

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