LC709203F mqtt

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tonyek
 
Posts: 2
Joined: Tue Sep 27, 2022 7:18 am

LC709203F mqtt

Post by tonyek »

HI
I've started a little project to try and make a diybms using some Raspberry pi pico w's
and some of your LC709203F Fuel gauges.The idea was to have the LC709203F's connected
to the pico's read the battery cells and then send the reading's over MQTT to the Broker.
I've managed to get this to work fine when the pico is powered through the USB port
but when powered from the battery through VSYS on the Pico the program seems to only run so far.
I.E the wifi connects then the Mqtt seems to connect but no messages are sent.

I'm now wondering if the LC709203F is operating correctly when powered by the battery it is monitoring.
do you think it should work or am I barking up the wrong tree.

Thanks tonyek

Code: Select all

# Complete project details at https://RandomNerdTutorials.com/micropython-mqtt-publish-bme280-esp32-esp8266/

import time
import ubinascii
import machine
import micropython
import network
import secrets
from LC709203F_CR import LC709203F
from simple import MQTTClient
from machine import Pin, I2C


import gc
gc.collect()

ssid = secrets.SSID
password = secrets.PW
mqtt_server = secrets.SERVER


client_id = ubinascii.hexlify(machine.unique_id())

topic_pub_volt = b'Fuel_Gauge/Cell_0/Voltage'
topic_pub_perc = b'Fuel_Gauge/Cell_0/Percentage'
topic_pub_temp = b'Fuel_Gauge/Cell_0/Temperature'
topic_pub_test = b'Fuel_Gauge/Cell_0/Test'
last_message = 0
message_interval = 20

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass

print('Connection successful')

led = machine.Pin('LED', machine.Pin.OUT)

for i in range(10):
    led.on()
    time.sleep(.5)
    led.off()
    time.sleep(.5)
    
i2c = I2C(1,scl=Pin(3), sda=Pin(2))

sensor = LC709203F(i2c)
sensor.thermistor_bconstant = 3950
sensor.thermistor_enable = True

def connect_mqtt():
  global client_id, mqtt_server
  client = MQTTClient(client_id, mqtt_server)
  #client = MQTTClient(client_id, mqtt_server, user=your_username, password=your_password)
  client.connect()
  print('Connected to %s MQTT broker' % (mqtt_server))
  return client

def restart_and_reconnect():
  print('Failed to connect to MQTT broker. Reconnecting...')
  time.sleep(10)
  machine.reset()
  
for i in range(10):
    led.on()
    time.sleep(.5)
    led.off()
    time.sleep(.5)
    
    
def read_sensor():
  try:
    volt = str(round(sensor.cell_voltage, 2)) 
    perc = str(round(sensor.cell_percent, 2))
    temp = str(round(sensor.cell_temperature, 2))

    return volt, perc, temp
    #else:
    #  return('Invalid sensor readings.')
  except OSError as e:
    return('Failed to read sensor.')

try:
  client = connect_mqtt()
except OSError as e:
  restart_and_reconnect()

while True:
  try:
    if (time.time() - last_message) > message_interval:
      volt, perc, temp = read_sensor()
      print(volt)
      print(perc)
      print(temp)
      client.publish(topic_pub_volt, volt)
      client.publish(topic_pub_perc, perc)      
      client.publish(topic_pub_temp, temp)

      last_message = time.time()
  except OSError as e:
    restart_and_reconnect()


User avatar
tonyek
 
Posts: 2
Joined: Tue Sep 27, 2022 7:18 am

Re: LC709203F mqtt

Post by tonyek »

Just a heads up.
Everything started working with just the battery connected after uf2 file update.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”