In this lesson, we will connect a switch button to the NodeMCU ,and send the switch status to OSOYOO IoT APP. When the button is pressed, NodeMCU will send the button status “pressed” to UDP APP. When the push button is released, “not pressed” will be sent.
Medium-sized clicky momentary switches are standard input “buttons” on electronic projects. These work best in a PCB but can be used on a solderless breadboard as shown in this tutorial. The pins are normally open (disconnected) and when the button is pressed they are momentarily closed.
Hardware:
Software:
In this lesson,we use D2(GPIO4) to control the switch,the connection is as below:
Note: This 1 k resistor is using as a pull down resistor, In such a circuit, when the switch is closed, the NodeMCU input is at a logical high value, but when the switch is open, the pull-down resistor pulls the input voltage down to ground (logical zero value), preventing an undefined state at the input.
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:
WiFi.begin(“your_SSID”, “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
unsigned int localPort = 8888;
We use the “8888” local port here, you can change it to your own local port.
3)Boad & COM Port Settings
After do that,choose the coresponding board type and port type as below,then upload the sketch to the NodeMCU.
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.
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.19) , NodeMCU is now waiting for UDP message at port 8888.
Onece the upload done,if wifi hotspot name and password setting is ok and UDP APP is connected, open the Serial Monitor,you will see following result:Keep pressing this button,the Serial Monitor will output “Button pressed” every 0.1 second;once release this button,the Serial Monitor will output “Button not pressed” every 0.1 second.
At the same time,open the UDP APP ,press any function button on the APP(F1 – F8…),
You will see the button status as below:
Press the button and click on the delete button to the right of the text dispkay box, you will see the latest status of the button.
In this modified code, we are manually assigning a remote IP address by creating an IPAddress
object with the appropriate IP address, and passing this object to the Udp.beginPacket()
function to specify the destination IP address for the UDP packet.
// Begin the UDP packet with destination IP and port
Udp.beginPacket(“your_UDP_APP_IP”, 8888);
You need to replace your_UDP_APP_IP with your phone’s IP address
This code checks for incoming data on the UDP connection using the parsePacket() method. If there is data available, it reads the first byte (assumed to be a character) using the read() method. If the received character is “F”(F1 button on the UDP APP control page), it then checks the state of the button and sends a UDP packet with the appropriate message.
// Begin the UDP packet with destination IP and port
Udp.beginPacket(“your_UDP_APP_IP”, 8888);
You need to replace your_UDP_APP_IP with your phone’s IP address
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.
SKU: DKRK100700
DownLoad Url osoyoo.com