Page 362 - ARM Based Microcontroller Projects Using MBED
P. 362

348                             14. ADVANCED PROJECTS

                               BEGIN/MAIN
                                     Define ESP-01 UART interface
                                     Configure all relays as outputs and de-activate them
                                     Set ESP-01 baud rate to 115200
                                     CALL ConnectToWiFi
                                     DO FOREVER
                                           IF command is ONx THEN
                                                 Activate relay x
                                           ELSE IF comman is OFFx THEN
                                                 De-activate relayx
                                           ENDIF
                                     ENDDO
                               END/MAIN


                               BEGIN/ConnectToWiFi
                                     Send commands to ESP-01 to connect to Wi-Fi
                               END/ConnectToWiFi
                 FIG. 14.17  Program PDL.

                 14.4.7 Program Listing

                   The program listing (program: esp) is shown in Fig. 14.18. At the beginning of the program
                 Nucleo-F411RE development board UART1 TX and UART1 RX pins are assigned to esp pins
                 RX and TX, respectively. Then, the GPIO ports connected to the relays are configured as dig-
                 ital outputs and all the relays are deactivated. The baud rate of the UART is set to 115200
                 which is the default value for the ESP-01. Function ConnectToWiFi is called to connect to
                 the Wi-Fi. The remainder of the program runs in an endless loop formed using a while state-
                 ment. Inside this loop data is received from the mobile device and the relay is controlled ac-
                 cordingly. Commands ONx activate relay x. Similarly, commands OFFx deactivates relay x.
                 Function strstr looks for a substring in a string and returns a nonzero value if the substring is
                 found. The reason for using the strstr function is because the data received from the mobile
                 device is in the following format +ID0,n: data (e.g., +ID0,3:ON1) where 0 is the link ID and n
                 is the number of characters received. Using the strstr function we can easily search for the
                 strings ONx or OFFx. Notice that logic HIGH deactivates a relay and logic LOW activates it.



                 AT+RST        Reset ESP-01
                 AT+CWMODE     Set ESP-01 mode (here it is set to Station mode)
                 AT+CWJAP      Set Wi-Fi ssid name and password
                 AT+CPIMUX     Set connection mode (here it is set to multiple connection)
                 AT+CIFSR      Returns the IP address (not used here)
                 AT+CIPSTART   Set TCP or UDP connection mode, destination IP address, and port number (here, UDP is used
                               with port number set to 5000. Destination IP address is set to “0.0.0.0” so that any device can
                               connect and send data over Port 5000).
   357   358   359   360   361   362   363   364   365   366   367