451 views|2 replies

20

Posts

8

Resources
The OP
 

【Digi-Key Follow me Issue 3】Remote wireless light reminder [Copy link]

 This post was last edited by eew_dy9f48 on 2023-12-12 20:58


Required Task 1: Use MicroPython system

esptool is the official flashing program of Espressif, which can be downloaded from Baidu. After the download is complete, use the following command to clear the contents of the current flash, where COMx is the serial port number corresponding to the development board.

python esptool.py -p COMx-b 460800 --before default_reset --chip esp32c3 erase_flash 

Then go to the micropython official website to download ESP32_GENERIC_C3-20231005-v1.21.0.bin, and use the command to complete the firmware flashing.

python esptool.py -p COMx-b 460800 --before default_reset --chip esp32c3 write_flash --flash_mode dio --flash_size detect 0x0 ESP32_GENERIC_C3-20231005-v1.21.0.bin

Next, write a simple program to verify whether MicroPython is successfully flashed:


Required Task 2: Drive the OLED screen on the expansion board

First install the required library files as shown below:

After installation, connect the OLED screen to the development board, connect the scl and sda pins to GPIO10 and GPIO9 respectively, and use 3.3V for power supply.

The code for screen initialization is very simple. First, initialize the I2C object, and then use this I2C object as a parameter to initialize an oled object. We try to write a text on the screen and draw a square as the outer frame, and we can see that everything is normal.

i2c = machine.SoftI2C(machine.Pin(10), machine.Pin(9))
oled = SSD1306_I2C(128, 64, i2c, vfilp=1, hfilp=1)

oled.fill(0)
oled.rect(5, 25, 116, 15, 1)
oled.text('Hello EEWORLD!', 8, 29, 1)
oled.show()

Required Task 3: Control the buzzer to play music

Here I use a passive buzzer, plug it directly into the breadboard, connect the positive pole to 3.3V, and the negative pole to GPIO8. In this way, when GPIO8 is LOW, the circuit is turned on, and when it is HIGH, the circuit is disconnected. The advantage of this wiring is that in general, the open-drain output capability of the microcontroller is higher than the push-pull output, which can better drive the buzzer to work.

The driving method uses the PWM method. You only need to set the appropriate PWM duty cycle, and then adjust the corresponding PWM frequency according to the order of the music score. When playing music, the screen will display the word "playing".

oled.fill(0)
oled.text('Playing', 30, 29, 1)
oled.show()
tones = {'1': 262, '2': 294, '3': 330, '4': 349, '5': 392, '6': 440, '7': 494, '-': 0}
melody = "334554321123322-334554321123211-"
beeper = machine.PWM(machine.Pin(8, machine.Pin.OUT))
for tone in melody:
    freq = tones[tone]
    if freq:
        beeper.init(duty=1000, freq=freq) 
    else:
        beeper.duty(0) 
    time.sleep_ms(400)
    beeper.duty(0) 
    time.sleep_ms(100)
beeper.deinit() 

Must-do Task 4: Connect to a WiFi network

Micropython has a built-in network library, which can easily connect to WiFi with a few lines of code. After the connection is successful, the local IP address will be obtained and displayed on the screen.

wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.disconnect()
wifi.connect(ssid, password)
while not wifi.isconnected():
    pass

oled.fill(0)
oled.text('IP Address:', 2, 20, 1)
oled.text(wifi.ifconfig()[0], 2, 35, 1)
oled.show()

Required Task 5: Using External Sensors

The external sensor I chose is a photoresistor sensor. The photoresistor sensor has two states: dark resistance and light resistance. The one I chose is 5506, with a light resistance of about 2-6k ohms and a dark resistance of 0.15M ohms. After using a resistor of about 10K for voltage division, it is connected to the GPIO2 pin. This pin has an ADC function on C3, so it can measure analog quantities. After measuring the reading using the built-in ADC method of micropython, a value of 0-4095 will be obtained. We display this value on the OLED and continue measuring for 5 seconds.

adc = machine.ADC(2)

t=time.time()
while time.time() - t < 5:
    oled.fill(0)
    oled.text('Brightness:', 2, 20, 1)
    oled.text(str(adc.read()), 2, 35, 1)
    oled.show()

Optional Task 6: Remote Wireless Lighting Reminder

To complete this task, some additional hardware is needed. First, Xiao is used as a sensor to obtain light brightness data. Another single-chip microcomputer is used as an execution unit to remind you to turn on the light or to automatically turn on and off the light. Any single-chip microcomputer with network function can be used. Here I used an esp32s3, which also runs micropython. The communication between them is carried out using the MQTT protocol commonly used in the Internet of Things. MQTT also requires a server as the MQTT Broker, which is made using a Raspberry Pi.

During the event, I also applied for a Raspberry Pi CM4 computing module. Its hardware configuration is comparable to that of the 4B, but it cannot be used directly because it is only a core board and needs to be used in conjunction with a baseboard.

The choice of baseboard is very wide in this project, with almost no restrictions. Because as an MQTT broker, we only need to ensure that the system can run normally and the WIFI can be connected. Therefore, there are only two requirements for the baseboard, one is power supply, and the other is TF card slot, which is responsible for storing firmware. From third-party B-type baseboards to reterminals, the above two requirements can be met. Here we choose a most common bigtree baseboard to use.

The first step is to insert the TF card and burn the firmware. This step only requires downloading Pi imager to complete the fool-proof burning. In order to use it as low power as possible and stably for a long time, it is best to burn the Lite version of the firmware, which consumes the least system resources.

Next, we want to make sure the system is up to date. To do this, enter the following command to update the system:

sudo apt update
sudo apt upgrade


This will update the system and install any available updates.

Next, you can officially install Mosquitto. Run the following command in the terminal:

sudo apt install mosquitto mosquitto-clients


During installation, the package manager will automatically configure the Mosquitto server to start at boot time, which is controlled by systemctl.

Once the installation is complete, the Mosquitto MQTT broker will be up and running on your device.

To verify that Mosquitto has been successfully installed and is running, you can use the following command:

sudo systemctl status mosquitto

This will display the status of the Mosquitto service. If the service has started successfully, you should see "active (running)" in the output.

However, so far this broker only supports commands sent by the local machine. If you want to truly act as a server and allow other client devices to access it, you need to configure it.

First open and edit the configuration file mosquitto.conf

sudo nano /etc/mosquitto/mosquitto.conf

Add the following content in it

listener 1883
allow_anonymous true

Finally, restart the service to complete the configuration.

sudo systemctl restart mosquitto

For the final effect, please refer to the last part of the demonstration video.

Download link: https://download.eeworld.com.cn/detail/eew_dy9f48/630258

This post is from DigiKey Technology Zone

Latest reply

Is the Raspberry Pi CM4 Compute Module useful?   Details Published on 2023-12-13 07:33
 
 

6570

Posts

0

Resources
2
 

Is the Raspberry Pi CM4 Compute Module useful?

This post is from DigiKey Technology Zone

Comments

Read the article carefully, MQTT BROKER runs on CM4  Details Published on 2023-12-13 11:20
 
 
 

20

Posts

8

Resources
3
 
Jacktang posted on 2023-12-13 07:33 Is the Raspberry Pi CM4 computing module useful?

Read the article carefully, MQTT BROKER runs on CM4

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list