Luis Silvio Cordova Rivadeneira
/
UPSM_W3_ESP8266
Comandos AT para modulo ESP8266
main.cpp@0:b9712fa89244, 2020-02-14 (annotated)
- Committer:
- lscordovar
- Date:
- Fri Feb 14 00:31:34 2020 +0000
- Revision:
- 0:b9712fa89244
Ejemplo de configuracion del modulo ESP8266
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lscordovar | 0:b9712fa89244 | 1 | /********************************************************************** |
lscordovar | 0:b9712fa89244 | 2 | ESP-01 BOARD RELAY CONTROL |
lscordovar | 0:b9712fa89244 | 3 | ========================== |
lscordovar | 0:b9712fa89244 | 4 | AT+RST Reset ESP-01 |
lscordovar | 0:b9712fa89244 | 5 | AT+CWMODE Set ESP-01 mode (here it is set to Station mode) |
lscordovar | 0:b9712fa89244 | 6 | AT+CWJAP Set Wi-Fi ssid name and password |
lscordovar | 0:b9712fa89244 | 7 | AT+CPIMUX Set connection mode (here it is set to multiple connection) |
lscordovar | 0:b9712fa89244 | 8 | AT+CIFSR Returns the IP address (not used here) |
lscordovar | 0:b9712fa89244 | 9 | AT+CIPSTART Set TCP or UDP connection mode, destination IP address, and port number (here, UDP is used |
lscordovar | 0:b9712fa89244 | 10 | with port number set to 5000. Destination IP address is set to “0.0.0.0” so that any device can |
lscordovar | 0:b9712fa89244 | 11 | connect and send data over Port 5000). |
lscordovar | 0:b9712fa89244 | 12 | |
lscordovar | 0:b9712fa89244 | 13 | |
lscordovar | 0:b9712fa89244 | 14 | Testing the Program |
lscordovar | 0:b9712fa89244 | 15 | The program can easily be tested with a mobile phone. You should install this app in the Play Store |
lscordovar | 0:b9712fa89244 | 16 | UDPSender by hastarin |
lscordovar | 0:b9712fa89244 | 17 | |
lscordovar | 0:b9712fa89244 | 18 | The steps to test the program are as follows: |
lscordovar | 0:b9712fa89244 | 19 | • Construct the circuit. |
lscordovar | 0:b9712fa89244 | 20 | • Compile and download the program to your development board. |
lscordovar | 0:b9712fa89244 | 21 | • Start the UDP Server apps on your mobile phone and set the Port number to 5000 and the |
lscordovar | 0:b9712fa89244 | 22 | destination IP address (ESP-01 IP address, which was 192.168.1.160 in this project). |
lscordovar | 0:b9712fa89244 | 23 | |
lscordovar | 0:b9712fa89244 | 24 | Enter command ON1 followed by the Enter key (required for the scanf function) on the mobile |
lscordovar | 0:b9712fa89244 | 25 | phone. Click Send to send the command to the development board. Relay 1 should now |
lscordovar | 0:b9712fa89244 | 26 | become active and the red LED corresponding to this relay will turn ON |
lscordovar | 0:b9712fa89244 | 27 | |
lscordovar | 0:b9712fa89244 | 28 | **********************************************************************/ |
lscordovar | 0:b9712fa89244 | 29 | #include "mbed.h" |
lscordovar | 0:b9712fa89244 | 30 | // |
lscordovar | 0:b9712fa89244 | 31 | // Define the expansion board to development board UART connections |
lscordovar | 0:b9712fa89244 | 32 | // |
lscordovar | 0:b9712fa89244 | 33 | #define TX PB_6//PA_9 // UART TX |
lscordovar | 0:b9712fa89244 | 34 | #define RX PB_7//PA_10 // UART RX |
lscordovar | 0:b9712fa89244 | 35 | //Serial esp(TX, RX); // UART TX,RX |
lscordovar | 0:b9712fa89244 | 36 | Serial esp(PA_2,PA_3); |
lscordovar | 0:b9712fa89244 | 37 | //Serial esp(PB_6,PB_7); // tx, rx |
lscordovar | 0:b9712fa89244 | 38 | // |
lscordovar | 0:b9712fa89244 | 39 | // Configure all relays as outputs and de-activate all relays |
lscordovar | 0:b9712fa89244 | 40 | // |
lscordovar | 0:b9712fa89244 | 41 | DigitalOut Relay1(PC_0, 1); |
lscordovar | 0:b9712fa89244 | 42 | DigitalOut Relay2(PC_1, 1); |
lscordovar | 0:b9712fa89244 | 43 | DigitalOut Relay3(PC_2, 1); |
lscordovar | 0:b9712fa89244 | 44 | DigitalOut Relay4(PC_3, 1); |
lscordovar | 0:b9712fa89244 | 45 | // |
lscordovar | 0:b9712fa89244 | 46 | // This function connects to the Wi-Fi |
lscordovar | 0:b9712fa89244 | 47 | // |
lscordovar | 0:b9712fa89244 | 48 | void ConnectToWiFi() |
lscordovar | 0:b9712fa89244 | 49 | { |
lscordovar | 0:b9712fa89244 | 50 | esp.printf("AT+RST\r\n"); |
lscordovar | 0:b9712fa89244 | 51 | wait(2); |
lscordovar | 0:b9712fa89244 | 52 | |
lscordovar | 0:b9712fa89244 | 53 | esp.printf("AT+CWMODE=1\r\n"); |
lscordovar | 0:b9712fa89244 | 54 | wait(3); |
lscordovar | 0:b9712fa89244 | 55 | |
lscordovar | 0:b9712fa89244 | 56 | esp.printf("AT+CWJAP=\"BTHomeSpot-XNH\",\"49346abaeb\"\r\n"); |
lscordovar | 0:b9712fa89244 | 57 | wait(10); |
lscordovar | 0:b9712fa89244 | 58 | |
lscordovar | 0:b9712fa89244 | 59 | esp.printf("AT+CPIMUX=1\r\n"); |
lscordovar | 0:b9712fa89244 | 60 | wait(3); |
lscordovar | 0:b9712fa89244 | 61 | |
lscordovar | 0:b9712fa89244 | 62 | esp.printf("AT+CIFSR\r\n"); |
lscordovar | 0:b9712fa89244 | 63 | wait(3); |
lscordovar | 0:b9712fa89244 | 64 | |
lscordovar | 0:b9712fa89244 | 65 | esp.printf("AT+CIPSTART=\"UDP\",\"192.168.1.178\",0,5000,2\r\n"); |
lscordovar | 0:b9712fa89244 | 66 | wait(3); |
lscordovar | 0:b9712fa89244 | 67 | } |
lscordovar | 0:b9712fa89244 | 68 | int main() |
lscordovar | 0:b9712fa89244 | 69 | { |
lscordovar | 0:b9712fa89244 | 70 | char Buffer[80]; // Buffer to store data |
lscordovar | 0:b9712fa89244 | 71 | esp.baud(115200); // Set baud rate to 115200 |
lscordovar | 0:b9712fa89244 | 72 | ConnectToWiFi(); // Connect to Wi-Fi |
lscordovar | 0:b9712fa89244 | 73 | // Receive data packets over UDP and activate/de-activate the |
lscordovar | 0:b9712fa89244 | 74 | // required relay. Function strstr(a,"b") returns a value greater than |
lscordovar | 0:b9712fa89244 | 75 | // 0 if string a contains sub-string "b". The command sent by the |
lscordovar | 0:b9712fa89244 | 76 | // mobile devuce must be terminated with an Enter key (required for |
lscordovar | 0:b9712fa89244 | 77 | // the scanf function). Buffer is cleared at the beginning of the loop |
lscordovar | 0:b9712fa89244 | 78 | // |
lscordovar | 0:b9712fa89244 | 79 | while(1) { |
lscordovar | 0:b9712fa89244 | 80 | for(int k = 0; k < 5; k++)Buffer[k] = 0; |
lscordovar | 0:b9712fa89244 | 81 | |
lscordovar | 0:b9712fa89244 | 82 | esp.scanf("%s",&Buffer); |
lscordovar | 0:b9712fa89244 | 83 | if(strstr(Buffer, "ON1") > 0) Relay1 = 0; |
lscordovar | 0:b9712fa89244 | 84 | else if(strstr(Buffer,"ON2") > 0) Relay2 = 0; |
lscordovar | 0:b9712fa89244 | 85 | else if(strstr(Buffer, "ON3")> 0) Relay3 = 0; |
lscordovar | 0:b9712fa89244 | 86 | else if(strstr(Buffer, "ON4") > 0) Relay4 = 0; |
lscordovar | 0:b9712fa89244 | 87 | else if(strstr(Buffer, "OFF1") > 0)Relay1 = 1; |
lscordovar | 0:b9712fa89244 | 88 | else if(strstr(Buffer, "OFF2") > 0)Relay2 = 1; |
lscordovar | 0:b9712fa89244 | 89 | else if(strstr(Buffer, "OFF3") > 0)Relay3 = 1; |
lscordovar | 0:b9712fa89244 | 90 | else if(strstr(Buffer, "OFF4") > 0)Relay4 = 1; |
lscordovar | 0:b9712fa89244 | 91 | } |
lscordovar | 0:b9712fa89244 | 92 | } |