物联网传感器+wifi传输+回复+显示

Posted colipso

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了物联网传感器+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 "HJhome"
#define STAPSK  "hphj29++"
#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("==========");
  
}

  

 

以上是关于物联网传感器+wifi传输+回复+显示的主要内容,如果未能解决你的问题,请参考以下文章

亿佰特WiFi无线通信模块在物联网智慧农场应用案例

基于lora及wifi无线通信的无线PLC在物联网远程IO控制场合中的应用

基于wifi无线PLC远程控制实现io开关量信号远程采集传输技术

实战物联网安防监控项目———把模拟数据传输到web网页web显示mjpeg-streamer视频图像

物联网数据采集

制作一个有趣的涂鸦物联网小项目(涂鸦模组SDK开发 CBU BK7231N WiFi+蓝牙模组 HSV彩色控制)