ES32D26 CircuitPython test code, publish states to MQTT server using Adafruit CircuitPython 10.0.0 build:doit_esp32_devkit_v1

## CircuitPython example for the ES32D26
## Some libs may not be used, I cleaned this up from another project.
# boot_out.txt
## Adafruit CircuitPython 10.0.0 on 2025-10-01; ESP32 Devkit V1 with ESP32
## Board ID:doit_esp32_devkit_v1
## UID:8875121BD145
## MAC:88:57:21:B1:1D:54
#config.json
#{
#  "wifi_info":{
#      "hostname":"ES32D26",
#      "ssid":    "myNetwork",
#      "psk":     "lookattheceiling"
#  },
#  "mqtt_info":{
#    "broker": "mosquitto.local",
#    "user":   "myuser",
#    "pass":   "dumbpassword",
#    "topic":  "ES32D26/"  
#  }
#}

import wifi
import adafruit_74hc595
import wws_74hc165 ## https://github.com/WoolseyWorkshop/WoolseyWorkshop_CircuitPython_74HC165
import board
import microcontroller
import busio
import digitalio
import time
import json
import os
import adafruit_simplemath
import adafruit_connection_manager
import socketpool
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import analogio

# meters to feet
def mtf(meters):
  return meters * 3.28084

# feet to meters
def ftm(feet):
  return feet * 0.3048

# doing these first so we can use config in other functions.
def load_config_json():
  try:
    # Open the JSON file in read mode ('r')
    f = open("/config.json", 'r') 
    config = json.loads(f.read())
    f.close
    return config
  except:
    pass

def save_config_json(config):
  try:
    f = open("/config.json", 'w')
    f.write(config)
    f.close
  except:
    pass

# My lazy logger TODO: make this a little smarter. Save a loglevel in the config, dont log anything higher than loglevel
def log(level,log_msg):
    # log levels
    # debug
    # verbose
    # default
    # none
    mqtt_client.publish(config["mqtt_info"]["topic"]+"log/"+level, log_msg)

## mqtt callbacks
def connected(client, userdata, flags, rc):
    # This function will be called when the client is connected
    # successfully to the broker.
    #print(f"Connected to "+config["mqtt_info"]["broker"]+"! Listening for topic changes on "+config["mqtt_info"]["topic"])
    # Subscribe to all changes on the following topic. This should maybe get it's own config variable.
    client.subscribe(config["mqtt_info"]["topic"]+"listenHere/#")

def disconnected(client, userdata, rc):
    # This method is called when the client is disconnected
    #print("Disconnected from "+config["mqtt_info"]["broker"]+"!")
    pass
    
def message(client, topic, message):
    # This method is called when a topic the client is subscribed to
    # has a new message.
    # Add your code here to do something when it gets a message.
    # Make sure not to subscribe to the same topic you are publishing 
    # to, there is no loopback protection.
    print("Topic:"+topic+"  Message:"+message)
    
def map_range(value,input_upper,input_lower,output_upper,output_lower):
    # This will scale your inputs to your desired output
    # input_lower is 0 and input_upper is 8195 for the esp32
    return adafruit_simplemath.map_range(value, input_lower, input_upper, output_lower, output_upper)

# load the config file
config = load_config_json()

# setup network
wifi.radio.hostname = config["wifi_info"]["hostname"]
wifi.radio.connect(config["wifi_info"]["ssid"], config["wifi_info"]["psk"])
pool = socketpool.SocketPool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
# setup IOs
## setup 74hc595  relays
relay_pin_data   = board.D12
relay_pin_clk    = board.D22
relay_pin_latch  = digitalio.DigitalInOut(board.D23)
relay_pin_oe     = digitalio.DigitalInOut(board.D13) ## active_low
relay_spi = busio.SPI(relay_pin_clk, MOSI=relay_pin_data)
sr = adafruit_74hc595.ShiftRegister74HC595(relay_spi, relay_pin_latch)
relay_pin_oe.direction = digitalio.Direction.OUTPUT
## now define the pin to a variable
relay1 = sr.get_pin(0)
relay2 = sr.get_pin(1)
relay3 = sr.get_pin(2)
relay4 = sr.get_pin(3)
relay5 = sr.get_pin(4)
relay6 = sr.get_pin(5)
relay7 = sr.get_pin(6)
relay8 = sr.get_pin(7)
## do some cleanup
relay1.value = False       # set one relay to off before enabling the outputs so we have a known status when enabling output
relay_pin_oe.value = False # This output is active low

## setup 74hc165 discrete inputs
## reminder, these return false when they are tied to ground.
inputs_pin_data  = board.D15
inputs_pin_clk   = board.D2
inputs_pin_load  = digitalio.DigitalInOut(microcontroller.pin.GPIO0)

inputs_spi = busio.SPI(inputs_pin_clk, MISO=inputs_pin_data)
inputs_sr  = wws_74hc165.ShiftRegister74HC165(inputs_spi, inputs_pin_load)

input1 = inputs_sr.get_pin(0)
input2 = inputs_sr.get_pin(1)
input3 = inputs_sr.get_pin(2)
input4 = inputs_sr.get_pin(3)
input5 = inputs_sr.get_pin(4)
input6 = inputs_sr.get_pin(5)
input7 = inputs_sr.get_pin(6)
input8 = inputs_sr.get_pin(7)

# analog inputs
## 4-20ma inputs
analog_i_1 = analogio.AnalogIn(microcontroller.pin.GPIO34)
analog_i_2 = analogio.AnalogIn(microcontroller.pin.GPIO39)
analog_i_3 = analogio.AnalogIn(microcontroller.pin.GPIO35)
analog_i_4 = analogio.AnalogIn(microcontroller.pin.GPIO36)
## 0-10v  inputs
analog_v_1 = analogio.AnalogIn(microcontroller.pin.GPIO14)
analog_v_2 = analogio.AnalogIn(microcontroller.pin.GPIO33)
analog_v_3 = analogio.AnalogIn(microcontroller.pin.GPIO27)
analog_v_4 = analogio.AnalogIn(microcontroller.pin.GPIO32)

# analog outputs
## analog outs, dip configurable for 0-10v or 4-20ma
analog_out_1 = analogio.AnalogOut(microcontroller.pin.GPIO25)
analog_out_2 = analogio.AnalogOut(microcontroller.pin.GPIO26)

# configure mqtt client
mqtt_client = MQTT.MQTT(
    broker=config["mqtt_info"]["broker"],
    username=config["mqtt_info"]["user"],
    password=config["mqtt_info"]["pass"],
    is_ssl=False,
    socket_pool=pool,
    ssl_context=ssl_context,
)

# set our LWT message
mqtt_client.will_set(config["mqtt_info"]["topic"]+'status','offline')

# set our callbacks
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
mqtt_client.on_message = message

# Connect the client to the MQTT broker.
print("Connecting to "+config["mqtt_info"]["broker"])
mqtt_client.connect()

log("default","System startup")

fiveSecondTimer = thirtySecondTimer = sixtySecondTimer = ninetySecondTimer = startTime = time.time()

while True:
    # Poll the message queue
    mqtt_client.loop()
    if fiveSecondTimer <= time.time():
      # Build our lists
      discrete_input_status  = [
          input1.value,
          input2.value,
          input3.value,
          input4.value,
          input4.value,
          input5.value,
          input6.value,
          input7.value,
          input8.value
      ]
      analog_input_status    = [
          analog_i_1.value,
          analog_i_2.value,
          analog_i_3.value,
          analog_i_4.value,
          #analog_v_1.value,
          #analog_v_2.value,
          #analog_v_3.value,
          #analog_v_4.value
      ]
      relay_output_status    = [
          relay1.value,
          relay2.value,
          relay3.value,
          relay4.value,
          relay5.value,
          relay6.value,
          relay7.value,
          relay8.value
      ]
      # Publish our lists
      mqtt_client.publish(config["mqtt_info"]["topic"]+"config", json.dumps(config))
      mqtt_client.publish(config["mqtt_info"]["topic"]+"discrete_input_status", str(discrete_input_status))
      mqtt_client.publish(config["mqtt_info"]["topic"]+"analog_input_status", str(analog_input_status))
      mqtt_client.publish(config["mqtt_info"]["topic"]+"relay_output_status", str(relay_output_status))
      fiveSecondTimer = time.time()+5
    
    if thirtySecondTimer <= time.time():
        thirtySecondTimer = time.time()+30
       
    if sixtySecondTimer <= time.time():
        sixtySecondTimer = time.time()+30

    if ninetySecondTimer <= time.time():
        ninetySecondTimer = time.time()+90
    # end of run
    log("debug","end of run")
Back to blog

Leave a comment