1
+ /* This example shows how to create WiFi Access point
2
+ * and control built-in LED.
3
+ *
4
+ * Created 30/01/2022
5
+ * By Augmented Robotics
6
+ *
7
+ * Check out https://roboheart.de/en_gb/ for more information about RoboHeart.
8
+ */
9
+
10
+
11
+ #include < RoboHeart.h>
12
+ #include < WiFi.h>
13
+
14
+ #define ROBOHEART_AP_SSID " RoboHeart WiFi"
15
+ #define ROBOHEART_AP_PASSWORD " Hercules4Makers"
16
+
17
+ WiFiServer server (80 );
18
+ // Current time
19
+ unsigned long currentTime = millis();
20
+ // Previous time
21
+ unsigned long previousTime = 0 ;
22
+ // Define timeout time in milliseconds (example: 2000ms = 2s)
23
+ const long timeoutTime = 2000 ;
24
+
25
+ void setup ()
26
+ {
27
+ Serial.begin (115200 );
28
+ Serial.println (" \n Creating AP." );
29
+ WiFi.mode (WIFI_AP);
30
+ WiFi.softAP (ROBOHEART_AP_SSID, ROBOHEART_AP_PASSWORD);
31
+ Serial.print (" AP Created with IP Gateway " );
32
+ Serial.println (WiFi.softAPIP ());
33
+
34
+ // Configure the LED pin
35
+ pinMode (LED_ROBOHEART, OUTPUT);
36
+ server.begin ();
37
+ }
38
+
39
+ void loop (){
40
+ WiFiClient client = server.available (); // Listen for incoming clients
41
+ String header;
42
+ if (client) {
43
+ currentTime = millis ();
44
+ previousTime = currentTime;
45
+ Serial.println (" New Client." ); // print a message out in the serial port
46
+ String currentLine = " " ; // make a String to hold incoming data from the client
47
+ while (client.connected () && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
48
+ currentTime = millis ();
49
+ if (client.available ()) { // if there's bytes to read from the client,
50
+ char c = client.read (); // read a byte, then
51
+ Serial.write (c); // print it out the serial monitor
52
+ header += c;
53
+ if (c == ' \n ' ) { // if the byte is a newline character
54
+ // if the current line is blank, you got two newline characters in a row.
55
+ // that's the end of the client HTTP request, so send a response:
56
+ if (currentLine.length () == 0 ) {
57
+ // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
58
+ // and a content-type so the client knows what's coming, then a blank line:
59
+ client.println (" HTTP/1.1 200 OK" );
60
+ client.println (" Content-type:text/html" );
61
+ client.println (" Connection: close" );
62
+ client.println ();
63
+
64
+ // turns the GPIOs on and off
65
+ if (header.indexOf (" GET /LED_ROBOHEART/on" ) >= 0 ) {
66
+ Serial.println (" LED_ROBOHEART on" );
67
+ digitalWrite (LED_ROBOHEART, HIGH);
68
+ } else if (header.indexOf (" GET /LED_ROBOHEART/off" ) >= 0 ) {
69
+ Serial.println (" LED_ROBOHEART off" );
70
+ digitalWrite (LED_ROBOHEART, LOW);
71
+ }
72
+
73
+ // Display the HTML web page
74
+ client.println (" <!DOCTYPE html><html>" );
75
+ client.println (" <head><meta name=\" viewport\" content=\" width=device-width, initial-scale=1\" >" );
76
+ client.println (" <link rel=\" icon\" href=\" data:,\" >" );
77
+ // CSS to style the on/off buttons
78
+ // Feel free to change the background-color and font-size attributes to fit your preferences
79
+ client.println (" <style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}" );
80
+ client.println (" .button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;" );
81
+ client.println (" text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}" );
82
+ client.println (" .button2 {background-color: #555555;}</style></head>" );
83
+
84
+ // Web Page Heading
85
+ client.println (" <body><h1>RoboHeart Access Point Example</h1>" );
86
+
87
+ // Display current state, and ON/OFF buttons for LED_ROBOHEART
88
+ client.println (" <p>LED_ROBOHEART - State " + String ((digitalRead (LED_ROBOHEART) == HIGH) ? " on" : " off" ) + " </p>" );
89
+
90
+ if ((digitalRead (LED_ROBOHEART))==LOW) {
91
+ client.println (" <p><a href=\" /LED_ROBOHEART/on\" ><button class=\" button\" >LED ON</button></a></p>" );
92
+ } else {
93
+ client.println (" <p><a href=\" /LED_ROBOHEART/off\" ><button class=\" button button2\" >LED OFF</button></a></p>" );
94
+ }
95
+
96
+ client.println (" </body></html>" );
97
+
98
+ // The HTTP response ends with another blank line
99
+ client.println ();
100
+ // Break out of the while loop
101
+ break ;
102
+ } else { // if you got a newline, then clear currentLine
103
+ currentLine = " " ;
104
+ }
105
+ } else if (c != ' \r ' ) { // if you got anything else but a carriage return character,
106
+ currentLine += c; // add it to the end of the currentLine
107
+ }
108
+ }
109
+ }
110
+ // Clear the header variable
111
+ header = " " ;
112
+ // Close the connection
113
+ client.stop ();
114
+ Serial.println (" Client disconnected." );
115
+ Serial.println (" " );
116
+ }
117
+ }
0 commit comments