//constructor from a rx16 response pkt. tXBeePacket::tXBeePacket( Rx16Response rx16 ) : data (NULL) { char * rsp = new char[rx16.getDataLength()+1]; rsp[rx16.getDataLength()] = '\0'; strncpy(rsp,(char * )rx16.getData(),rx16.getDataLength()); remoteAddress = rx16.getRemoteAddress16(); data = rsp; }
void printResponseCb(Rx16Response& rx, uintptr_t data) { Print *p = (Print*)data; p->println("Rx16Response received:"); printField(p, F(" From: 0x"), rx.getRemoteAddress16()); printField(p, F(" Rssi: 0x"), rx.getRssi()); printField(p, F(" Receive options: 0x"), rx.getOption()); if (rx.getDataLength() > 8) p->print(" Payload:\r\n "); else p->print(" Payload: "); printHex(*p, rx.getFrameData() + rx.getDataOffset(), rx.getDataLength(), F(" "), F("\r\n "), 8); p->println(); }
int main(int argc, char *argv[]) { int bytesAvail; int byteRead; int bufferIndex; if (readIni("SensorRelay.ini") != 0) { printf("Ini file SensorRelay.ini not found! See ya.\n"); exit(1); } //!!!!!!!!!!!!!!!TEST CODE - REMOVE!!!!!!!!!!!!!!!!!!!!!!!! //SITE testSite = sites["XivelyHome"]; //GMXively *test = new GMXively(testSite.apiKey.c_str()); //test->SendData(testSite.feedID, "TempOffice2", 23.45); nextUploadCheck = DateTime::seconds() + 60; nextWeatherUpdateTime = DateTime::seconds() + 10; // get outside temperature right away nextSendTime = DateTime::seconds() + 10; // send time to sensor right away GMUtils::Initialize(logFileName.c_str(), debugMode); GMUtils::Log("SensorRelay v053116 starting..."); if (!wunderAPI.empty()) { weather = new GMWunderground(wunderAPI, wunderCountry, wunderCity, 0); } if (!xBeePort.empty()) { // NOTE - the original code I used didn't disable flow control so I'm leaving it enabled. Should work without it, though gmSerial = new GMSerial(xBeePort.c_str(), 9600, true); if (gmSerial->Open() != 0) { GMUtils::Log("Unable to open serial port %s. See ya!", xBeePort.c_str()); exit(1); } xbee.setSerial(*gmSerial); } if (!serialPort.empty()) { gmBuffSerial = new GMBufferedSerial(serialPort.c_str(), 9600, false); if (gmBuffSerial->Open_USB() != 0) { GMUtils::Log("Unable to open serial port %s. See ya!", serialPort.c_str()); exit(1); } } while (1) { if (!xBeePort.empty()) { xbee.readPacket(); if (xbee.getResponse().isAvailable()) { // got something uint8_t apiID = xbee.getResponse().getApiId(); if (apiID == RX_16_RESPONSE || apiID == RX_64_RESPONSE) { // got a rx packet if (apiID == RX_16_RESPONSE) { xbee.getResponse().getRx16Response(rx16); //option = rx16.getOption(); //data = rx16.getData(0); //memcpy(buffer, rx16.getData(), rx16.getDataLength()); string data = string((char *)rx16.getData(), rx16.getDataLength()); //buffer[rx16.getDataLength() + 1] = 0x00; if (debugMode) { GMUtils::Write("Remote address %d, RSSI %d, data length %d", rx16.getRemoteAddress16(), rx16.getRssi(), rx16.getDataLength()); GMUtils::Write("Data %s", data.c_str()); } processSensorData(data); } //else //{ //xbee.getResponse().getRx64Response(rx64); //option = rx64.getOption(); //data = rx64.getData(0); //} // TODO check option, rssi bytes //flashLed(statusLed, 1, 10); // //// set dataLed PWM to value of the first byte in the data //analogWrite(dataLed, data); } else if (xbee.getResponse().getApiId() == RX_16_IO_RESPONSE) { xbee.getResponse().getRx16IoSampleResponse(ioSample); uint16_t address = ioSample.getRemoteAddress16(); if (debugMode) { for (int k = 0; k < ioSample.getSampleSize(); k++) { printf("Sample %d: ", k + 1); // assume that any analog sample will be in the first 5 pins for (int i = 0; i <= 5; i++) { if (ioSample.isAnalogEnabled(i)) { printf("A%d = %d", i, ioSample.getAnalog(i, k)); } } printf("\n"); } } // assume that any analog sample will be in the first 5 pins for (int i = 0; i <= 5; i++) { if (ioSample.isAnalogEnabled(i)) { char tempbuffer[20]; sprintf(tempbuffer, "%d.%d", address, i); string sensorID = string(tempbuffer); double values = 0; for (int k = 0; k < ioSample.getSampleSize(); k++) { values += ioSample.getAnalog(i, k); } storeSensorValue(sensorID, values / ioSample.getSampleSize()); } } } else { // not something we were expecting //flashLed(errorLed, 1, 25); GMUtils::Log("xbee getResponse getApiId returned %d\n", apiID); } } else if (xbee.getResponse().isError()) { GMUtils::Log("Error reading packet. Error code %d\n ", xbee.getResponse().getErrorCode()); //nss.println(xbee.getResponse().getErrorCode()); // or flash error led } } if (!serialPort.empty()) { // serial port directly connected to sensor source gmBuffSerial->FillBuffer(); if (gmBuffSerial->ReadAvail() > 0) { if (debugMode) { GMUtils::Write("serial port buffer contains %d bytes", gmBuffSerial->ReadAvail()); } uint16_t bytesRead = gmBuffSerial->ReadUpToChar(buffer, 1000, 0x0a); if (bytesRead > 0) { if (debugMode) { GMUtils::Write("Serial port returned %d: %s", byteRead, buffer); } processSensorData(GMUtils::UnsignedBytesToString(buffer)); } } // bytesAvail = gmBuffSerial->ReadAvail(); // // if (bytesAvail > 0) // { // if (bytesAvail >= sizeof(buffer)) // { // bytesAvail = sizeof(buffer) - 1; // allow for ending null // } // bufferIndex = 0; // for (int i = 0; i < bytesAvail; i++) // { // buffer[bufferIndex++] = gmBuffSerial->Read(); // //printf("Read %d\n", byteRead); // } // buffer[bufferIndex] = 0; // if (debugMode) // { // printf("Read %d: %s\n", bytesAvail, buffer); // } // //processSensorData(string(reinterpret_cast< char const* >(buffer))); // processSensorData(GMUtils::UnsignedBytesToString(buffer)); // } } if (nextUploadCheck <= DateTime::seconds()) { checkForNextUpload(); nextUploadCheck = DateTime::seconds() + 60; } if (weatherEnabled && nextWeatherUpdateTime <= DateTime::seconds()) { if (getWeather() == 0) { sendWeather(temperatureC); } // NOTE - to avoid exceeding WeatherUnderground rate limit, don't immediately retry if call fails nextWeatherUpdateTime = DateTime::seconds() + WEATHER_MEASUREMENT_INTERVAL; } if (sendTimeEnabled && nextSendTime <= DateTime::seconds()) { sendTime(); nextSendTime = DateTime::seconds() + 60; } usleep(1000000); } //gmSerial->serialClose(); return 0; }