I'm currently reasonably new to both PHP and mySQL and trying to send data to an SQL database using an esp8266 wifi module, but I'm getting the notice
+IPD,0,348:
Notice: Undefined index: lic_no inC:\xampp\htdocs\test1.php onle<2\aphdstt.h/>nln<>< bNie/: dfn nx otoib:xp\dc\s1hr/Etr a uesuy0LSD
But I'm still getting a null
-value in my SQL database on all columns.
I've tried many of the things suggested on this forum without luck.
My PHP code:
<?php
$dbhost = 'localhost';
$dbuser = 'Ecte350';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$lic_no = isset( $_POST['lic_no'] )? $_POST['lic_no']: false;
$speed = isset( $_POST['speed'] )? $_POST['speed']: false;
$location = isset( $_POST['location'] )? $_POST['location']: false;
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'INSERT INTO data '.
'(lic_no, speed, location) '. 'VALUES ("'.$_POST["lic_no"].'", "'.$_POST["speed"].'", "'.$_POST["location"].'" )';
mysql_select_db('ecte350');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
And this is my Arduino code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
//////////////////////
// Library Includes //
//////////////////////
// SoftwareSerial is required (even you don't intend on
// using it).
#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
//#define GPSECHO false
///////////////////
//GPS definitions//
///////////////////
#include <Adafruit_GPS.h>
//SoftwareSerial mySerial(3, 2); //
//Adafruit_GPS GPS(&mySerial);
//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "amg982";
const char myPSK[] = "ashleigh";
//////////////////////////////
// ESP8266Server definition //
//////////////////////////////
// server object used towards the end of the demo.
// (This is only global because it's called in both setup()
// and loop()).
ESP8266Server server = ESP8266Server(80);
IPAddress comServer (192,168,43,151);
//////////////////
// HTTP Strings //
//////////////////
IPAddress destServer (127,0,0,1);
const String htmlHeader = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n\r\n"
"<!DOCTYPE HTML>\r\n"
"<html>\r\n";
const String httpRequest = "POST /test.php\n"
"Host: 192.168.43.151\n"
"Connection: close\r\n";
// All functions called from setup() are defined below the
// loop() function. They modularized to make it easier to
// copy/paste into sketches of your own.
void setup()
{
// Serial Monitor is used to control the demo and view
// debug information.
/////////////
//GPS SETUP//
/////////////
Serial.begin(4800);
// GPS.begin(9600);
// GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); //recommended minimum only
// GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //update rate@1HZ
// GPS.sendCommand(PGCMD_ANTENNA);// anntenna status update
/*******END******/
serialTrigger(F("Press any key to begin."));
// initializeESP8266() verifies communication with the WiFi
// shield, and sets it up.
initializeESP8266();
// connectESP8266() connects to the defined WiFi network.
connectESP8266();
// displayConnectInfo prints the Shield's local IP
// and the network it's connected to.
displayConnectInfo();
serialTrigger(F("Press any key to connect client."));
clientDemo();
serialTrigger(F("Press any key to test server."));
serverSetup();
}
void loop()
{
serverDemo();
}
void initializeESP8266()
{
// esp8266.begin() verifies that the ESP8266 is operational
// and sets it up for the rest of the sketch.
// It returns either true or false -- indicating whether
// communication was successul or not.
// true
int test = esp8266.begin();
if (test != true)
{
Serial.println(F("Error talking to ESP8266."));
errorLoop(test);
}
Serial.println(F("ESP8266 Shield Present"));
}
void connectESP8266()
{
// The ESP8266 can be set to one of three modes:
// 1 - ESP8266_MODE_STA - Station only
// 2 - ESP8266_MODE_AP - Access point only
// 3 - ESP8266_MODE_STAAP - Station/AP combo
// Use esp8266.getMode() to check which mode it's in:
int retVal = esp8266.getMode();
// if (retVal != ESP8266_MODE_STA)
// { // If it's not in station mode.
// Use esp8266.setMode([mode]) to set it to a specified
// mode.
retVal = esp8266.setMode(ESP8266_MODE_STA);
// if (retVal < 0)
// {
// Serial.println(F("Error setting mode."));
//errorLoop(retVal);
// }
// }
Serial.println(F("Mode set to station"));
// esp8266.status() indicates the ESP8266's WiFi connect
// status.
// A return value of 1 indicates the device is already
// connected. 0 indicates disconnected. (Negative values
// equate to communication errors.)
retVal = esp8266.status();
if (retVal <= 0)
{
Serial.print(F("Connecting to "));
Serial.println(mySSID);
// esp8266.connect([ssid], [psk]) connects the ESP8266
// to a network.
// On success the connect function returns a value >0
// On fail, the function will either return:
// -1: TIMEOUT - The library has a set 30s timeout
// -3: FAIL - Couldn't connect to network.
retVal = esp8266.connect(mySSID, myPSK);
if (retVal < 0)
{
Serial.println(F("Error connecting"));
errorLoop(retVal);
}
}
}
void displayConnectInfo()
{
char connectedSSID[24];
memset(connectedSSID, 0, 24);
// esp8266.getAP() can be used to check which AP the
// ESP8266 is connected to. It returns an error code.
// The connected AP is returned by reference as a parameter.
int retVal = esp8266.getAP(connectedSSID);
if (retVal > 0)
{
Serial.print(F("Connected to: "));
Serial.println(connectedSSID);
}
// esp8266.localIP returns an IPAddress variable with the
// ESP8266's current local IP address.
IPAddress myIP = esp8266.localIP();
Serial.print(F("My IP: ")); Serial.println(myIP);
}
void clientDemo()
{
// To use the ESP8266 as a TCP client, use the
// ESP8266Client class. First, create an object:
ESP8266Client client;
// ESP8266Client connect([server], [port]) is used to
// connect to a server (const char * or IPAddress) on
// a specified port.
// Returns: 1 on success, 2 on already connected,
// negative on fail (-1=TIMEOUT, -3=FAIL).
int retVal = client.connect(comServer, 80);
if (retVal <= 0)
{
Serial.println(F("Failed to connect to server."));
return;
}
// print and write can be used to send data to a connected
// client connection.
client.print(httpRequest);
// available() will return the number of characters
// currently in the receive buffer.
while (client.available())
Serial.write(client.read()); // read() gets the FIFO char
// connected() is a boolean return value - 1 if the
// connection is active, 0 if it's closed.
if (client.connected())
client.stop(); // stop() closes a TCP connection.
}
void serverSetup()
{
// begin initializes a ESP8266Server object. It will
// start a server on the port specified in the object's
// constructor (in global area)
server.begin();
Serial.print(F("Server started!"));
Serial.println(esp8266.localIP());
Serial.println();
}
void serverDemo()
{
float lic_no = 15;
// available() is an ESP8266Server function which will
// return an ESP8266Client object for printing and reading.
// available() has one parameter -- a timeout value. This
// is the number of milliseconds the function waits,
// checking for a connection.
ESP8266Client client = server.available(500);
if (client)
{
Serial.println(F("Client Connected!"));
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank)
{
if (client.connect(comServer, 80)==1) {
client.print("POST /test.php? "); // This
client.print("&lic_no="); // This
client.print(lic_no); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
client.println("HTTP/1.1"); // Part of the GET request
client.println("Host: 192.168.43.151"); // 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\r\n"); // Part of the GET request telling the server that we are over transmitting the message
client.println(); // Empty line
client.println(); // Empty line
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");
}
}
if (c == '\n')
{
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r')
{
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1000);
// close the connection:
client.stop();
Serial.println(F("Client disconnected"));
}
}
// errorLoop prints an error code, then loops forever.
void errorLoop(int error)
{
Serial.print(F("Error: ")); Serial.println(error);
Serial.println(F("Looping forever."));
for (;;)
;
}
// serialTrigger prints a message, then waits for something
// to come in from the serial port.
void serialTrigger(String message)
{
Serial.println();
Serial.println(message);
Serial.println();
while (!Serial.available())
;
while (Serial.available())
Serial.read();
}