Jump to content









Photo

Arduino/xbee can send stream once then connection fails

arduino xbee arduino xbee data stream

  • Please log in to reply
1 reply to this topic

#1 JoshuaKranabetter

JoshuaKranabetter

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 10 December 2014 - 12:52 AM

This program reads wireless xbee data and sends it to carriots as a tempurature. The program is able to connect once and it sends the data "25" (in degrees C, yes its a little hot in here) but after that everything goes downhill. I have attached a screenshot of the data from the serial monitor. The program will not be able to connect again and after about 7 cycles my temperature data seems to mess up.
Thanks in advance, any feedback is appreciated.
 

/*
Equipment: 2 xbees, 1 arduino with ethernet sheild, 1 tmp36 teperature sensor
 
This program reads wireless xbee data and sends it to carriots as a tempurature.
 
By Joshua Kranabetter
 
 */
 
#include <SPI.h>
#include <Ethernet.h>
 
const String APIKEY="fa013e73d356997d405fd389d5a8504b8dbab54fa9c3d44474e7045e0b4e9b94"; 
const String DEVICE="defaultDevice@JoshuaKranabetter.JoshuaKranabetter";
 
#define ADC_VREF 4.8828125
IPAddress server(82,223,244,60);
const int numElements=1;
 
 
struct xbeeAPIFrameStruct {
  int  length;
  byte frameType;
  byte longSrcAddr[8];
  byte shortSrcAddr[2];
  byte rcvOptions;
  byte numSamples;
  byte dChanMask[2];
  byte aChanMask;
  int  analogSample[8];
  byte checksum;
};
 
EthernetClient client;
struct xbeeAPIFrameStruct xbeeAPIFrame;
 
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x80, 0x5A };
IPAddress ip(192,168,0,177);
int tempC;
 
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
Serial.println(F("Starting"));
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  delay(1000);                    // Give the Ethernet shield a second to initialize
}
 
 
void loop() {
  
  delay(5000); 
 
 
          
          getXbeeData();
  
          
        int x = xbeeAPIFrame.analogSample[0];
        float y = (x*(ADC_VREF));
          
           
          // now print out the temperature
          tempC = ((y - 500) / 100) ;
          Serial.println(xbeeAPIFrame.analogSample[0]);
          Serial.println(y);
         Serial.println(tempC);
           sendStream();
  
          
 
  }
     
   
   // Send stream to Carriots
void sendStream()
{
 
      
 
  if (client.connect(server, 80)) {   // If there's a successful connection
    Serial.println(F("connected"));
    // Build the data field
    String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":1356390000,\"data\":{\"Temperature\":\""+tempC+"\"}}";
 
    // Make a HTTP request
    client.println("POST /streams HTTP/1.1");
    client.println("Host: api.carriots.com");
    client.println("Accept: application/json");
    client.println("User-Agent: Arduino-Carriots");
    client.println("Content-Type: application/json");
    client.print("carriots.apikey: ");
    client.println(APIKEY);
    client.print("Content-Length: ");
    int thisLength = json.length();
    client.println(thisLength);
    client.println("Connection: close");
    client.println();
 
    client.println(json);
  }
  else {
    // If you didn't get a connection to the server:
    Serial.println(F("connection failed"));
  }
 
 
}
 
 
 
void getXbeeData()
{
  int analogLow;
  int analogHigh;
  int lengthLow;
  int lengthHigh;
  
  // make sure everything we need is in the buffer
  if (Serial.available() >= 21) {
    // look for the start byte
    if (Serial.read() == 0x7E) {
      
      
      
      lengthHigh  = Serial.read();
      lengthLow  = Serial.read();
      xbeeAPIFrame.length = lengthLow + (lengthHigh * 256);
      xbeeAPIFrame.frameType = Serial.read();
      for (int i = 0; i < 8; i++) {
        xbeeAPIFrame.longSrcAddr[i] = Serial.read();
      }
      for (int i = 0; i < 2; i++) {
        xbeeAPIFrame.shortSrcAddr[i] = Serial.read();
      }
      xbeeAPIFrame.rcvOptions   = Serial.read();
      xbeeAPIFrame.numSamples   = Serial.read();
      xbeeAPIFrame.dChanMask[0] = Serial.read(); // Digital Channel mask lower
      xbeeAPIFrame.dChanMask[1] = Serial.read(); // Digital Channel mask upper
      xbeeAPIFrame.aChanMask    = Serial.read();
      analogHigh                 = Serial.read();
      analogLow                 = Serial.read();
      xbeeAPIFrame.analogSample[0] = analogLow + (analogHigh * 256);
      //xbeeAPIFrame.checksum     = Serial.read();
    }
  }
}

Attached Files



#2 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 15 December 2014 - 11:37 AM

Hello

 
Include this code in the loop section and tell us the result

 

void loop() {
	delay(5000); 

	getXbeeData();

	int x = xbeeAPIFrame.analogSample[0];
	float y = (x*(ADC_VREF));

	// now print out the temperature
	tempC = ((y - 500) / 100) ;
	Serial.println(xbeeAPIFrame.analogSample[0]);
	Serial.println(y);
	Serial.println(tempC);
	sendStream();
	delay(500);

	while (client.available()) {
		char c = client.read();
		Serial.print(c);
	}

	if (!client.connected()) {
		client.stop();
	}
}

 

 

Hope it helps







Also tagged with one or more of these keywords: arduino xbee, arduino, xbee, data, stream

2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users