我正在建造一个由 arduino 供电的空调遥控器。我有与 pusher.com 合作的实际 IR 远程触发器,但现在想为房间温度和 AC 单元的当前状态添加 xively 馈送(打开或关闭,使用光敏电阻从电源 LED 读取)
当我将 Xively 代码添加到草图并上传时,arduino 冻结了。我把它缩小到 int ret = xivelyclient.put(feed, xivelyKey); 它从 xively 库中调用 put 函数。如果您将此行注释掉,则推送程序将照常运行。
如何让 pusher 和 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
}