[Serial2Yeelink] Processing版,Windows和Ubuntu测试可用
知道Yeelink很有一段时间,可是苦于自己没有网络模块,也没有机会去买,所以就是只有看看别人做的东西,了解一下。
为了能测试一下Yeelink的功能,就想用串口发送数据给电脑,然后通过电脑上传数据到Yeelink。在极客工坊找到了这样的帖子http://www.geek-workshop.com/forum.php?mod=viewthread&tid=1294,原来早有人写了一个通过串口发送到程序。下载下来测试了一下很好使,但是发现程序只有开始,没有结束,很悲剧。
家里的一本processing的书买回来很久了,一直没有看。于是就趁着这几天能够抽点时间,用Processing自己写了一个程序。基本上实现了功能,悲剧的是这个程序不光没有结束,连开始也没有。不过目前在Ubuntu和Windows xp上测试可用
程序的使用:
在程序所在文件夹中,需要一个配置文件,文件名为default.ini。
内容如下:
注意:每行键名的冒号后都有一空格,串口和波特率间有一空格。
测试的效果图:(线性有点不好,改了改,勉强可看。)
与官方网页上的图形比较:
发送成功界面:
发送失败界面:
发送失败会有一定的错误码返回,我自己测试了几个,并将错误说明如下:
返回码:200 发送成功
返回码:403 Apikey错误
返回码:406 服务器拒收,可能是发送间隔小于10秒,或者数据格式或数据长度或设备号或传感器号错误。
有时可能显示返回码为“hea”,这种情况是404错误,页面未找到,可能是传送的地址有误。这个一般是程序错误了。
(汗,返回码为“hea”这为一个bug,这些错误码都是自己在调试的时候根据服务器返回的信息了解的,不一定准确。Windows用户可以通过使用processing直接运行程序从控制台返回的结果来查看发送过程,Linux用户可以直接从shell运行程序,程序会直接返回相关信息。由于串口权限问题,Ubuntu下需要rootq权限才能打开串口,请使用sudo运行程序。)
由于没有条件,Mac上就没有测试了,有条件的童鞋,自己使用源码试试吧。Mac的串口应该为/dev/ttyUSB*吧。
下面附上程序。第一次使用processing,代码其乱无比,惨不忍睹。各位勉强看看吧。
import processing.net.*;
import processing.serial.*;
PFont f;//////////////
Serial myPort;// Create object from Serial class
Client c;
float [] dataGroup=new float;
String[] lines;
String data, temp, wendu, U_ApiKey, sensorID, deviceID, port, Rate;
int val, wdLength, i=1;
long count=0;
void setup() {
dataGroup=20.00;
size(300, 200);
background(50);
fill(200);
f=createFont(" Arial ", 36, true);///////
textFont(f, 12);
textSize(16);
text("Wait the data...", 10, 20);
println("Wait the data....");
lines = loadStrings("default.ini");
String[] pieces;
//println(lines);
if (lines.equals("")) {
pieces = split(lines, ' ');//获取串口
// println(pieces);
if (pieces.length == 3) {
println(pieces);
if (pieces.equals("Port:")) {
port = pieces;
Rate = pieces;
text("Port:"+port, 195, 60);
println("Port:"+port);
text("Rate:"+Rate, 195, 80);
println("Rate:"+Rate);
}
}
pieces = split(lines, ' ');//获取ApiKey
// println(pieces);
if (pieces.length == 2) {
if (pieces.equals("U-ApiKey:")) {
U_ApiKey = pieces ;
println(U_ApiKey);
}
}
pieces = split(lines, ' ');//获取deviceID
//println(pieces);
if (pieces.length == 2) {
if (pieces.equals("deviceID:"))
deviceID = pieces;
text("deviceID:"+deviceID, 195, 20);
println("device:ID"+deviceID);
}
pieces = split(lines, ' ');//获取sensorID
// println(pieces);
if (pieces.length == 2) {
if (pieces.equals("sensorID:")) {
sensorID = pieces;
text("sensorID:"+sensorID, 195, 40);
println("sensorID:"+sensorID);
}
}
}
myPort = new Serial(this, port, int(Rate));///设置串口
myPort.buffer(15);
for (int i=1;i<=59;i++)
{
dataGroup=300;////////显示数组赋初值
}
}
////////////////////////线性移动转换/////////////////////////////////
void proce()
{
for (int i=0;i<59;i++) {
dataGroup =dataGroup ;//为数据显示
}
}
void draw() {
background(50);
fill(200);
text("Wait the date...", 10, 20);
println("Wait the date....");
text("deviceID:"+deviceID, 195, 20);
println("deviceID:"+deviceID);
text("sensorID:"+sensorID, 195, 40);
println("sensorID:"+sensorID);
text("Potr:"+port, 195, 60);
println("Port:"+port);
text("Rate:"+Rate, 195, 80);
println("Rate:"+Rate);
while ( myPort.available () <= 0) {
delay(500);///等待串口数据
}
temp = myPort.readString();
delay(500);
println(temp);
if (temp.length()>=14) {
///串口就收不稳定,个人觉得这样写好很多
text("Receive value: "+temp.substring(8, 13), 10, 40);
print("\r\n");
wendu=temp.substring(8, 13);
println("wendu: "+wendu);
wdLength=wendu.length()+10;//获取发送数据长度
c = new Client(this, "api.yeelink.net", 80);//建立连接
c.write("POST /v1.0/device/"+deviceID+"/sensor/"+sensorID+"/datapoints");
print("POST /v1.0/device/"+deviceID+"/sensor/"+sensorID+"/datapoints");
c.write(" HTTP/1.1\r\n");//采用1.1长时连接
print(" HTTP/1.1\r\n");
c.write("U-ApiKey: "+U_ApiKey+"\r\n");
print("U-ApiKey: "+U_ApiKey+"\r\n");
c.write("Content-Type: application/x-www-form-urlencoded\r\n");
print("Content-Type: application/x-www-form-urlencoded\r\n");
c.write("Host: api.yeelink.net\r\n");
print("Host: api.yeelink.net\r\n");
c.write("Content-Length: " + wdLength + "\r\n");
print("Content-Length: " + wdLength + "\r\n");
c.write("\r\n");
print("\r\n");
c.write("{\"value\":" + wendu + "}\r\n");
print("{\"value\":" + wendu + "}\r\n");
println("\r\nPost done.");
text("Post done.", 10, 60);
delay(500);//等待服务器响应时间,没有测试大概选择的时间
if (c.available() > 0) {
data = c.readString();
println(data);////显示服务器返回数据
String ack=data.substring(9, 12);//获取返回码
if (ack.equals("200") == true) {
print("Return code: " + ack + "\r\nPost success.\r\n");
text("Return code: " + ack, 10, 80);
fill( 0, 200, 0);
text("Post success",10,100);
dataGroup =float(wendu);//发送成功 记录发送数据
if (i>=60) {
proce();
i=59;
}
fill(200);
text((int)(++count), 250, 198);
print("Count: " + count + "\r\n");
}
else {
print("Return code: " + ack + "\r\nPost failed.");
text("Return code: " + ack, 10, 80);
fill(200, 0, 0);
text("Post failed.", 10, 100);
}
}
c.stop();//////断开连接
}
//////////////////////////////温度线绘制////////////////////////////
stroke(200, 200, 0);
strokeWeight(2);
for (int acc=0;acc<59;acc++) {
int x1=acc*5, x2=(acc+1)*5;
float y1=190-dataGroup*2, y2=190-dataGroup*2;//放大数据曲线显示
if (dataGroup!=300&& dataGroup!=300) {
line(x1, y1, x2, y2);
}
}
}
附上windows和Linux上的程序及源码。
(程序在写的开始就只是用了一个a23的名字做测试,后面一直修改,一直添加,到最后了,也难得改了,就叫他a23吧。)
(汗,linux的程序超大了,10M,我分成两个包了。解压后把文件改为.zip再解压就是了。不好意思了各位。嫌麻烦的就下windows的吧,里面也是有源码的。)
本帖最后由 vp110 于 2012-9-28 21:30 编辑
忘了说明:
Arduino端的发送格式和 Serial2Yeelink 一样。
都是 yeelink:20.25 这样的格式。目前软件只设置了 取5个字符,也就这个样要不就是102.5这样。可以将第126行的
wendu=temp.substring(8, 13);
修改为
wendu=temp.substring(8);
然后就是数据上传的间隔需大于十秒,不然服务器不接收。
------------------------------------------------------------------------------------------------------
悲剧port写成potr了,源程序已改过 不买5100的模块的话其实很便宜啊,我的模块才35块钱,测试过做WEBSERVER 和访问网站都没问题,函数完全兼容 接线灵活。 enc28j60 很好很强大 darkorigin 发表于 2012-9-20 22:05 static/image/common/back.gif
不买5100的模块的话其实很便宜啊,我的模块才35块钱,测试过做WEBSERVER 和访问网站都没问题,函数完全兼容 ...
enc28j60知道便宜好使,可洒家这是在偏远的农村,买个东西不容易。单买一个很不划算。。。最近也没有机会去买。。。就测试测试。。:lol :funk:貌似很酷的样子 迷你强 发表于 2012-9-22 18:32 static/image/common/back.gif
貌似很酷的样子
:'(强子哥有吗?你一说我才发现 data 写成了 date 了
悲剧。。。。
页面所贴代码以改过。。。。
vp110 发表于 2012-9-22 22:29 static/image/common/back.gif
强子哥有吗?你一说我才发现 data 写成了 date 了
悲剧。。。。
还在画图。。。。。。。W5100还有几天就能上架了 迷你强 发表于 2012-9-23 08:21 static/image/common/back.gif
还在画图。。。。。。。W5100还有几天就能上架了
额。。是吗?透露下价格吧?:lol 很不错的,顶一个!
页:
[1]