Skip to content

Commit

Permalink
Using new Database Class structure, rewritten send_data
Browse files Browse the repository at this point in the history
  • Loading branch information
petrkr committed Dec 10, 2019
1 parent 6e54a07 commit 6a6c9cb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
from time import sleep, sleep_ms
import urequests
from util.octopus import Env
from util.database import Database

class SendData():
pass

class SendHydroponicsData(SendData):
class HydroponicsDatabase(Database):
def __init__(self, config):
self.urlPOST = config.get("urlpost") # "http://www.yourweb.org/iot18/add18.php"
self.place = config.get("place")
self.deviceID = Env.uID
self.__urlPOST = config.get("urlpost") # "http://www.yourweb.org/iot18/add18.php"
self.__place = config.get("place")
self.__deviceID = Env.uID

"""
OLD: DO NOT USE GET to send data, NEVER EVER!
Expand All @@ -34,22 +33,27 @@ def send_data_get(self, val_type="log_ver", val=0): # GET >
return("Err.send_data_get")
"""

def send_form_data(self, val_type, val): # POST >
def __send_form_data(self, val_type, val): # POST >
header = {}
header["Content-Type"] = "application/x-www-form-urlencoded"
try:
postdata = "device={0}&place={1}&value={2}&type={3}".format(self.deviceID, self.place, val, val_type)
postdata = "device={0}&place={1}&value={2}&type={3}".format(self.__deviceID, self.__place, val, val_type)
print(postdata)
res = urequests.post(self.urlPOST, data=postdata, headers=header)
return("send_form_data_post.OK")
res = urequests.post(self.__urlPOST, data=postdata, headers=header)
res.close()
print("send_form_data_post.OK")
except Exception as e:
print("Exception: {0}".format(e))
return("Err.send_form_data_post")

print("Err.send_form_data_post")

def log_device(self, ver):
logVer = int(float(ver)*100)
self.send_form_data_post("log_ver", logVer)
self.write(log_ver = logVer)

def write(self, *args, **kwargs):
for k, v in kwargs.items():
self.__send_form_data(k, v)


"""
# -----------------------
Expand Down
14 changes: 7 additions & 7 deletions iot-board-micropython-esp32/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
from machine import Pin, UART, RTC, Timer
from util.pinout import set_pinout
from util.octopus import getFree, map, printLog, printTitle, i2c_init, oled_init, time_init, getVer, get_hhmm, w
from hydroponics.send_data import SendHydroponicsData
from hydroponics.hydroponic_database import HydroponicsDatabase
import _thread

ver = 0.55 # int(*100) > db
ver = 0.56 # int(*100) > db
# last update 3.11.2019
getFree(True)

Expand Down Expand Up @@ -142,20 +142,20 @@ def check_point(num, mess):
def send_data():
if ts:
temp = int(ts.get_temp()*10)
print(sender.send_form_data("temp",temp), str(temp))
print(sender.write(temp=temp), str(temp))
sleep(1)

if ios.get("mois"):
mois1 = get_moisture()
print(sender.send_form_data("mois1",mois1), str(mois1))
print(sender.write(mois1=mois1), str(mois1))
sleep(1)

if sbh:
light = 0 # todo
sleep(1)
try:
light = int(sbh.luminance(BH1750.ONCE_HIRES_1))
print(sender.send_form_data("ligh1",light), str(light))
print(sender.write(ligh1=light), str(light))
except:
pass

Expand Down Expand Up @@ -426,14 +426,14 @@ def button3Action():
"""

check_point(6,"Loading sender object.")
sender = SendHydroponicsData(config)
sender = HydroponicsDatabase(config)

check_point(7,"start main loop >")
displMessage("")
timer_init()
getFree(True)

print(sender.send_form_data("pg2_ver",int(ver*100)))
print(sender.write(pg2_ver=int(ver*100)))
sleep(2)
send_data() # firts test send data

Expand Down

0 comments on commit 6a6c9cb

Please sign in to comment.