Lab 03_Python NodeMCU Serial Communication
Lab 03_Python NodeMCU Serial Communication
▪ In parallel communication, where many bits are sent at the same time.
Serial Communication
▪ The baud rate specifies how fast the data is sent over the bus and it is
specified in bits-per-second or bps.
▪ You can actually choose any speed for the baud rate.
▪ However, there are specific values that are known as industry standards.
▪ The most common and widely-used standardized value is 9600.
Serial.begin(9600);
▪ In the serial port context, “9600 baud” means that the serial port is capable
of transferring a maximum of 9600 bits per second.
▪ Other standard baud rates include: 1200, 2400, 4800, 19200, 38400,
57600 and 115200.
UART Protocol: Transmitter and Receiver
▪ When device A wants to transmit data to device B, it will share data via its
transmitter’s pin and device B receiver will receive the sent data.
UART1 UART2
UART Protocol: Transmitter and Receiver
UART1 UART2
UART Protocol: Data Packet
8-bit integral RH
8-bit decimal RH
Criteria Description
Operating Voltage 3.3V to 5.5V
Communication Serial
Output Signal Digital
Temperature Range 0°C to 50°C
Temperature Accuracy ±2°C
Humidity Range 20% to 90%
Humidity Accuracy ±5%
Refresh Rate ~ 2 seconds
DHT11: Pinout
VCC S GND
DHT11: Installing Library
▪ NodeMCU ESP8266
▪ DHT11 Sensor
▪ Jumpers
▪ Breadboard
DHT11: NodeMCU ESP8266 Pinout
PIN GPIO Why Not Safe?
HIGH at boot
D0 GPIO16
Used to wake up from deep sleep
D1 GPIO5 -
D2 GPIO4 -
Connected to FLASH button
D3 GPIO0
Boot fails if pulled LOW
HIGH at boot
D4 GPIO2
Boot fails if pulled LOW
D5 GPIO14 -
D6 GPIO12 -
D7 GPIO13 -
Required for boot
D8 GPIO15
Boot fails if pulled HIGH
DHT11: Circuit
DHT11: Steps
void setup() {
Serial.begin(9600); // Start serial monitor
dht.begin(); // Start DHT sensor
}
void loop() {
delay(2000); // Wait a few seconds between measurements
// Print temperature
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("°C ");
// Print humidity
Serial.print("Humidity: ");
Serial.print(h);
Serial.println("%");
}
NodeMCU & Python Serial Communication
NodeMCU & Python Serial Communication
void setup() {
Serial.begin(9600); // Start serial monitor
dht.begin(); // Start DHT sensor
}
void loop() {
delay(2000); // Wait a few seconds between measurements
// Print temperature
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("°C ");
// Print humidity
Serial.print("Humidity: ");
Serial.print(h);
Serial.println("%");
}
NodeMCU & Python Serial Communication: Python Program
# Import the PySerial library for serial communication
import serial
try:
while True:
# Check if there is data available in the input buffer
if ser.in_waiting > 0:
# Read all bytes until a newline character is detected
line = ser.readline()
# Print data
print(line)
except:
# Close the serial connection
ser.close()
print("Serial connection closed.")
NodeMCU & Python Serial Communication: Output
Python & NodeMCU Serial Communication
Python & NodeMCU Serial Communication: Circuit
Python & NodeMCU Serial Communication: Python Program
# Import the PySerial library for serial communication
import serial
try:
while True:
# Get command from the user
cmd = input('Enter the command: ')
void setup() {
Serial.begin(9600); // Start serial monitor
pinMode(LED_PIN, OUTPUT); // Initialize the pin D6 as an output
}
void loop() {
// Read the incoming byte if available
if(Serial.available()){ // Check if there is a message available
char cmd = Serial.read(); // Read the incoming byte
try:
while True:
try:
# Capture audio from the microphone for 2 seconds
with sr.Microphone() as source:
print("Say something.")
audio = recognizer.listen(source, phrase_time_limit=2)
void setup() {
Serial.begin(9600); // Start serial monitor
pinMode(LED_PIN, OUTPUT); // Initialize the pin D6 as an output
}
void loop() {
// Read the incoming byte if available
if(Serial.available()){ // Check if there is a message available
char cmd = Serial.read(); // Read the incoming byte