TI RSLK Library  0.2.0
Wifi_Remote.cpp
1 #include "Wifi_Remote.h"
2 
3 bool left_button_pressed;
4 bool right_button_pressed;
5 bool up_button_pressed;
6 bool down_button_pressed;
7 
8 extern String wifi_name;
9 extern String wifi_password;
10 bool up_down_pressed = false;
11 
12 WiFiServer myServer(80);
13 uint8_t oldCountClients = 0;
14 uint8_t countClients = 0;
15 
16 void wifi_setup() {
17 
18  // Start WiFi and create a network with wifi_name as the network name
19  // with wifi_password as the password.
20  Serial.print("Starting AP... ");
21  WiFi.beginNetwork((char *)wifi_name.c_str(), (char *)wifi_password.c_str());
22  while (WiFi.localIP() == INADDR_NONE)
23  {
24  // print dots while we wait for the AP config to complete
25  Serial.print('.');
26  delay(300);
27  }
28 
29  digitalWrite(RED_LED, HIGH);
30  Serial.println("DONE");
31 
32  Serial.print("LAN name = ");
33  Serial.println(wifi_name.c_str());
34  Serial.print("WPA password = ");
35  Serial.println(wifi_password.c_str());
36 
37 
38  IPAddress ip = WiFi.localIP();
39  Serial.print("Webserver IP address = ");
40  Serial.println(ip);
41 
42  Serial.print("Web-server port = ");
43  myServer.begin(); // start the web server on port 80
44  Serial.println("80");
45  Serial.println();
46 }
47 
48 void wifiLoop()
49 {
50 
51  // IPAddress ip = WiFi.localIP();
52  // Serial.print("Webserver IP address = ");
53  // Serial.println(ip);
54  countClients = WiFi.getTotalDevices();
55 
56  // Did a client connect/disconnect since the last time we checked?
57  if (countClients != oldCountClients)
58  {
59  if (countClients > oldCountClients)
60  { // Client connect
61  // digitalWrite(RED_LED, !digitalRead(RED_LED));
62  Serial.println("Client connected to AP");
63  for (uint8_t k = 0; k < countClients; k++)
64  {
65  Serial.print("Client #");
66  Serial.print(k);
67  Serial.print(" at IP address = ");
68  Serial.print(WiFi.deviceIpAddress(k));
69  Serial.print(", MAC = ");
70  Serial.println(WiFi.deviceMacAddress(k));
71  Serial.println("CC3200 in AP mode only accepts one client.");
72  }
73  }
74  else
75  { // Client disconnect
76  // digitalWrite(RED_LED, !digitalRead(RED_LED));
77  Serial.println("Client disconnected from AP.");
78  Serial.println();
79  }
80  oldCountClients = countClients;
81  }
82 
83  WiFiClient myClient = myServer.available();
84  int val;// if you get a client,
85 
86  if (myClient)
87  {
88 
89  //Serial.println(". Client connected to server"); // print a message out the serial port
90  char buffer[150] = {0}; // make a buffer to hold incoming data
91  int8_t i = 0;
92 
93 
94  while (myClient.connected())
95  { // loop while the client's connected
96 
97 
98  if (myClient.available())
99  { // if there's bytes to read from the client,
100  char c = myClient.read(); // read a byte, then
101  //Serial.write(c); // print it out the serial monitor
102  if (c == '\n') { // if the byte is a newline character
103 
104  // if the current line is blank, you got two newline characters in a row.
105  // that's the end of the client HTTP request, so send a response:
106 
107 
108  if (strlen(buffer) == 0)
109  {
110  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
111  // and a content-type so the client knows what's coming, then a blank line:
112  myClient.println("HTTP/1.1 200 OK");
113  myClient.println("Content-type:text/html");
114  myClient.println();
115  myClient.print("<!DOCTYPE html><html><head>");
116  myClient.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\"> ");
117  myClient.print(" <style>i{border: solid black; border-width: 0 3px 3px 0;");
118  myClient.print(" display: inline-block; padding: 3px;}.right{transform: rotate(-45deg); -webkit-transform:");
119  myClient.print(" rotate(-45deg); width: 100px; height:100px;}.left{transform: rotate(135deg);");
120  myClient.print(" -webkit-transform: rotate(135deg); width: 100px; height:100px;}.up{transform: rotate(-135deg);");
121  myClient.print(" -webkit-transform: rotate(-135deg); width: 100px; height:100px;}.down{transform: rotate(45deg);");
122  myClient.print(" -webkit-transform: rotate(45deg); width: 100px; height:100px;}.noselect{-webkit-touch-callout:");
123  myClient.print(" none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none;");
124  myClient.print(" -ms-user-select: none; user-select: none;}</style></head><body><p id=\"demo\"></p><br><br><br><table style=\"padding-left:");
125  myClient.print(" 30px\" class=\"noselect\"><tr><td></td><td><p ontouchstart=\"as('up','/GOOD_S')\" ontouchend=\"as2('up','/GOOD_E')\" ontouchcancel=\"as2('up','/GOOD_E')\">");
126  myClient.print(" <i class=\"up noselect\"></i></p></td><td></td><td></td></tr><tr><td><p ontouchstart=");
127  myClient.print(" \"as('left','/LEFT_S')\" ontouchend=\"as2('left','/LEFT_E')\" ontouchcancel=\"as2('left','/LEFT_E')\"><i class=\"left noselect\"></i></p></td><td></td>");
128  myClient.print(" <td></td><td><p ontouchstart=\"as('right','/RIGHT_S')\" ontouchend=\"as2('right','/RIGHT_E')\" ontouchcancel=\"as2('right','/RIGHT_E')\"><i class=\"right ");
129  myClient.print(" noselect\"></i></p></td></tr><tr><td></td><td><p ontouchstart=\"as('down','/DOWN_S')\" ontouchend");
130  myClient.print(" =\"as2('down','/DOWN_E')\" ontouchcancel=\"as2('down','/DOWN_E')\"><i class=\"down noselect\"></i></p></td><td></td><td></td></tr></table>");
131  myClient.print("<script>function as(direction,two){document.getElementById(\"demo\").innerHTML=\"Touch Start \"");
132  myClient.print(" +direction; sometime(\"Touch Start \"+direction,two);}function as2(direction,two)");
133  myClient.print(" {document.getElementById(\"demo\").innerHTML=\"Touch End \"+direction; ");
134  myClient.print(" sometime(\"Touch End \"+direction,two);}function sometime(thing,two)");
135  myClient.print(" {fetch(two).catch(function(error){document.getElementById(\"demo\").innerHTML");
136  myClient.print(" =\"Error \"+thing;});}</script></body></html>");
137 
138 
139  // The HTTP response ends with another blank line:
140  myClient.println();
141  // break out of the while loop:
142  break;
143  }
144  else
145  { // if you got a newline, then clear the buffer:
146  memset(buffer, 0, 150);
147  i = 0;
148  }
149  }
150  else if (c != '\r')
151  { // if you got anything else but a carriage return character,
152  buffer[i++] = c; // add it to the end of the currentLine
153  }
154 
155  String text = buffer;
156  //Serial.println(buffer);
157  // Check to see if the client request was "GET /H" or "GET /L":
158  if(text.indexOf("GET ")!= -1 && text.indexOf("HTTP/1.1") != -1) {
159 
160 
161  if (text.startsWith("GET /LEFT_S "))
162  {
163  Serial.println("Left button pressed");
164  left_button_pressed = true;
165  }
166  if (text.startsWith("GET /LEFT_E "))
167  {
168  left_button_pressed = false;
169  Serial.println("Left button released");
170  }
171 
172 
173  if (text.startsWith("GET /RIGHT_S "))
174  {
175  right_button_pressed = true;
176  Serial.println("Right button pressed");
177  }
178  if (text.startsWith("GET /RIGHT_E "))
179  {
180  right_button_pressed = false;
181  Serial.println("Right button released");
182  }
183 
184 
185  if (text.startsWith("GET /GOOD_S "))
186  {
187  up_down_pressed = true;
188  up_button_pressed = true;
189  down_button_pressed = false;
190  Serial.println("Up button pressed");
191 
192  }
193  if (text.startsWith("GET /GOOD_E "))
194  {
195  up_down_pressed = false;
196  up_button_pressed = false;
197  Serial.println("Up button released");
198  }
199 
200 
201  if (text.startsWith("GET /DOWN_S "))
202  {
203  up_down_pressed = true;
204  down_button_pressed = true;
205  up_button_pressed = false;
206  Serial.println("Down button pressed");
207  }
208  if (text.startsWith("GET /DOWN_E "))
209  {
210  up_down_pressed = false;
211  down_button_pressed = false;
212  Serial.println("Down button released");
213  }
214  }
215  }
216 
217  }
218 
219  // close the connection:
220  myClient.stop();
221  Serial.println(". Client disconnected from server");
222  //Serial.println();
223  }
224  custom_logic();
225 }