0% found this document useful (0 votes)
35 views

Arduino ESP8266 at Commands - Code - Ino at Master Oksbwn - Arduino ESP8266 at Commands GitHub

This code demonstrates how to use AT commands to connect an Arduino with an ESP8266 WiFi module. It initializes the ESP8266 by connecting to a WiFi network, then sends an HTTP GET request with parameters to a server and reads the response. The code includes functions for sending AT commands, reading responses, and handling errors or exceptions during the process.

Uploaded by

franco
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Arduino ESP8266 at Commands - Code - Ino at Master Oksbwn - Arduino ESP8266 at Commands GitHub

This code demonstrates how to use AT commands to connect an Arduino with an ESP8266 WiFi module. It initializes the ESP8266 by connecting to a WiFi network, then sends an HTTP GET request with parameters to a server and reads the response. The code includes functions for sending AT commands, reading responses, and handling errors or exceptions during the process.

Uploaded by

franco
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Arduino-ESP8266-AT-Commands/Code.ino at master · oksbwn/Arduin... https://github.com/oksbwn/Arduino-ESP8266-AT-Commands/blob/mas...

oksbwn / Arduino-ESP8266-AT-Commands Public

Code Issues Pull requests Actions Projects Security Insights

master

Arduino-ESP8266-AT-Commands / Code / Code.ino

oksbwn Updated Code History

1 contributor

117 lines (101 sloc) 3.8 KB

1 #include <SoftwareSerial.h>
2 #define WIFI_SSID "#####" //WIFI SSID
3 #define WIFI_PASS "********" // Wifi Password
4 #define SERVER_IP "192.168.0.1" // IP of the target server.
5 #define TIMEOUT 5000 //Timeout for the serial communication
6 #define CONTINUE false
7 #define HALT true
8 #define NO_OF_PARAMETERS 1 //No of parameters sending to the server.
9
10 SoftwareSerial esp8266SerialPort(10, 11); // RX, TX
11
12 void exception(String msg){ //Exception occured. Print msg and stops.
13 Serial.println(msg);
14 Serial.println("HALT");
15 while(true){
16 readResponseData("");
17 delay(60000);
18 }
19 }
20 boolean readResponseData(String keyword){ //Receive data from the serial port. Returns true if keyword ma
21 String response;
22 long deadline = millis() + TIMEOUT;
23 while(millis() < deadline){
24 if (esp8266SerialPort.available()){
25 char ch = esp8266SerialPort.read(); // Read one character from serial port and append to a string.
26 response+=ch;
27 if(keyword!=""){
28 if(response.indexOf(keyword)>0){ //Searched keyword found.
29 Serial.println(response);
30 return true;
31 }
32 }
33 }
34 }
35 Serial.println(response);
36 return false;
37 }
38
39 boolean sendCommand(String command, String acknowledgement, boolean stopOnError)
40 {
41 esp8266SerialPort.println(command);
42 if (!readResponseData(acknowledgement))
43 if (stopOnError)

1 of 3 26/03/2023, 20:38
Arduino-ESP8266-AT-Commands/Code.ino at master · oksbwn/Arduin... https://github.com/oksbwn/Arduino-ESP8266-AT-Commands/blob/mas...

44 exception(command+" Failed to execute.");


45 else
46 return false; // Let the caller handle it.
47 return true; // ack blank or ack found
48 }
49
50 boolean initializeESP8266Module(){
51 esp8266SerialPort.begin(9600);
52 esp8266SerialPort.setTimeout(TIMEOUT);
53 delay(2000);
54
55 //sendCommand("AT+RST", "ready", HALT); // Reset & test if the module is ready
56 sendCommand("AT+GMR", "OK", CONTINUE); // Retrieves the firmware ID (version number) of the module.
57 sendCommand("AT+CWMODE?","OK", CONTINUE);// Get module access mode.
58 sendCommand("AT+CWMODE=1","OK", HALT); // Station mode
59 sendCommand("AT+CIPMUX=1","OK", HALT); // Allow multiple connections (we'll only use the first).
60
61 String cmd = "AT+CWJAP=\""; cmd += WIFI_SSID; cmd += "\",\""; cmd += WIFI_PASS; cmd += "\"";
62 for(int counter=0;counter<5;counter++){
63 if (sendCommand(cmd, "OK", CONTINUE)){ // Join Access Point
64 Serial.println("Connected to WiFi.");
65 break;
66 }else if(counter==4)
67 exception("Connection to Wi-Fi failed. Please Check");
68 }
69
70 delay(5000);
71
72 sendCommand("AT+CWSAP=?", "OK", CONTINUE); // Test connection
73 sendCommand("AT+CIFSR","OK", HALT); // Echo IP address. (Firmware bug - should return "OK".)
74 }
75
76 void setup()
77 {
78 Serial.begin(9600);
79 Serial.println("ESP8266 Demo");
80 initializeESP8266Module();
81 Serial.println("Module is ready.");
82 }
83
84 void loop()
85 {
86 String cmd = "AT+CIPSTART=0,\"TCP\",\""; cmd += SERVER_IP; cmd += "\",80"; //Start a TCP connection. t
87 if (!sendCommand(cmd, "OK", CONTINUE))
88 return;
89 delay(2000);
90
91 if (!sendCommand("AT+CIPSTATUS", "OK", CONTINUE))// Check for TCP Connection status.
92 return;
93
94 String tag[NO_OF_PARAMETERS]={"NAME"}; //Tags for the parameters
95 String data[NO_OF_PARAMETERS]={"my Name"}; //Values for the parameters
96
97 cmd = "GET /ESP8266/index.php?";//Path to the target server file.
98 for(int count=0;count<NO_OF_PARAMETERS;count++){
99 if(count!=0)
100 cmd+="&";
101 cmd+=tag[count];
102 cmd+="="+data[count];
103 }
104 Serial.println(cmd);
105 cmd+=" HTTP/1.1\r\nHost: "; cmd += SERVER_IP; cmd += ":80\r\n\r\n";

2 of 3 26/03/2023, 20:38
Arduino-ESP8266-AT-Commands/Code.ino at master · oksbwn/Arduin... https://github.com/oksbwn/Arduino-ESP8266-AT-Commands/blob/mas...

106 if (!sendCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE)){


107 sendCommand("AT+CIPCLOSE", "", CONTINUE);
108 Serial.println("Connection timeout.");
109 return;
110 }
111
112 sendCommand(cmd, "OK", CONTINUE);// Send data to server.
113
114 readResponseData("");// Read response data received from server.
115 exception("ONCE ONLY");
116 }
117

3 of 3 26/03/2023, 20:38

You might also like