An Arduino over USB over WiFi: the board runs a small web server, and the laptop sends it ON and OFF in HTTP requests with ArduinoConnectorWIFIBase. The board answers, and the example prints the answer.
What you need
- an Arduino UNO R4 WiFi, or another board that the WiFiS3 library supports;
- the sketch in the comment at the top of arduino_wifi.cpp, with your network's name and password in ssid and password. It joins the network, prints its address on the serial monitor and listens on port 8005;
- the laptop on the same network.
Running the example
Put the board's address in the code, http://192.168.0.70:8005 in the example, and run
Type ON or OFF and Enter; the board switches its LED and answers LED is ON or LED is OFF. e switches it off and stops.
The code
#include <iostream>
#include <chrono>
#include <thread>
{
struct ArduinoONCMD : public ArduinoCMDBase
{
virtual std::string
get_cmd() const final;
};
std::
string ArduinoONCMD::
get_cmd()
const {
return "ON"; }
{
virtual std::string get_cmd() const final;
};
std::string ArduinoOFFCMD::get_cmd() const { return "OFF"; }
}
{
using namespace example;
ArduinoConnectorWIFIBase connector("http://192.168.0.70:8005");
ArduinoONCMD on_cmd;
ArduinoOFFCMD off_cmd;
std::string user_input;
while (true)
{
std::cout << "Enter command ON/OFF or e (to exit): ";
std::getline(std::cin, user_input);
if (user_input == "e")
{
connector.send_cmd(off_cmd);
break;
}
if (user_input == "ON")
{
auto response = connector.send_cmd(on_cmd);
std::cout << "Arduino response: " << response << std::endl;
}
if (user_input == "OFF")
{
auto response = connector.send_cmd(off_cmd);
std::cout << "Arduino response: " << response << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
Definition arduino_connector_wifi_base.h:15
int main()
Definition discounted_returns.cpp:19
Definition pytorch_linear_regression.cpp:17
Definition arduino_connector_base.h:18
virtual std::string get_cmd() const final
Definition arduino_usb.cpp:45