title: Sending Arduino data to mySQL database using NodeMCU ESP8266
body: <p>Kindly help me in my code. I have arduino code that is sending and getting data from mySql. It is running well and storing data to the database. The problem is that, if I switch ON the button the value 1 is stored in the database, but when I switch OFF the button the value 0 is not stored in the database.</p>
  <p><strong>Here is my arduino code:</strong></p>
  <pre><code>//Arduino Code
  #include &lt;SoftwareSerial.h&gt;
  
  SoftwareSerial s(5,6);//Rx,Tx
  int buttonPinBulb = 11;
  int relay1 = 10;
  int buttonBulb;
  int currentStatus = LOW;
  unsigned long lastMillis = 0;
  const unsigned long debounceTime = 100;
  
  void setup() {
    // put your setup code here, to run once:
    pinMode(buttonPinBulb, INPUT_PULLUP);
    pinMode(relay1, OUTPUT);
    s.begin(115200);
  }
  
  void loop() {
    // put your main code here, to run repeatedly:
    buttonBulb = digitalRead(buttonPinBulb);
    bulbOnOff(buttonBulb);
  }
  
  int bulbOnOff(int buttonBulb) {
    unsigned long currentMillis = millis();
    // protect against overflow
    if ( (currentMillis - lastMillis &gt; debounceTime) || (currentMillis &lt; lastMillis)) {
  
      if (buttonBulb != currentStatus) {
        digitalWrite(relay1, buttonBulb);
        //Serial.println(!buttonBulb);
        currentStatus = buttonBulb;
  
        // update database here
        if(s.available()&gt;0)
              {
              s.write(!buttonBulb);
              }
  
        lastMillis = currentMillis;
      }
    }
    return 0;
  }
  </code></pre>
  <p><strong>The following is nodeMCU ESP8266 code</strong></p>
  <pre><code>#include &lt;ESP8266WiFi.h&gt;
  #include &lt;WiFiClient.h&gt;
  #include &lt;ESP8266WebServer.h&gt;
  #include &lt;ESP8266HTTPClient.h&gt;
  #include &lt;SoftwareSerial.h&gt;
  
  SoftwareSerial s(D6, D5); //RX,TX
  int buttonBulb;
  int Led_OnBoard = 2;
  const char* ssid = &quot;iPhone&quot;;                  // Your wifi Name
  const char* password = &quot;Qaser.shah.123&quot;;          // Your wifi Password
  const char *host = &quot;172.20.10.6&quot;; //Your pc or server (database) IP, example : 192.168.0.0 , if you are a windows os user, open cmd, then type ipconfig then look at IPv4 Address.
  
  void setup() {
    // put your setup code here, to run once:
    wifiConnection();
    s.begin(115200);
  }
  
  int wifiConnection() {
    pinMode(Led_OnBoard, OUTPUT);       // Initialize the Led_OnBoard pin as an output
    Serial.begin(115200);
    WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
    delay(1000);
    WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot
  
    WiFi.begin(ssid, password);     //Connect to your WiFi router
    Serial.println(&quot;&quot;);
  
    Serial.print(&quot;Connecting&quot;);
    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
      digitalWrite(Led_OnBoard, LOW);
      delay(250);
      Serial.print(&quot;.&quot;);
      digitalWrite(Led_OnBoard, HIGH);
      delay(250);
    }
  
    digitalWrite(Led_OnBoard, HIGH);
    //If connection successful show IP address in serial monitor
    Serial.println(&quot;&quot;);
    Serial.println(&quot;Connected to Network/SSID&quot;);
    Serial.print(&quot;IP address: &quot;);
    Serial.println(WiFi.localIP());  //IP address assigned to your ESP
    Serial.begin(115200);
  }
  
  void loop() {
    // put your main code here, to run repeatedly:
    s.write(&quot;s&quot;);
    if (s.available() &gt; 0)
    {
      buttonBulb = s.read();
      Serial.println(buttonBulb);
      int result = updateDatabase(buttonBulb);
      if(result != 0){
        //error updating database
        Serial.print(&quot;error updating database&quot;);
        }
    }
  }
  
  int updateDatabase(int buttonBulb){
      HTTPClient http;    //Declare object of class HTTPClient
  
    //String ButtonState;
    String buttonValueSend, postData;
    
    buttonValueSend = String(buttonBulb);   //String to interger conversion
   
    //Post Data
    postData = &quot;buttonBulb=&quot; + buttonValueSend;
    
    http.begin(&quot;http://172.20.10.6/Nodemcu_db_record_view/InsertDB.php&quot;);              //Specify request destination
    http.addHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);    //Specify content-type header
  
    int httpCode = http.POST(postData);   //Send the request
    String payload = http.getString();    //Get the response payload
    
    Serial.println(httpCode);   //Print HTTP return code
    Serial.println(payload);    //Print request response payload
    Serial.println(&quot;Button Value send=&quot; + buttonValueSend);
  
    http.end();  //Close connection
    return 0;
    }
  </code></pre>
  <p><strong>The following is my php code which stores the Arduino data</strong></p>
  <pre><code>&lt;?php
  //Creates new record as per request
      //Connect to database
      $servername = &quot;localhost&quot;;      //example = localhost or 192.168.0.0
      $username = &quot;root&quot;;     //example = root
      $password = &quot;&quot;; 
      $dbname = &quot;automation&quot;;
  
      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn-&gt;connect_error) {
          die(&quot;Database Connection failed: &quot; . $conn-&gt;connect_error);
      }
  
      //Get current date and time
      date_default_timezone_set('Asia/Karachi');
      $d = date(&quot;Y-m-d&quot;);
      $t = date(&quot;H:i:s&quot;);
  
      if(!empty($_POST['buttonBulb']))
      {
          $buttonBulb = $_POST['buttonBulb'];
          
          $sql = &quot;INSERT INTO project (ButtonState, Date, Time) VALUES ('&quot;.$buttonBulb.&quot;', '&quot;.$d.&quot;', '&quot;.$t.&quot;')&quot;; //nodemcu_ldr_table = Youre_table_name
  
          if ($conn-&gt;query($sql) === TRUE) {
              echo &quot;OK&quot;;
          } else {
              echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . $conn-&gt;error;
          }
      }
  
      $conn-&gt;close();
  ?&gt;
  </code></pre>
  <p>See the image of output
  <a href="https://i.stack.imgur.com/jcOOm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jcOOm.png" alt="enter image description here" /></a>
  In this image OK is printing at Value 1 but not printing OK at value 0</p>
  <p>I think there is something wrong or I forgot something, that is why it is not happening.</p>
  <p>Thanks a lot for any appericiation.</p>