我正在建造一个arduino动力空调遥控器。我有实际的红外遥控触发器与pusher.com一起工作,但是现在想要为房间的温度和交流单元的当前状态添加xively输入(打开或关闭,使用光敏电阻从电源LED读取)
当我将Xively代码添加到草图并上传它时,arduino会冻结。我把它缩小到int ret = xivelyclient.put(feed,xivelyKey);它从xively库调用put函数。如果你对这一行进行评论,那么推送器会照常运行。
我如何使推动器和xively共存?他们是否在争夺以太网屏蔽上的连接? (我以为我读过以太网屏蔽可以同时处理4个连接)
以下代码:
#include <SPI.h>
#include <Ethernet.h>
#include <PusherClient.h>
#include <HttpClient.h>
#include <Xively.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };
PusherClient client;
char xivelyKey[] = "myxivelykeyhere";
#define xivelyFeed 1454201282
int IRledPin = 8;
int sensorPin = 0;
// Define the strings for our datastream IDs
char sensorId[] = "temp";
XivelyDatastream datastreams[] = {
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(15552, datastreams, 1 /* number of datastreams */);
EthernetClient Eclient;
XivelyClient xivelyclient(Eclient);
void setup() {
pinMode(IRledPin,OUTPUT);
Serial.begin(9600);
Serial.println("I'm Alive");
if (Ethernet.begin(mac) == 0) {
Serial.println("Init Ethernet failed");
for(;;)
;
}
if(client.connect("336b1e021d66c95fad49")) {
client.bind("togglePower", togglePower);
client.subscribe("ac");
Serial.println("Connected!");
}
else {
while(1) {
}
Serial.println("Can't connect!!");
}
}
void loop() {
if (client.connected()) {
client.monitor();
}
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degrees F");
datastreams[0].setFloat(temperatureF);
Serial.println("Uploading it to Xively");
int ret = xivelyclient.put(feed, xivelyKey);
Serial.print("xivelyclient.put returned ");
// Serial.println(ret);
delay(8000);
}
void togglePower(String data) {
Serial.println("togglePower() was triggered");
pulseIR(8860);
delayMicroseconds(4360);
pulseIR(600);
delayMicroseconds(1580);
pulseIR(600);
delayMicroseconds(500);
pulseIR(600);
delayMicroseconds(480);
pulseIR(600);
delayMicroseconds(500);
pulseIR(600);
delayMicroseconds(480);
pulseIR(600);
delayMicroseconds(500);
pulseIR(600);
delayMicroseconds(500);
pulseIR(580);
delayMicroseconds(1600);
pulseIR(600);
delayMicroseconds(480);
pulseIR(600);
delayMicroseconds(1600);
pulseIR(600);
delayMicroseconds(1580);
pulseIR(600);
delayMicroseconds(500);
pulseIR(580);
delayMicroseconds(520);
pulseIR(580);
delayMicroseconds(1600);
pulseIR(600);
delayMicroseconds(1580);
pulseIR(600);
delayMicroseconds(500);
pulseIR(600);
delayMicroseconds(1580);
pulseIR(600);
delayMicroseconds(500);
pulseIR(580);
delayMicroseconds(500);
pulseIR(600);
delayMicroseconds(500);
pulseIR(600);
delayMicroseconds(500);
pulseIR(580);
delayMicroseconds(500);
pulseIR(600);
delayMicroseconds(500);
pulseIR(580);
delayMicroseconds(1600);
pulseIR(580);
delayMicroseconds(520);
pulseIR(580);
delayMicroseconds(1600);
pulseIR(600);
delayMicroseconds(1600);
pulseIR(520);
delayMicroseconds(1660);
pulseIR(520);
delayMicroseconds(1660);
pulseIR(520);
delayMicroseconds(1680);
pulseIR(580);
delayMicroseconds(1600);
pulseIR(520);
delayMicroseconds(580);
pulseIR(520);
delayMicroseconds(41480);
pulseIR(8840);
delayMicroseconds(2200);
pulseIR(540);
delayMicroseconds(28564);
pulseIR(8880);
delayMicroseconds(2140);
pulseIR(560);
}
void pulseIR(long microsecs) {
// we'll count down from the number of microseconds we are told to wait
cli(); // this turns off any background interrupts
while (microsecs > 0) {
// 38 kHz is about 13 microseconds high and 13 microseconds low
digitalWrite(IRledPin, HIGH); // this takes about 3 microseconds to happen
delayMicroseconds(10); // hang out for 10 microseconds
digitalWrite(IRledPin, LOW); // this also takes about 3 microseconds
delayMicroseconds(10); // hang out for 10 microseconds
// so 26 microseconds altogether
microsecs -= 26;
}
sei(); // this turns them back on
}
答案 0 :(得分:1)
你的Arduino很可能用完了RAM,你应该measure it。
或者你可以:
在不使用库的情况下将数据发送到Xively(示例可以在Arduino IDE中找到文件 - »示例 - »以太网 - »(CosmClient,CosmClientString) <那些相当过时但仍然可以正常工作,你可能想用Xively替换Cosm
停止使用推杆,只需使用ws://api.xively.com:8080/
即可为频道或整个设备提供非常类似的发布/订阅功能
第二个解决方案需要更多工作和JSON解析器。但是,还有另一个选项to use MQTT,您可以使用Arduino IDE附带的 PubSub 库,因为至少 v1.0 。与WebSoket端点不同,Xively的MQTT桥不需要JSON解析器,并且可以使用CSV数据格式。然后你可以很容易地在JavaScript app中使用WebSocket,除非你在Pusher alredy上实现了很多东西,我建议你只使用MQTT for Xively / Arduino。
如果您仍然遇到一些内存问题,则应使用您所包含的每个库的最基本示例来衡量使用情况。为了快速简便的解决方案,您可以尝试使用Arduino Due,希望它也有更大的增长空间。
答案 1 :(得分:0)
您还应该考虑使用http://devicehub.net,他们现在有一个非常简单的HTTP api http://devicehub.net/about/api#api-guide,而平台是开源而不是xively