LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices


Reply
  Search this Thread
Old 12-05-2016, 09:04 AM   #1
istabraq
LQ Newbie
 
Registered: Nov 2016
Posts: 11

Rep: Reputation: Disabled
Forbidden 403 nodemcu Apache


hi im using nodemcu trying to connect to mysql data base but i have 400 bad request
please help:

Code:
#include <ESP8266WiFi.h>

const char* ssid     = "****";
const char* password = "****";
const char* host = "192.168.0.112";
const unsigned int SENSOR_PIN = A0;
String strURL = "";
void setup()
{
  Serial.begin(115200);
  delay(10);

  // 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() {
 int sensor = analogRead(SENSOR_PIN);
  //String sensor = String(sensor);

    WiFiClient client; 
  if (client.connect(host, 80)) {
 
    Serial.println("connected");

strURL = "GET /connect.php?sensor=";
strURL += (int)sensor;
strURL += " HTTP/1.1\r\n";
client.println(strURL);
client.println("Host: localhost\r\n");
client.print("Connection: close\r\n\r\n");

client.println();
  }
    else {
    Serial.println("–> connection failed/n");
  }
  delay(50000);
  
   if (client.available()) {
     char c = client.read();    
     Serial.print(c);
   }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
     Serial.println();
     Serial.println("disconnecting.");
     client.stop();

     for(;;)     ;  
   }
}
PHP Code:
<?php 
$servername 
"localhost"
$username "root";  
$password "";

//$sensor = $_GET["sensor"]; 
$conn mysql_connect($servername$username$password);
if (
$conn) {
    echo 
"Connected successfully";

else {
    echo 
"connection failed";
}

$conndb mysql_select_db('coe'$conn);

echo  
"<br>";

//$sql_insert ="insert into pulses (sensor) values ('$sensor')";
$sql_insert ="insert into pulses (sensor) values ('".$_GET["sensor"]."')";

if(
$sql_insert){
    echo 
"insert successfull";
}
else {
    echo 
"insert failed";
}

$result mysql_query($sql_insert);
if(
$result){
    echo 
"insert successfull";
}
else {
    echo 
"insert failed" mysql_error($result);
}

?>
 
Old 12-07-2016, 09:10 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,680

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
I unfamiliar with Arduino programing but might be able to help with basic troubleshooting.

You posted two different errors 400 and 403. Which one is correct?

On the server itself you should be able to check your php script by entering:

http://localhost/connect.php?sensor=1 (or whatever is a suitable value for your sensor).

Using the example as a guide I would first try sending a fixed value. The two examples show different methods but try the first.
https://www.arduino.cc/en/Reference/WiFiClient
https://www.arduino.cc/en/Tutorial/WiFiWebClient


Code:
 if (client.connect(server, 80)) {
    client.println("GET /connect.php?sensor=1 HTTP/1.1");
    client.println();
...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Apache - 403 Forbidden / madsovenielsen Slackware 3 08-10-2011 02:44 PM
Apache - 403 Forbidden habl Slackware 9 09-09-2008 02:08 AM
Apache 403 Forbidden mrmike Linux - Newbie 3 07-10-2004 11:31 AM
Apache Webserver 403 Forbidden Errors (User not in apache group?) Mankind75 Mandriva 4 07-08-2004 05:30 AM
Apache 403 Forbidden quozt Linux - Networking 10 02-24-2004 04:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 10:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration