以下是一个使用ESP8266模块连接WiFi、上传湿温度至onenet,并获取命令以控制舵机的示例代码。该代码不使用DallasTemperature库。

#include <SoftwareSerial.h>

SoftwareSerial espSerial(3, 2); // RX, TX
String ssid = "your_wifi_ssid"; // WiFi SSID
String password = "your_wifi_password"; // WiFi Password
String apiKey = "your_onenet_api_key"; // OneNet API Key
String deviceId = "your_onenet_device_id"; // OneNet Device ID
String temperatureVar = "temperature"; // OneNet Temperature Variable Name
String humidityVar = "humidity"; // OneNet Humidity Variable Name
String commandVar = "command"; // OneNet Command Variable Name
int servoPin = 9; // Servo Control Pin
int servoAngle = 0; // Servo Angle

void setup() {
  // Start Serial Communication
  Serial.begin(9600);
  espSerial.begin(9600);
  pinMode(servoPin, OUTPUT);
  // Connect to WiFi
  connectWiFi();
}

void loop() {
  // Read Temperature & Humidity
  float temperature = readTemperature();
  float humidity = readHumidity();
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C");
  Serial.print("\tHumidity: ");
  Serial.print(humidity);
  Serial.print(" %");
  // Upload Data to OneNet
  uploadData(temperature, humidity);
  // Check for Commands from OneNet
  String command = getCommand();
  if (command != "") {
    Serial.print("\tCommand: ");
    Serial.print(command);
    // Parse Command & Control Servo
    servoAngle = parseCommand(command);
    controlServo(servoAngle);
  }
  Serial.println();
  delay(5000); // Wait for 5 seconds
}

void connectWiFi() {
  Serial.println("Connecting to WiFi...");
  espSerial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
  while (!espSerial.find("OK")) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println();
  Serial.println("WiFi Connected!");
}

float readTemperature() {
  // Code to read Temperature Sensor
}

float readHumidity() {
  // Code to read Humidity Sensor
}

void uploadData(float temperature, float humidity) {
  String data = "{\"" + temperatureVar + "\":" + String(temperature) + ",\"" + humidityVar + "\":" + String(humidity) + "}";
  String length = String(data.length());
  espSerial.println("AT+CIPSTART=\"TCP\",\"api.heclouds.com\",80");
  while (!espSerial.find("OK")) {
    Serial.print(".");
    delay(1000);
  }
  espSerial.println("AT+CIPSEND=" + length);
  while (!espSerial.find(">")) {
    Serial.print(".");
    delay(1000);
  }
  espSerial.println(data);
  while (!espSerial.find("SEND OK")) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println();
  Serial.println("Data Uploaded!");
}

String getCommand() {
  String command = "";
  espSerial.println("AT+CIPSTART=\"TCP\",\"api.heclouds.com\",80");
  while (!espSerial.find("OK")) {
    Serial.print(".");
    delay(1000);
  }
  espSerial.println("AT+CIPSEND=56");
  while (!espSerial.find(">")) {
    Serial.print(".");
    delay(1000);
  }
  espSerial.println("GET /devices/" + deviceId + "/datapoints?datastream_id=" + commandVar + "&limit=1 HTTP/1.1\r\nHost: api.heclouds.com\r\napi-key: " + apiKey + "\r\n\r\n");
  while (!espSerial.find("+IPD")) {
    Serial.print(".");
    delay(1000);
  }
  String response = espSerial.readStringUntil('\r');
  if (response.indexOf("200 OK") != -1) {
    String data = espSerial.readStringUntil('\r');
    int index = data.indexOf("value");
    if (index != -1) {
      command = data.substring(index + 8, index + 9);
    }
  }
  Serial.println();
  Serial.println("Command: " + command);
  return command;
}

int parseCommand(String command) {
  int angle = 0;
  if (command == "0") {
    angle = 0;
  } else if (command == "1") {
    angle = 45;
  } else if (command == "2") {
    angle = 90;
  } else if (command == "3") {
    angle = 135;
  } else if (command == "4") {
    angle = 180;
  }
  return angle;
}

void controlServo(int angle) {
  int pulseWidth = map(angle, 0, 180, 500, 2500);
  digitalWrite(servoPin, HIGH);
  delayMicroseconds(pulseWidth);
  digitalWrite(servoPin, LOW);
  delay(20 - pulseWidth / 1000);
}
``
Arduino要实现连接WiFi上传湿温度至onenet并获取命令以控制舵机给出示例代码不用DallasTemperature库

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

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