第一篇真正的文章:nodeMCU esp8266自学笔记

法克啊,本来我的底线是要自己建网站的。。。各种原因没有成功只好在简书上写了。。。

这篇文章是宗旨是怎么用arduino IDE来给ESP8266写程序。ESP8266是我最近发现的很黑科技的一个东西。首先,他继承了WIFI功能,本身也有GPIO之类的,这些以前的模块都能做到到是也没有什么了,但是价格非常之便宜,淘宝只需要25块钱,比普通的Arduino还要便宜一些。而最diao的是他只要安装一个插件,就可以直接用Arduino IDE写程序,就像是给Arduino写程序一样。很多Arduino的库也可以直接使用。

首先,几乎所有的部分都是来自于 Adafruit的网站

恩,然后需要把Arduino升级到1.6.4以上。

这里开始可能电脑要翻墙,但是如果的电信的网络好像也可以不翻


选择这里preferences偏好设置

在那个文本框输入http://arduino.esp8266.com/stable/package_esp8266com_index.json

选择boards manager

搜索esp8266,然后点击安装

之后对话框下面会有一个进度调,要下载大约6m的文件,但是速度很慢。

之后就可以把我的代码拷进来,整体界面和processing很像


点击工具,然后按照我的选择,端口选择最下面的一个

然后就可以像给Arduino写程序一样写了。

烧写

这时候要拔掉控制板的220v插头。
烧写程序的时候要先把控制板查到usb口上,然后在插口旁边有两个按钮,先按住标有flash的按钮,然后按住标有reset的按钮,板子上的蓝灯会闪烁一下,然后点击arduino上的upload或者下载按钮。

UPDATE:
做了一个闪电云,然后拿回来以后不知道为什么就不能用了,直接接引脚是有用的,用usb供电就不行,用万用表量了一下VCC有一个限流电阻好像是断了。用杜邦线短接以后就行了。

然后不知道为什么现在烧程序不用想以前那样按按钮了。。。

然后这里是天气云(只能实现一个天气类型的展示)的代码

    /*
     *  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);
      }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容