bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
An Arduino over WiFi

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

./arduino_wifi

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

//
// Created by alex on 7/5/25.
//
#include <iostream>
#include <chrono>
#include <thread>
/*
* // Code for Arduino
*
#include <WiFiS3.h>
char ssid[] = "YOU-SSID";
char password[] = "YOUR-PASSWORD";
int status = WL_IDLE_STATUS;
WiFiServer server(8005);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
delay(1000);
if (Serial.available()) {
Serial.println("Connecting to WiFi...");
}
while (status != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(ssid);
status = WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nWiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("HTTP server started");
}
void loop() {
WiFiClient client = server.available();
if (client) {
//client.flush();
Serial.println("Client connected.");
String request = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
if (c == '\n' && request.endsWith("\r\n\r\n")) {
break; // End of HTTP headers
}
}
}
String body = "";
while (client.available()) {
body += (char)client.read();
}
if(body == "ON"){
digitalWrite(LED_BUILTIN, HIGH);
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nLED is ON\n";
client.print(response);
}
else if(body == "OFF"){
digitalWrite(LED_BUILTIN, LOW);
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nLED is OFF\n";
client.print(response);
}
delay(1);
client.stop();
Serial.println("Client disconnected.");
}
}
*/
namespace example
{
struct ArduinoONCMD : public ArduinoCMDBase
{
virtual std::string get_cmd() const final;
};
std::string ArduinoONCMD::get_cmd() const { return "ON"; }
struct ArduinoOFFCMD : public ArduinoCMDBase
{
virtual std::string get_cmd() const final;
};
std::string ArduinoOFFCMD::get_cmd() const { return "OFF"; }
} // namespace example
int main()
{
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