database connection error

Bluetooth, XBee, RF......

database connection error

Postby izzat » Mon Mar 06, 2017 2:10 am

i always get this error."--> connection failed". i guess it cause by this statement "if (client.connect(server, 80))". what you guys think ?

arduino code
CODE: SELECT_ALL_CODE
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <CytronWiFiServer.h>
#include <SoftwareSerial.h>
#define trigPin 13
#define echoPin 12
ESP8266Client client;

//connection to wifi
const char *ssid = "iPhone";     
const char *pass = "kasipecah10";   
IPAddress ip(172, 20, 10 , 1);   

//ultrasonic sensor
void sensorDetection()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  if ((distance <= 7))
  {
    saveDb();
    Serial.print(distance);
    Serial.println(" cm");
    delay(5000);
  }

  else
  {
    Serial.println ("range is to far, try again with short range");
  }
  delay(1000);
}

void setup() {

  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  if (!wifi.begin(2, 3))
  {
    Serial.println(F("Error talking to shield"));
    while (1);
  }
  Serial.print(F("Mode: "));
  Serial.println(wifi.getMode()); // 1- station mode, 2- softap mode, 3- both
  wifi.config(ip);
  Serial.println(F("Start wifi connection"));
  if (!wifi.connectAP(ssid, pass))
  {
    Serial.println(F("Error connecting to WiFi"));
    while (1);
  }
  Serial.print(F("Connected to ")); Serial.println(wifi.SSID());
  Serial.println(F("IP address: "));
  Serial.println(wifi.localIP());
  wifi.updateStatus();
  Serial.println(wifi.status()); //2- wifi connected with ip, 3- got connection with servers or clients, 4- disconnect with clients or servers, 5- no wifi
  sensorDetection();
}

void loop()
{
  sensorDetection();
}

//connection and save to database
char server[] = "172.20.10.10"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
void saveDb()
{
  // Connect to the server (your computer or web page)
  if (client.connect(server, 80))
  {
    Serial.print("You Have 1 Mail!\n");
    client.print("GET /write_data.php?"); // This
    client.print("value="); // This
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 172.20.10.10"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.stop();    // Closing connection to server
  }

  else
  {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }
}


php code
CODE: SELECT_ALL_CODE
<?php

    // Prepare variables for database connection
   
    $dbusername = "root";  // enter database username, I used "arduino" in step 2.2
    $dbpassword = "";  // enter database password, I used "arduinotest" in step 2.2
    $servera = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"

    // Connect to your database

    $dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
    $dbselect = mysql_select_db("test",$dbconnect);

    // Prepare the SQL statement

    $sql = "INSERT INTO history (value) VALUES ('".$_GET["value"]."')";   

    // Execute SQL statement

    mysql_query($sql);

?>
izzat
Greenhorn
 
Posts: 3
Joined: Mon Feb 20, 2017 10:56 pm

Re: database connection error

Postby izzat » Mon Mar 06, 2017 2:16 am

i tried to declare char y = 1 and change this statement "if (client.connect(server, 80))" to -> if (y = 1) and i get "You Have 1 Mail!". That why i think the problem is with this statement "if (client.connect(server, 80))". but i dont know what is the problem with this statement.
izzat
Greenhorn
 
Posts: 3
Joined: Mon Feb 20, 2017 10:56 pm

Re: database connection error

Postby bengchet » Mon Mar 06, 2017 10:05 am

Hi,

You can try a few things here.

1) Remove wifi.config(ip), allow your shield gets random IP address assigned by router.
2) Change char server[] ="172.20.10.10" to IPAddress server(172, 20, 10, 10).
3) Make your request into one liner such as client.println("GET /write_data.php?value= HTTP/1.1/r/nHost: 172.20.10.10/r/nConnection: close")
4) I don't know if you are trying to get the data from the database, but seems like I can't find the code for reading the result from the database. You might need do some coding. Before reading the result, avoid using client.stop(). Look at CytronWiFiDemo sketch void clientTest() for the example to retrieve the result.

Hope it helps. Thanks.
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am


Return to Wireless Device

Who is online

Users browsing this forum: No registered users and 1 guest

cron