Java Arduino处理客户端无法将文件上载到PHP服务器
背景 我想要压力传感器(它们正在工作)触发内置摄像头,在猫睡觉时拍照,上传照片并给我发电子邮件,这样我就可以在网站上查看实时图像 PHP服务器 我有一个Java Arduino处理客户端无法将文件上载到PHP服务器,java,network-programming,arduino,processing,Java,Network Programming,Arduino,Processing,背景 我想要压力传感器(它们正在工作)触发内置摄像头,在猫睡觉时拍照,上传照片并给我发电子邮件,这样我就可以在网站上查看实时图像 PHP服务器 我有一个php服务器在这个结构的根上运行127.0.0.1:8080: NetworkedCat --> data --> script-cat.php index.html NetworkedCat.pde
php服务器在这个结构的根上运行
127.0.0.1:8080
:
NetworkedCat -->
data --> script-cat.php
index.html
NetworkedCat.pde
img.jpg
save2web.php
swiftmailer --> libs, etc
在浏览器上测试,save2web.php
和cat script.php
正在工作,也就是说,脚本正在上传和发送电子邮件
Arduino
Arduino应用程序应执行以下操作:
NetworkedCat.pde
不是由串行事件触发的
请注意:
在另一个端口80
打开localhost
,因为php服务器
在8080
上工作
如果我缩短处理代码,只测试图像捕获和上传,它就可以工作了。因此,该bug必须与串行事件相关
为什么下面的处理代码不起作用
/*Serial String reader
Context: Arduino
Reads in a string of characters until it gets a linefeed (ASCII 10).
then converts the string into a number
*/
import processing.serial.*;
import processing.video.*;
import processing.net.*;
Serial myPort; //the serial port
float sensorValue = 0; //the value form the sensor
float prevSensorValue = 0; //previous value from the sensor
int threshold = 200; //above this number, the cat is on the mat
int currentTime = 0; //the current time as a single number
int lastMailTime = 0; //last minute you sent a mail
int mailInterval = 60; //minimum seconds betweeen mails
String mailUrl = "cat-script.php";
int lastPicture = 0; //last minute you sent a picture
int lastPictureTime = 0; //last minute you sent a picture
int pictureInterval = 10; //minimum seconds between pictures
Capture myCam; //camera capture library instance
String fileName = "img.jpg"; //file name for the picture
//location on your server for the picture script:
String pictureScriptUrl = "save2web.php";
String boundary = "----H4rkNrF"; //string boundary for the post request
Client thisClient; //instance for the net library
//float xPos = 0; //horizontal position of the graph
//float lastXPos = 0; //previous horizontal position
void setup(){
size(400, 300);
//list all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[7], 9600);
//reads bytes into a buffer until you get a newline (ASCII 10);
myPort.bufferUntil('\n');
//set initial background and smooth drawing:
background(#543174);
smooth();
//for a list of cameras on your computer, use this line:
println(Capture.list());
//use the default camera for capture at 30 fps
myCam = new Capture(this, width, height, 30);
myCam.start();
}
void draw(){
//make a single number fmor the current hour, minute, and second
currentTime = hour() * 3600 + minute() * 60 + second();
if (myCam.available() == true) {
//draw the camera image to the screen;
myCam.read();
set(0, 0, myCam);
//get the time as a string
String timeStamp = nf(hour(), 2) + ":" + nf(minute(), 2)
+ ":" + nf(second(), 2) + " " + nf(day(), 2) + "-"
+ nf(month(), 2) + "-" + nf(year(), 4);
//draw a dropshadow for the time text:
fill(15);
text(timeStamp, 11, height - 19);
//draw the main time next
fill(255);
text(timeStamp, 10, height - 20);
}
}
void serialEvent (Serial myPort){
//get the ASCII string
String inString = myPort.readStringUntil('\n');
if (inString != null){
//trim off any whitespace:
inString = trim(inString);
//convert to an int and map to the screen height
sensorValue = float(inString);
//println(sensorValue);
sensorValue = map(sensorValue, 0, 1023, 0, height);
println(sensorValue);
if (sensorValue > threshold){
if(currentTime - lastPictureTime > pictureInterval){
PImage thisFrame = get();
thisFrame.save(fileName);
postPicture();
lastPictureTime = currentTime;
}
//if the last reading was less than the threshold
//then the cat just got on the mat
if(prevSensorValue <= threshold){
println("Cat is on the mat.");
sendMail();
}
}
else{
//if the sensor value is less than the threshold,
//and the previous value was greater, then the cat
//just left the mat
if (prevSensorValue > threshold){
println("Cat is not on the mat.");
}
}
//save the current value for the next time
prevSensorValue = sensorValue;
}
}
void sendMail(){
//how long has passed since the last mail
int timeDifference = currentTime - lastMailTime;
if( timeDifference > mailInterval){
String[] mailScript = loadStrings(mailUrl);
println("results from mail script:");
println(mailScript);
//save the current minute for next time
lastMailTime = currentTime;
}
}
这就是提出的问题:
java.lang.NullPointerException
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at javax.imageio.stream.FileCacheImageOutputStream.close(FileCacheImageOutputStream.java:238)
at com.sun.imageio.stream.StreamCloser$CloseAction.performAction(StreamCloser.java:130)
at com.sun.imageio.stream.StreamCloser$1.run(StreamCloser.java:74)
at java.lang.Thread.run(Thread.java:745)
加上一个而不是一个数字
例外:
sensorValue=map(NaN,1023,0,高度)代码>
我的系统是Unix。最好在特定版本的stack exchange上问这个问题
我的建议是让Arduino给(猫)拍照。并将图片上传到PHP web应用程序
然后,PHP web应用程序应接收文件上载(图片),并向希望通过电子邮件接收的用户发送电子邮件
似乎您正在端口8080
上运行PHP服务器,那么Arduino处理应用程序也需要连接到该端口!因此,请更新您的代码,以便客户端连接到服务器:
Arduino处理(客户端)需要知道PHP服务器在网络上的位置。因此,它需要知道服务器的DNS名称或IP地址。因此,请更正下面代码中的字符串“name或ip”
//open a new connection to the server
thisClient = new Client(this, "name-or-ip", 8080);
提示:如果Arduino处理与PHP服务器在同一台计算机上运行,则“localhost”
将用作服务器连接。字符串mailUrl
应包括整个字符串:“http://127.0.0.1:8080/cat-script.php“
,而且不仅仅是相对于根目录的脚本位置cat script.php
另外,在save2web.php
脚本中有一个bug-在这个问题中不存在,它阻止了图像的上传<代码>移动上传的文件($fileTempName,$path.fileName)代码>未包含“.fileName”
)
现在一切正常。你检查过你试图上传文件的目录的权限了吗?@vnpnlz没有,但是如果我绕过处理运行PHP脚本,文件会成功上传到同一个目录。在这段代码中,你的后期图片中似乎有一些bug:1。方法POST
和路径之间必须有一个空格。2.斜线应位于路径的开头。3. <代码>\r\n
应在HTTP中用作换行符,而不是\n
。4.您在数据的开头和结尾有多余的换行符。@MikeCAT,它必须是一个Cat,对于Cat项目。。。1.好的。您的意思是在声明变量时,“/save2web.php”
?3.您的意思是只在这里“HTTP/1.1\r\n”
?4.“数据”是什么意思?@Verhagen功能:我想要压力传感器(它们正在工作)触发内置摄像头,在猫睡觉时拍照,上传图片并发电子邮件给我,这样我就可以在网站上查看实时图像。嘿,我仍然得到与上述相同的Java异常,尽管将端口切换到8080。但我意识到我在这一行也遇到了NaN
异常:sensorValue=map(NaN,01023,0,height)代码>;不知道为什么sensorValue
是NaN
;控制台上正确打印浮点数。。
//open a new connection to the server
thisClient = new Client(this, "name-or-ip", 8080);