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;
}
I tried this and I get a failed connection error and I don't want to use a router.
I solved the problem using ESP32. Thanks everyone.