Go Down

Topic: xbee and arduino wifi shield (Read 119 times) previous topic - next topic


Dec 10, 2019, 08:20 pm Last Edit: Dec 10, 2019, 08:21 pm by dave401 Reason: Photo too large
Hi Folks,

I'm in the middle of a project at the moment that takes data from an arduino with an xbee s2 connected to it and sends that data to a coordinator xbee connected to an arduino.

What i'm trying to do at the moment and failing is to send the data from the coordinator xbee to ThingSpeak via wifi.

I only have the normal arduino wifi shield thats piggy backed onto the arduino.

I've connected the xbee s2 to the 5v, ground and the Rx of the wifi shield.

The code i'm using is this

Code: [Select]

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include "WiFi.h"
#include "ThingSpeak.h"
#include <SoftwareSerial.h>
SoftwareSerial XBee(2, 3);

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME680 bme; // I2C
//Adafruit_BME680 bme(BME_CS); // hardware SPI
//Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

char ssid[] = "WiFi_Booster";        // your network SSID (name)
char pass[] = "RWTFYWTBLY";    // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
char APIKey[] = "7N09113FNS651OWF";             // enter your channel's Write API Key
unsigned long channel_id = "914317";
const int updateThingSpeakInterval = 20 * 1000; // 20 second interval at which to update ThingSpeak

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;

// Initialize Arduino Ethernet Client
WiFiClient client;

void setup() {

  while (!Serial);

  if (!bme.begin(0x76)) {  // ********** change from the default address (0x77) to 0x76
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:


  // Set up oversampling and filter initialization
  bme.setGasHeater(320, 150); // 320*C for 150 ms


void loop() {

while (XBee.available() > 0)
  { // If data comes in from XBee, send it out to serial monitor

    float airQuality = XBee.read();

    Serial.println("Posting " + String(airQuality, 2) + " to ThingSpeak");
    ThingSpeak.setField(5, String(airQuality, 2));

  if (! bme.performReading()) {
    Serial.println("Failed to perform reading :(");
  Serial.print("Temperature = ");
  Serial.println(" *C");
  //ThingSpeak temp field
  Serial.println("Posting " + String(bme.temperature, 2) + " to ThingSpeak");
  ThingSpeak.setField(1, String(bme.temperature, 2));

  Serial.print("Pressure = ");
  Serial.print(bme.pressure / 100.0);
  Serial.println(" hPa");

  //ThinSpeak Pressure field
  Serial.println("Posting " + String(bme.pressure, 2) + " to ThingSpeak");
  ThingSpeak.setField(2, String(bme.pressure, 2));

  Serial.print("Humidity = ");
  Serial.println(" %");

  //ThingSpeak humidity field
  Serial.println("Posting " + String(bme.humidity, 2) + " to ThingSpeak");
  ThingSpeak.setField(3, String(bme.humidity, 2));

  Serial.print("Gas = ");
  Serial.print(bme.gas_resistance / 1000.0);
  Serial.println(" KOhms");

  //ThingSpeak gas field
  Serial.println("Posting " + String(bme.gas_resistance / 1000.0, 2) + " to ThingSpeak");
  ThingSpeak.setField(4, String(bme.gas_resistance / 1000.0, 2));

  Serial.print("Approx. Altitude = ");
  Serial.println(" m");


  ThingSpeak.writeFields(channel_id, APIKey);

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(" dBm");

When I run it without the xbee code which is the while (XBee.abailable() > 0) {..........etc it posts the data to ThingSpeak just fine.

However, when i add that code, it either completely interupts the wifi and it also doesn't run whats contained in the XBee while loop so its recieving no data from the end device.  I have tried the xbee code thats in the script on its on and i can recieve the data.  Its just when its all combined that it doesn't work.

I don't have access to any other shields and was hoping that it would be a straight forward thing but alas not.

I've added an image of my set up.  The sensor on the breadboard is a BME680 and the data i'm recieving from the end device is from a grove air quality sensor.

I really just don't know if its my code, wiring error or compatibility issues with the xbee and the wifi shield.  

Thanks in advance


Your code looks like it was designed for a S1 Xbee in transparent mode.
You talk to S2 Xbees using the API mode.

Go Up