Good afternoon.
I'm still a beginner at microcontrollers and XBees and with this problem, I'm still a bit clueless. The Arduino and XBees send data when connected to the PC, but does not when the former is powered through its barrel jack connector. I am following Matthijs Koojiman's book for the examples and modified the codes a bit. The codes work as I said. The only things I observe when I use the barrel jack power to the Arduino is that the RX and TX pins of the XBee Explorers keep on blinking.
I'll be including the coordinator code afterwards.
Thank you for any help.
Here is the code:
- Sender [router] Arduino
#include <XBee.h>
#include <Printers.h>
#include <AltSoftSerial.h> //I connected the digital pins 2 and 3 to digital pins 8 and 9, respectively. I
#include <DHT.h> //followed Koojiman's book, Building Wireless Sensor Networks with Arduino
#include "binary.h"
#define DebugSerial Serial
#define XBeeSerial SoftSerial
XBeeWithCallbacks xbee;
AltSoftSerial SoftSerial;
DHT dht(4, DHT11);
int activateBuzzer = 6;
int tamperLED = 12;
int tempLED = 13;
int actvtnCode1 = -291;
int actvtnSqnceNtwrkAlrm2 = 130522;
int analogPin = 0;
int manualButton = 7;
int buzzerLED = 5;
int current;
//int kuryente;
long millis_held;
long secs_held;
long prev_secs_held;
byte previous = LOW;
unsigned long firstTime;
void setup() {
pinMode(tempLED, OUTPUT);
pinMode(tamperLED, OUTPUT);
pinMode(buzzerLED, OUTPUT);
pinMode(activateBuzzer, OUTPUT);
digitalWrite(manualButton, LOW);
DebugSerial.begin(115200);
XBeeSerial.begin(9600);
xbee.begin(XBeeSerial);
DebugSerial.println(F("Starting..."));
delay(1);
xbee.onPacketError(printErrorCb, (uintptr_t)(Print*)&DebugSerial);
xbee.onResponse(printErrorCb, (uintptr_t)(Print*)&DebugSerial);
xbee.onZBRxResponse(processRxPacket);
dht.begin();
// sendPacket(1,0);
}
void sendPacket(int automaticDetect, int manualActivate) {
ZBTxRequest txRequest;
if (automaticDetect == 1 && manualActivate == 0){
txRequest.setAddress64(0x0000000000000000);
AllocBuffer<9> packet;
packet.append<uint8_t>(1); //type of frame is transmit request
packet.append<float>(dht.readTemperature());
packet.append<float>(dht.readHumidity());
txRequest.setPayload(packet.head, packet.len());
DebugSerial.print("Automatic Detection!");
}
else if(automaticDetect == 0 && manualActivate == 1){
txRequest.setAddress64(0x0000000000000000);
AllocBuffer<3> packet;
packet.append<uint8_t>(1); //type of frame is transmit request
packet.append<int>(actvtnSqnceNtwrkAlrm2);
txRequest.setPayload(packet.head, packet.len());
DebugSerial.print("Manual Activation!");
}
xbee.send(txRequest);
DebugSerial.print("\nPacket sent!\n");
}
void processRxPacket(ZBRxResponse& rx, uintptr_t) {
Buffer b(rx.getData(), rx.getDataLength());
uint8_t type = b.remove<uint8_t>();
XBeeAddress64 addr = rx.getRemoteAddress64();
if (addr == 0x0013A200410407BC && type == 1 && b.len() == 2)
// if (addr == 0x0000000000000000 && type == 1 && b.len() == 2)
{
int codeSqnce1 = b.remove<int>();
if (codeSqnce1 == actvtnCode1){
for (int i = 0; i<3; i++){
digitalWrite(activateBuzzer, HIGH);
delay(500);
digitalWrite(activateBuzzer, LOW);
delay(500);
}
DebugSerial.println(F("Network Alarm Activated!"));
//DebugSerial.println(codeSqnce1);
return;
}
}
/* if (addr == 0x0013A2004146B5DB && type == 1 && b.len() == 2) {
int codeSqnce2 = b.remove<int>();
if (codeSqnce2 == actvtnCode2 ){
digitalWrite(activateBuzzer, HIGH);
delay(10000);
DebugSerial.println(("Buzzer Activated!"));
DebugSerial.println(codeSqnce2);
}
}
*/
DebugSerial.println(F("Unknown or invalid packet"));
printResponse(rx, DebugSerial);
}
void checkVoltage (int tempsensor){ //tamper detection for DHT
if (tempsensor>=700){
// DebugSerial.print("\nSensor connected");
digitalWrite(tamperLED, LOW);
}
else if (tempsensor<700){
// DebugSerial.print("\nSensor disconnected");
digitalWrite(tamperLED, HIGH);
}
}
void manualCallPoint(int kuryente){
//paste code here:
kuryente= digitalRead(manualButton);
if (kuryente == HIGH && previous == LOW && (millis() - firstTime)>200){
firstTime = millis();
}
millis_held = (millis()-firstTime);
secs_held = millis_held/1000;
if (millis_held > 50){
if (kuryente==HIGH && secs_held>prev_secs_held){
ledBlink(1, 50, buzzerLED);
}
if (kuryente==LOW && previous==HIGH){
if (secs_held<=0){
ledBlink(1, 750, buzzerLED);
DebugSerial.println(F("Hi!"));
}
if (secs_held >=1 && secs_held <3){
ledBlink(10, 200, buzzerLED);
DebugSerial.println(F("Hello!"));
}
if (secs_held>=3){
digitalWrite(activateBuzzer, HIGH);
delay(1000);
DebugSerial.println(F("\nSeconds held: "));
DebugSerial.print(secs_held);
sendPacket(0,1);
DebugSerial.println(F("\n\nBuzzer alert sent!"));
}
}
}
previous = kuryente;
prev_secs_held = secs_held;
}
void ledBlink(int times, int lengthms, int pinnum){
for (int x=0; x<times;x++) {
digitalWrite(pinnum, HIGH);
delay (lengthms);
digitalWrite(pinnum, LOW);
delay(lengthms);
}
}
unsigned long last_tx_time = 0;
void loop() {
xbee.loop();
current = digitalRead(manualButton);
manualCallPoint(current);
int value = analogRead(analogPin);
checkVoltage(value);
/*
DebugSerial.print("\nVoltage: ");
DebugSerial.print(value);
*/
//temperature and humidity display code may be considered optional. since
//it additionally slows down the processing of the manual call point
//and outputs are generally not needed for the coordinator device for the temp and humidity
//DebugSerial.print("\nTemp.: ");
// DebugSerial.println(dht.readTemperature());
//DebugSerial.print("Humid.: ");
// DebugSerial.println(dht.readHumidity());
// delay(1000);
float temp = dht.readTemperature();
float humid = dht.readHumidity();
if (temp>=34||humid>=70){
if (millis() - last_tx_time > 10000) {
sendPacket(1,0);
//DebugSerial.print(actvtnSqnceNtwrkAlrm2);
last_tx_time = millis();
}
for (int i = 0; i<2; i++){
digitalWrite(tempLED, HIGH);
delay(500);
digitalWrite(tempLED, LOW);
delay(500);
}
}
else if (temp<=33||humid<=69){
digitalWrite(tempLED, HIGH);
}
}