使用以太网盾将 Arduino UNO 直接连接到我的笔记本电脑并执行 POST 请求

问题描述 投票:0回答:1

我正在我一个学期的大学项目中制作一个小型温室原型。在这个项目中,我想使用 Arduino UNO 收集传感器数据并将其发送到我自己计算机上 localhost:8080 上运行的服务器。我已经在我的服务器和arduino上测试了数据收集阶段,一切都按我想要的方式工作。到目前为止一切都很好,但我想在 UNO 和我的本地服务器之间建立一种单向通信。我设计了这种与EthernetShield(W5100核心)的通信,仅使用中间的以太网电缆,并且该电缆直接连接到计算机(通过Type-C集线器)。但我缺少这个沟通部分。我将在下面的 Arduino UNO 上运行代码,等待您的帮助。非常感谢您提前的答复。

//import external libraries
#include "DHT.h"
#include "ArduinoJson.h"
#include <SPI.h>
#include <Ethernet.h>

//define pins
#define DHTPIN 2
#define DHTTYPE DHT11
#define LDRPIN A0
#define SOIL_MOISTUREPIN A1
#define WATER_LEVELPIN A2
#define RELAY1_PIN 8

//define variables
const unsigned long routineHttpSendInterval = 10000;
unsigned long previousTime = 0;
const float temperatureThresholdValue = 20.0;
const float lightAmountThresholdValue = 35.0;


//define dht
DHT dht(DHTPIN, DHTTYPE);

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(127, 0, 0, 1);
EthernetClient client;

void setup() {
  // put your setup code here, to run once:
  Ethernet.begin(mac);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Serial.begin(9600);
  dht.begin();
  pinMode(RELAY1_PIN, OUTPUT);
  digitalWrite(RELAY1_PIN, OUTPUT);
}

void loop() {
  //get current time milis
  unsigned long currentTime = millis();
  //get sensor datas
  float humidity = readHumidity();
  float celcius = readTemperatureCelcius();
  float fahrenheit = readTemperatureFahrenheit();
  float lightLevel = readLightLevel();
  float soilMoisture = readSoilMoisture();
  float waterLevel = readWaterLevel();
  //create post json
  StaticJsonDocument<200> jsonDocument;
  jsonDocument["humidity"] = humidity;
  jsonDocument["temperatureAsC"] = celcius;
  jsonDocument["temperatureAsF"] = fahrenheit;
  jsonDocument["lightAmount"] = lightLevel;
  jsonDocument["soilMoisture"] = soilMoisture;
  jsonDocument["waterLevel"] = waterLevel;
  //routine data sender
  if (currentTime - previousTime >= routineHttpSendInterval) {
    /*
    * send data with opcode 0
    */
    jsonDocument["operationType"] = 0;
    char jsonBuffer[512];
    serializeJson(jsonDocument, jsonBuffer);
    if (client.connect(server, 8080)) {  // localhost ve 8080 portuna bağlanma
      client.println("POST /greenhouse/save HTTP/1.1");
      client.println("Host: 127.0.0.1:8080/api");  // localhost ve 8080 portu
      client.println("Content-Type: application/json");
      client.print("Content-Length: ");
      client.println(strlen(jsonBuffer));
      client.println();
      client.println(jsonBuffer);
    } else {
      Serial.println("Bağlantı başarısız!");
    }
    previousTime = currentTime;
  }
  //if the temperature is lower than the threshold, start the heat engine and send data
  if (readTemperatureCelcius() <= temperatureThresholdValue) {
    /*
      send data with opcode 1 and run heat engine
    */
  }
  //If the amount of light is low, turn on the light and send the data
  if (readLightLevel() <= lightAmountThresholdValue) {
    /*
      send data with opcode 2 and open lights
    */
  }

  /*
  readHumidity();
  readTemperatureCelcius();
  readTemperatureFahrenheit();

  Serial.print("Light level: ");
  Serial.println(map(analogRead(LDRPIN), 0, 1023, 0, 100));

  Serial.print("Soil Moisture: ");
  Serial.println(map(analogRead(SOIL_MOISTUREPIN), 0, 1023, 100, 0));

  Serial.print("Water level: ");
  Serial.println(map(analogRead(WATER_LEVELPIN), 0, 1023, 0, 100));

  Serial.println();
*/
  delay(5000);
}
/*

*/
float readHumidity() {
  float humidity = dht.readHumidity();
  if (!isnan(humidity)) {
    Serial.print("Humidity %");
    Serial.println(humidity);
    return humidity;
  } else {
    Serial.println("Failed to read humidity from DHT sensor!");
    return NAN;
  }
}

float readTemperatureCelcius() {
  float celcius = dht.readTemperature();
  if (!isnan(celcius)) {
    Serial.print("Celcius °C");
    Serial.println(celcius);
    return celcius;
  } else {
    Serial.println("Failed to read temperature as celcius from DHT sensor!");
    return NAN;
  }
}

float readTemperatureFahrenheit() {
  float celcius = dht.readTemperature(true);
  if (!isnan(celcius)) {
    // Fahrenheit cinsinden sıcaklık değerini Serial'e yazdırın
    float fahrenheit = (celcius * 9.0 / 5.0) + 32.0;
    Serial.print("Fahrenheit °F");
    Serial.println(fahrenheit);
    return fahrenheit;
  } else {
    Serial.println("Failed to read temperature as fahrenheit from DHT sensor!");
    return NAN;
  }
}

float readLightLevel() {
  float lightLevel = (analogRead(LDRPIN) / 1023.0) * 100;
  Serial.print("Light level: ");
  Serial.println(lightLevel);
  return lightLevel;
}

float readSoilMoisture() {
  float soilMoisture = (analogRead(SOIL_MOISTUREPIN) / 1023.0) * 100;
  Serial.print("Soil Moisture: ");
  Serial.println(soilMoisture);
  return soilMoisture;
}

float readWaterLevel() {
  float waterLevel = (analogRead(WATER_LEVELPIN) / 1023.0) * 100;
  Serial.print("Water level: ");
  Serial.println(waterLevel);
  return waterLevel;
}

我尝试了此操作,但出现连接失败错误,并且我不想使用路由器。

arduino ethernet
1个回答
0
投票

我使用 ESP32 解决了这个问题。谢谢大家。

© www.soinside.com 2019 - 2024. All rights reserved.