hi
when i try to run a script PHP from arduino i have this error , lease simeone help me
[WiFiEsp] Data packet send error (1)
[WiFiEsp] Failed to write to socket 3
hi
when i try to run a script PHP from arduino i have this error , lease simeone help me
[WiFiEsp] Data packet send error (1)
[WiFiEsp] Failed to write to socket 3
The arduino sketch is :
#include <SoftwareSerial.h>
#include "WiFiEsp.h"
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
//----------------------------------------------------------------------
char ssid[] = "LG K4 (2017)"; // your network SSID (name)
char pass[] = "*****"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
SoftwareSerial Serial1(10, 11);
// Initialize the Ethernet client object
WiFiEspClient client;
//----------------------------------------------------------------------
/---- Capteur ----/
int gazPin = 2;
int buzzer = 7;
/---- ----/
// Your threshold value. You might need to change it.
int sensorThres = 300;
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(192,168,43,163); // IP of the MySQL server here (@ ip de pc)
char user[] = "root"; // MySQL user login username
char password[] = ""; // MySQL user login password
MySQL_Connection conn((Client *)&client);
//char server[] = "192,168,43,163";
// Initiate the query class instance
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(gazPin, INPUT);
Serial.begin(9600); // initialize serial for ESP module
while (!Serial); // wait for serial port to connect
Serial1.begin(9600); // initialize ESP module
WiFi.init(&Serial1);
status=WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
else {
Serial.println("Connected to network");
IPAddress ip = WiFi.localIP();
Serial.print("My IP address is: ");
Serial.println(ip);
}
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
Serial.println("Connected to MySQL");
delay(1000);
}else
Serial.println("Connection failed.");
}
void loop() {
// put your main code here, to run repeatedly:
Gaz_test();
delay(5000);
}
/**************************************************************************
**
*
***************************************************************************/
void Gaz_test(){
int gval=analogRead(gazPin);
if (gval >=sensorThres )
{
Serial.println("Gaz opened");
tone(buzzer, 600, 300);
Serial.print("Attention !!");
Serial.print("!!! -Gaz- !!!");
delay(500);
}
else
{
noTone(buzzer);
}
Serial.print(" LGP :");
Serial.println(gval);
row_values *row = NULL;
int id = 0;
// Sample query
char query[] = "SELECT ID_fait FROM smart_home.fait WHERE ID_Objet =2 " ;
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(query);
// Fetch the columns (required) but we don't use them.
column_names *columns = cur_mem->get_columns();
// Read the row (we are only expecting the one)
do {
row = cur_mem->get_next_row();
if (row != NULL) {
id = atol(row->values[0]);
Serial.println(query);
}
} while (row != NULL);
// Deleting the cursor also frees up memory used
delete cur_mem;
// Show the result
Serial.print(" id = ");
Serial.println(id);
if(id==0){
if (client.connected()) {
client.println("GET /add.php?"); // This
client.println("Nom="); // This
client.println("Temperature");
client.println("&&");
client.println("Valeur="); // This
client.println(gval);
client.println("&&");
client.println("ID_Objet="); // This
client.println(gazPin);
// 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.0\r\n"); // Part of the GET request
client.println("Host: 192,168,43,163");
// 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.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");
}
}
else{
if (client.connected()) {
client.println("GET /update.php?"); // This
client.println("Valeur="); // This
client.println(gval);
client.println("&&");
client.println("ID_Objet="); // This
client.println(gazPin);
// 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.0\r\n"); // Part of the GET request
client.println("Host: 192,168,43,163");
// 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.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");
}
}
}
client.println("GET /add.php?"); // This
client.println("Nom="); // This
client.println("Temperature");
client.println("&&");
client.println("Valeur="); // This
client.println(gval);
client.println("&&");
client.println("ID_Objet="); // This
client.println(gazPin);
// 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.0\r\n"); // Part of the GET request
The separator between name=value pairs is NOT &&. It is &.
The client you are using is the one to connect to the MySQL instance. You can NOT make GET requests to that instance.
// Read the row (we are only expecting the one)
do {
row = cur_mem->get_next_row();
if (row != NULL) {
id = atol(row->values[0]);
Serial.println(query);
}
} while (row != NULL);
Why are you printing the query after each row?