|
| 1 | +Connecting with Arduino over WiFi |
| 2 | +======================================== |
| 3 | + |
| 4 | +.. code-block:: |
| 5 | +
|
| 6 | + #include <WiFiS3.h> |
| 7 | +
|
| 8 | + char ssid[] = "YOU-SSID"; |
| 9 | + char password[] = "YOUR-PASSWORD"; |
| 10 | +
|
| 11 | + int status = WL_IDLE_STATUS; |
| 12 | + WiFiServer server(8005); |
| 13 | +
|
| 14 | +
|
| 15 | + void setup() { |
| 16 | +
|
| 17 | + pinMode(LED_BUILTIN, OUTPUT); |
| 18 | +
|
| 19 | + Serial.begin(9600); |
| 20 | + delay(1000); |
| 21 | + if (Serial.available()) { |
| 22 | + Serial.println("Connecting to WiFi..."); |
| 23 | + } |
| 24 | +
|
| 25 | + while (status != WL_CONNECTED) { |
| 26 | + Serial.print("Connecting to "); |
| 27 | + Serial.println(ssid); |
| 28 | + status = WiFi.begin(ssid, password); |
| 29 | + delay(5000); |
| 30 | + } |
| 31 | +
|
| 32 | + Serial.println("\nWiFi connected."); |
| 33 | + Serial.print("IP address: "); |
| 34 | + Serial.println(WiFi.localIP()); |
| 35 | +
|
| 36 | + server.begin(); |
| 37 | + Serial.println("HTTP server started"); |
| 38 | +
|
| 39 | + } |
| 40 | +
|
| 41 | + void loop() { |
| 42 | +
|
| 43 | + WiFiClient client = server.available(); |
| 44 | +
|
| 45 | + if (client) { |
| 46 | + //client.flush(); |
| 47 | + Serial.println("Client connected."); |
| 48 | + String request = ""; |
| 49 | + while (client.connected()) { |
| 50 | + if (client.available()) { |
| 51 | + char c = client.read(); |
| 52 | + request += c; |
| 53 | + if (c == '\n' && request.endsWith("\r\n\r\n")) { |
| 54 | + break; // End of HTTP headers |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + String body = ""; |
| 60 | + while (client.available()) { |
| 61 | + body += (char)client.read(); |
| 62 | + } |
| 63 | +
|
| 64 | + if(body == "ON"){ |
| 65 | + digitalWrite(LED_BUILTIN, HIGH); |
| 66 | + String response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nLED is ON\n"; |
| 67 | + client.print(response); |
| 68 | + } |
| 69 | + else if(body == "OFF"){ |
| 70 | + digitalWrite(LED_BUILTIN, LOW); |
| 71 | + String response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nLED is OFF\n"; |
| 72 | + client.print(response); |
| 73 | +
|
| 74 | + } |
| 75 | +
|
| 76 | + delay(1); |
| 77 | + client.stop(); |
| 78 | + Serial.println("Client disconnected."); |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | +.. code-block:: |
| 83 | +
|
| 84 | + #include "rlenvs/boards/arduino/arduino_connector_wifi_base.h" |
| 85 | + #include <iostream> |
| 86 | +
|
| 87 | + namespace example |
| 88 | + { |
| 89 | + using rlenvscpp::boards::arduino::ArduinoCMDBase; |
| 90 | + using rlenvscpp::boards::arduino::ArduinoConnectorWIFIBase; |
| 91 | +
|
| 92 | + struct ArduinoONCMD: public ArduinoCMDBase |
| 93 | + { |
| 94 | + virtual std::string get_cmd()const final; |
| 95 | + }; |
| 96 | +
|
| 97 | + std::string ArduinoONCMD::get_cmd() const |
| 98 | + { |
| 99 | + return "ON"; |
| 100 | + } |
| 101 | +
|
| 102 | + struct ArduinoOFFCMD: public ArduinoCMDBase |
| 103 | + { |
| 104 | + virtual std::string get_cmd()const final; |
| 105 | + }; |
| 106 | +
|
| 107 | + std::string ArduinoOFFCMD::get_cmd() const |
| 108 | + { |
| 109 | + return "OFF"; |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + int main() { |
| 114 | +
|
| 115 | +
|
| 116 | + using namespace example; |
| 117 | +
|
| 118 | + ArduinoConnectorWIFIBase connector("http://192.168.0.70:8005"); |
| 119 | + ArduinoONCMD on_cmd; |
| 120 | + ArduinoOFFCMD off_cmd; |
| 121 | +
|
| 122 | + std::string user_input; |
| 123 | + while (true) |
| 124 | + { |
| 125 | + std::cout << "Enter command ON/OFF or e (to exit): "; |
| 126 | + std::getline(std::cin, user_input); |
| 127 | +
|
| 128 | + if (user_input == "e") { |
| 129 | + connector.send_cmd(off_cmd); |
| 130 | + break; |
| 131 | + } |
| 132 | +
|
| 133 | + if (user_input == "ON") |
| 134 | + { |
| 135 | + auto response = connector.send_cmd(on_cmd); |
| 136 | + std::cout<<"Arduino response: "<<response<<std::endl; |
| 137 | + } |
| 138 | +
|
| 139 | + if (user_input == "OFF") |
| 140 | + { |
| 141 | + auto response = connector.send_cmd(off_cmd); |
| 142 | + std::cout<<"Arduino response: "<<response<<std::endl; |
| 143 | + } |
| 144 | +
|
| 145 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 146 | + } |
| 147 | +
|
| 148 | + return 0; |
| 149 | + } |
| 150 | +
|
| 151 | +
|
| 152 | +
|
| 153 | +References |
| 154 | +---------- |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
0 commit comments