程序代写案例-ECPS 206

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
ECPS 206
Lab 2
Hardware Setup
● Recommend to install the heat sinks
○ https://www.youtube.com/watch?v=E-4GaAz7XNM
● Recommend to install t
he case and power the fan with 3.3V or 5V.
● Connect 4 LEDs with wires and resistors (to protect LEDs)
● Connect a T-type extension board and cables
○ https://www.youtube.com/watch?v=RCIL35e47mk
RPi 4: Connect with Breadboard and LEDs
RPi 4: Layout
● Board & Intro:
https://www.etechnophiles.com/raspberry-pi-4-gpio-pinout-specifications-and-sc
hematic/
● Pins: https://pi4j.com/1.4/pins/rpi-4b.html
● Datasheet: https://datasheets.raspberrypi.org/rpi4/raspberry-pi-4-datasheet.pdf
● Pick some GPIO XX for LED connections
RPi 4: Connect RGB Light Sensor TCS34725
From To Description
RPi / G TCS34725 /GND Ground for I2C Light Sensor
RPi / 3V3 (PIN 1) TCS34725 / 3V3 3.3V Power for I2C Light Sensor
RPi / SDA1 I2C (PIN 3) TCS34725 /SDA SDA for I2C Light Sensor
RPi / SCL1 I2C (PIN 5) TCS34725 /SCL SCL for I2C Light Sensor
TCS34725 / LED TCS34725 / INT Connectng these two pins together allow for softwarecontrol of bright LED on TCS34725 board
ESP8266: Ambient Light sensor (photoresistor)
● Follow the instructions and example code to test connection.
○ https://www.instructables.com/id/NodeMCU-With-LDR/
ESP8266: Ambient Light sensor (photoresistor)
● Example code (link from
previous slide, or check similar
one in File > Examples > Analog
> AnalogInOutSerial) utilizes
analog signal read function.
Software Setup
● Recommend to use Raspberry Pi Imager to pre-configure OS
● Refer to the link to download the Imager on your laptop to write OS image into SD card:
https://www.raspberrypi.com/software/
● How to use Imager: https://www.youtube.com/watch?v=ntaXWS8Lk34
● If you have monitor & keyboard & mouse, choose the Raspberry Pi OS (32-bit).
● Configure WiFi: https://www.seeedstudio.com/blog/2021/01/25/three-methods-to-configure-raspberry-pi-wifi/
● If you don’t have monitor and would like to work on SSH Linux console with cellphone hotspot, you can follow these
steps in Imager: https://www.pragmaticlinux.com/2021/08/raspberry-pi-headless-setup-with-the-raspberry-pi-imager/
● Note that after pressed Ctrl-Alt-X for Advanced options, these are recommended:
● Checked “Set hostname”. Optional to change the “raspberrypi” to preferred name.
● Checked “Enable SSH”. Select “Use password…” and type any password
● Checked “Configured WiFi”. Type your hotspot WiFi SSID & password. Change WiFi country to US
(faster by delete it and type)
● Checked “Set locale settings”, choose America/… and us
● Checked “Skip first-run wizard”
● Unchecked “Play sound…”, “Eject media…”, “Enable telemetry”.
● Save to close options
● Press Write and wait for it.
RPi 4: Install the Raspberry Pi OS
Logged in Raspberry Pi OS via either GUI desktop or SSH.
Open console:
1. Turn on I2C Interface
● sudo raspi-config
● Choose Interfacing Options > I2C > Yes
2. Install the necessary packages
● sudo apt-get update
● sudo apt-get install -y git build-essential python3-dev python3-setuptools python3-pip python3-smbus
3. Reboot
● sudo reboot
4. Install Adafruit Python TCS34725
● git clone https://github.com/adafruit/Adafruit_Python_TCS34725.git
● cd Adafruit_Python_TCS34725/
● sudo python3 setup.py install
5. Run the simpletest.py example in examples/ folder: python3 simpletest.py
RPi 4: Install the TCS34725 python library
Network issue
● Using “UCInet Mobile Access”
○ Find the MAC address of ESP8266
■ https://techtutorialsx.com/2017/04/09/esp8266-get-mac-address/
○ Find the MAC address of RPi 4
■ In the terminal or SSH, Type the command “cat /sys/class/net/wlan0/address”
○ Register the MAC addresses to UCI
■ http://apps.oit.uci.edu/mobileaccess/registration/
● Or use your cellphone hotspot function to setup manageable and isolated
network
○ Recommend and it allows broadcast for other HWs.
Python UDP examples for RPi
● Basic Python UDP echo client example:
○ https://pymotw.com/3/socket/udp.html
● Python UDP Send/Recv examples:
○ https://stackoverflow.com/a/27893987
○ set the timeout to know the 5 seconds is up.
C UDP Example for ESP8266
● Reference:
https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/udp-examples.h
tml
● Example: https://siytek.com/esp8266-udp-send-receive/
● Or, you can also check in IDE: File > Examples > ESP8266Wifi > Udp.
change the “your-ssid” to the Wifi network name and “your-password” if there
is a password or empty otherwise.
#define STASSID "your-ssid"
#define STAPSK "your-password"
○ Try using RPi 4’s terminal with the following command to confirm the network is working:
○ nc -u 8888
C UDP Example for ESP8266
● Example to get IP address from socket and blink the IP address on ESP8266
○ https://github.com/rin67630/Blink-my-IP
Hint
● Design your message format to send
○ When ESP8266 receives first message, keep the IP and port of RPi globally for later use.
○ Use sprintf and strncmp to deal with string type messages on ESP8266.
○ On ESP8266’s side, lux value can be converted to bytes buffer before sending (use itoa () )
○ On RPi’s side, recv length is recommended to be equal to buffer length set in ESP8266. Then
actual value is decoded from bytes buffer.
● The values from TCS34725 have different range (based on the settings) than the value from analog
photoresistor (0-1024). It is easier to scale the value of one side to meet the yellow LED condition
under normal ambient light.
○ If initially photoresistor senses 850 and TCS34725 senses 150, you can calculate like (lux -
850) / (1024-850) * (1024) + 150 on ESP8266 side.
○ Suggested settings: TCS34725_INTEGRATIONTIME_101MS and TCS34725_GAIN_4X
■ Relevant constants in python library:
https://github.com/adafruit/Adafruit_Python_TCS34725/blob/996396cd095522d788b7b1
5d20fd44e566c8464e/Adafruit_TCS34725/TCS34725.py#L77
● Onboard LEDs on ESP8266 are OFF only when HIGH value is digitalWrite.
● Use threading to create threads or shorter socket timeout with counter to control the blink white
LED.
Demo Video for HW2
https://uci.yuja.com/V/Video?v=3759002&node=12858963&a=1832739861
When started, RPi should
turn on the white LED and
send a UDP message to
ESP8266 to initiate the
communication.
After receiving the first UDP
message from RPi, ESP8266
should flash its onboard LED
every 500 ms, collect the light
sensor value every 500 ms, and
send the average value in the
last 5 seconds back to the RPi
every 2 seconds via UDP.
ESP8266 prints the sliding
window every time it collects a
sensor value. ESP8266 also
prints the average when it is
time to send the average value.
Whenever RPi receives a
message containing the remote
light reading, RPi should turn off
the white LED. It will turn on the
green LED if the local light value
is higher, or the yellow LED if
the local value is within ±20%
of the remote value, or the red
LED if the local value is lower
than the remote value.
Whenever RPi receives a
message containing the remote
light reading, RPi should turn off
the white LED. It will turn on the
green LED if the local light
value is higher, or the yellow
LED if the local value is within
±20% of the remote value, or the
red LED if the local value is lower
than the remote value.
Whenever RPi receives a
message containing the remote
light reading, RPi should turn off
the white LED. It will turn on the
green LED if the local light value
is higher, or the yellow LED if the
local value is within ±20% of the
remote value, or the red LED if
the local value is lower than
the remote value.
If RPi hasn't received a message from ESP8266 for 5 seconds, RPi flashes the
white LED on and off every 500 ms to indicate an error condition (e.g. ESP8266 is
accidentally turned off). It keeps flashing until another light value is received and goes
back to Step 4.
If RPi hasn't received a message from ESP8266 for 5 seconds, RPi flashes the white
LED on and off every 500 ms to indicate an error condition (e.g. ESP8266 is accidentally
turned off). It keeps flashing until another light value is received and goes back to
Step 4.

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468