Reed switch monitor via cloud

Hello everyone,

I am really confused, I am using an Arduino mega 2560 with a genuine Wi-Fi shield. I have set up two XBEE’s one as a coordinator and other as an end point monitoring the reed switch at the end point.

With the Wi-Fi shield I have carried out a simple task by uploading the value via a switch to the Xively cloud and this works perfectly.

The problem which I am having is uploading the data from my XBEE via the Wi-Fi shield. I cannot figure out how to read the value and integrate the code. Any information or help will be very very useful. Below is the code which worked for the simple test:

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

char ssid = “Hiran111”; // your network SSID (name)
char pass = “rosie123”; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

// Your Xively key to let you upload data
char xivelyKey = “fpmTHchVVQuTRWKTkeeGC0cKKVuIJtWDdLlgEQQ0fd13Pfld”;

#define xivelyFeed 255108374
//char sensorID = “BUTTON_CHANNEL”;

// Analog pin which we’re monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;

// Define the strings for our datastream IDs
char sensorId = “sensor_reading”;
XivelyDatastream datastreams = {
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(xivelyFeed, datastreams, 1 /* number of datastreams */);

WiFiClient client;
XivelyClient xivelyclient(client);

void printWifiStatus() {
// print the SSID of the network you’re attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(" dBm");
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(sensorPin, INPUT);
Serial.println(“Starting single datastream upload to Xively…”);
Serial.println();

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println(“Connected to wifi”);
printWifiStatus();
}

void loop() {
int sensorValue = digitalRead(sensorPin);
datastreams[0].setFloat(sensorValue);

Serial.print("Read sensor value ");
Serial.println(datastreams[0].getFloat());

Serial.println(“Uploading it to Xively”);
int ret = xivelyclient.put(feed, xivelyKey);
Serial.print("xivelyclient.put returned ");
Serial.println(ret);

Serial.println();
delay(15000);
}


This is my code for my XBEE which reads the value of the Reed switch at the end point.

int readValue = 0;
int x;

void setup() {
//pinMode(12,OUTPUT);
Serial1.begin(9600);
}

void loop() {
if(Serial.available() > 13) {
//Serial.println(Serial.read(), HEX);
for(int i=0; i<14; i++) {
x =Serial.read();
// Serial.print("i= “);
//Serial.print(i);
//Serial.print(” “);
//Serial.print(x,HEX);
//Serial.print(” ");
if (i == 12) { if (x == 0) {Serial.print(“LOW”); /digitalWrite(12, LOW);/}
else {Serial.print(“HIGH”); /digitalWrite(12, HIGH);/ }
delay(1000);

}
}
}
}

Thank you in advance

Also wanted to add, is it possible to integrate the code which i have written for the XBEE, or does another approach need to be taken?

First the usual: please use the code tags </> around your code, it makes it much easier to read.

like this

Have a look at the Xbee tutorial for how to get started. Basically your wifi end needs to do a Serial.read() from the Xbee shield instead of the current digitalRead() although its likely you will have to convert between characters and numbers.