diff --git a/BME280/read.py b/BME280/read.py index 74cde0b..97b56fb 100755 --- a/BME280/read.py +++ b/BME280/read.py @@ -1,5 +1,5 @@ # Script to read data from BME280 -# Sensor connected to i2c on address 77 (use sudo i2cdetect -y 1 to get the address ) +# Sensor connected to i2c on address 76 (use sudo i2cdetect -y 1 to get the address ) # sudo python3 /var/www/nebuleair_pro_4g/BME280/read.py import board diff --git a/README.md b/README.md index 1708914..8fa1a47 100755 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ sudo chmod 777 /dev/ttyAMA* ## I2C -Decibel meter and BME280 is connected via I2C. +Decibel meter, BME280 and the RTC module (DS3231) is connected via I2C. Need to activate by modifying `sudo nano /boot/firmware/config.txt` @@ -82,14 +82,23 @@ sudo chmod 777 /dev/i2c-1 Attention: sometimes activation with config.txt do not work, you need to activate i2c with `sudo raspi-config` and go to "Interface" -> I2C -> enable. +I2C addresses: use `sudo i2cdetect -y 1` to check the connected devices. + ### BME280 The python script is triggered by the main loop every minutes to get instant temp, hum and press values (no need to have a minute average). +BME280 address is 0x76. + +### RTC module (DS3231) + + ### Noise sensor As noise varies a lot, we keep the C program running every seconds to create a moving average for the last 60 seconds (we also gather max and min values). -To keep the script running at boot and stay on we create a systemd service +To keep the script running at boot and stay on we create a systemd service. + +Nois sensor address is 0x48. Create the service with `sudo nano /etc/systemd/system/sound_meter.service` and add: ``` @@ -151,11 +160,10 @@ And set the base URL for Sara R4 communication: ### With only 1 NPM -Loop every minutes to get the PM values and send it to the server: +Loop every minutes to get the PM values and send it to the server (we use flock to be sure the previous script is over before start the new one): ``` -* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1 - +* * * * * flock -n /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.lock /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1 ``` All in one: @@ -164,7 +172,7 @@ All in one: @reboot chmod 777 /dev/ttyAMA* @reboot /var/www/nebuleair_pro_4g/boot_hotspot.sh >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1 @reboot sleep 30 && /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/sara_setURL.py ttyAMA2 data.nebuleair.fr >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1 -* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1 +* * * * * flock -n /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.lock /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1 0 0 */2 * * > /var/www/nebuleair_pro_4g/logs/loop.log ``` diff --git a/RTC/read.py b/RTC/read.py new file mode 100644 index 0000000..8fc5ae1 --- /dev/null +++ b/RTC/read.py @@ -0,0 +1,58 @@ +''' +Script to read time from RTC module +I2C connection +Address 0x68 +/usr/bin/python3 /var/www/nebuleair_pro_4g/RTC/read.py +''' +import smbus2 +import time +import json +from datetime import datetime + + +# DS3231 I2C address +DS3231_ADDR = 0x68 + +# Registers for DS3231 +REG_TIME = 0x00 + +def bcd_to_dec(bcd): + return (bcd // 16 * 10) + (bcd % 16) + +def read_time(bus): + data = bus.read_i2c_block_data(DS3231_ADDR, REG_TIME, 7) + seconds = bcd_to_dec(data[0] & 0x7F) + minutes = bcd_to_dec(data[1]) + hours = bcd_to_dec(data[2] & 0x3F) + day = bcd_to_dec(data[4]) + month = bcd_to_dec(data[5]) + year = bcd_to_dec(data[6]) + 2000 + return (year, month, day, hours, minutes, seconds) + +def main(): + # Read RTC time + bus = smbus2.SMBus(1) + year, month, day, hours, minutes, seconds = read_time(bus) + rtc_time = datetime(year, month, day, hours, minutes, seconds) + + # Get current system time + system_time = datetime.now() #local + utc_time = datetime.utcnow() #UTC + + # Print both times + #print(f"RTC module Time: {rtc_time.strftime('%Y-%m-%d %H:%M:%S')}") + #print(f"Sys local Time: {system_time.strftime('%Y-%m-%d %H:%M:%S')}") + #print(f"Sys UTC Time: {utc_time.strftime('%Y-%m-%d %H:%M:%S')}") + + # Create JSON output + time_data = { + "rtc_module_time": rtc_time.strftime('%Y-%m-%d %H:%M:%S'), + "system_local_time": system_time.strftime('%Y-%m-%d %H:%M:%S'), + "system_utc_time": utc_time.strftime('%Y-%m-%d %H:%M:%S') + } + + print(json.dumps(time_data, indent=4)) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/config.json.dist b/config.json.dist index c49d167..a47b920 100755 --- a/config.json.dist +++ b/config.json.dist @@ -11,6 +11,7 @@ "NextPM_5channels": false, "i2C_sound": false, "i2c_BME": false, + "i2c_RTC": false, "sshTunnel_port": 59228, "npm1_status": "connected", "SARA_R4_general_status": "connected", diff --git a/html/admin.html b/html/admin.html index 715ac02..fbc8b78 100755 --- a/html/admin.html +++ b/html/admin.html @@ -54,7 +54,7 @@