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,8 +18,11 @@ void resetSensor() {
#include <Ethernet3.h> #include <Ethernet3.h>
// Configure MAC address and IP: // Configure MAC address and IP:
byte mac[] = {0x61, 0x2C, 0xF2, 0x09, 0x73, 0xBE}; byte mac[] = { 0x61, 0x2C, 0xF2, 0x09, 0x73, 0xBE };
char T[20]; char T[8];
char H[8];
char message[20];
// the real one IPAddress ip(10, 11, 1, 22); // Static IP // the real one IPAddress ip(10, 11, 1, 22); // Static IP
@ -41,8 +44,9 @@ void setup() {
// SHT31 First readout // SHT31 First readout
Serial.begin(9600); Serial.begin(9600);
while (!Serial); while (!Serial)
Serial.println ("Looking for SHT31"); ;
Serial.println("Looking for SHT31");
if (!sht31.begin(0X44)) { if (!sht31.begin(0X44)) {
Serial.println("Couldn't find SHT31"); Serial.println("Couldn't find SHT31");
@ -53,10 +57,14 @@ void setup() {
float h = sht31.readHumidity(); float h = sht31.readHumidity();
if (!isnan(t)) { if (!isnan(t)) {
Serial.print("Temperature (C)= "); Serial.println(t);} Serial.print("Temperature (C)= ");
Serial.println(t);
}
if (!isnan(h)) { if (!isnan(h)) {
Serial.print("Humidity = "); Serial.println(h); } Serial.print("Humidity = ");
Serial.println(h);
}
// W5500 first readout // W5500 first readout
@ -71,43 +79,69 @@ void setup() {
// Initialize Ethernet: // Initialize Ethernet:
Ethernet.init(W5500_CS_PIN); Ethernet.init(W5500_CS_PIN);
Ethernet.begin(mac, W5500_ip); Ethernet.begin(mac, W5500_ip);
delay (1500); delay(1500);
Serial.print("W5500 IP: "); Serial.print("W5500 IP: ");
Serial.println(Ethernet.localIP()); Serial.println(Ethernet.localIP());
// Send a message through socket // Send a message through socket
Serial.println("Sending test message"); Serial.println("Sending test message");
delay (1000); delay(1000);
if (client.connect (serverIP, port)) { if (client.connect(serverIP, port)) {
client.write("test"); client.write("Connection check");
client.stop(); client.stop();
Serial.println("Test Message sent"); Serial.println("Test Message sent");
} else { } else {
Serial.println("Connection failed");} Serial.println("Connection failed");
}
} }
void loop() { void loop() {
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
// Measure temp. and humidity every 5 seconds // Measure temp. and humidity every 5 seconds
delay (30000); delay(15000);
float t = sht31.readTemperature(); float t = sht31.readTemperature();
float h = sht31.readHumidity(); float h = sht31.readHumidity();
// Send to Eilon's computer // Send to Eilon's computer
if (client.connect(serverIP, port)) {
client.write("Temp");
if (!isnan(h)) { client.stop();
Serial.print("Humidity = "); Serial.println(h); } } else {
Serial.println("Connection failed");
if (client.connect (serverIP, port)) { }
if (client.connect(serverIP, port)) {
if (!isnan(t)) { if (!isnan(t)) {
dtostrf(t, 6, 2, 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.write(T);
client.stop(); } client.stop();
} else { Serial.println("message sent");
Serial.println("Connection failed");} }
} 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: with conn:
data = conn.recv(1024) data = conn.recv(1024)
if data: if data:
print(f"Received from {addr} at {datetime.now()}: {data.decode().strip()}") print(f"Received from {addr} at {datetime.now()}: {data}")
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)

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 port = 5005
# InfluxDB settings # InfluxDB settings
BUCKET = "Data_FerDy" BUCKET = "data"
ORG = "QF" ORG = "QF"
TOKEN = "hm_S3eCnpU72215MwHyDYljHpvw7bxtxtRhEJmUkeBTa1wEJsGv02kZF1jwaMJ2Jeo-gY-57kOQEDTP8bOc0Fg==" TOKEN = "dd_SteCtXS0sSuEA43GpkUOns0mImV76oyT1vz-M11_aiSvMIR1u05kocjIQR8MaZfiqURm-1RockNGkAjLfvA=="
URL = "http://smartlab.physi.uni-heidelberg.de:8086/" URL = "http://smartlab.physi.uni-heidelberg.de:8086/"
@ -99,11 +99,39 @@ if __name__ == "__main__":
influx_client, write_api = init_influxdb(URL, TOKEN, ORG) influx_client, write_api = init_influxdb(URL, TOKEN, ORG)
setup_logger ("PhillipsDeskTemp.") setup_logger ("PhillipsDeskTemp.")
logging.info ("Reading the temperature on Phillip's desk") logging.info ("Reading the temperature on Phillip's desk")
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: while 1:
T = read_temp (ip, port, influx_client, write_api)
if (T is not None and T != "test"): conn, addr = s.accept()
T = float (T) with conn:
p1 = influxdb_client.Point ("Office").tag("Table", "Phillip's").field ("temp_on_desk", T) 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) influx_client, write_api = write_data_to_influxDB (influx_client, write_api, p1)
time.sleep (update_interval) 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