Go Down

Topic: Arduino101 (BLE) + WifiShield101 (WiFi) problem (Read 207 times) previous topic - next topic

freedry

Hello everyone! First time poster here.

I have an Arduino 101 paired with an Arduino WiFi Shield 101.
I have had success running scripts using the BLE capabilities of the Arduino 101 alone.
I have had success running scripts using the Wifi capabilities of the Arduino Wifi Sheild 101 alone.
But I cannot get both capabilities working at the same time.

I've simplified my large script to a smaller one to get to the root of the problem.
Please see my code below:
Code: [Select]

//BLE initialization
#include <CurieBLE.h>
BLEService ledService("80CF0000-7C33-DDB6-1A84-AA1C4D44FC97"); // BLE LED Service
BLEUnsignedCharCharacteristic counterxCharacteristic("80CF0001-7C33-DDB6-1A84-AA1C4D44FC97", BLERead | BLEWrite | BLENotify);
BLEUnsignedCharCharacteristic counterzCharacteristic("80CF0002-7C33-DDB6-1A84-AA1C4D44FC97", BLERead | BLEWrite | BLENotify);

//Wifi initialization
#include <SPI.h>
#include <WiFi101.h>

// Position variables
int iPosition = 0;
int jPosition = 0;

// Wifi Variables
char ssid[] = "REPLACE THIS"; // your network SSID (name)
char pass[] = "REPLACE THIS"; // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(5001); // the port you are using
WiFiClient client;

int32_t locationInt = 000; // variable that will be incrementally changed
int32_t locationIntSizeRef = 999; // dummy variable used for size
char locationChar[sizeof(locationIntSizeRef)]; // properly sets size of array

char objIdentifier[ ] = "B";
char locationCharToPrintSizeRef[] = "BX001"; // dummy variable used for size
char locationCharToPrint [sizeof(locationCharToPrintSizeRef)]; // properly sets size of array


void setup() {
  // Setup Serial
  Serial.begin(9600);

  // Setup BLE
  BLE.begin();
  BLE.setLocalName("Arduino");
  BLE.setAdvertisedService(ledService);
  ledService.addCharacteristic(counterxCharacteristic);
  ledService.addCharacteristic(counterzCharacteristic);
  BLE.addService(ledService);
  counterxCharacteristic.setValue(0);
  counterzCharacteristic.setValue(0);
  BLE.advertise();

  //Setup Wifi
  while (!Serial) {
    ; // wait for serial port to connect.
  }
  // 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);
  }
  server.begin();
  // I'm connected now, so print out the status:
  printWifiStatus();
 
 
  delay(10000);
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
 
      if (true == false) {
        Serial.println("Something really messed up");
      }
      else {
        // put your main code here, to run repeatedly:
        delay(50);

        //fake location script, incrementing locationInt
        locationInt = locationInt + 1;
        if (locationInt > 100) {
          locationInt = 000;
        }
        iPosition = locationInt+100;
        jPosition = locationInt+200;
        String xyString = String(iPosition) + "," + String(jPosition);
        Serial.println(xyString);
       
        //writing of BLE values
        counterxCharacteristic.writeValue(iPosition);
        counterzCharacteristic.writeValue(jPosition);
        delay(100);

        //Wifi Stuff
        WiFiClient client = server.available();   // Wait for a new client
        Serial.println("DEBUG CHECK 0");
        delay(100);
        Serial.println("DEBUG CHECK 1");
        sprintf(locationChar, "%03ld", locationInt+300); // formats number into 012 instead of 12
        Serial.println("DEBUG CHECK 2");
        // combining the locationint  with other variables, into an array
        locationCharToPrint [0] = objIdentifier [0];
        locationCharToPrint [1] = 'X';
        locationCharToPrint [2] = locationChar[0];
        locationCharToPrint [3] = locationChar[1];
        locationCharToPrint [4] = locationChar[2];

        // serially print that array
        Serial.println("locationCharToPrint:");
        Serial.println(locationCharToPrint);
        Serial.println("DEBUG CHECK 3");  ////////investigate problem between here
        // wifi-ly print that array
        client.setTimeout(10);
        client.flush();
        client.println(locationCharToPrint);
        Serial.println("DEBUG CHECK 3.5");  /////////and here
      }
    }
    // when the central disconnects, print it out:
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

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 the code, the Arduino:
1) First, it connects to the WiFi network (need to set the ssid[] and pass[] variables to your personal WiFi login credentials). Success.
2) Then, it waits for me to pair a device using BLE to the Arduino (I use an iPhone app called "nRF Connect" and it's worked well for me). Followed by continuously updating BLE values. Success.
3) Then, I connect to the Arduino via WiFi (The only way I've been doing this is with a personally made Unity app on my laptop - if anyone has a better suggestion i'd highly appreciate it).  The Unity app is able to read maybe one lingering WiFi output (ex. "BX369"), but then the Arduino freezes after serially outputting "BX370" and "DEBUG CHECK 3", seemingly getting frozen on "client.println(locationCharToPrint);".

I'm very stumped because I've been able to get the BLE and WiFi working separately, and the fact that the Unity app is able to read a seemingly lingering message of "BX369" makes me think it's working in some capacity.  This leads me to believe it's either a hardware issue or my misunderstanding of how WiFi connections work.

Any help is greatly appreciated!

Juraj

#1
Apr 04, 2019, 09:24 am Last Edit: Apr 04, 2019, 09:28 am by Juraj
add locationCharToPrint[5] = 0;

and I would change the initialization of this variables to

const char objIdentifier = 'B';
char locationCharToPrint[6];

and then

locationCharToPrint[0] = objIdentifier;


freedry

Thank you Juraj!

The formatting changes didn't help solve the overall issue, but it definitely makes my code cleaner.


After a lot of tinkering, I found that removing all "delay( )" mentions in the "void loop()" got it working for short periods of time.
Unfortunately, it still breaks after some time, possibly due to some issue surrounding "sprintf()" (maybe a memory problem?) or possibly still some timing issue.

Not sure exactly why removing the "delay()"s solved the main issue for now, since I tried to make my code timing insensitive/independent, in that it wouldn't wait for a client to read a print before moving onwards in the code.  Anyone with insight into timing involving client.println or insight into consistently crashing on the "sprintf" function would be appreciated!

Juraj

#3
Apr 21, 2019, 08:09 am Last Edit: Apr 21, 2019, 08:11 am by Juraj
did you add zero terminator to locationCharToPrint?
show the changed code

Go Up