These are example files from the tutorial by @boneskull as seen on Get on The Good Foot with MicroPython on the ESP32, Part 2
Last active
November 12, 2021 22:45
-
-
Save boneskull/1f5ae354815c6db5b1cb05ad2cb6232b to your computer and use it in GitHub Desktop.
MicroPython on ESP32: MQTT and DS18B20 temperature sensor full example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def connect(): | |
import network | |
sta_if = network.WLAN(network.STA_IF) | |
if not sta_if.isconnected(): | |
print('connecting to network...') | |
sta_if.active(True) | |
sta_if.connect('<YOUR SSID>', '<YOUR PASSWORD>') | |
while not sta_if.isconnected(): | |
pass | |
print('network config:', sta_if.ifconfig()) | |
def no_debug(): | |
import esp | |
# this can be run from the REPL as well | |
esp.osdebug(None) | |
no_debug() | |
connect() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from ds18x20 import DS18X20 | |
from machine import Pin | |
from onewire import OneWire | |
class TemperatureSensor: | |
""" | |
Represents a Temperature sensor | |
""" | |
def __init__(self, pin): | |
""" | |
Finds address of single DS18B20 on bus specified by `pin` | |
:param pin: 1-Wire bus pin | |
:type pin: int | |
""" | |
self.ds = DS18X20(OneWire(Pin(pin))) | |
addrs = self.ds.scan() | |
if not addrs: | |
raise Exception('no DS18B20 found at bus on pin %d' % pin) | |
# save what should be the only address found | |
self.addr = addrs.pop() | |
def read_temp(self, fahrenheit=True): | |
""" | |
Reads temperature from a single DS18X20 | |
:param fahrenheit: Whether or not to return value in Fahrenheit | |
:type fahrenheit: bool | |
:return: Temperature | |
:rtype: float | |
""" | |
self.ds.convert_temp() | |
time.sleep_ms(750) | |
temp = self.ds.read_temp(self.addr) | |
if fahrenheit: | |
return self.c_to_f(temp) | |
return temp | |
@staticmethod | |
def c_to_f(c): | |
""" | |
Converts Celsius to Fahrenheit | |
:param c: Temperature in Celsius | |
:type c: float | |
:return: Temperature in Fahrenheit | |
:rtype: float | |
""" | |
return (c * 1.8) + 32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import time | |
from temperature import TemperatureSensor | |
from umqtt.robust import MQTTClient | |
class TemperatureClient: | |
""" | |
Represents an MQTT client which publishes temperature data on an interval | |
""" | |
def __init__(self, client_id, server, pin, fahrenheit=True, topic=None, | |
**kwargs): | |
""" | |
Instantiates a TemperatureSensor and MQTTClient; connects to the | |
MQTT broker. | |
Arguments `server` and `client_id` are required. | |
:param client_id: Unique MQTT client ID | |
:type client_id: str | |
:param server: MQTT broker domain name / IP | |
:type server: str | |
:param pin: 1-Wire bus pin | |
:type pin: int | |
:param fahrenheit: Whether or not to publish temperature in Fahrenheit | |
:type fahrenheit: bool | |
:param topic: Topic to publish temperature on | |
:type topic: str | |
:param kwargs: Arguments for MQTTClient constructor | |
""" | |
self.sensor = TemperatureSensor(pin) | |
self.client = MQTTClient(client_id, server, **kwargs) | |
if not topic: | |
self.topic = 'devices/%s/temperature/degrees' % \ | |
self.client.client_id | |
else: | |
self.topic = topic | |
self.fahrenheit = bool(fahrenheit) | |
self.client.connect() | |
def publishTemperature(self): | |
""" | |
Reads the current temperature and publishes a JSON payload on the | |
configured topic, e.g., `{"unit": "F", "degrees": 72.5}` | |
""" | |
t = self.sensor.read_temp(self.fahrenheit) | |
payload = dict(degrees=t) | |
if self.fahrenheit: | |
payload['unit'] = 'F' | |
else: | |
payload['unit'] = 'C' | |
self.client.publish(self.topic, json.dumps(payload)) | |
def start(self, interval=60): | |
""" | |
Begins to publish temperature data on an interval (in seconds). | |
This function will not exit! Consider using deep sleep instead. | |
:param interval: How often to publish temperature data (60s default) | |
:type interval: int | |
""" | |
while True: | |
self.publishTemperature() | |
time.sleep(interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
I am looking to hire an ESP32 Python programmer to help me to develop a firework interface for ESP32 I2S for a mic on I2S ICS-43434 IC and play wav using I2S MAX98357A IC. The target is work needed Google speech recognition and Google text to speech from ESP32.
The development environment must be https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo
If you have an experience on ESP32 MicroPython_ESP32_psRAM_LoBo Python please reply my email [email protected]
And call my Skype nissim.test
The work is milestone-based and payment will be as milestone below,
We will provide the hardware for that :
I2S ICS-43434 IC and play wav using MAX98357A IC
As:
You Say to Mic I2S ICS-43434 “What is the temperature now?”
Get back the text in ESP32 Python “What is the temperature now”
ESP32 Send to TTS “The temperature now is 30 degree”
The return wav or Flak been played to using MAX98357A over I2S
a. Google API use wav Audio Encoding name: FLAC Free Lossless Audio Codec 16-bit or 24-bit required for streams.
b. So, FLAC can be done in C or Python. But interface must be from Python. See https://goo.gl/mtrwVN
Contact :
Nissim Zur
[email protected]
Skype: nissim.test