I found an issue with deepsleep in my code as shown below:
The deepsleep part of the code will be ignored by the interpreter while it shouldn't, please help to check.
It seem like when i run the deepsleep code without other parts like temperature and humidity data acquire and mqtt transmit it works just fine, but when i put everything together, it just skip the deepsleep part and i don't know why.
Code: Select all
import machine,time,antp,network,ubinascii
from robust import MQTTClient
from machine import Pin
from sht30 import SHT31
i2c = machine.I2C(sda=machine.Pin(4), scl=machine.Pin(15))
led = Pin(25, Pin.OUT, value=0) #led pin on wifikit32 is gpio25
sht = SHT31(i2c)
CLIENT_ID = str(ubinascii.hexlify(network.WLAN().config('mac')).decode())
SERVER = "192.168.89.216"
TOPIC = b"temperature"
def create_csv(name):
with open(name, "a") as log:
fle = open(name,'r')
check_csv = fle.readline()
if len(check_csv) == 0:
log.write("{0},{1},{2}\n".format('Timestamp','Temperature','Humidity'))
log.close()
def write_csv(name,timestamp,temp,humid):
with open(name, "a") as log:
log.write("{0},{1},{2}\n".format(timestamp,temp,humid))
log.close()
def main(server=SERVER):
c = MQTTClient(CLIENT_ID, server)
c.connect()
print("Connected to %s, waiting for temperature reading" % server)
#create_csv(name)
while True:
while True:
timestamp_raw = time.localtime()
timestamp = str(timestamp_raw[0])+'-'+str(timestamp_raw[1])+'-'+str(timestamp_raw[2])+' '+\
str(timestamp_raw[3])+':'+str(timestamp_raw[4])+':'+str(timestamp_raw[5])
temp_humi = sht.get_temp_humi()
temp_humi_str = CLIENT_ID + ',' + str(temp_humi[0]) + ',' + str(temp_humi[1])
print(CLIENT_ID,timestamp,temp_humi)
c.publish(TOPIC, temp_humi_str)
led.value(0)
#write_csv(name,timestamp,temp,rh)
#time.sleep(30)
led.value(1)
c.disconnect()
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
print('woke from a deep sleep')
main()
print('in deepsleep now')
machine.deepsleep(50000)