zoukankan      html  css  js  c++  java
  • 【物联网】传感器+wifi传输+回复+显示

    https://www.jianshu.com/p/cb0274d612b5

    https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572896567081&di=850e6d2014877a76396298eb9eef2b2b&imgtype=0&src=http%3A%2F%2Flearning.grobotronics.com%2Fwp-content%2Fuploads%2F2013%2F07%2Furl_uploaded_file_135884635050fe598fa6c4e.gif

    其他内容再补充,太晚啦。

    #include <Arduino.h>
    #include <U8g2lib.h>
    #include "DHT.h"
    #include <ESP8266WiFi.h>
    #include <WiFiClientSecure.h>
    
    
    #ifdef U8X8_HAVE_HW_SPI
    #include <SPI.h>
    #endif
    #ifdef U8X8_HAVE_HW_I2C
    #include <Wire.h>
    #endif
    #define SCL 5
    #define SDA 4
    #define DHTPIN 13
    #define DHTTYPE DHT11   // DHT 11
    
    #ifndef STASSID
    #define STASSID "******"
    #define STAPSK  "******"
    #endif
    
    const char* ssid = STASSID;
    const char* password = STAPSK;
    
    const char* host = "192.168.31.203";
    const int httpsPort = 5001;
    
    U8G2_SSD1306_128X32_UNIVISION_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // Adafruit Feather ESP8266/32u4 Boards + FeatherWing OLED
    //U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
    
    DHT dht(DHTPIN, DHTTYPE);
    
    void setup() {
      // put your setup code here, to run once:
      u8g2.begin();  
      u8g2.enableUTF8Print();
      Serial.begin(115200);
      Serial.println(F("DHTxx test!"));
    
      dht.begin();
      
      Serial.print("connecting to ");
      Serial.println(ssid);
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
    
    uint8_t m = 24;
    
    void loop() {
      
      delay(2000);
    
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      // Read temperature as Celsius (the default)
      float t = dht.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f = dht.readTemperature(true);
    
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println(F("Failed to read from DHT sensor!"));
        return;
      }
    
      // Compute heat index in Fahrenheit (the default)
      float hif = dht.computeHeatIndex(f, h);
      // Compute heat index in Celsius (isFahreheit = false)
      float hic = dht.computeHeatIndex(t, h, false);
    
      Serial.print(F("Humidity: "));
      Serial.print(h);
      Serial.print(F("%  Temperature: "));
      Serial.print(t);
      Serial.print(F("°C "));
      Serial.print(f);
      Serial.print(F("°F  Heat index: "));
      Serial.print(hic);
      Serial.print(F("°C "));
      Serial.print(hif);
      Serial.println(F("°F"));
    
      //u8g2.setFont(u8g2_font_unifont_t_chinese1);  // use chinese2 for all the glyphs of "你好世界"
      u8g2.setFont(u8g2_font_wqy16_t_gb2312b);
      //u8g2.setFont(u8g2_font_6x13_tn);
      u8g2.firstPage();
      do {
      u8g2.setFontDirection(0);
      u8g2.clearBuffer();
      u8g2.setCursor(10, 13);
      u8g2.print("-温度"+(String)t+"C" );
      u8g2.setCursor(10, 30);
      u8g2.print("-湿度"+(String)h+"%");    // Chinese "Hello World" 
      u8g2.sendBuffer();
    
      //u8g2.drawStr(0,15,"Hello World!");
      }while(u8g2.nextPage());
      delay(1000);
    
      WiFiClient client;
      Serial.print("connecting to ");
      Serial.println(host);
    
      if (!client.connect(host, httpsPort)) {
        Serial.println("connection failed");
        return;
      }
    
      // Wait a few seconds between measurements.
      delay(1000);
    
    
      
     
      String url = "/";
      Serial.print("requesting URL: ");
      Serial.println(url);
    
      // 组拼HTTPS请求的Header
      String jsonStr = String("") + 
                        "{"Humidity":"+h+","+
                        ""Temperature":"+t+"}";
      /*
      "{'datastreams':[{" +
                "'id':'" + dataId + "'" +
                ",'datapoints':[{" +
                "'value':" + data +
                "}]}]}";
                */
      Serial.println(jsonStr);
      String getStr = String("POST ") + url + " HTTP/1.1
    " +
                   "Host: " + host + "
    " +
                   "User-Agent: arduino
    " +
                   "Connection: close
    ";
    
      client.print(getStr);   // 发送Headers头
      client.print(String("") + "Content-Length:" + jsonStr.length() + "
    
    "); // 发送Header头-数据内容长度(注意:
    
    是结尾)
      client.print(jsonStr);  // 发送json数据
    
      Serial.println("request sent");
    
      Serial.println("==========");
      Serial.println("send was:");
      Serial.println(jsonStr);   // 打印发送的请求数据
      Serial.println("reply was:");
      String line = "";
      while (client.connected() || client.available())
    {
      if (client.available())
      {
        line = client.readStringUntil('
    ');
        
      }
    }
      Serial.println(line);
      Serial.println("==========");
      
    }
    

      

  • 相关阅读:
    etcd
    mesos+marathon+zookeeper+docker
    安装好dashboard 登录出现错误
    最小化centos7离线安装docker环境
    centons7安装ftp
    TensorFlow运行模型demo时常见问题
    centos7全新系统安装TensorFlow
    vmware创建虚拟机并安装centos7系统
    python使用moviepy模块对视频进行操作
    iis7/8隐藏banner信息
  • 原文地址:https://www.cnblogs.com/colipso/p/11795977.html
Copyright © 2011-2022 走看看