Connecting Arduino UNO directly to my laptop using ethernet shield and performing POST request

Hello. I am making a small greenhouse prototype in my one semester university project. In this project, I want to collect sensor data with Arduino UNO and send it to my server running on localhost:8080 on my own computer. I have tested the data collection stages on my server and arduino and everything works as I want. Everything is fine so far, but I want to establish a one-way communication between UNO and my local server. I have designed this communication with EthernetShield (W5100 core) using only an Ethernet cable in between and this cable is connected directly to the computer (via Type-C hub). But I am missing this communication part. I am leaving the code running on Arduino UNO below and I am waiting for your help. Thank you very much for your answers in advance.

//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;
}

Have you configured the ethernet port of the laptop?

"localhost" is only on your laptop and not accessible from the outside world.

"server" means that the laptop is the server. Hence the Arduino needs to be the client; your Arduino is currently a server as shown below. It also shows that the Arduino's address is "localhost".

And that is probably as much as I can help with regarding your problem.

I haven't done any configuration on my computer, in fact I came here to get help because I can't find an example or a related document similar to what I want to do.

I don't know anything about the EthernetShield library, this is the first time I have used it. I will read the library documentation and learn the concepts first thing, thank you for your warning.

No, it doesn't look like it. As far as I can tell, the posted sketch is an incomplete ethernet setup.

I'd recommend @canecerin to study the several examples of Arduino Ethernet sketches; e.g. this one: Ethernet/examples/WebClient/WebClient.ino at master · arduino-libraries/Ethernet · GitHub
Note how they set up the Ethernet stack first, and then proceed connecting the client object to the server.

127.0.0.1 is the address of localhost. It's not possible to connect to a server with this address; at best, it'll make a loopback connection to the Arduino itself, but either way, it's not going to work.

The network configuration for a direct connection between laptop and Arduino needs to be:

  • Hardware: either a crosslink ethernet cable or (common these days) auto-detect network adapter on either side that automatically crosses the connections. Alternatively, connect the Arduino and the laptop together with a hub or router.
  • Windows configuration: configure the network adapter to use manual IP address, e.g. with the address 192.168.178.1 subnet mask 255.255.255.0 and leave the 'router/gateway' field empty. On the Arduino, use e.g. network address 192.168.178.2 and the subnet needs to be the same as on the Windows machine.
  • In the Arduino sketch, set the server address to 192.168.178.1.
    Note that when choosing device IP addresses, they need to be unique on the network (no conflicting addresses) and when using mask 255.255.255.0 the first three sections of the IP addresses need to be the same on the machines that need to talk to each other. If you use the addresses I gave as examples above, it should work.

Remember to reconfigure your Windows ethernet adapter back to automatic IP when/if you connect it to a regular wired connection (e.g. your home router).

Do a search on the web; e.g. configure wired ethernet YourOperatingSystem.

I solved the problem using ESP32. Thanks everyone.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.