Page 364 - ARM Based Microcontroller Projects Using MBED
P. 364
350 14. ADVANCED PROJECTS
{
char Buffer[80]; // Buffer to store data
esp.baud(115200); // Set baud rate to 115200
ConnectToWiFi(); // Connect to Wi-Fi
// Receive data packets over UDP and activate/de-activate the
// required relay. Function strstr(a,"b") returns a value greater than
// 0 if string a contains sub-string "b". The command sent by the
// mobile devuce must be terminated with an Enter key (required for
// the scanf function). Buffer is cleared at the beginning of the loop
//
while(1)
{
for(int k = 0; k < 5; k++)Buffer[k] = 0;
esp.scanf("%s",&Buffer);
if(strstr(Buffer, "ON1") > 0) Relay1 = 0;
else if(strstr(Buffer,"ON2") > 0) Relay2 = 0;
else if(strstr(Buffer, "ON3")> 0) Relay3 = 0;
else if(strstr(Buffer, "ON4") > 0) Relay4 = 0;
else if(strstr(Buffer, "OFF1") > 0)Relay1 = 1;
else if(strstr(Buffer, "OFF2") > 0)Relay2 = 1;
else if(strstr(Buffer, "OFF3") > 0)Relay3 = 1;
else if(strstr(Buffer, "OFF4") > 0)Relay4 = 1;
}
}
FIG. 14.18, CONT’D
Function ConnectToWiFi sends the following commands to the ESP-01 to connect to
the Wi-Fi: Notice that small delays are used after each command. Command AT+CWJAP
requires longer delay.
14.4.8 Testing the Program
The program can easily be tested with a mobile phone. You should install a UDP Server
apps on your Android mobile phone before starting the test. There are many freely available
UDP apps in the Play Store. The one installed and used in this project is called the UDP
Sender by hastarin (hastarin@gmail.com) as shown in Fig. 14.19.
The steps to test the program are as follows:
• Construct the circuit.
• Compile and download the program to your development board.
• Start the UDP Server apps on your mobile phone and set the Port number to 5000 and the
destination IP address (ESP-01 IP address, which was 192.168.1.160 in this project). Enter
command ON1 followed by the Enter key (required for the scanf function) on the mobile
phone. Click Send to send the command to the development board. Relay 1 should now
become active and the red LED corresponding to this relay will turn ON. Fig. 14.20 shows
the command entered on the mobile phone.