Authorized Retailers

Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES ここでご購入を!

Introduction

In this course, we will introduce how to use NodeMCU and UDP protocol with sensors to make a Smart Access Control System.

Preparation

Hardware:

Software:

Connection

NodeMCU I2C 1602 LCD
Vin VCC
GND GND
D1 SCL
D2 SDA

Overhere,we connect the servo to D3 pin.

Upload Sketch

Connect the NodeMCU to computer via USB cable,open this sketch by using Arduino IDE(Version1.6.4+):

Edit the code to fit your own WiFi settings as following operations:

1)Hotspot Configration:

const char* ssid = “your ssid”;
const char* password = “your password”;

Find above code line,put your own ssid and password on there.

2)UDP local port Settings

         // Define the local port to listen on
      const int udpPort = 8888;

We use the “8888” local port here, you can change it to your own local port.

3)Permission IP Settings

IPAddress allowedIP1(192, 168, 0, 5);  // first permission ip
IPAddress allowedIP2(192, 168, 0, 101);  // sencond permission ip
You can check the IP address of your allowed phone through the serial monitor, or through your router, and then replace the corresponding IP address with the above. Of course, you can increase or decrease the number of people with access.
4)I2C Adress Settings
// LCD I2C address settings
LiquidCrystal_I2C lcd(0x27, 16, 2);

*0x27 means the address of this 1602 I2C LCD display,different LCD may have different address,if the LCD do not work,please connect your 1602 I2C LCD dispaly to your NodeMCU,then upload below code to your NodeMCU, you will get the I2C address from the Serial Monitor.You can check following link to get more usages about 1602 I2C LCD dispaly  : https://osoyoo.com/2014/12/07/16×2-i2c-liquidcrystal-displaylcd/.

Boad & COM Port Settings

After do that,choose the coresponding board type and port type as below,then upload the sketch to the NodeMCU.

UDP APP Settings

About how to config the UDP APP,check this link.

Click the setting icon button, you can see the UDP configration page.

UDP response  text display enable button – Enable text play

UDP Incoming Port – Use this port to listen for and accept connections from remote UDP devices, keep the default setting : “8888”

IP address – Change it to your NodeMCU IP address, you can get the IP address from the Arduino IDE Serial Monitor after the code upload down.

Port – Keep the default setting: “8888”.

Keep other settings as default, then click the “Save” button to save your changes.

Running Result

Upload the code to Arduino board, then open Arduino Serial monitor in the upper right corner.

Now please set your Baud Rate to 115200

you will see NodeMCU IP address as following:

Above result shows your NodeMCU IP address (in my case, 192.168.0.15) , NodeMCU is now waiting for UDP message at port 8888.

Upon starting, the LCD displays “Smart home access system”. The system listens for UDP packets. When it receives an “F” character from a specified IP address, it rotates the servo motor to 90 degrees, representing the door opening. The LCD then displays a personalized welcome message for a specific user for 3 seconds. Afterward, the servo motor returns to its initial position (0 degrees), and the LCD displays “Smart home access system” again.

Open the Serial Monitoe and you will see:

If the system receives an “F” character from a different specified IP address, it follows the same process but displays a different welcome message for that user.

If the system receives data from any other IP address, it displays “NO permission” on the LCD for 3 seconds before returning to the default “Smart home access system” message.


Note:

Please make sure that you have correctly set your WiFi network name (SSID) and password in the code. Additionally, you need to change the allowed IP addresses (allowedIP1 and allowedIP2) to the actual IP addresses you want to grant access to.

Furthermore, please ensure that you have installed the required libraries in the Arduino IDE: Wire, LiquidCrystal_I2C, ESP8266WiFi, and WiFiUdp. You can search for these libraries in the “Library Manager” in the Arduino IDE and install them.

If you have any further questions or need additional assistance, please feel free to ask.


FAQ

Question A:  Why could OSOYOO IoT APP send message to NodeMCU without even knowing the IP address of the NodeMCU board?
Answer: OSOYOO IoT APP default destination IP is 192.168.1.255 which is a broadcasting IP address. Broadcasting means the APP will send UDP message to all device in the same Wifi Network.
So even we does not set destination IP address to NodeMCU IP, the NodeMCU can still get the message.

Question B: Can I change default sending destination IP address and Port Number of OSOYOO IoT APP?
Yes, you can! Osoyoo IoT APP default IP is 192.168.1.255 and default port is 8888. If you need to change the setting,
For example, if our NodeMCU IP address is 192.168.0.8, here is the guide to make the change to 192.168.0.8:
1)Open APP, click Setting button in upper right corner
2)Use  192.168.0.8 to replace default IP 192.168.1.255
3)keep default port number 8888 without changing
4)turn on the switch of UDP Response Text Display
5)Click Save button to save the changes you just made
6)Click Back Arrow to go back APP front UI

Question A:  I touched my APP F1 key, but no message shows up in my NodeMCU serial monitor, why?
Sometimes, broadcasting IP Address is not necessarily 192.168.1.255, for example, if your NodeMCU IP address is 192.168.0.8, the wifi network broadcasting IP is 192.168.0.255.
So we suggest you replace 192.168.1.255 with 192.168.0.255 to match your network mask of NodeMCU. Of course you can directly set the NodeMCU IP as destination in APP.

If you have any question about OSOYOO NodeMCU products, feel free to contact us or leave comments here.

In next lesson, we will connect a sensor to NodeMCU and send sensor data to  another  Moquitto client software.

We need to note that ESP8266 is a 2.4G WiFi device, please access to 2.4G network, if you access to a 5G router network, there will be a prompt that the network connection is not successful.

Part details

SKU:  DKRK100700

No. Picture Name Link
1 OSOYOO NodeMCU Board https://osoyoo.store/products/osoyoo-nodemcu-module-usb-c-esp8266-esp-12f-wifi-development-board-with-ch340-for-arduino-ide-micropython-includes-tutorial?variant=41099777638511
2 Servo motor https://osoyoo.store/products/sg90-micro-servo-for-arduino-raspberry-pi-robot?variant=31957821096047
3 LED(6 x White, 6 x Red, 6 x Yellow, 6x Green) https://osoyoo.store/products/led-packs-for-arduino?variant=31957816442991
4 Buzzer https://osoyoo.store/products/piezo-buzzer-module-1pcs?variant=39879251656815
5 Push Button and Hats https://osoyoo.store/products/push-button-and-hats-5pcs?variant=39879253131375
6 Photoresistor(Light Sensor) https://osoyoo.store/products/light-sensor?variant=41099777736815
7 potentiometer(10K lohm adjustable resistor) https://osoyoo.store/products/potentionmeter-10k-adjustable-resistor-3pcs?variant=39879281016943
8 I2C LCD Display (16*2) https://osoyoo.store/products/i2c-1602-lcd-display-module-blue-backlight?variant=41099777933423
9  Temperature&Humidity Sensor Module https://osoyoo.store/products/dht11-temperature-humidity-sensor?variant=41099777998959
10 Resistors https://osoyoo.store/products/osoyoo-electronic-component-kit-total-1390-pcs-led-diodes-30-values-resistors-electrolytic-capacitor-package-ceramic-capacitors-common-diodes-common-transistor?variant=29503912706166
11 4*AA Battery Holder Box https://osoyoo.store/products/battery-box-for-4pcs-aaa-battery?variant=41099778031727
12 4 LED Module https://osoyoo.store/products/4-digit-7-segment-display?variant=41099778064495
13 Stepper Motor +Bridge https://osoyoo.store/products/stepper-motor-with-driver-28byj-48-5v-dc?variant=41099778130031
14 Tlit Sensor Module https://osoyoo.store/products/sw-520d-basic-tilt-switch?variant=41099778162799
15 Infrared Remote Controller https://osoyoo.store/products/infrared-remote-controller-for-arduino-raspberry-pi-microbit-stemmodel-2019011500?variant=31958087532655