php - Moving a web server hosted on an Arduino to a local server -


i have code reads switch state , displays on webpage. webpage stored on sd card of ethernet shield.

but moved area. want use same principle, instead of web page stored on sd card want on xampp server of local computer. changes need do? please suggest.

arduino code:

#include <spi.h> #include <ethernet.h> #include <sd.h>  // size of buffer used capture http requests #define req_buf_sz   50  // mac address ethernet shield sticker under board byte mac[] = {   0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed }; ipaddress ip(192, 168, 0, 20); // ip address, may need change depending on network ethernetserver server(80); // create server @ port 80 file webfile; // web page file on sd card char http_req[req_buf_sz] = {   0}; // buffered http request stored null terminated string char req_index = 0; // index http_req buffer  void setup() {  // disable ethernet chip   pinmode(10, output);   digitalwrite(10, high);   serial.begin(9600); // debugging  // initialize sd card   serial.println("initializing sd card...");   if (!sd.begin(4)) {     serial.println("error - sd card initialization failed!");     return; // init failed   }   serial.println("success - sd card initialized.");   // check index.htm file   if (!sd.exists("index.htm")) {     serial.println("error - can't find index.htm file!");     return; // can't find index file   }   serial.println("success - found index.htm file.");   pinmode(7, input); // switch attached arduino pin 7   pinmode(8, input); // switch attached arduino pin 8   ethernet.begin(mac, ip); // initialize ethernet device   server.begin(); // start listen clients }  void loop() {   ethernetclient client = server.available(); // try client   if (client) { // got client?     boolean currentlineisblank = true;     while (client.connected()) {       if (client.available()) { // client data available read         char c = client.read(); // read 1 byte (character) client         // buffer first part of http request in http_req array (string)         // leave last element in array 0 null terminate string (req_buf_sz - 1)         if (req_index < (req_buf_sz - 1)) {           http_req[req_index] = c; // save http request character           req_index++;         }         // last line of client request blank , ends \n         // respond client after last line received         if (c == '\n' && currentlineisblank) {           // send standard http response header           client.println("http/1.1 200 ok");           // remainder of header follows below, depending on if           // web page or xml page requested           // ajax request - send xml file           if (strcontains(http_req, "ajax_inputs")) {             // send rest of http header             client.println("content-type: text/xml");             client.println("connection: keep-alive");             client.println();             // send xml file containing input states             xml_response(client);           } else {             // web page request             // send rest of http header             client.println("content-type: text/html");             client.println("connection: keep-alive");             client.println();             // send web page             webfile = sd.open("index.htm"); // open web page file             if (webfile) {               while(webfile.available()) {                 client.write(webfile.read()); // send web page client               }               webfile.close();             }           }           // display received http request on serial port           serial.print(http_req);           // reset buffer index , buffer elements 0           req_index = 0;           strclear(http_req, req_buf_sz);           break;         }         // every line of text received client ends \r\n         if (c == '\n') {           // last character on line of received text           // starting new line next character read           currentlineisblank = true;         } else if (c != '\r') {           // text character received client           currentlineisblank = false;         }       } // end if (client.available())     } // end while (client.connected())     delay(1); // give web browser time receive data     client.stop(); // close connection   } // end if (client) }  // send xml file switch statuses , analog value void xml_response(ethernetclient cl) {   int analog_val;   cl.print("<?xml version = \"1.0\" ?>");   cl.print("<inputs>");   cl.print("<button1>");   if (digitalread(7)) {     cl.print("on");   } else {     cl.print("off");   }   cl.print("</button1>");   cl.print("<button2>");   if (digitalread(8)) {     cl.print("on");   } else {     cl.print("off");   }   cl.print("</button2>"); // read analog pin a2   analog_val = analogread(2);   cl.print("<analog1>");   cl.print(analog_val);   cl.print("</analog1>");   cl.print("</inputs>"); }  // sets every element of str 0 (clears array) void strclear(char *str, char length) {   (int = 0; < length; i++) {     str[i] = 0;   } }  // searches string sfind in string str // returns 1 if string found // returns 0 if string not found char strcontains(char *str, char *sfind) {   char found = 0;   char index = 0;   char len;   len = strlen(str);   if (strlen(sfind) > len) {     return 0;   }   while (index < len) {     if (str[index] == sfind[found]) {       found++;       if (strlen(sfind) == found) {         return 1;       }     } else {       found = 0;     }     index++;   }   return 0; } 

sd card webpage:

<!doctype html> <html>   <head>     <title>arduino sd card web page using ajax xml</title>     <script>     function getarduinoinputs() {       nocache = "&nocache=" + math.random() * 1000000;       var request = new xmlhttprequest();       request.onreadystatechange = function() {         if (this.readystate == 4) {           if (this.status == 200) {             if (this.responsexml != null) {               // extract xml data xml file (containing switch states , analog value)               document.getelementbyid("input1").innerhtml =                 this.responsexml.getelementsbytagname('button1')[0].childnodes[0].nodevalue;               document.getelementbyid("input2").innerhtml =                 this.responsexml.getelementsbytagname('button2')[0].childnodes[0].nodevalue;               document.getelementbyid("input3").innerhtml =                 this.responsexml.getelementsbytagname('analog1')[0].childnodes[0].nodevalue;             }           }         }       }       request.open("get", "ajax_inputs" + nocache, true);       request.send(null);       settimeout('getarduinoinputs()', 1000);     }   </script>   </head>   <body onload="getarduinoinputs()">     <h1>arduino inputs sd card web page using ajax xml</h1>     <p>button 1 (pin 7): <span id="input1">...</span></p>     <p>button 2 (pin 8): <span id="input2">...</span></p>     <p>analog (a2): <span id="input3">...</span></p>   </body> </html> 

your program big, understanding logic tough. question need clear. can guide in right direction.

you can make use of this.

here should :

  • you first need host website on local host, use php serve backend. create php code , link website, in such way when request php page arduino should in turn load webpage. basic structure :

    <?php  // connection information $servername = "localhost";    $username = "root"; $password = ""; $dbname = "arduinotest";  // create connection $conn = new mysqli($servername, $username, $password, $dbname);  // sql query $sql = "insert testdata (mytime) values ('".$_get["value"]."')";  // execute query $conn->query($sql);  // close connection $conn->close(); ?> 
  • then modify arduino code in such way can request php page on local machine. basic structure of :

    #include <spi.h> #include <ethernet.h>  byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed };  ipaddress ip(192,168,0,10); ipaddress server(192,168,0,4);   ethernetclient client;  int data = 10;  void setup() {  serial.begin(9600); ethernet.begin(mac, ip);    }  void loop() {  if (client.connect(server, 80)) { serial.println("connected successfully\n");  // print debugging serial.print("get /dbtest.php?"); serial.print("value="); serial.print(data);  serial.println(" http/1.1"); serial.print("host: "); serial.println(server); serial.println("connection: close");  client.print("get /dbtest.php?"); client.print("value="); client.print(data);  client.println(" http/1.1"); client.print("host: "); client.println(server); client.println("connection: close"); client.println(); client.println(); client.stop();  }    else {     serial.println("connection failed\n");    }   delay(30000); } 

this video link helpful.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -