#include <WiFi.h>
#include <DHT.h>
#include <PubSubClient.h>
#include <Servo.h>
#include<SoftwareSerial.h>

// WiFi信息
const char* ssid = "HONOR Play4T";
const char* password = "12345678";

// OneNet信息
const char* server = "mqtt.heclouds.com";
const int port = 6002;
const char* deviceId = "1099655073";
const char* apiKey = "L=ljQ23ELQoOmTvhDGkFKnMZ77E=";
const char* topicPub = "/topicPub";
const char* topicSub = "/topicSub";

// 温湿度传感器信息
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float tempC;
float humid;

// 伺服舵机信息
Servo myservo;
int servoPos = 0;

//esp8266
SoftwareSerial myserial(3, 2);
WiFi wifi(myserial);

// MQTT客户端
// 使用 ESP8266HTTPClient 代替 WiFiClient
HTTPClient client;

// 连接WiFi和MQTT
void setup_wifi_mqtt() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  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());
}

// 湿温度传感器初始化
void setup_sensors() {
  dht.begin();
}

// 获取湿温度
void get_sensors() {
  tempC = dht.readTemperature();
  humid = dht.readHumidity();
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.println(" *C");
  Serial.print("Humidity: ");
  Serial.print(humid);
  Serial.println(" %");
}

// 连接OneNet并上传数据
void connect_onenet() {
  // 使用 HTTPClient 发送数据到 OneNet
  client.begin(server);
  client.addHeader("Content-Type", "application/json");
  String payload = "{\"datastreams\":[\{"id\":\"temperature\",\"datapoints\":[\{"value\":\";
  payload += String(tempC);
  payload += "\"}]},\{"id\":\"humidity\",\"datapoints\":[\{"value\":\";
  payload += String(humid);
  payload += "\"}]}]}";
  int httpCode = client.POST(payload);
  if (httpCode > 0) {
    Serial.print("HTTP Code: ");
    Serial.println(httpCode);
    Serial.println(client.getString());
  } else {
    Serial.println("Error on HTTP request");
  }
  client.end();
}

// 订阅命令并控制舵机
void subscribe_control() {
  // 使用 HTTPClient 接收命令并控制舵机
  client.begin(server);
  client.addHeader("Content-Type", "application/json");
  int httpCode = client.GET("/topicSub");
  if (httpCode > 0) {
    Serial.print("HTTP Code: ");
    Serial.println(httpCode);
    String response = client.getString();
    Serial.println(response);
    if (response == "0") {
      servoPos = 0;
    } else if (response == "1") {
      servoPos = 180;
    } else {
      servoPos = 0;
    }
    myservo.write(servoPos);
  } else {
    Serial.println("Error on HTTP request");
  }
  client.end();
}

void setup() {
  Serial.begin(9600);
  setup_wifi_mqtt();
  setup_sensors();
  myservo.attach(7);
}

void loop() {
  get_sensors();
  connect_onenet();
  subscribe_control();
  delay(1000);
}

注意:

  1. 以上代码仅供参考,您需要根据您的具体需求进行修改。
  2. 使用 ESP8266HTTPClient 需要您自行安装相应的库文件,可以通过 Arduino IDE 的库管理器进行安装。
  3. 确保您已配置好 OneNet 平台的账号和设备信息,以及设备的网络连接。
  4. 在使用 HTTPClient 进行数据传输时,请注意 OneNet API 的具体格式和参数要求。

如果您需要更详细的帮助,请提供以下信息:

  1. 您使用的 ESP8266 模块型号
  2. 您使用的 OneNet 设备类型
  3. 您希望使用 OneNet API 的具体功能

希望以上信息对您有所帮助。

ESP8266 温湿度传感器数据上传到 OneNet 并控制舵机 (无需 WiFiClient)

原文地址: https://www.cveoy.top/t/topic/oLUJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录