Dr. Kayarvizhy N Dr. Kayarvizhy N

Internet of Things

Zigbee with Arduino

September 13, 2016All posts

Introduction

The Zigbee S2 module runs native Zigbee protocol (Don’t use the S1 module which doesn’t have config). There are various ways  in which Zigbee can be interfaced with microcontrollers like Arduino Uno and PC. In this case we have used two Zigbee S2s with two Arduino Unos.

Project

In this experiment we will interface the Zigbee with Arduino Uno. Prior to this we need the right firmware and also need to configure one zigbee as router and another as communicator. Also the destination and source addresses have to set properly for them to interact with each other. Please refer this  Zigbee S2 Post (second experiment especially) to understand that. In this experiment we will Arduino Uno just as a means of Serial communication between 2 zigbees.zigbee-pinout

Pin Configuration

Arduino Sensor
3.3V VCC
GND GND (Pin 10)
Pin 2 (Rx) Tx (Pin 2)
Pin 3 (Tx) Rx (Pin 3)

 

Circuit connections

Code

#include "SoftwareSerial.h"


SoftwareSerial Serial1(2,3);

void sendToSerialAndWaitForResponse (const char *cmd, const char *resp, bool waitForResponse, int duration) {
  String bytes;
  Serial.print ("CMD: "); Serial.println(cmd);
  do {
    Serial.print(".");
    Serial1.println (cmd);
    delay(duration);
    bytes = Serial1.readString();
  } while ( (waitForResponse) && (bytes.indexOf(resp)<0));
  Serial.print ("RESPONSE: ");
  Serial.print (bytes.c_str());
  Serial.println("\n------------------------------");
}


// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println ("Welcome to the Zigbee world ...");

  // Put the Zigbee in Command Mode
  Serial1.print("+++");
  delay(50);
  String bytes = Serial1.readString();
  Serial.println(bytes);
  sendToSerialAndWaitForResponse ("AT", "OK", true, 100);
  sendToSerialAndWaitForResponse ("ATID", "2001", true, 100);
  sendToSerialAndWaitForResponse ("ATSL", "", false, 100);
  sendToSerialAndWaitForResponse ("ATDL", "", false, 100);
  sendToSerialAndWaitForResponse ("ATCN", "", false, 100);  
}

// the loop function runs over and over again forever
void loop() {
  if (Serial1.available()) {
    while (Serial1.available()) {
      Serial.write(Serial1.read());  
    }
  }
  if (Serial.available()) {
    while (Serial.available()) {
      Serial1.write(Serial.read());  
    }
  }  
}

 

0 0 votes
Article Rating
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted