ESP8266 to XAMPP server - php

Recently I have been trying to work with my ESP. I have set up a server using XAMPP on my laptop. I can access that server using 127.0.0.2. I know that the content viewed on this page is present in the htdocs folder in XAMPP.
So I have created a folder named TEST in htdocs. The ESP is connected to PSoC and is sending some data at regular intervals to the TEST folder on the server. My question is: what is the IP address that will be needed to connect to the server when I use the AT+CIPSTART command on the ESP side?
Is it 127.0.0.2 or some other IP from the router? I have tried sending data to ThingSpeak before and there they provide a ready-made GET request link to send data to the server. What will be the GET request link in my case if the server is created using XAMPP? Please help as I am new to networking. Thank you.
Ok. I wrote a php file which takes value in a variable SAP and writes into a text file which is stored in the ht docs folder. The php code is as follows
'
$content = "SAP ID :".$var1." is present for the lecture \r\n";
echo $content;
echo "<br >";
$status = file_put_contents('attendance_record.txt',$content,FILE_APPEND);
if ($status != false)
{
echo "Data is written to the file :p ";
}
else
{
echo "Data was not written into the file :( ";
}
?>'
After then I enable the Xampp server and access this file by using 'http://127.0.0.2/receiver.php?SAP=104' Until this point everything is working fine and the text file is being updated. Now the same thing is needed to be done via AT commands using the ESP. The following lines of code is running on the ESP side.
AT+CWJAP="SSID","PASSWORD"
AT+CIPSTART="TCP","192.168.0.104",80
AT+CIPSEND=35
GET /receiver.php?SAP=69 HTTP/1.1
Now i'm not getting any updates on the text file. Any help will be appreciated. Thank you.

172.0.0.2 is the same at calling 'localhost' so you would need your laptop's IP for the ESP to connect to. Depending on your OS you can find that opening your terminal and write ipconfig(windows) or ifconfig(linux) On a MAC go to System Preferences > Network and your IP is displayed under'Status:'

Related

Website to read file on diff raspberry pi and display values based on variable

I'm trying to create a website and 2 raspberry pi's that will connect to the website.
The goal is:
rpiA's display will be different from rpiB.
I'll put a file on each rpi containing their name. When I visit the website using rpiA, rpiA's name will be displayed (vice versa).
What I did is I placed a file in var/www/html/name.php
<?php
$xname= 'RPI-001';
echo $xname;
?>
Then on the website I placed:
$device_name = file_get_contents('http://127.0.0.1/name.php');
echo $device_name;
However, the result is always empty. I checked the allow_url_fopen and it is On. The reason maybe the 127.0.0.1 is broad and it needs a specific ip?
I also tried curl but the result is Error 404.
Is there another way to do this?
I did not consider login or session since I'm using a small screen in rpi and it will be hard to type without vnc.
127.0.0.1 is the IP of the system running the request. If you run that file_get_contents on any central server and access this using a browser, it will always access the server itself.
If you want to access any website on the system that is accessing the website, you need to use the IP address of the client, like file_get_contents('http:/' . $_SERVER['REMOTE_ADDR'] . '/name.php')

Is there a way in PHP to store printjobs that are sent via a Windows PC?

I am working on a project for school. I am wondering if there is a way in PHP to listen for print jobs and store them once received? I currently have a webserver that is accepting webhooks from other applications, parsing out the JSON and storing in a MsSQL. Is this the wrong path to take? thanks
Ok – I don’t know if this is a solution but here is my attempt at trying to help :)
Disclaimer: I am not affiliated to any of the links posted here and (or) make no recommendations.
General assumptions:
PHP is the server side language used in all instances (the theory should work with others as well – using PHP as the question was tagged as such)
Sender and receiver servers are different (it is more easier if both are on the same box – but not a show stopper)
Access to both sender and receiver webservers
Windows set up (WAMP or alternative)
You are securing your set up / directory accesses as you go along
Option 1 (Custom sender / receiver)
On the box that is sending the print request:
Create a PHP script to output the files to a temp directory
Create file with required extension (please see documentation if required)
Put contents
Close connection / file - https://www.php.net/manual/en/function.file-put-contents.php
Use powershell or alternative to put the files in the temp directory into a remote directory on the
https://blogs.msdn.microsoft.com/luisdem/2016/08/31/powershell-how-to-copy-a-local-file-to-remote-machines/
Run this at an interval of your choice
On the box that is receiving the file:
Create a PHP script to read files on remote
Show files to print in a list which can be downloaded to local terminal / device and printed
Option 2 (Leverage an existing print server)
In this option you would only need to amend the sender webserver config:
[inspired by: https://www.hashbangcode.com/article/printing-directly-php]
Download php_printer.dll for your version (https://windows.php.net/downloads/pecl/snaps/printer/0.1.0-dev/)
Enable in php.ini via: printer.default_printer=PHP_INI_ALL extension=php_printer.dll
Set the printer as your print server address and send your output to print directly from the Webserver as follows from your application:
`
$data= "Hello";
printer_set_option($ph, PRINTER_MODE, "RAW");
printer_write($ph, $data);
printer_close($ph);
}
else "Couldn't connect...";
?>'
Option 3 (Use a plugin):
https://www.neodynamic.com/products/printing/raw-data/php/

fopen() failure on external server

I had created this CMS system which uses a mySQL database and creates a file on the server.
The database side works as intended, but the creation of the file not so well.
Everything works perfectly on my localhost but the creation of the file is not working right on the external WWW server.
I had created a basic script to test the functionality of fopen() on the server. That had worked correctly, but for the real script it doesn't work.
This is the code in the real script:
if(fopen(strtolower("../News/" . $titleURL . ".php"), "w+")){
$createdPage = fopen(strtolower("../News/" . $titleURL . ".php"), "w+");
echo "page created";
}else{
echo "creation failed";
}
When this page is run, I get creation failed.
This code runs fine on the localhost, but why not on the external site?
EDIT:
My hosting service does not block fopen()
IT is very likely that the configuration for your host server (and/or the target server) does not allow any file manipulation on/by a foreign server.
Problem is resolved. I had just cleaned up the code a bit, cleaning up a lot of concatenating URLs. It appears the server did not appreciate me concatenating file locations. Everything is working fine.

How to upload file in php from browser with no user input

I am working on a php web app .
I need to upload a file to the web server, with customer info - customers.csv.
but this process needs to be automated ,
The file will be generated in a Point of Sale app , and the app can open a browser window with the url ...
first i taught i would do something like this www.a.com/upload/&file=customers.csv
but read on here that is not possible,
then i taught i would set a value for the file upload field and submit form automatically after x seconds. Discovered thats not possible .
Anybody with a solution , will be appreciated .
EDIT
I have tried this and it works ,file is uploaded to remote server
is it working only because the php script is running on the same pc where csv is sitting ???
$file = 'c:\downloads\customers.csv';
$remote_file = 'customers.csv';
// set up basic connection
$conn_id = ftp_connect('host.com');
// login with username and password
$login_result = ftp_login($conn_id,'user','password');
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
This is of course not possible, imagine how this could be abused to upload on linux as example the /etc/passwd. The only way it might be possible is to use a Java Applet, but this is for sure not the best way.
You could try to let your PoS Application make a web request with the customers.csv file and let a WebAPI handle the upload, this may be possible, but I have no expierence with Point of Sale Applications.
Best might be, if the solution above cannot be considered, to just prompt the user to provide the file above and check over name + content if it is the correct one.
This is a bit tricky, but if your CSV is not too long, you could encode it in base64, send to the webserver as a GET parameter and then, in the server side, decode and store it as a CSV file.
If the file is too big to do that, you have to use other method, like the java applet pointed by #D.Schalla or even install and configure a FTP server, and make the Point of Sale app uploads the file there.
Other alternative, specially good if you cannot modify the sale app, is to install a web server in the client side and write a small php script to handle the upload process. In this way, the sale app could call a local url (something like: http:// localhost/upload.php) and it's this script the one in charge to upload the file which can be achieve with a classical HTTP POST, a FTP connection or any other way you can think about.
MY Solution , which will work with out setting up web server on client side.
This is for windows but can be adapted to linux
On client side
Local Application opens cmd and runs this command ftp -n -s:C:\test.scr
WHICH opens test.scr - a file with ftp commands e.g.
open host.com
user1
passwOrd
put C:\downloads\customers.csv public_html/customers.csv
more info here :
http://support.microsoft.com/kb/96269
more commands :
http://www.nsftools.com/tips/MSFTP.htm#put

PHP MySQL fails when page is viewed remotely

I have two servers on my local network - one a web frontend and the other a MySQL backend. I have a PHP script that looks like this:
<?php
error_reporting(-1);
echo "Connecting...\n";
$link = mysql_connect("192.168.1.15", "-----", "-----") or die(mysql_error());
echo "Communicating with the server...";
mysql_query("INSERT INTO .....
//More code down here...
?>
This script is called on my web frontend to connect to the backend server. When this script is accessed from the local network (i.e. when I open the page by going to http://192.168.1.14), the script outputs
Connecting...
Communicating with the server...
and a row is added to the database, as it should. However when I connect remotely (i.e. going to http://myDomainName.com/mysql_insert_script.php) from a connection not on the local network, all I see is:
Connecting...
No error messages follow, the script just cuts off, and no data is added to the database. When I place a second, 'proxy' script on the server that simply requires() the above script and then I access the proxy remotely, everything works fine. Below is the proxy script, so you can get a better idea of what works and what does not:
<?php
//this script makes it appear that the mysql_script is being viewed from the local network
//I exist on the web frontend at 192.168.1.14
require("http://192.168.1.15/mysql_insert_script.php");
?>
I am sorry if I can't provide any more information, but I am stumped. Any help would be appreciated.
Chris
P.S. - I have verified that the mysql server is accessible from external hosts on the local network, but I have a firewall that prevents connections from outside my network. I don't think this would matter, however, as the MySQL server and the PHP script connecting to it are both run on the local net.
you got wrong server name

Categories