Skip to content

Instantly share code, notes, and snippets.

@cpbotha
Last active March 7, 2018 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpbotha/81a083e4ab4cdc6b60ef543dedbee5fd to your computer and use it in GitHub Desktop.
Save cpbotha/81a083e4ab4cdc6b60ef543dedbee5fd to your computer and use it in GitHub Desktop.
// what we would like to see: first LED then second LED lights up on the WS2812 strip
// what we see instead: first LED (indicates startup), but then first 4 LEDs go on together, meaning xbee never talked back
// with SoftwareSerial(2,3) and the SparkFun shield switched to dline (vs UART), it does work.
#include <FastLED.h>
#include <XBee.h>
#include <Printers.h>
#define DATA_PIN 9
#define NUM_LEDS 4
CRGBArray<NUM_LEDS> leds;
// construct XBee object to be use as API to the xbee module
XBee xbee = XBee();
// sets the command to get the MY address
uint8_t myCmd[] = {'M', 'Y'};
// Sets the command to exit AT mode.
uint8_t cnCmd[] = {'C','N'};
// Initialises the AT Request and Response.
AtCommandRequest atRequest = AtCommandRequest();
AtCommandResponse atResponse = AtCommandResponse();
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
Serial.begin(9600);
xbee.begin(Serial);
// startup delay for xbee to wake-up (I'm desperate)
delay(5000);
// show that we're doing something
leds[0] = CHSV(0.5, 255, 255);
FastLED.show();
addressRead();
}
void loop() {
// put your main code here, to run repeatedly:
}
void addressRead()
{
// Sets the AT Command to Serial Low Read.
atRequest.setCommand(myCmd);
// Sends the AT Command.
xbee.send(atRequest);
// Waits for a response and checks for the correct response code.
if (xbee.readPacket(5000))
{
if(xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE)
{
xbee.getResponse().getAtCommandResponse(atResponse);
if(atResponse.isOk())
{
// second LED means we could read MY from the xbee
leds[1] += CHSV(128, 255, 192);
}
}
}
else {
// ERROR: we keep on arriving here :( :( :(
leds(0,3) = CHSV(128, 255, 192);
}
// Sets the AT Command to the Close Command.
atRequest.setCommand(cnCmd);
xbee.send(atRequest);
FastLED.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment