Reading temp. and humidity simultaneously

This commit is contained in:
Eilon Zohar 2025-07-01 17:25:02 +02:00
parent 513d4a0188
commit db993cae11
5 changed files with 133 additions and 47 deletions

View File

@ -18,17 +18,20 @@ void resetSensor() {
#include <Ethernet3.h>
// Configure MAC address and IP:
byte mac[] = {0x61, 0x2C, 0xF2, 0x09, 0x73, 0xBE};
char T[20];
byte mac[] = { 0x61, 0x2C, 0xF2, 0x09, 0x73, 0xBE };
char T[8];
char H[8];
char message[20];
// the real one IPAddress ip(10, 11, 1, 22); // Static IP
// Define CS and RST pins:
#define W5500_CS_PIN 10 // 8 in E LEGO
#define W5500_RST_PIN 9 //10 in E LEGO
#define W5500_CS_PIN 10 // 8 in E LEGO
#define W5500_RST_PIN 9 //10 in E LEGO
IPAddress serverIP(10, 44, 1, 238); // Computer
IPAddress W5500_ip(10, 44, 1, 22); // Change the last digit
IPAddress W5500_ip(10, 44, 1, 22); // Change the last digit
// Don't change
const int port = 5005;
@ -41,23 +44,28 @@ void setup() {
// SHT31 First readout
Serial.begin(9600);
while (!Serial);
Serial.println ("Looking for SHT31");
while (!Serial)
;
Serial.println("Looking for SHT31");
if (!sht31.begin(0X44)) {
if (!sht31.begin(0X44)) {
Serial.println("Couldn't find SHT31");
while (1) delay(100);
}
Serial.println("SHT31 Found");
Serial.println("SHT31 Found");
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (!isnan(t)) {
Serial.print("Temperature (C)= "); Serial.println(t);}
Serial.print("Temperature (C)= ");
Serial.println(t);
}
if (!isnan(h)) {
Serial.print("Humidity = "); Serial.println(h); }
Serial.print("Humidity = ");
Serial.println(h);
}
// W5500 first readout
pinMode(W5500_RST_PIN, OUTPUT);
@ -71,43 +79,69 @@ void setup() {
// Initialize Ethernet:
Ethernet.init(W5500_CS_PIN);
Ethernet.begin(mac, W5500_ip);
delay (1500);
delay(1500);
Serial.print("W5500 IP: ");
Serial.println(Ethernet.localIP());
// Send a message through socket
Serial.println("Sending test message");
delay (1000);
if (client.connect (serverIP, port)) {
client.write("test");
delay(1000);
if (client.connect(serverIP, port)) {
client.write("Connection check");
client.stop();
Serial.println("Test Message sent");
} else {
Serial.println("Connection failed");}
Serial.println("Connection failed");
}
}
void loop() {
// put your main code here, to run repeatedly:
// Measure temp. and humidity every 5 seconds
delay (30000);
delay(15000);
float t = sht31.readTemperature();
float h = sht31.readHumidity();
// Send to Eilon's computer
if (!isnan(h)) {
Serial.print("Humidity = "); Serial.println(h); }
if (client.connect (serverIP, port)) {
if (!isnan(t)) {
dtostrf(t, 6, 2, T);
client.write(T);
client.stop(); }
if (client.connect(serverIP, port)) {
client.write("Temp");
client.stop();
} else {
Serial.println("Connection failed");}
Serial.println("Connection failed");
}
if (client.connect(serverIP, port)) {
if (!isnan(t)) {
dtostrf(t, 6, 2, T);
snprintf(message, sizeof(message), "T %s", T);
Serial.println(message);
//client.write((const uint8_t*)message, strlen(message)); // Send to server
client.write(T);
client.stop();
Serial.println("message sent");
}
} else {
Serial.println("Connection failed");
}
delay(15000);
if (client.connect(serverIP, port)) {
client.write("Humidity");
client.stop();
} else {
Serial.println("Connection failed");
}
if (client.connect(serverIP, port)) {
if (!isnan(h)) {
dtostrf(h, 6, 2, H);
snprintf(message, sizeof(message), "H %s", H);
Serial.println(message);
client.write(H);
//client.write((const uint8_t*)message, strlen(message)); // Send to server
client.stop();
Serial.println("message sent");
}
} else {
Serial.println("Connection failed");
}
}

View File

@ -20,9 +20,5 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
with conn:
data = conn.recv(1024)
if data:
print(f"Received from {addr} at {datetime.now()}: {data.decode().strip()}")
row = pd.DataFrame ({"Date": [str (datetime.now()) [:10]], "Time": [str (datetime.now()) [11:19]], "Temperature": [data.decode().strip()]})
Data_to_influx = pd.concat ([Data_to_influx, row], ignore_index= 1)
csv_file_name = "C:/Users/Iluz1/Desktop/Data/a.csv"
Data_to_influx.to_csv (csv_file_name)
print(f"Received from {addr} at {datetime.now()}: {data}")

View File

@ -0,0 +1,4 @@
2025-07-01 12:07:23,564 - INFO - Logger set up complete
2025-07-01 12:07:23,564 - INFO - Reading the temperature on Phillip's desk
2025-07-01 12:07:28,770 - INFO - Logger set up complete
2025-07-01 12:07:28,770 - INFO - Reading the temperature on Phillip's desk

View File

@ -21,9 +21,9 @@ ip = "0.0.0.0"
port = 5005
# InfluxDB settings
BUCKET = "Data_FerDy"
BUCKET = "data"
ORG = "QF"
TOKEN = "hm_S3eCnpU72215MwHyDYljHpvw7bxtxtRhEJmUkeBTa1wEJsGv02kZF1jwaMJ2Jeo-gY-57kOQEDTP8bOc0Fg=="
TOKEN = "dd_SteCtXS0sSuEA43GpkUOns0mImV76oyT1vz-M11_aiSvMIR1u05kocjIQR8MaZfiqURm-1RockNGkAjLfvA=="
URL = "http://smartlab.physi.uni-heidelberg.de:8086/"
@ -99,11 +99,39 @@ if __name__ == "__main__":
influx_client, write_api = init_influxdb(URL, TOKEN, ORG)
setup_logger ("PhillipsDeskTemp.")
logging.info ("Reading the temperature on Phillip's desk")
while 1:
T = read_temp (ip, port, influx_client, write_api)
if (T is not None and T != "test"):
T = float (T)
p1 = influxdb_client.Point ("Office").tag("Table", "Phillip's").field ("temp_on_desk", T)
influx_client, write_api = write_data_to_influxDB (influx_client, write_api, p1)
time.sleep (update_interval)
with socket.socket (socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind ((ip, port))
s.listen()
# print ("Listening")
temp_or_hum = "a"
while 1:
conn, addr = s.accept()
with conn:
data = conn.recv (1024)
message = data.decode().strip()
#T = read_temp (ip, port, influx_client, write_api)
if (message is not None and temp_or_hum == "T"):
print (str (datetime.now () )[:19] + ": Temp is " + message)
T = float (message)
p1 = influxdb_client.Point ("FerDy").tag("Table", "Philipp's").field ("temp_on_desk", T)
temp_or_hum = "a"
influx_client, write_api = write_data_to_influxDB (influx_client, write_api, p1)
time.sleep (update_interval/2)
elif (message is not None and temp_or_hum == "H"):
print (str (datetime.now () )[:19] + ": humidity is " + message)
H = float (message)
p1 = influxdb_client.Point ("FerDy").tag("Table", "Philipp's").field ("hum_on_desk", H)
temp_or_hum = "a"
influx_client, write_api = write_data_to_influxDB (influx_client, write_api, p1)
time.sleep (update_interval/2)
elif (message is not None):
temp_or_hum = message[0]

View File

@ -0,0 +1,24 @@
2025-07-01 12:04:21,940 - INFO - Logger set up complete
2025-07-01 12:04:21,940 - INFO - Reading the temperature on Phillip's desk
2025-07-01 12:05:54,673 - INFO - Logger set up complete
2025-07-01 12:05:54,673 - INFO - Reading the temperature on Phillip's desk
2025-07-01 12:09:29,668 - INFO - Logger set up complete
2025-07-01 12:09:29,668 - INFO - Reading the temperature on Phillip's desk
2025-07-01 16:42:01,247 - INFO - Logger set up complete
2025-07-01 16:42:01,247 - INFO - Reading the temperature on Phillip's desk
2025-07-01 16:45:29,238 - INFO - Logger set up complete
2025-07-01 16:45:29,239 - INFO - Reading the temperature on Phillip's desk
2025-07-01 16:46:28,621 - INFO - Logger set up complete
2025-07-01 16:46:28,621 - INFO - Reading the temperature on Phillip's desk
2025-07-01 16:49:50,243 - INFO - Logger set up complete
2025-07-01 16:49:50,243 - INFO - Reading the temperature on Phillip's desk
2025-07-01 16:50:28,107 - INFO - Logger set up complete
2025-07-01 16:50:28,108 - INFO - Reading the temperature on Phillip's desk
2025-07-01 16:50:53,880 - INFO - Logger set up complete
2025-07-01 16:50:53,881 - INFO - Reading the temperature on Phillip's desk
2025-07-01 17:20:51,260 - INFO - Logger set up complete
2025-07-01 17:20:51,260 - INFO - Reading the temperature on Phillip's desk
2025-07-01 17:21:00,372 - INFO - Logger set up complete
2025-07-01 17:21:00,372 - INFO - Reading the temperature on Phillip's desk
2025-07-01 17:21:36,585 - INFO - Logger set up complete
2025-07-01 17:21:36,585 - INFO - Reading the temperature on Phillip's desk