1

我正在尝试打开和关闭连接到 Tinterkit 继电器的 LED 灯泡。

借助官方的 Arduino Wifi shield,我正在使用连接到互联网的 Arduino UNO r3。

我已经做了一个简单的网站,有两个按钮可以向我的 Xively 帐户发送 1(开)或 0(关)。

我编写了一个代码来检测最后发布的频道值。该代码工作正常,我能够大约每 3 秒检测一次 1 或 0。问题是继电器不通勤。

拜托,我会很感激你的帮助来解决这个问题。

这是代码:

#include <SPI.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <Xively.h>
#include <TinkerKit.h>

char ssid[] = "XXXXXX"; 
char pass[] = "XXXXXX";   

int state;
int status = WL_IDLE_STATUS;

char myIntStream[]="LED";
char xivelyKey[] = "XXXXXX";

#define FEED_ID XXXXXX

TKMosFet relay(O0); 

XivelyDatastream datastreams[] = {
  XivelyDatastream(myIntStream, strlen(myIntStream), DATASTREAM_INT),
};

XivelyFeed feed(FEED_ID,datastreams,1);

WiFiClient client;
XivelyClient xivelyclient(client);

void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void setup() {
  Serial.begin(9600);
  Serial.println("LED light bulb");
  Serial.println();

  while ( status != WL_CONNECTED) { 
    Serial.println("Connecting to internet ...");    
    status = WiFi.begin(ssid, pass);    
    Serial.println("");
    delay(5000);
  } 

  Serial.println("Connected to wifi");
  printWifiStatus();
}

void loop() {
  Serial.println();
  Serial.print("Datastream is:");
  Serial.println(datastreams[0]);

  Serial.print("LED value = ");
  state = datastreams[0].getInt();
  Serial.print(state);

  if(state == 0){    
    relay.off();  
    Serial.println(" => Light is on.");    
  }
  else if(state == 1){    
    relay.on();  
    Serial.println(" => Light is off.");      
  }
  xivelyclient.get(feed, xivelyKey);  
}
4

0 回答 0