Can't commute a relay with Arduino wifi shield + Xively

116 Views Asked by At

I’m trying to turn on and off an LED light bulb connected to a Tinterkit Relay.

I’m using an Arduino UNO r3 connected to internet thanks to an official Arduino Wifi shield.

I’ve done a simple website with two buttons to send a 1 (on) or a 0 (off) to my Xively account.

I’ve written a code to detect the last posted channel value. The code is working fine and I’m able to detect a 1 or a 0 every 3 seconds approximately. The problem is that the relay doesn’t commute.

Please please, I would appreciate a lot your help to solve this problem.

Here is the code:

#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);  
}
0

There are 0 best solutions below