Skip to content

Commit

Permalink
getting data from arduino. Write to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Korzhak committed Apr 22, 2019
1 parent bdf0779 commit 54faa11
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
venv
.venv
__pycache__
db.sqlite3
.idea
Empty file removed arduino/cron.py
Empty file.
21 changes: 14 additions & 7 deletions arduino/serial_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from ts.models import LogThermostat

pattern = "temp=([0-9.]+); co2=([0-9.]+); hum=([0-9.]+); on=([01]); current_state=([0-9])"
pattern = r"([\d.]+)"


def get_data_from_box():
Expand All @@ -28,13 +28,20 @@ def get_data_from_box():
while amount_received < amount_expected:
data = sock.recv(1024)
amount_received += len(data)

res = re.match(pattern, data.decode())
print(data.decode())
res = re.findall(pattern, data.decode())
print(res[2])
obj = LogThermostat(
temp=float(res.group(1)),
co2=float(res.group(2)),
on=bool(int(res.group(4))),
current_state=res.group(5)
thermostat_state=bool(int(res[0])),
current_state=res[1],
temp=float(res[2]),
set_temp=float(res[3]),
co2=float(res[4]),
set_co2=float(res[5]),
light=bool(int(res[6])),
light_R=int(res[7]),
light_G=int(res[8]),
light_B=int(res[9]),
)
obj.save()

Expand Down
2 changes: 1 addition & 1 deletion thermostat_web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@
STATIC_URL = '/static/'

CRONJOBS = [
('*/1 * * * *', 'arduino.serial_read.get_data_from_box')
('*/1 * * * *', 'arduino.serial_read.get_data_from_box', '>> /tmp/schedule_job.log')
]
62 changes: 62 additions & 0 deletions ts/migrations/0003_auto_20190421_2303.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Generated by Django 2.2 on 2019-04-21 23:03

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ts', '0002_remove_logthermostat_hum'),
]

operations = [
migrations.RemoveField(
model_name='logthermostat',
name='on',
),
migrations.AddField(
model_name='logthermostat',
name='light',
field=models.BooleanField(default=False, verbose_name='Стан освітлення'),
),
migrations.AddField(
model_name='logthermostat',
name='light_B',
field=models.IntegerField(default=0, verbose_name='B'),
),
migrations.AddField(
model_name='logthermostat',
name='light_G',
field=models.IntegerField(default=0, verbose_name='G'),
),
migrations.AddField(
model_name='logthermostat',
name='light_R',
field=models.IntegerField(default=0, verbose_name='R'),
),
migrations.AddField(
model_name='logthermostat',
name='set_co2',
field=models.FloatField(blank=True, null=True, verbose_name='Встановлений рівень CO2'),
),
migrations.AddField(
model_name='logthermostat',
name='set_temp',
field=models.FloatField(default=0, verbose_name='Встановлена температура'),
),
migrations.AddField(
model_name='logthermostat',
name='thermostat_state',
field=models.BooleanField(default=False, verbose_name='Стан термостату'),
),
migrations.AlterField(
model_name='logthermostat',
name='co2',
field=models.FloatField(blank=True, null=True, verbose_name='Рівень CO2'),
),
migrations.AlterField(
model_name='logthermostat',
name='temp',
field=models.FloatField(default=0, verbose_name='Температура'),
),
]
20 changes: 14 additions & 6 deletions ts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@


class LogThermostat(models.Model):
temp = models.FloatField("Температура")
co2 = models.FloatField("CO2", blank=True, null=True)
thermostat_state = models.BooleanField("Стан термостату", default=False)
current_state = models.CharField("Стан термостату", max_length=20)

on = models.BooleanField("On")
temp = models.FloatField("Температура", default=0)
set_temp = models.FloatField("Встановлена температура", default=0)

current_state = models.CharField("Стан термостату", max_length=20)
co2 = models.FloatField("Рівень CO2", blank=True, null=True)
set_co2 = models.FloatField("Встановлений рівень CO2", blank=True, null=True)

light = models.BooleanField("Стан освітлення", default=False)

light_R = models.IntegerField("R", default=0)
light_G = models.IntegerField("G", default=0)
light_B = models.IntegerField("B", default=0)

time = models.DateTimeField(auto_now=True)

def __str__(self):
return f"{self.time.strftime('%Y-%m-%d %H:%M:%S')} | State: {'On' if self.on else 'Off'} | t: {self.temp}" \
f"{' | co2:' + str(self.co2) if self.co2 else ''}"
return f"{self.time.strftime('%Y-%m-%d %H:%M:%S')} | State: {'On' if self.thermostat_state else 'Off'} | t: {self.temp}" \
f"{' | light:' + str(self.light) if self.light else ''}"

0 comments on commit 54faa11

Please sign in to comment.