I need to update a record in a MySQL table with Arduino UNO. I want to send data from HC-SR06 sensor to db. Firstly I need to see whether a record is updated or not. I am using Ethernet shield and Arduino UNO.
Here is my Arduino source code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177};
EthernetServer server(80);
String txData ="";
String wname = "sensor1";
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
txData = "name="+ wname;
EthernetClient client = server.available();
if (client) {
delay (1000);
Serial.println(" client is ok-->");
boolean current_line_is_blank = true;
while (client.connected())
{
delay (1000);
Serial.println(" client connected-->");
if (client.available())
{
delay (1000);
Serial.println(" client available-->");
Serial.println("Connected to MySQL server. Sending data...");
client.print("POST /update_data.php HTTP/1.1\n");
client.print("Host: 192.168.1.177:80\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(txData.length());
client.print("\n\n");
client.print(txData);
Serial.println("Successfull");
delay (1000);
}
}
delay(1);
client.stop();
}
}
Here is the update_data.php file:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
echo "dbconnect.php will run";
// Connect to MySQL
include("dbconnect.php");
// Prepare the SQL statement
$query = "update arduino.sensors SET value=1
where name = '$_POST[name]' ";
// Go to the review_data.php (optional)
//header("Location: review_data.php");
if(!#mysql_query($query))
{
echo "&Answer; SQL Error - ".mysql_error();
return;
mysql_close();
}
?>
Here is the dbconnect.php file:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$Username = "root"; // enter your username for mysql
$Password = "1234"; // enter your password for mysql
$Hostname = "localhost"; // this is usually "localhost" unless your database resides on a different server
$Database = "arduino"; //database name
$dbh = mysql_connect($Hostname , $Username, $Password) or die (mysql_error());;
if (!$dbh){
die('MySQL ERROR: ' . mysql_error());
}
#mysql_select_db($Database) or die ('MySQL Error:'.mysql_error());
?>
Client is started and I see the logs on browser like this, bu sensor1 record is not updated.
name=sensor1POST /update_data.php HTTP/1.1
Host: 192.168.1.177:80
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 12
You are literally sending:
update arduino.sensors SET value=1
where name = '$_POST[name]'
to the database. Unless you have a sensor called $_POST[name] nothing will happen!
You need to send the value of variable $_POST[name] as a parameter. I suggest you research Prepared Statements, as building a query by concatenating strings is open to SQL injection attacks.
Related
I need to send HTTP POST request using ESP32 module, to my REST API server which coded using python. But when running following code, an error comes. Running on Windows 10 OS. WiFi is connecting with the module. here I need to send "A" to the REST API server.
Code - Running on VSCode PlatformIO IDE.
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char *ssid = "***need to fill***";
const char *password = "***need to fill***";
const char *serverName = "***need to fill***";
String sensorReadings;
String httpGETRequest(const char *serverName);
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void loop()
{
//Check WiFi connection status
if (WiFi.status() == WL_CONNECTED)
{
sensorReadings = httpGETRequest(serverName);
Serial.println(sensorReadings);
JSONVar myObject = JSON.parse(sensorReadings);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined")
{
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(myObject);
// myObject.keys() can be used to get an array of all the keys in the object
JSONVar keys = myObject.keys();
for (int i = 0; i < keys.length(); i++)
{
JSONVar value = myObject[keys[i]];
Serial.print(keys[i]);
Serial.print(" = ");
Serial.println(value);
}
}
else
{
Serial.println("WiFi Disconnected");
}
delay(2000);
}
String httpGETRequest(const char *serverName)
{
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
http.addHeader("Content-Type", "application/json");
// Reading one byte from serial buffer
uint8_t buffer;
Serial.readBytes(&buffer, 1);
// Dummy data to check
// this is the hex value for
// 'A' = 0x41
buffer = 0x41;
// creating payload buff
char buff[100];
sprintf(buff, "{\"Serial_Data\": \"%c\"}", buffer);
// Send HTTP POST request
int httpResponseCode = http.POST(buff);
String payload = "{}";
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
Error comes as below,
[E][WiFiClient.cpp:258] connect(): socket error on fd 54, errno: 113, "Software caused
connection abort"
Error code: -1
{}
JSON object = {}
Though this is very late I have an answer that could help any that will run into this problem. Follow the steps.
Make sure you are running your service on a WiFi IP that your PC and the ESP-32 are connected to. In my case, my Pc is connected to the WiFi with the IP 192.168.43.178 and I am using FastAPI, so I have the following line of code in my run.py
import unicorn
if __name__ == "__main__":
uvicorn.run("app.main:app", host="192.168.43.178", port=8080, reload=True)
Increase the delay. When the dataset is huge, the ESP-32 needs more time to send it, therefore it is necessary to increase the delay to suit your needs of the data sending.
I want to connect my NODEMCU wifi module to live server and then comunicate with rest API. While I was calling simple GET method with plain-text content then everything works fine, problem arises while calling POST and JSON data. Though my server API seems to work fine on ARC(Rest API testing Application).
Working With
Windows 10
Arduino IDE 1.8.12
Linux Live Server (hosted on BIGROCK)
Secure Domain (https://)
API directory permission is 755
LIVE SERVER
<?php
if (strtoupper($_SERVER['REQUEST_METHOD']) != 'POST') {
throw new Exception('Only POST requests are allowed');
}
$content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
if (stripos($content_type, 'application/json') === false) {
throw new Exception('Content-Type must be application/json');
}
$body = file_get_contents("php://input");
$object = json_decode($body, true);
if (!is_array($object)) {
throw new Exception('Failed to decode JSON object');
}
print_r($object);
?>
Arduino IDE
#include <ESP8266WiFi.h>
const char* ssid = "**********";
const char* password = "**********";
const char* host = "www.ameyakrishi.com";
void setup()
{
Serial.begin(115200);
delay(2000);
Serial.print("[Connecting to "); Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");delay(500);
}
Serial.println(" Connected]");delay(1000);
}
void loop()
{
WiFiClient client;
Serial.print("[Connecting to "); Serial.print(host);delay(500);
if (client.connect(host, 80))
{
Serial.println(" Connected]");delay(1000);
String postData = "{\"key\":\"papa\",\"val\":999}";
client.print(String("POST /automation/app.php") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/json \r\n" +
"Content-Length: " + postData.length() + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
while (client.connected() || client.available())
{
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("\n[Disconnected]");delay(1000);
}
else
{
Serial.println("connection failed!]");delay(1000);
client.stop();
}
delay(30000);
}
Serial Monitor Output
All connections are succeed and then print the 500 Internal Server Error response in format.
im trying to connect mysql and nodemcu with php(laravel), but when im running arduino script it come with error that i don't know. can someone help me?
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include<DHT.h> DHT dht(14, DHT11);
//#define TRIGGER_PIN 5 //D1
//#define ECHO_PIN 4 //D2
const char* ssid = "PS-E3";
const char* password = "Pesona30";
const char* host = "192.168.1.19";
WiFiClient client;
const int httpPort = 80;
String url; long duration, distance;
float kelembaban, suhu;
unsigned long timeout;
void setup() {
Serial.begin(9600);
dht.begin();
delay(10);
// pinMode(TRIGGER_PIN, OUTPUT);
// pinMode(ECHO_PIN, INPUT);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void baca_suhu(){
kelembaban = dht.readHumidity();
suhu = dht.readTemperature();
Serial.print("kelembaban : ");
Serial.println(kelembaban);
Serial.print("Suhu : ");
Serial.println(suhu);
}
void loop() {
// Serial.print("baca jarak ");
// baca_jarak(); baca_suhu();
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
//return;
}
// We now create a URI for the request
url = "localhost:8000/cuaca/tampil/save/";
url += suhu;
url += "/";
url += kelembaban;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
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");
Serial.println();
delay(5000);
}
on that host ip already with the ip of my computer.
and the http port already same with the xampp, i dont understand why it come error
anyway this is my script in laravel
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers;
class SimpanController extends Controller
{
public function index(){
$cuaca = DB::table('tb_cuaca')->orderBy('Kode','ASC')->paginate(10);
//passing data ke view
return view('tampilcuaca',['cuaca' => $cuaca]);
}
public function save($suhu, $kelembaban)
{
$date = date("Y-m-d");
// insert data ke table sekolah
DB::table('tb_cuaca')->insert([
'Suhu' => $suhu,
'Kelembaban' => $kelembaban,
'Tanggal' => $date
]);
// alihkan halaman ke halaman tampilsekolah
echo "berhasil";
return redirect('/tampildata');
}
public function tampildata(){
}
// public function store(Request $request)
// {
// // insert data ke table sekolah
// DB::table('tb_prestasi')->insert([
// 'Nama_Sekolah' => $request->nama,
// 'Prestasi' => $request->prestasi
// ]);
// // alihkan halaman ke halaman tampilsekolah
// return redirect('/prestasi');
// }
}
the error come like this
Connecting to PS-E3
scandone
.....scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 7
cnt
connected with PS-E3, channel 5
dhcp client start...
....ip:192.168.1.5,mask:255.255.255.0,gw:192.168.1.1
.
WiFi connected
IP address: 192.168.1.5
kelembaban : 79.00
Suhu : 29.10
connecting to 192.168.1.19
Requesting URL: localhost:8000/cuaca/tampil/save/29.10/79.00
HTTP/1.1 400 Bad Request
Date: Thu, 16 Jan 2020 05:41:33 GMT
Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.9
Vary: accept-language,accept-charset
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=utf-8
Content-Language: en
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Bad request!</title>
<link rev="made" href="mailto:postmaster#localhost" />
<style type="text/css"><!--/*--><![CDATA[/*><!--*/
body { color: #000000; background-color: #FFFFFF; }
a:link { color: #0000CC; }
p, address {margin-left: 3em;}
span {font-size: smaller;}
/*]]>*/--></style>
</head>
<body>
<h1>Bad request!</h1>
<p>
Your browser (or proxy) sent a request that
this server could not understand.
</p>
<p>
If you think this is a server error, please contact
the webmaster.
</p>
<h2>Error 400</h2>
<address>
192.168.1.19<br />
<span>Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.9</span>
</address>
</body>
</html>
closing connection
help me
i already know what the problem is. you only need declared port 8000 only in host variable. it's get bad request because i'm using url with :8000 (because that way to use laravel with localhost). it's work and the data can record in mysql.
I need some support with a personal project Im working on. I have a connected device which sends JSON data at a defined interval (every 1 min / 5 min/ 15 mins etc) to a specific IP address on port 8080.
The JSON that is sent is in following format:
{
"MeterSN": “1234”,
"Status": ###,
“Variable1”: “###”,
"Variable2”: “###”,
"Variable3”: ###
}
I have started building a PHP Rest API to process this data but am somehow not able to save the to mySQL.
Here is what I have so far:
meterdata.php
class MeterDataInput{
private $conn;
private $table_name = "meterdata";
public $MeterSN;
public $StatusA;
public $Variable1;
public $Variable2;
public $Variable3;
public function __construct($db){
$this->conn = $db;
}
}
function createMeterRecord(){
$query = "INSERT INTO
" . $this->table_name . "
SET
MeterSN=:MeterSN, Status=:Status, Variable1=:Variable1, Variable2=:Variable2, Variable3=:Variable3";
// prepare query
$stmt = $this->conn->prepare($query);
// sanitize
$this->MeterSN=htmlspecialchars(strip_tags($this->MeterSN));
$this->Status=htmlspecialchars(strip_tags($this->Status));
$this->Variable1=htmlspecialchars(strip_tags($this->Variable1));
$this->Variable2=htmlspecialchars(strip_tags($this->Variable2));
$this->Variable3=htmlspecialchars(strip_tags($this->Variable3));
// bind values
$stmt->bindParam(":MeterSN", $this->MeterSN);
$stmt->bindParam(":Status", $this->Status);
$stmt->bindParam(":Variable1", $this->Variable1);
$stmt->bindParam(":Variable2", $this->Variable2);
$stmt->bindParam(":Variable3", $this->Variable3);
// execute query
if($stmt->execute()){
return true;
}
return false;
}
index.php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// get database connection
include_once 'config/db.php';
// instantiate MeterData object
include_once 'objects/meterdata.php';
$database = new Database();
$db = $database->getConnection();
$meterdata = new MeterDataInput($db);
// get posted data
$data = json_decode(file_get_contents("php://input"));
// make sure data is not empty
if(!empty($data->MeterSN)){
// set product property values
$meterdata->MeterSN = $data->MeterSN;
$meterdata->Status = $data->Status;
$meterdata->Variable1 = $data->Variable1;
$meterdata->Variable2 = $data->Variable2;
$meterdata->Variable3 = $data->Variable3;
// create the meter data entry
if($meterdata->createMeterRecord()){
// set response code - 201 created
http_response_code(201);
// update the status
echo json_encode(array("message" => "Data record was added"));
}
// if unable to create the record
else{
// set response code - 503 service unavailable
http_response_code(503);
// tell the user
echo json_encode(array("message" => "Unable to add record."));
}
}
// data is incomplete
else{
// set response code - 400 bad request
http_response_code(400);
// tell the user
echo json_encode(array("message" => "Unable to create data record. Data is incomplete."));
}
Obviously i also have config.php and db.php
I am not sure where i am going wrong, however I am not able to see the records popupate within mySQL.
Ok I am having real difficulty solving this. I'm trying to connect to a mysql database from a zend application and i receive the following error:
Message: No database adapter present
I have checked and double checked the connection credentials and they should be fine. The code should be fine too as it works ok in the development environment. If I deliberately change the password to be incorrect in the development environment, I get exactly the same error, which leads me to believe that maybe this is the case, despite my checking!
Any thoughts would be very welcome. If there's nothing obviously wrong here then maybe I need to look at the server/db/php settings?
Thanks!
Bootstrap code:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initPlaceholders(){
Zend_Session::start();
$this->bootstrap('View');
$view = $this->getResource('View');
$view->doctype('XHTML1_STRICT');
// Set the initial stylesheet:
$view->headLink()->appendStylesheet('/css/global.css');
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Pog_');
Zend_Controller_Action_HelperBroker::addPath(
APPLICATION_PATH . '/controllers/helpers',
'Application_Controller_Action_Helper_');
}
}
Config file:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.view[] =
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view.helperPath.View_Helper = APPLICATION_PATH "/views/helpers"
database.adapter = pdo_mysql
database.params.host = localhost
database.params.username = user
database.params.password = password
database.params.dbname = test
DB connection helper:
/**
* Constructor: initialize plugin loader
*
* #return void
*/
public function __construct()
{
try{
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
$dbAdapter = Zend_Db::factory($config->database);
$dbAdapter->getConnection();
$this->connection = $dbAdapter;
} catch (Zend_Db_Adapter_Exception $e) {
echo 'perhaps a failed login credential, or perhaps the RDBMS is not running';
} catch (Zend_Exception $e) {
echo 'perhaps factory() failed to load the specified Adapter class';
}
}
public function getDbConnection(){
return $this->connection;
}
}
Index:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
Define your database as a resource
resources.db.adapter = pdo_mysql
resources.db.params.host = localhost
resources.db.params.username = user
resources.db.params.password = password
resources.db.params.dbname = test
In your main files you then need to do nothing but initiate a query without having to worry about assigning the database fvrom your config - its done in the inside, the DB resource is always chosen as the default adapter for your database transactions