ok I got other problems!
1 - On light sleep I still have 40ma of current. I think it is the wifi. How to turn it off completely?
right now I'm using. disconnect() and active("Down").
~40ma on lightsleep and up to 80ma when transmitting.
2 - Using MQTT with a lipo battery it should be good to read VSys. but machine.ADC(29) freeze the wifi.
To counteract this I read GPIO29 pad, use machine.ADC(3), change the pad to NO (pulls,In and OUT), read_u16() and then put GPIO29 back like it was.
This is my code.
Code: Select all
import machine
import rp2
import network
import ubinascii
import time
import sys
from secrets import secrets
from onewire import OneWire
from ds18x20 import DS18X20
from umqtt.simple import MQTTClient
from machine import Pin
sensor = (0x28,0xff,0x69,0x67,0xc1,0x17,0x01,0x98)
#sensor DS18b20 on pin 28
ds = DS18X20(OneWire(machine.Pin(28)))
sensor_temp = machine.ADC(machine.ADC.CORE_TEMP)
conversion_factor = 3.3 / (65535)
last_message = 0
message_interval = 5
counter = 0
#
# Set country to avoid possible errors / https://randomnerdtutorials.com/micropython-mqtt-esp32-esp8266/
rp2.country('CA')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# If you need to disable powersaving mode
# See the MAC address in the wireless chip OTP
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print('mac = ' + mac)
# Other things to query
# print(wlan.config('channel'))
# print(wlan.config('essid'))
# print(wlan.config('txpower'))
# Load login data from different file for safety reasons
ssid = secrets['ssid']
pw = secrets['pw']
broker = secrets['broker']
sub_topic = secrets['subtopic']
pub_topic = secrets['pubtopic']
pub_topic2 = secrets['pubtopic2']
pub_topic3 = secrets['pubtopic3']
#client_id = ubinascii.hexlify(machine.unique_id())
#client_id = mac
client_id = secrets['client_id']
def setPad(gpio, value):
machine.mem32[0x4001c000 | (4+ (4 * gpio))] = value
def getPad(gpio):
return machine.mem32[0x4001c000 | (4+ (4 * gpio))]
#if machine.reset_cause() != machine.SOFT_RESET:
if True:
#wlan.init(network.WLAN.STA)
# configuration below MUST match your home router settings!!
wlan.ifconfig(('192.168.0.21', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
if not wlan.isconnected():
# change the line below to match your network ssid, security and password
wlan.connect(ssid,pw)
while not wlan.isconnected():
machine.idle() # save power while waiting
# Wait for connection with 10 second timeout
timeout = 10
while timeout > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
timeout -= 1
print('Waiting for connection...')
time.sleep(1)
# Handle connection error
# Error meanings
# 0 Link Down
# 1 Link Join
# 2 Link NoIp
# 3 Link Up
# -1 Link Fail
# -2 Link NoNet
# -3 Link BadAuth
if wlan.status() != 3:
raise RuntimeError('Wi-Fi connection failed')
else:
led = machine.Pin('LED', machine.Pin.OUT)
for i in range(wlan.status()):
led.on()
time.sleep(.1)
led.off()
print('Connected')
status = wlan.ifconfig()
print('ip = ' + status[0])
### Topic Setup ###
def sub_cb(topic, msg):
#print((topic, msg))
if msg == b'LEDon':
print('Device received LEDon message on subscribed topic')
led.value(1)
if msg == b'LEDoff':
print('Device received LEDoff message on subscribed topic')
led.value(0)
def connect_and_subscribe():
global client_id, mqtt_server, topic_sub
client = MQTTClient(client_id, broker)
client.set_callback(sub_cb)
client.connect()
client.subscribe(sub_topic)
print('Connected to %s MQTT broker as client ID: %s, subscribed to %s topic' % (broker, client_id, sub_topic))
return client
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()
try:
client = connect_and_subscribe()
except OSError as e:
restart_and_reconnect()
led = machine.Pin('LED', machine.Pin.OUT)
while True:
try:
led.value(True)
ds.convert_temp()
reading = sensor_temp.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706)/0.001721
pub_msg = str(temperature)
print(pub_topic2," ",pub_msg)
client.publish(pub_topic2, pub_msg)
time.sleep_ms(50)
pub_msg = "%6.1f" % (ds.read_temp(sensor))
print(pub_topic2," ",pub_msg)
client.publish(pub_topic, pub_msg)
time.sleep_ms(50)
oldpad = getPad(29)
setPad(29,128) #no pulls, no output, no input
Vsys = machine.ADC(3)
v = Vsys.read_u16()
setPad(29,oldpad)
pub_msg = "%5.2f" % (v*3*conversion_factor)
print(pub_topic3," ",pub_msg)
client.publish(pub_topic3, pub_msg)
time.sleep_ms(500)
led.value(False)
wlan.disconnect()
wlan.active("Down")
time.sleep_ms(100)
machine.lightsleep(120000)
except OSError as e:
pass
restart_and_reconnect()
This is the MQTT output using a 3.7V lipo battery full charge right now
Code: Select all
pi@Pihome:~ $ mosquitto_sub -v -t "PicoW/#" | xargs -d$'\n' -L1 bash -c 'date "+%Y-%m-%d %T.%3N $0"'
2022-07-09 16:15:59.890 PicoW/tempCPU 31.2577
2022-07-09 16:15:59.999 PicoW/temp 27.4
2022-07-09 16:16:00.114 PicoW/Vsys 4.05
2022-07-09 16:18:17.959 PicoW/tempCPU 31.72584
2022-07-09 16:18:18.005 PicoW/temp 27.5
2022-07-09 16:18:18.223 PicoW/Vsys 4.05