W13 - Networking & Comm

GROUP ASSIGNMENT

For this week group assignment we decided to connect our HelloBoards with an I2C Bus adapting my scripts. This was an easy and simple way to communicate our boards, we first wanted to use Bluetooth, but we didn’t had enough modules or we only had BLE modules and we had some troubles with them.

For I2C to work we need two PullUp resistors, like this:

Since we’ll be using Attiny44s and it doesn’t support the Arduino wire library. The first step is to add the AttinyCore from this link to arduino, then install it on the boards manager.

By adapting Josep’s code, we managed to get all boards talking and changing the master’s color when pressing the buttons.

This is the master’s code:

C
#include <Wire.h>

int State[] = {0, 0, 0};

int but=8;
int R=7;
int G=2;
int B=3;

int Gustavo = 1;
int Diar = 2;

void setup() {
  Wire.begin();        
  delay(5000);
}

void loop() {
  if (digitalRead(but)== HIGH) {
    analogWrite(R,255);
    analogWrite(G,0);
    analogWrite(B,255);
    State[0] = 1;
  }

  else {
    analogWrite(R,255);
    analogWrite(G,255);
    analogWrite(B,0);
    State[0] = 0;
  }
  readU();
  writeU();
  delay(10);
}

void readU() {
  Wire.requestFrom(Gustavo, 1);
  State[1] = Wire.read();
  Wire.requestFrom(Diar, 1);
  State[2] = Wire.read();
}

void writeU() {
  Wire.beginTransmission(Gustavo);
  Wire.write(State[2]);
  Wire.endTransmission();
  Wire.beginTransmission(Diar);
  Wire.write(State[0]);
  Wire.endTransmission();
}

This is the slaves:

C
#include <Wire.h>

int LED = 1;  //Change it for your LED pin
int But = 2;  //Change it for your Button pin

int Gustavo = 1;
int Diar = 2;
int LEDState = 0;
int ButState = 0;

void setup() {
  Wire.begin(Diar); //Change the name               
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}

void loop() {
  if (digitalRead(But)==HIGH) {
    ButState = 1;
  }
  else{
    ButState = 0;
  }
  if (LEDState == 1) {
    digitalWrite(LED, HIGH);
  }
  else {
    digitalWrite(LED, LOW);
  }

}

void receiveEvent() {
  LEDState=Wire.read();
}

void requestEvent() {
  Wire.write(ButState);
}

INDIVIDUAL ASSIGNMENT

I want to use Bluetooth because learning how to use it will allow me to add a very spreaded comunication system to many gadgets in the future.

My wish though was to work with LORA.

If you want to know why Bluetooth is called Blue Tooth

For Dummies

Evreyones favourite.

I choose this board because many of my peers had chosen this, and I suggested we could do the group assignment connecting our boards.

Besides troubleshooting would be easier to solve… or so I hoped. Life… that cynical seashore.

I´m using a Microchip RN4871 Here is a users guide This is a lowpow Bluetooth, Bluetooth Low Energy technology operates in the same spectrum range (the 2.400–2.4835 GHz ISM band) as Classic Bluetooth technology, but uses a different set of channels. Instead of the Classic Bluetooth 79 1-MHz channels, Bluetooth Low Energy has 40 2-MHz channels. Within a channel, data is transmitted using Gaussian frequency shift modulation, similar to Classic Bluetooth’s Basic Rate scheme. The bit rate is 1 Mbit/s, and the maximum transmit power is 10 mW.

Key Bluetooth 4.2 Features Not Available with Bluetooth 4.1:

IoT Capabilities:

Low-power IP (IPv6/6LoWPAN) Bluetooth Smart Internet Gateways (GATT) With BLE 4.2 Bluetooth Smart sensors can transmit data over the internet.

Security:

LE Privacy 1.2 LE Secure Connections With new, more power efficient and highly secure features, BLE 4.2 provides additional benefits allowing only trusted owners to track device location and confidently pair devices.

Speed:

250% Faster 10x More Capacity Compared to previous versions, BLE 4.2 enables 250% faster and more reliable over-the-air data transmission and 10x more packet capacity.

Connections:

To make one:

The connections of the arduino:

I used the BLUETOOTH 4.2 BLE MODULE, SHIELD, 10kohm R,1uF C, and a Voltage rectifier. It uses a rectifier to adapt the general 5V of the board to 3.3V every day electronics shifts more and more towards 3.3V powering.

While checking all the connections after soldering I was worried that the External antenna and GND where shortcircuit, and also connected to the shield that acts like a faraday cage, which made me thing that faraday cages didn’t have to be grounded necessarily. In regards of the first part, RF-GND, someone else on the internet had the same question and contacted the manufacturer, the answer “Regarding you question of RF_IN short with GND, it’s normal. You can ignore this point.”, about the faraday cage-RF shield, In cages as the RF wave tries to pass through the parallel lines of metal it creates a field effect that won’t let the waves pass through so the power is spread and absorbed by the Faraday cage, ok; but if you are using sheet metal or foil to block RF then you need to ground those other wise they will reflect the RF, in a RF communication system, you don´t want a bunch of reflected RF waves (EM field) around it, because you may not be affecting what is inside the shield, but you may be interfering the waves that you actually you want to receive… I guess.

Testing

Is not working, if I connect the FTDI directly I see 3V3 at the RX/TX of the BT, but if I connect the arduino I see 5V.

I check the postregulator voltage and I see 4.9! if connected to the arduino, but 3.3 if connected to the FTDI!!!

I tried everything, I can’t make it work!! I can’t detect it with my computer (MAC) nor my phone (android)

I try to detect it stand alone (FTDI-BT) and nothing.

I try to do it through Terminal, but I could only detect my computer’s BT I Port and the FTDI

I ended up connecting the FTDI directly to the BT again, and at arduino serial monitor:

$$$

Then

GND to know the name

Change the name

SN FABBLUE

OK, the chip is alive

I can’t veryfy TX/RX with the arduino because first I have to make sure that the aduino communicates at 3V3 not 5V, I must make an Voltage postregulator.

IOS is specially tricky as one has to set up the device with a within certain parameters.

I used MICROCHIP app in a colleagues phone (android) to detect the darn thing because I couldn’t do it with my phone (android) nor my computer (ios).

And it connected

Now I found a library BLE

#include <RN487x_BLE.h>

Installed it and found a program that has Everything

google it :

BLE_Peripheral.ino

Now I´m going to make a voltage regulator for the arduino.

Files

The code:

int ledPin = 13;
int state = 0;
int flag = 0;
int Lampcolor=0;
int daynight=0;
 int night=0;//level is considered night
 int incomingByte=0;  
 int GUNSHOT=0; //flag that states there is  a gunshot
 int Emergencybit=0; //this is where the flag of gunshot is stored, i could put a register in paralel with serial, and connect the first pin to an input interrupt
 int Noiselvlin=0; // is where the part of the byte corresponding with the sound is stored, up to 127, this could be done in the reaspberry,
 String tipo;

void setup() {
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);

 Serial.begin(9600); // Default connection rate for my BT module
}

void loop() {

 if(Serial.available() > 0){ //LEO BLUETOOTH
 // state = Serial.read();
  incomingByte = Serial.read(); // ME LO GUARDO EN UN BYTE
  Emergencybit=incomingByte & 128;//isolating the gunshot alarm FIRST BIT
                                   // I TRYED BINARY BEFORE
  Noiselvlin=incomingByte & 127; // LOWER BITS ARE THE NOISE
 flag=0;
                     //Serial.println(Emergencybit);//TO SEE RESULT OF BINARY OPS
                    //     Serial.println(Noiselvlin);
                   ///     Serial.println(incomingByte);// TO SEE WHAT HE GETS
 }

   if(Emergencybit>0) { //TO DECIDE IF IS A GUNSHOT

      tipo="Gunshot"; //CHANGE THE STRING
      //Openticket;// UNTIL HERE USEFUL
      Serial.println(Emergencybit);
      Serial.println(Noiselvlin);
      Serial.println(incomingByte);
      if(daynight==night){ // USELESS
        Lampcolor=2;
        digitalWrite(ledPin, HIGH);//TEST
        }
      }
 if (state == '0') {
 digitalWrite(ledPin, LOW);
 if(flag == 0){
 Serial.println("LED: off");

 flag = 1;
 }
 }

 //else if (state == '1') {
 //digitalWrite(ledPin, HIGH);
 if(flag == 0){
 Serial.println("LED: on");
 flag = 1;
 }
 //}
}

CONNECTING THE THEREMIN TO ANOTHER BOARD

I had some troubles connecting my HC-10 modules to get into AT mode, why is that, it seems that TX HAS to recieve 3,3V as soon as I used a voltage divisor I was able to get into AT mode and change the parameters to conver them into.

Also is very important that to enter mode you have to press the button if you have it before and while you connect VCC. If you dont have a button, which I do, yo have to put 3.3V in pin 34.

More into it here.

This tutorial useful too:

Remember that you may have set your communication to 9600bps, but the AT mode is always 38400bps!! Set your arduino monitor accordingly!!

SLAVE

First lets make it easy to identify

Next lets make an easy PWD

Final Config:

MASTER

The same as before

Final Config:

Now lets pair it with the slave

I wanted to connect the theremin to the speaker, but as it had to be two boards made by myself.

So I opted to just turn on and off a LED on another board depending on a given frequency.

The given frequency is given by the equation:

F=1/(500ms+(10*2loop))

So every time in passes by the loop function each semiperiod will be 10ms slower

This is made just to show that it changes.

The boards can be seen in W19 and W10 respectively.

First I try direct serial communication just to see if it works.

You can notice that if I unplug the cable the blinking stops.

Then I try it with the Master and Slave.

I had use the voltage regulator for the Theremin (Master) board and a voltage divider for the Funduino (Slave) Just to see that both systems work as well in this case.

In the future I woulld integrate ether in the boards.

Here is the Theremin code.

////
//BLUETOOTH
#include <SoftwareSerial.h>
#define RxD 1// for me 1 pero asi puedo usar PWM
#define TxD 2// for me 0
#define DEBUG_ENABLED  1
   //int led=4

SoftwareSerial blueToothSerial(RxD,TxD);
//setup BT
void setupBlueToothConnection()
{
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=HC-05\r\n"); //set the bluetooth name as "HC-05"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here

  delay(2000); // This delay is required.
  //blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  blueToothSerial.print("bluetooth connected!\n");

  delay(2000); // This delay is required.
  blueToothSerial.flush();
}


//// End of BT

int PWM_pin = 0; // Speaker on PB0 (pin 5) of ATtiny85
const int ldrPin = 4; //Note
int LDR=0;

const int photoTPin=3;//Volumen
int PhotoT=0;

void setup() {
  //
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();

  //pinMode(led,OUTPUT);
  //digitalWrite(led,HIGH);
  //
  pinMode(ldrPin, INPUT);
  pinMode(photoTPin, INPUT);
  pinMode(PWM_pin, OUTPUT);
}
int LDR2=0;
int PhotoT2=0;
int dtpause =500;

void loop() {

  PhotoT=analogRead(photoTPin);
  LDR=analogRead(ldrPin*10); // 10*ldrPin Change in frequency

  LDR=LDR/10;
LDR2=map (LDR,0,100, 1, 6);
PhotoT2=map (PhotoT,0,1023, 0, 100);
//LDR2=map (LDR,0,100, 1, 5);
//PhotoT2=map (PhotoT,400,1023, 0, 4);

  //blueToothSerial.println("LDR");
 //blueToothSerial.println(LDR2);
 // blueToothSerial.println("PhotoT");
  //blueToothSerial.println(PhotoT2);
  //blueToothSerial.println("dtpause");
  //blueToothSerial.println(dtpause);

  dtpause =  dtpause+10;//1*PhotoT2/30; //resultin F
  //int L1=10, L2=210, L3=100; //Volume min

   analogWrite(PWM_pin,25* LDR2); //Wave
   blueToothSerial.write("1");
   delay(dtpause);
   analogWrite(PWM_pin, 0);
  blueToothSerial.write("0");
   delay(dtpause);

}

Here is the Funduino code.

#define softserial

//Led pins
const int LED  = 13;
const int LED_ext  = A0;


#ifdef softserial
  #include <SoftwareSerial.h>
  const int BTRX = 0;  // 11
  const int BTTX = 1;  // 10
  SoftwareSerial SerialBT(BTRX, BTTX);
#else
  HardwareSerial SerialBT = Serial1;
#endif



String msg; // Lo que se envía



void setup() {
  SerialBT.begin(9600);
  SerialBT.println("Bluetooth- is linked bro");
  pinMode(LED, OUTPUT);
}

void loop() {
 if (SerialBT.available()){      
    msg = SerialBT.readString();
    SerialBT.print(msg);
    if (msg == "1") {
        digitalWrite(LED, HIGH);
        //digitalWrite(LED_ext, HIGH);
        SerialBT.print("LED pin");
        SerialBT.print(LED);
        SerialBT.println(" ON BABY");
     }
     else
     if (msg == "0") {
        digitalWrite(LED, LOW);
        //digitalWrite(LED_ext, LOW);
        SerialBT.print("LED PIN ");
        SerialBT.print(LED);
        SerialBT.println("OOOH X(");
     }
    // else {
      //  SerialBT.print("What?");
      //  SerialBT.print(msg);
      //  SerialBT.println("don't get it bro");
     //}
   }
}    

Here are the readings from the TX pin in the Theremin and the RX pin in the Funduino, the giberish is due to me separating the probe from the pin of one to the pin of the other.

And lo and behold!!

Here we can se once I do the reset in the funduino:

  • Pairing of the two blueTooths (notice the blinking changes when it happens)
  • How the blinking starts.

  • How it stops blinking as it stops receiving data as I remove the TX cable from the Master.

Here we can see how the frequency has changed with every loop, now it stays longer ON and OFF.

Here we can see how when I unplug the TX of the Theremin the blinking stops and it restarts as soon as TX is resumed.