AmebaMotors - Use Ameba as Server to Control Motors
Introduction to AmebaMotors
AmebaMotors is a library which provides API related to controlling motors.
Please download the library: AmebaMotors(https://github.com/ambiot/ambd_arduino/raw/master/Arduino_zip_libraries/AmebaMotors.zip)
And add the library to Ameba: https://www.arduino.cc/en/Guide/Libraries#toc4
• L298N H-Bridge x 1
• 4-wheel motorcar or 2-wheel motorcar+Universal wheel
In this example, we connect Ameba to WiFi and use Ameba as server, the user can control a 4-wheel/2-wheel motorcar through a webpage.
First, connect Ameba to the L298N H-Bridge and the motorcar.
To know more about motor movement and the technical details of the L298N H-Bridge, please check out this link https://www.amebaiot.com/en/ameba-arduino-amebamotors-basic/.
Open the example, “Files” -> “Examples” -> “AmebaWiFi” -> “WiFiControlCar”.
You will see we use the following pins in the example:
• We connect Ameba 5V to L298N +12V to supply power. However, not every L298N accepts 5V power supply, if this does not work, please connect L298N +12V to other power supply (e.g., +12V) and use L298N +5V to supply power to Ameba.
• The correct wiring of the motor depends on each model (may be opposite). Please run the test program first, make sure it runs correctly before assembling the motorcar.
• For convenience purposes, it’s recommended to use Dupont line to organize the wiring of motors and L298N.
Every time you modify your program, please remember to unplug the power of L298N to avoid the motor running unexpectedly. Connect Ameba to power, upload the program, and then connect L298N to power when you are going to test the program.
Then, upload the code to Ameba
In the sample code, modify the highlighted snippet to corresponding information.
Upload the code and press the reset button on Ameba. When the connection is established, you will see the message “To see this page in action, open a browser to http://xxx.xxx.xxx.xxx” in the Arduino IDE, as shown in the figure:
Next, open the browser of a computer or a cell phone under the same WiFi domain, enter the address in the message.
In the webpage, you can press the corresponding button to control the motor car in any of the 4 directions.
Use WiFi.begin() to establish WiFi connection.
To get the information of a WiFi connection:
Use WiFi.SSID() to get SSID of the current connected network.
Use WiFi.RSSI() to get the signal strength of the connection.
Use WiFi.localIP() to get the IP address of Ameba.
Use WiFiServer server() to create a server that listens on the specified port.
Use server.begin() to tell the server to begin listening for incoming connections.
Use server.available() to get a client that is connected to the server and has data available for reading.
Use client.connected to get whether or not the client is connected.
Use client.println() to print data followed by a carriage return and newline.
Use client.print() to print data to the server that a client is connected to.
Use client.available() to return the number of bytes available for reading.
Use client.read() to read the next byte received from the server the client is connected to.
Use client.stop() to disconnect from the server the client is connected to.