複数のESP32からソケット通信でデータを受信するプログラム
This project consists of scripts for setting up a microcontroller (like an ESP32) to manage network connections and run a simple server. The main components are boot.py for network configuration and ch_main.py for handling HTTP server functionality.
You can get MicroPython library for INA219 from this link below:
https://github.com/chrisb2/pyb_ina219.git
・Network Configuration: SSID名を定義して,password.pyをinstall
・Wi-Fi Functions:
・wifiscan()
: Scans available Wi-Fi networks.
・connect_lab_wifi(timeout)
: Connects to a specified lab Wi-Fi network.
・connect_home_wifi(timeout)
: Connects to a specified home Wi-Fi network.
・connect_esp_wifi(timeout)
: Connects to a specified ESP Wi-Fi network.
・Access Point Functions:
・ap_activate()
: Activates the device's access point.
・ap_deactivate()
: Deactivates the device's access point.
・System Initialization: Prints a boot message and executes ch_main.py.
The script hadles the following:
・Server Setup:
・Initializes and starts a server socket on port 80.
・Client Handling:
・Manages client connections, receives data, and handles errors.
・Main Execution:
・Activates the access point via boot.ap_activate()
.
・Starts the server and continuously accepts client connections, spawning new threads to handle each connection.
This script handles Wi-Fi connectivity and data transmission.
・Imports:
・boot
: Contains functions for Wi-Fi connection.
・socket
, time
, _thread
: Standard Python modules.
・Global Variables:
・s
: A socket object.
・Functions:
・send()
: Creates a socket connection to a server.
・send_to_server()
: Connects to Wi-Fi and sends a message to the server at regular intervals.
・Main Execution:
・Starts a new thread that runs the send_to_server function.
This script interfaces with an INA219 sensor to measure and log voltage, current, and power.
・Imports
・Modules for handling hardware interaction (Pin, I2C, RTC), INA219 sensor (INA219), logging (INFO), time, and threading (_thread).
・Constants
・SHUNT_OHMS: The value of the shunt resistor.
・Initialization
・Initializes I2C communication.
・Configures the INA219 sensor.
・Functions:
・get_info()
:Retrieves voltage, current, and power measurements from the INA219 sensor.
・write_to_csv()
:Writes the measurements to a CSV file with a timestamp.
・Main Execution
・Starts a new thread to run the write_to_csv function.
Create a password.py
file with the required Wi-Fi passwords:
LAB_WIFI_PASS = 'your_lab_wifi_password'
HOME_WIFI_PASS = 'your_home_wifi_password'
WEBREPL_PASS = 'your_webrepl_password'
- Upload
boot.py
,ch_main.py
, andpassword.py
to your ESP32. - Execute the
boot.py
script. This will configure the network settings and start the web server.
booted system
---- AP is activated -----
('192.168.4.1', '255.255.255.0', '192.168.4.1', '0.0.0.0')
Server started at 192.168.4.1:80
accepting......
CH accepts message from multiple nodes.
connected from: ('192.168.4.2', 52469)
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
accepting......
connected from: ('192.168.4.3', 64312)
hello from F
accepting......
・Ensure the necessary hardware components (Wi-Fi module, INA219 sensor) are connected and configured properly.
・Run cm_main.py to establish Wi-Fi connectivity and start sending data to the server.
・Run get_current.py to start logging voltage, current, and power measurements to a CSV file.
>>> ---ESPのWi-Fi[ESP_F]に接続��ます---
---- wifi is connected ----
----[192.168.4.2]に接続----
Wi-Fi connected
Connected to 192.168.4.1:80
Sent: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
The output CSV file