So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Nautical Space
#89534 Hai, I'm a newbie here. I've a problem with my project. I'd like to send my sensor data to local database(MySql/XAMPP). I'm using Wemos D1 mini and max30102 as a sensor. It's success to connect to WiFi but in the serial monitor it's failed to connect to localhost.

This is my ESP8266/Wemos code
Code: Select all#include <ESP8266WiFi.h>
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
WiFiClient client;

char * ssid     = "fjarian";
char * password = "rgyp3776";
char * host = "localhost";


void setup()
{
 
   Serial.begin(115200);
  Serial.println("Initializing...");
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST))
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }
  Serial.println("Place your index finger on the sensor with steady pressure.");
  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
  particleSensor.enableDIETEMPRDY();
  delay(1000);
    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}



void loop()
{
  long irValue = particleSensor.getIR();
  float temperature = particleSensor.readTemperature();

  while (checkForBeat(irValue) == true)
  {
    //We sensed a beat!
    long delta = millis() - lastBeat;
    lastBeat = millis();
   

    beatsPerMinute = 60 / (delta / 1000.0);

    if (beatsPerMinute < 255 && beatsPerMinute > 20)
    {
      rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
      rateSpot %= RATE_SIZE; //Wrap variable

      //Take average of readings
      beatAvg = 0;
      for (byte x = 0 ; x < RATE_SIZE ; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
    }
  }
  float bpm = beatsPerMinute;
  int avgbpm = beatAvg;

   
    Serial.print("connecting to ");
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
    }

 


    // This will send the request to the server
 client.print(String("GET http://localhost/iot_project/connect.php?") +
                          ("&temperature=") + temperature +
                          ("&bpm=") + bpm +
                          ("&avgbpm=") + avgbpm +
                          " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout > 1000) {
            Serial.println(">>> Client Timeout !");
            client.stop();
            return;
        }
    }

    // Read all the lines of the reply from server and print them to Serial
    while(client.available()) {
        String line = client.readStringUntil('\r');
        Serial.print(line);
       
    }

    Serial.println();
    Serial.println("closing connection");
}


and this is my php code for connection
Code: Select all
<html>
<body>

<?php

$dbname = 'example';
$dbuser = 'example'; 
$dbpass = '';
$dbhost = 'localhost';

$connect = @mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

if(!$connect){
   echo "Error: " . mysqli_connect_error();
   exit();
}

echo "Connection Success!<br><br>";

$temperature = $_GET["temperature"];
$humidity = $_GET["bpm"];
$humidity = $_GET["avgbpm"];


$query = "INSERT INTO iot_project (temperature, bpm, avgbpm) VALUES ('$temperature', '$bpm', '$avgbpm')";
$result = mysqli_query($connect,$query);

echo "Insertion Success!<br>";

?>
</body>
</html>
User avatar
By Pablo2048
#89544 Not really any need to going back. Just the Nautical Space didn't realize, that the `localhost` is symbolic name for loopback (127.0.0.1), and would be used only in case of testing on the same PC, where the web reside. Solution of this problem is to get the real ip address of the PC and put it into sketch `host` variable :D ...