ESP8266开发板连接onenet—HTTP

ESP8266开发板连接onenet—HTTP

    • 前言
    • 程序
    • 实验现象

前言

大家好,我是往事匆匆,第一次发博客 如果有不好的地方请批评指正 共同学习,今天给大家分享一点关于esp8266连接onenet的程序代码。用的是Arduino 软件编写的程序,程序实现的功能是上传温度和湿度。
元件和工具:

  1. esp8266 NodeMCU
  2. OLED12864
  3. DHT22
  4. 杜邦线
  5. Arduino IDE

程序

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <U8g2lib.h>
#include "DHT.h"

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ D3, /* data=*/ D1, /* reset=*/ U8X8_PIN_NONE);

#define DHTPIN D2     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
const char *ssid     = "Tenda_71A6B8";//这里写入网络的ssid
const char *password = "123456789";//wifi密码
const char *host = "183.230.40.33";//修改为Server服务端的IP,即你电脑的IP,确保在同一网络之下。

WiFiClient client;
const int tcpPort = 80;//修改为你建立的Server服务端的端口号,此端口号是创建服务器时指定的。
uint8_t LED = 0;
// char temp[5]={0};
void setup()
{
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");//会通过usb转tll模块发送到电脑,通过ide集成的串口监视器可以获取数据。
  Serial.println(ssid);

  WiFi.begin(ssid, password);//启动

  //在这里检测是否成功连接到目标网络,未连接则阻塞。
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }

  //几句提示
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  dht.begin(); //DHT22
  u8g2.begin();
  u8g2.enableUTF8Print();   // enable UTF8 support for the Arduino print() function
  u8g2.setFont(u8g2_font_wqy16_t_gb2312a); // choose a suitable font  (设置字体)
  u8g2.setFontDirection(0);
  u8g2.clearBuffer();

  // 设置睡眠模式(可选参数:WIFI_NONE_SLEEP、WIFI_LIGHT_SLEEP、WIFI_MODEM_SLEEP )
  WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
  delay(2000);
}


void loop()
{
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
   uint16_t  h = (uint16_t )dht.readHumidity();
   // Read temperature as Celsius (the default)
   uint16_t  t = (uint16_t )dht.readTemperature();

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_wqy12_t_gb2312a);
  u8g2.setCursor(50, 15);
  //   itoa(t,temp,5);
  u8g2.print(t);
  u8g2.drawStr(70, 15, "C");
  u8g2.setCursor(50, 40);
  //   itoa(h,temp,5);
  u8g2.print(h);
  u8g2.drawStr(70, 40, "%");
  u8g2.setCursor(0, 15);
  u8g2.print("温度");
  u8g2.drawStr(33, 15, ":");
  u8g2.setCursor(0, 40);
  u8g2.print("湿度");
  u8g2.drawStr(33, 40, ":");
  u8g2.sendBuffer();
  while (!client.connected())//若未连接到服务端,则客户端进行连接。
  {
    if (!client.connect(host, tcpPort))//实际上这一步就在连接服务端,如果连接上,该函数返回true
    {
      Serial.println("connection....");
      delay(500);
    }
  }
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);                      // Wait for a second
  digitalWrite(LED_BUILTIN, HIGH);

  if (client.connected()) {              //若连接成功,就发送POST 请求上报数据
    String url = "http://api.heclouds.com/devices/591386183/datapoints?type=3";  //参数type=3表示用 简洁数据格式,591386183是你在移动OneNET上创建的设备ID 不是产品的ID!!!! 一定要记住
    client.print(String("POST ") + url + " HTTP/1.1\r\n");                                             //使用HTTP/1.1协议
    client.print(String("api-key: ") + "=fPRMs5D7DlAOLMMqE5U=32WaJA=\r\n");           //一定要改为设备的API key,不是产品的API key!!!! 一定要记住
    client.print(String("Host: ") + "api.heclouds.com\r\n");
    client.print("Connection: close\r\n");

    String data = "{'SoilHUM':" + String(h) + ",'TEM':" +  String(t) + ",'LED':" +  String(LED) + "}";
    //      Serial.write(data);
    Serial.write("data:");
    Serial.print(data);
    Serial.print(String(data.length()));
    Serial.print("Content-Length:" + String(data.length()) + "\r\n\r\n");
    client.print("Content-Length:" + String(data.length()) + "\r\n\r\n");
    client.print(data);
  }
  client.stop();                     //关闭HTTP客户端
  delay(40000); 
  while (client.available())//available()表示是否可以获取到数据
  {
    uint8_t c = client.read();
    Serial.write(c);
  }
}

实验现象

ESP8266开发板连接onenet—HTTP_第1张图片
ESP8266开发板连接onenet—HTTP_第2张图片

你可能感兴趣的:(esp8266)