Go Down

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

dave401

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() {

  
  Serial.begin(9600);
  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: ");
    Serial.println(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:
    delay(10000);


  }


  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms

  printWifiStatus();
  ThingSpeak.begin(client);
}


void loop() {

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

    float airQuality = XBee.read();
    Serial.println(airQuality);

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


  if (! bme.performReading()) {
    Serial.println("Failed to perform reading :(");
    return;
  }
  Serial.print("Temperature = ");
  Serial.print(bme.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.print(bme.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.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");

  Serial.println();

  ThingSpeak.writeFields(channel_id, APIKey);
  delay(2000);
}

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

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

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(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

mikb55

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