This program solves the ESP module with only simple commands.
- Upload the esp code to your ESP module (help here)
- Upload the arduino code to Arduino
- Connect the Arduino digital port 7 with the ESP GPIO0 pin, which is labeled D3!!!
- Connect the Arduino digital port 8 with the ESP GPIO2 pin, which is labeled D4!!!
- If you are not using nodeMCU but ESP-01, you will need a Bi-Directional Logic Level Converter. (this or this)
Then there is nothing more than using the program
void wifi(byte id,String data[]){}
void wifi(byte id){}
The wifi function requires a byte data, which is id, or if necessary, additional arguments. To do this, create a String array
String arg[]={"Wifi_SSID","WiFi_PASSWD"};
wifi(1,arg);
To extract response data, create a String variable and read op_read() or just print out.
String result=op_read();
OR
Serial.println(op_read());
Command Codes:(These are the first parameters of the wifi function)
1 - Connect to wifi. Return: IP address as String
String arg[]={"SSID","PASSWD"};
wifi(1,arg);
2 - still empty
3 - scan the available networks. Return: the available netowrks name, Signal Strength, and the security.
wifi(3);
4 - Check your Wifi connection Returns: Connected/Disconnected
wifi(4);
9 - Disconnect from any networks Return: ok
wifi(9);
21-Http client. Return the source code.
String arg[]={"http://vanenet.hu/"};
wifi(21,arg);
Principle of operation
I always keep the issued command in the task node named task. The first address is the id that is the ID of the command. (eg 1: connection to the network) and the second address the required arguments divided by "\t". We'll send Serial data to ESP.
The first character is converted to bytes and examined for a task with such an identifier. Then I mapped the inverter of the id, so where there was bit 1 there would be 0 bit and where 0 was 1. (eg, the id of id 1 will be 254) This will be the response identifier. We will execute the command and then add the return value of the function to the answer. We send the response back to the Arduino on the Serial port and check whether the invert value matches the issued command ID. If we return with the result, and if not, then we have already given a new command to the esp, but it has not done the previous one. In this case, read the return values into the wrong variable.
If you are interested in more interesting projects, then follow me and see my previous projects.
Comments