title: Arduino - control LED via wifi based on mysql database value
body: <p>I have:</p>
  
  <ul>
  <li>home server setup with MySQL database and available to network</li>
  <li>Arduino with WiFi shield connected to LEDs</li>
  </ul>
  
  <p>Both above are working hunky dory - the Arduino connects to the WiFi and is assigned an IP address.</p>
  
  <p>What I'm now trying to do is write some Arduino code that would read the value stored in the web server database.  </p>
  
  <p>If I can work out how to do that, I could take it from there. Could someone provide an example of how to do so?</p>
  
  <h3>Extra detail on what I've done so far:</h3>
  
  <ul>
  <li>Below code connects to wifi using wifi shield.</li>
  <li>Am using Arduino Uno and v 1.05 of IDE</li>
  <li>PHP/MYSQL set up on local NAS - ip 192.168.0.102/web/homeserver</li>
  </ul>
  
  <p>So in short, just need to know what's required to query a web server/db from an arduino already connected to the Network.</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;WiFi.h&gt;
  
  char ssid[] = "SSID_IS_HERE";     //  your network SSID (name) 
  char pass[] = "MY_PASSWORD";  // your network password
  int status = WL_IDLE_STATUS;     // the Wifi radio's status
  
  void setup() {
      // Initialize serial and wait for port to open:
      Serial.begin(9600); 
      while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
      }
  
      // check for the presence of the shield:
      if (WiFi.status() == WL_NO_SHIELD) {
      Serial.println("WiFi shield not present"); 
      // don't continue:
      while(true);
      } 
  
      // attempt to connect to Wifi network:
      while ( status != WL_CONNECTED) { 
      Serial.print("Attempting to connect to WPA SSID: ");
      Serial.println(ssid);
      // Connect to WPA/WPA2 network:    
      status = WiFi.begin(ssid, pass);
  
      // wait 10 seconds for connection:
      delay(10000);
      }
  
      // you're connected now, so print out the data:
      Serial.print("You're connected to the network");
      printCurrentNet();
      printWifiData();
  }
  
  void loop() {
      // check the network connection once every 10 seconds:
      delay(10000);
      printCurrentNet();
  }
  
  void printWifiData() {
      // print your WiFi shield's IP address:
      IPAddress ip = WiFi.localIP();
      Serial.print("IP Address: ");
      Serial.println(ip);
      Serial.println(ip);
  
      // print your MAC address:
      byte mac[6];  
      WiFi.macAddress(mac);
      Serial.print("MAC address: ");
      Serial.print(mac[5],HEX);
      Serial.print(":");
      Serial.print(mac[4],HEX);
      Serial.print(":");
      Serial.print(mac[3],HEX);
      Serial.print(":");
      Serial.print(mac[2],HEX);
      Serial.print(":");
      Serial.print(mac[1],HEX);
      Serial.print(":");
      Serial.println(mac[0],HEX);
  }
  
  void printCurrentNet() {
      // print the SSID of the network you're attached to:
      Serial.print("SSID: ");
      Serial.println(WiFi.SSID());
  
      // print the MAC address of the router you're attached to:
      byte bssid[6];
      WiFi.BSSID(bssid);    
      Serial.print("BSSID: ");
      Serial.print(bssid[5],HEX);
      Serial.print(":");
      Serial.print(bssid[4],HEX);
      Serial.print(":");
      Serial.print(bssid[3],HEX);
      Serial.print(":");
      Serial.print(bssid[2],HEX);
      Serial.print(":");
      Serial.print(bssid[1],HEX);
      Serial.print(":");
      Serial.println(bssid[0],HEX);
  
      // print the received signal strength:
      long rssi = WiFi.RSSI();
      Serial.print("signal strength (RSSI):");
      Serial.println(rssi);
  
      // print the encryption type:
      byte encryption = WiFi.encryptionType();
      Serial.print("Encryption Type:");
      Serial.println(encryption,HEX);
      Serial.println();
  }
  </code></pre>