Components and supplies
Arduino UNO
meArm
Arduino MKR1000
Standard LCD - 16x2 White on Blue
Project description
Code
Master Code (MKR1000)
arduino
Webserver Code for the MKR1000
1/* 2 WiFi Web Server Contrôle de robot meArm, affichage LCD et led 3 4 created 18 Mars 2018 5 par Fabrice bonne 6 7 */ 8 9#include <SPI.h> 10#include <WiFi101.h> 11#include <Wire.h> 12// size of buffer used to capture HTTP requests 13#define REQ_BUF_SZ 90 14// size of buffer that stores the incoming string 15#define TXT_BUF_SZ 50 16 17char ssid[] = "****"; // your network SSID (name) 18char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string 19char req_index = 0; // index into HTTP_req buffer 20char txt_buf[TXT_BUF_SZ] = {0}; // buffer to save text to 21 22int led = LED_BUILTIN; 23int status = WL_IDLE_STATUS; 24WiFiServer server(80); 25 26void setup() { 27 //Initialize serial and wait for port to open: 28 Wire.begin(); // Rejoindre le bus I2C (Pas besoin d adresse pour le maitre) 29 Serial.begin(9600); 30 while (!Serial) { 31 ; // wait for serial port to connect. Needed for native USB port only 32 } 33 34 Serial.println("Access Point Web Server"); 35 36 pinMode(led, OUTPUT); // set the LED pin mode 37 digitalWrite(led, LOW); 38 // print the network name (SSID); 39 Serial.print("Creating access point named: "); 40 Serial.println(ssid); 41 42 // Create open network. Change this line if you want to create an WEP network: 43 status = WiFi.beginAP(ssid); 44 if (status != WL_AP_LISTENING) { 45 Serial.println("Creating access point failed"); 46 // don't continue 47 while (true); 48 } 49 50 // wait 10 seconds for connection: 51 delay(10000); 52 53 // start the web server on port 80 54 server.begin(); 55 56 // you're connected now, so print out the status 57 printWiFiStatus(); 58} 59 60 61void loop() { 62 // compare the previous status to the current status 63 if (status != WiFi.status()) { 64 // it has changed update the variable 65 status = WiFi.status(); 66 67 if (status == WL_AP_CONNECTED) { 68 byte remoteMac[6]; 69 70 // a device has connected to the AP 71 Serial.print("Device connected to AP, MAC address: "); 72 WiFi.APClientMacAddress(remoteMac); 73 Serial.print(remoteMac[5], HEX); 74 Serial.print(":"); 75 Serial.print(remoteMac[4], HEX); 76 Serial.print(":"); 77 Serial.print(remoteMac[3], HEX); 78 Serial.print(":"); 79 Serial.print(remoteMac[2], HEX); 80 Serial.print(":"); 81 Serial.print(remoteMac[1], HEX); 82 Serial.print(":"); 83 Serial.println(remoteMac[0], HEX); 84 digitalWrite(led, HIGH); 85 } else { 86 // a device has disconnected from the AP, and we are back in listening mode 87 Serial.println("Device disconnected from AP"); 88 digitalWrite(led, LOW); 89 } 90 } 91 92 WiFiClient client = server.available(); // listen for incoming clients 93 94 if (client) { // if you get a client, 95 Serial.println("new client"); // print a message out the serial port 96 boolean currentLineIsBlank = true; 97 while (client.connected()) { 98 if (client.available()) { // client data available to read 99 char c = client.read(); // read 1 byte (character) from client 100 // limit the size of the stored received HTTP request 101 // buffer first part of HTTP request in HTTP_req array (string) 102 // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1) 103 if (req_index < (REQ_BUF_SZ - 1)) { 104 HTTP_req[req_index] = c; // save HTTP request character 105 req_index++; 106 } 107 // last line of client request is blank and ends with \ 108 109 // respond to client only after last line received 110 if (c == '\ 111' && currentLineIsBlank) { 112 // send a standard http response header 113 client.println("HTTP/1.1 200 OK"); 114 // remainder of header follows below, depending on if 115 // web page or XML page is requested 116 // Ajax request - send XML file 117 if (StrContains(HTTP_req, "ajax_inputs")) { 118 // send rest of HTTP header 119 client.println("Content-Type: text/xml"); 120 client.println("Connection: keep-alive"); 121 client.println(); 122 123 // print the received text to the Serial Monitor window 124 // if received with the incoming HTTP GET string 125 if (GetText(txt_buf, TXT_BUF_SZ)) { 126 Serial.println("\ \ 127Received Text:"); 128 Serial.println(txt_buf); 129 Wire.beginTransmission(4); 130 Wire.write(txt_buf); 131 Wire.endTransmission(); 132 } 133 } 134 else { // web page request 135 // send rest of HTTP header 136 client.println("Content-Type: text/html"); 137 client.println("Connection: keep-alive"); 138 client.println(); 139 // send web page 140 client.println("<!DOCTYPE html>"); 141 client.println("<head>"); 142 client.println("<title>Control meArm</title>"); 143 client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>"); 144 client.println("<script type='text/javascript'>"); 145 146 //--fonctions javascript 147 //fonction texte 148 client.println("function SendText(){"); 149 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 150 client.println("var request = new XMLHttpRequest();"); 151 client.println("strText = \\"&txt=\\" + document.getElementById('txt_form').form_text.value + \\"&end=end\\";"); 152 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 153 client.println("request.send(null);"); 154 client.println("}"); 155 156 //fonction verticale 157 client.println("function Verticale(){"); 158 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 159 client.println("var request = new XMLHttpRequest();"); 160 client.println("strText = \\"&txt=\\" + \\"Vertical=\\" + document.getElementById('pos_v').posiv_range.value + \\"&end=end\\";"); 161 client.println("positiv = document.getElementById('pos_v').posiv_range.value;"); 162 client.println("document.getElementById('reportv').value=positiv;"); 163 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 164 client.println("request.send(null);"); 165 client.println("}"); 166 167 //fonction horizontale 168 client.println("function Horizontale(){"); 169 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 170 client.println("var request = new XMLHttpRequest();"); 171 client.println("strText = \\"&txt=\\" + \\"Horizontal=\\" + document.getElementById('pos_h').posih_range.value + \\"&end=end\\";"); 172 client.println("positih = document.getElementById('pos_h').posih_range.value;"); 173 client.println("document.getElementById('reporth').value=positih;"); 174 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 175 client.println("request.send(null);"); 176 client.println("}"); 177 178 //fonction angulaire 179 client.println("function Angulaire(){"); 180 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 181 client.println("var request = new XMLHttpRequest();"); 182 client.println("strText = \\"&txt=\\" + \\"Angle=\\" + document.getElementById('pos_a').posia_range.value + \\"&end=end\\";"); 183 client.println("positia = document.getElementById('pos_a').posia_range.value;"); 184 client.println("document.getElementById('reporta').value=positia;"); 185 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 186 client.println("request.send(null);"); 187 client.println("}"); 188 189 //fonction pince 190 client.println("function Pince(){"); 191 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 192 client.println("var request = new XMLHttpRequest();"); 193 client.println("strText = \\"&txt=\\" + \\"Ouverture=\\" + document.getElementById('pos_p').posip_range.value + \\"&end=end\\";"); 194 client.println("positip = document.getElementById('pos_p').posip_range.value;"); 195 client.println("document.getElementById('reportp').value=positip;"); 196 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 197 client.println("request.send(null);"); 198 client.println("}"); 199 client.println("</script>"); 200 client.println("</head>"); 201 client.println("<div>"); 202 203 //--Input texte et Input Range 204 //Envoi de texte 205 client.println("<form id='txt_form' name='frmText'>"); 206 client.println("<center><textarea name='form_text' rows='2' cols='16'></textarea></center>"); 207 client.println("</form>"); 208 client.println("<center><input type=submit value='Send Text' onclick=SendText() /></center></div>"); 209 client.println("<div>"); 210 client.println("<center>Bouger le robot</center><BR><BR>"); 211 212 //Verticalement 213 client.println("<form id='pos_v' name='posText'>"); 214 client.println("<center>Verticalement</center><BR>"); 215 client.println("<center>Bas<input type=range name='posiv_range' min='20' max='90' value='45' step='1' onchange=Verticale() />Haut</center>"); 216 client.println("</form>"); 217 client.println("<center><input type=text id='reportv' size='3'/></center><BR>"); 218 219 //Horizontalement 220 client.println("<form id='pos_h' name='posText'>"); 221 client.println("<center>Horizontalement</center><BR>"); 222 client.println("<center>Proche<input type=range name='posih_range' min='0' max='80' value='45' step='1' onchange=Horizontale() />Loin</center>"); 223 client.println("</form>"); 224 client.println("<center><input type=text id='reporth' size='3'/></center><BR>"); 225 226 //Angulaire 227 client.println("<form id='pos_a' name='posText'>"); 228 client.println("<center>Tourner le robot</center><BR>"); 229 client.println("<center>Gauche<input type=range name='posia_range' min='0' max='180' value='90' step='1' onchange=Angulaire() />Droite</center>"); 230 client.println("</form>"); 231 client.println("<center><input type=text id='reporta' size='3'/></center><BR>"); 232 233 //Pince 234 client.println("<form id='pos_p' name='posText'>"); 235 client.println("<center>Pince</center><BR>"); 236 client.println("<center>Ouvert<input type=range name='posip_range' min='0' max='90' value='45' step='1' onchange=Pince() />Fermee</center>"); 237 client.println("</form>"); 238 client.println("<center><input type=text id='reportp' size='3'/></center><BR>"); 239 240 client.println("</div>"); 241 client.println("</body>"); 242 client.println("</html>"); 243 } 244 // reset buffer index and all buffer elements to 0 245 req_index = 0; 246 StrClear(HTTP_req, REQ_BUF_SZ); 247 break; 248 } 249 // every line of text received from the client ends with \ \ 250 251 if (c == '\ 252') { 253 // last character on line of received text 254 // starting new line with next character read 255 currentLineIsBlank = true; 256 } 257 else if (c != '\ ') { 258 // a text character was received from client 259 currentLineIsBlank = false; 260 } 261 } // end if (client.available()) 262 } // end while (client.connected()) 263 delay(1); // give the web browser time to receive the data 264 client.stop(); // close the connection 265 } // end if (client) 266} 267 268// extract text from the incoming HTTP GET data string 269// returns true only if text was received 270// the string must start with "&txt=" and end with "&end" 271// if the string is too long for the HTTP_req buffer and 272// "&end" is cut off, then the function returns false 273boolean GetText(char *txt, int len) 274{ 275 boolean got_text = false; // text received flag 276 char *str_begin; // pointer to start of text 277 char *str_end; // pointer to end of text 278 int str_len = 0; 279 int txt_index = 0; 280 281 // get pointer to the beginning of the text 282 str_begin = strstr(HTTP_req, "&txt="); 283 if (str_begin != NULL) { 284 str_begin = strstr(str_begin, "="); // skip to the = 285 str_begin += 1; // skip over the = 286 str_end = strstr(str_begin, "&end"); 287 if (str_end != NULL) { 288 str_end[0] = 0; // terminate the string 289 str_len = strlen(str_begin); 290 291 // copy the string to the txt buffer and replace %20 with space ' ' 292 for (int i = 0; i < str_len; i++) { 293 if (str_begin[i] != '%') { 294 if (str_begin[i] == 0) { 295 // end of string 296 break; 297 } 298 else { 299 txt[txt_index++] = str_begin[i]; 300 if (txt_index >= (len - 1)) { 301 // keep the output string within bounds 302 break; 303 } 304 } 305 } 306 else { 307 // replace %20 with a space 308 if ((str_begin[i + 1] == '2') && (str_begin[i + 2] == '0')) { 309 txt[txt_index++] = ' '; 310 i += 2; 311 if (txt_index >= (len - 1)) { 312 // keep the output string within bounds 313 break; 314 } 315 } 316 } 317 } 318 // terminate the string 319 txt[txt_index] = 0; 320 got_text = true; 321 } 322 } 323 324 return got_text; 325} 326 327// sets every element of str to 0 (clears array) 328void StrClear(char *str, char length) 329{ 330 for (int i = 0; i < length; i++) { 331 str[i] = 0; 332 } 333} 334 335// searches for the string sfind in the string str 336// returns 1 if string found 337// returns 0 if string not found 338char StrContains(char *str, char *sfind) 339{ 340 char found = 0; 341 char index = 0; 342 char len; 343 344 len = strlen(str); 345 346 if (strlen(sfind) > len) { 347 return 0; 348 } 349 while (index < len) { 350 if (str[index] == sfind[found]) { 351 found++; 352 if (strlen(sfind) == found) { 353 return 1; 354 } 355 } 356 else { 357 found = 0; 358 } 359 index++; 360 } 361 362 return 0; 363} 364 365 366 367void printWiFiStatus() { 368 // print the SSID of the network you're attached to: 369 Serial.print("SSID: "); 370 Serial.println(WiFi.SSID()); 371 372 // print your WiFi shield's IP address: 373 IPAddress ip = WiFi.localIP(); 374 Serial.print("IP Address: "); 375 Serial.println(ip); 376 377 // print the received signal strength: 378 long rssi = WiFi.RSSI(); 379 Serial.print("signal strength (RSSI):"); 380 Serial.print(rssi); 381 Serial.println(" dBm"); 382 // print where to go in a browser: 383 Serial.print("To see this page in action, open a browser to http://"); 384 Serial.println(ip); 385 386}
Master Code (MKR1000)
arduino
Webserver Code for the MKR1000
1/* 2 WiFi Web Server Contrôle de robot meArm, affichage LCD et led 3 4 created 18 Mars 2018 5 par Fabrice bonne 6 7 */ 8 9#include <SPI.h> 10#include <WiFi101.h> 11#include <Wire.h> 12// size of buffer used to capture HTTP requests 13#define REQ_BUF_SZ 90 14// size of buffer that stores the incoming string 15#define TXT_BUF_SZ 50 16 17char ssid[] = "****"; // your network SSID (name) 18char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string 19char req_index = 0; // index into HTTP_req buffer 20char txt_buf[TXT_BUF_SZ] = {0}; // buffer to save text to 21 22int led = LED_BUILTIN; 23int status = WL_IDLE_STATUS; 24WiFiServer server(80); 25 26void setup() { 27 //Initialize serial and wait for port to open: 28 Wire.begin(); // Rejoindre le bus I2C (Pas besoin d adresse pour le maitre) 29 Serial.begin(9600); 30 while (!Serial) { 31 ; // wait for serial port to connect. Needed for native USB port only 32 } 33 34 Serial.println("Access Point Web Server"); 35 36 pinMode(led, OUTPUT); // set the LED pin mode 37 digitalWrite(led, LOW); 38 // print the network name (SSID); 39 Serial.print("Creating access point named: "); 40 Serial.println(ssid); 41 42 // Create open network. Change this line if you want to create an WEP network: 43 status = WiFi.beginAP(ssid); 44 if (status != WL_AP_LISTENING) { 45 Serial.println("Creating access point failed"); 46 // don't continue 47 while (true); 48 } 49 50 // wait 10 seconds for connection: 51 delay(10000); 52 53 // start the web server on port 80 54 server.begin(); 55 56 // you're connected now, so print out the status 57 printWiFiStatus(); 58} 59 60 61void loop() { 62 // compare the previous status to the current status 63 if (status != WiFi.status()) { 64 // it has changed update the variable 65 status = WiFi.status(); 66 67 if (status == WL_AP_CONNECTED) { 68 byte remoteMac[6]; 69 70 // a device has connected to the AP 71 Serial.print("Device connected to AP, MAC address: "); 72 WiFi.APClientMacAddress(remoteMac); 73 Serial.print(remoteMac[5], HEX); 74 Serial.print(":"); 75 Serial.print(remoteMac[4], HEX); 76 Serial.print(":"); 77 Serial.print(remoteMac[3], HEX); 78 Serial.print(":"); 79 Serial.print(remoteMac[2], HEX); 80 Serial.print(":"); 81 Serial.print(remoteMac[1], HEX); 82 Serial.print(":"); 83 Serial.println(remoteMac[0], HEX); 84 digitalWrite(led, HIGH); 85 } else { 86 // a device has disconnected from the AP, and we are back in listening mode 87 Serial.println("Device disconnected from AP"); 88 digitalWrite(led, LOW); 89 } 90 } 91 92 WiFiClient client = server.available(); // listen for incoming clients 93 94 if (client) { // if you get a client, 95 Serial.println("new client"); // print a message out the serial port 96 boolean currentLineIsBlank = true; 97 while (client.connected()) { 98 if (client.available()) { // client data available to read 99 char c = client.read(); // read 1 byte (character) from client 100 // limit the size of the stored received HTTP request 101 // buffer first part of HTTP request in HTTP_req array (string) 102 // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1) 103 if (req_index < (REQ_BUF_SZ - 1)) { 104 HTTP_req[req_index] = c; // save HTTP request character 105 req_index++; 106 } 107 // last line of client request is blank and ends with \ 108 109 // respond to client only after last line received 110 if (c == '\n' && currentLineIsBlank) { 111 // send a standard http response header 112 client.println("HTTP/1.1 200 OK"); 113 // remainder of header follows below, depending on if 114 // web page or XML page is requested 115 // Ajax request - send XML file 116 if (StrContains(HTTP_req, "ajax_inputs")) { 117 // send rest of HTTP header 118 client.println("Content-Type: text/xml"); 119 client.println("Connection: keep-alive"); 120 client.println(); 121 122 // print the received text to the Serial Monitor window 123 // if received with the incoming HTTP GET string 124 if (GetText(txt_buf, TXT_BUF_SZ)) { 125 Serial.println("\ \ 126Received Text:"); 127 Serial.println(txt_buf); 128 Wire.beginTransmission(4); 129 Wire.write(txt_buf); 130 Wire.endTransmission(); 131 } 132 } 133 else { // web page request 134 // send rest of HTTP header 135 client.println("Content-Type: text/html"); 136 client.println("Connection: keep-alive"); 137 client.println(); 138 // send web page 139 client.println("<!DOCTYPE html>"); 140 client.println("<head>"); 141 client.println("<title>Control meArm</title>"); 142 client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>"); 143 client.println("<script type='text/javascript'>"); 144 145 //--fonctions javascript 146 //fonction texte 147 client.println("function SendText(){"); 148 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 149 client.println("var request = new XMLHttpRequest();"); 150 client.println("strText = \\"&txt=\\" + document.getElementById('txt_form').form_text.value + \\"&end=end\\";"); 151 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 152 client.println("request.send(null);"); 153 client.println("}"); 154 155 //fonction verticale 156 client.println("function Verticale(){"); 157 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 158 client.println("var request = new XMLHttpRequest();"); 159 client.println("strText = \\"&txt=\\" + \\"Vertical=\\" + document.getElementById('pos_v').posiv_range.value + \\"&end=end\\";"); 160 client.println("positiv = document.getElementById('pos_v').posiv_range.value;"); 161 client.println("document.getElementById('reportv').value=positiv;"); 162 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 163 client.println("request.send(null);"); 164 client.println("}"); 165 166 //fonction horizontale 167 client.println("function Horizontale(){"); 168 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 169 client.println("var request = new XMLHttpRequest();"); 170 client.println("strText = \\"&txt=\\" + \\"Horizontal=\\" + document.getElementById('pos_h').posih_range.value + \\"&end=end\\";"); 171 client.println("positih = document.getElementById('pos_h').posih_range.value;"); 172 client.println("document.getElementById('reporth').value=positih;"); 173 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 174 client.println("request.send(null);"); 175 client.println("}"); 176 177 //fonction angulaire 178 client.println("function Angulaire(){"); 179 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 180 client.println("var request = new XMLHttpRequest();"); 181 client.println("strText = \\"&txt=\\" + \\"Angle=\\" + document.getElementById('pos_a').posia_range.value + \\"&end=end\\";"); 182 client.println("positia = document.getElementById('pos_a').posia_range.value;"); 183 client.println("document.getElementById('reporta').value=positia;"); 184 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 185 client.println("request.send(null);"); 186 client.println("}"); 187 188 //fonction pince 189 client.println("function Pince(){"); 190 client.println("nocache = \\"&nocache=\\" + Math.random() * 1000000;"); 191 client.println("var request = new XMLHttpRequest();"); 192 client.println("strText = \\"&txt=\\" + \\"Ouverture=\\" + document.getElementById('pos_p').posip_range.value + \\"&end=end\\";"); 193 client.println("positip = document.getElementById('pos_p').posip_range.value;"); 194 client.println("document.getElementById('reportp').value=positip;"); 195 client.println("request.open(\\"GET\\", \\"ajax_inputs\\" + strText + nocache, true);"); 196 client.println("request.send(null);"); 197 client.println("}"); 198 client.println("</script>"); 199 client.println("</head>"); 200 client.println("<div>"); 201 202 //--Input texte et Input Range 203 //Envoi de texte 204 client.println("<form id='txt_form' name='frmText'>"); 205 client.println("<center><textarea name='form_text' rows='2' cols='16'></textarea></center>"); 206 client.println("</form>"); 207 client.println("<center><input type=submit value='Send Text' onclick=SendText() /></center></div>"); 208 client.println("<div>"); 209 client.println("<center>Bouger le robot</center><BR><BR>"); 210 211 //Verticalement 212 client.println("<form id='pos_v' name='posText'>"); 213 client.println("<center>Verticalement</center><BR>"); 214 client.println("<center>Bas<input type=range name='posiv_range' min='20' max='90' value='45' step='1' onchange=Verticale() />Haut</center>"); 215 client.println("</form>"); 216 client.println("<center><input type=text id='reportv' size='3'/></center><BR>"); 217 218 //Horizontalement 219 client.println("<form id='pos_h' name='posText'>"); 220 client.println("<center>Horizontalement</center><BR>"); 221 client.println("<center>Proche<input type=range name='posih_range' min='0' max='80' value='45' step='1' onchange=Horizontale() />Loin</center>"); 222 client.println("</form>"); 223 client.println("<center><input type=text id='reporth' size='3'/></center><BR>"); 224 225 //Angulaire 226 client.println("<form id='pos_a' name='posText'>"); 227 client.println("<center>Tourner le robot</center><BR>"); 228 client.println("<center>Gauche<input type=range name='posia_range' min='0' max='180' value='90' step='1' onchange=Angulaire() />Droite</center>"); 229 client.println("</form>"); 230 client.println("<center><input type=text id='reporta' size='3'/></center><BR>"); 231 232 //Pince 233 client.println("<form id='pos_p' name='posText'>"); 234 client.println("<center>Pince</center><BR>"); 235 client.println("<center>Ouvert<input type=range name='posip_range' min='0' max='90' value='45' step='1' onchange=Pince() />Fermee</center>"); 236 client.println("</form>"); 237 client.println("<center><input type=text id='reportp' size='3'/></center><BR>"); 238 239 client.println("</div>"); 240 client.println("</body>"); 241 client.println("</html>"); 242 } 243 // reset buffer index and all buffer elements to 0 244 req_index = 0; 245 StrClear(HTTP_req, REQ_BUF_SZ); 246 break; 247 } 248 // every line of text received from the client ends with \ \ 249 250 if (c == '\n') { 251 // last character on line of received text 252 // starting new line with next character read 253 currentLineIsBlank = true; 254 } 255 else if (c != '\r') { 256 // a text character was received from client 257 currentLineIsBlank = false; 258 } 259 } // end if (client.available()) 260 } // end while (client.connected()) 261 delay(1); // give the web browser time to receive the data 262 client.stop(); // close the connection 263 } // end if (client) 264} 265 266// extract text from the incoming HTTP GET data string 267// returns true only if text was received 268// the string must start with "&txt=" and end with "&end" 269// if the string is too long for the HTTP_req buffer and 270// "&end" is cut off, then the function returns false 271boolean GetText(char *txt, int len) 272{ 273 boolean got_text = false; // text received flag 274 char *str_begin; // pointer to start of text 275 char *str_end; // pointer to end of text 276 int str_len = 0; 277 int txt_index = 0; 278 279 // get pointer to the beginning of the text 280 str_begin = strstr(HTTP_req, "&txt="); 281 if (str_begin != NULL) { 282 str_begin = strstr(str_begin, "="); // skip to the = 283 str_begin += 1; // skip over the = 284 str_end = strstr(str_begin, "&end"); 285 if (str_end != NULL) { 286 str_end[0] = 0; // terminate the string 287 str_len = strlen(str_begin); 288 289 // copy the string to the txt buffer and replace %20 with space ' ' 290 for (int i = 0; i < str_len; i++) { 291 if (str_begin[i] != '%') { 292 if (str_begin[i] == 0) { 293 // end of string 294 break; 295 } 296 else { 297 txt[txt_index++] = str_begin[i]; 298 if (txt_index >= (len - 1)) { 299 // keep the output string within bounds 300 break; 301 } 302 } 303 } 304 else { 305 // replace %20 with a space 306 if ((str_begin[i + 1] == '2') && (str_begin[i + 2] == '0')) { 307 txt[txt_index++] = ' '; 308 i += 2; 309 if (txt_index >= (len - 1)) { 310 // keep the output string within bounds 311 break; 312 } 313 } 314 } 315 } 316 // terminate the string 317 txt[txt_index] = 0; 318 got_text = true; 319 } 320 } 321 322 return got_text; 323} 324 325// sets every element of str to 0 (clears array) 326void StrClear(char *str, char length) 327{ 328 for (int i = 0; i < length; i++) { 329 str[i] = 0; 330 } 331} 332 333// searches for the string sfind in the string str 334// returns 1 if string found 335// returns 0 if string not found 336char StrContains(char *str, char *sfind) 337{ 338 char found = 0; 339 char index = 0; 340 char len; 341 342 len = strlen(str); 343 344 if (strlen(sfind) > len) { 345 return 0; 346 } 347 while (index < len) { 348 if (str[index] == sfind[found]) { 349 found++; 350 if (strlen(sfind) == found) { 351 return 1; 352 } 353 } 354 else { 355 found = 0; 356 } 357 index++; 358 } 359 360 return 0; 361} 362 363 364 365void printWiFiStatus() { 366 // print the SSID of the network you're attached to: 367 Serial.print("SSID: "); 368 Serial.println(WiFi.SSID()); 369 370 // print your WiFi shield's IP address: 371 IPAddress ip = WiFi.localIP(); 372 Serial.print("IP Address: "); 373 Serial.println(ip); 374 375 // print the received signal strength: 376 long rssi = WiFi.RSSI(); 377 Serial.print("signal strength (RSSI):"); 378 Serial.print(rssi); 379 Serial.println(" dBm"); 380 // print where to go in a browser: 381 Serial.print("To see this page in action, open a browser to http://"); 382 Serial.println(ip); 383 384}
Slave Code (Arduino UNO)
arduino
The code for the slave card, Arduino UNO. It is used to read the I2C transmission, and convert the orders into movements and LCD display
1/* 2 WiFi Web Server Contrôle de moteur, affichage LCD et led (Slave) 3 4 created 18 Mars 2018 5 par Fabrice bonne 6 7 */ 8 9#include <Servo.h> 10#include <LiquidCrystal.h> 11#include <Wire.h> // Librairie pour la communication I2C 12 13 14LiquidCrystal lcd(7,6,5,4,3,2); //lcd en bornes 2 à 7 15Servo MVertical; 16Servo MHorizontal; 17Servo MAngle; 18Servo MPince; 19 20void setup() { 21Wire.begin(4); // Rejoindre le bus à l'adresse #4 22Wire.onReceive(receiveEvent); // Preparer une fonction spécifique a la reception de donnee 23Serial.begin(9600); 24lcd.begin(16, 2); //démarrage du module LCD 25lcd.clear(); 26lcd.setCursor(0,0); 27 28MVertical.attach(10); 29MHorizontal.attach(8); 30MAngle.attach(9); 31MPince.attach(11); 32 33MVertical.write(45); 34MHorizontal.write(45); 35MAngle.write(90); 36MPince.write(45); 37 38lcd.clear(); 39lcd.setCursor(0,0); 40lcd.print("EN ATTENTE"); 41lcd.setCursor(0,1); 42lcd.print("D'ENVOI"); 43} 44 45void loop() 46{ 47 delay(500); 48} 49 50// Fonction qui s execute si quelque chose est present sur l interface 51void receiveEvent(int howMany) 52{ 53String data = ""; 54String aff1 = ""; 55String aff2 = ""; 56String recherche = ""; 57int dl; 58bool Mot = false; 59 60while (Wire.available()) { //lecture des ordres envoyés 61 data += (char)Wire.read(); 62} 63 Serial.println(data); 64 recherche = "Vertical="; //Recherche des ordres moteurs 65 if (data.indexOf(recherche)!=-1){ 66 aff1 = "Vertical="; 67 aff2 = data.substring(9,12); 68 Serial.print("Nouvelle valeur V="); 69 Serial.println(aff2); 70 MVertical.write(aff2.toInt()); 71 Mot = true ; 72 } 73 recherche = "Horizontal="; 74 if (data.indexOf(recherche)!=-1){ 75 aff1 = "Horizontal="; 76 aff2 = data.substring(11,14); 77 Serial.print("Nouvelle valeur H="); 78 Serial.println(aff2); 79 MHorizontal.write(aff2.toInt()); 80 Mot = true ; 81 } 82 recherche = "Angle="; 83 if (data.indexOf(recherche)!=-1){ 84 aff1 = "Angle="; 85 aff2 = data.substring(6,9); 86 Serial.print("Nouvelle valeur A="); 87 Serial.println(aff2); 88 MAngle.write(aff2.toInt()); 89 Mot = true ; 90 } 91 recherche = "Ouverture="; 92 if (data.indexOf(recherche)!=-1){ 93 aff1 = "Ouverture="; 94 aff2 = data.substring(10,13); 95 Serial.print("Nouvelle valeur O="); 96 Serial.println(aff2); 97 MPince.write(aff2.toInt()); 98 Mot = true ; 99 } 100 if (Mot == false) { //Si c'est un texte, découpage et affichage 101 dl = data.length(); 102 Serial.print("longueur :"); 103 Serial.println(dl); 104 if (dl > 16) { 105 aff1 = data.substring(0, 16); 106 aff2 = data.substring(16, dl); 107 Serial.print("découpage :"); 108 Serial.println(aff1); 109 Serial.println(aff2); 110 } 111 else { 112 aff1 = data; 113 Serial.println("découpage non requis"); 114 } 115 } 116 Serial.println(); 117 lcd.clear(); //Affichage des infos moteur et texte en Relay 118 lcd.setCursor(0,0); 119 lcd.print(aff1); 120 lcd.setCursor(0,1); 121 lcd.print(aff2); 122} 123
Comments
Only logged in users can leave comments
Lightrider
0 Followers
•0 Projects
Table of contents
Intro
3
0