torsdag 1 oktober 2015

Setting up a server

This work session started by researching how to get the Bluetooths to talk to each other. By doing this we want to compare the individual cubes color to each other in order to achieve that the color depend on the maximum pressure from both cubes. It took a long time to find how to do this. We started off by trying to find how a mobile can automatically send messages back to the Bluetooths in order to get this to work. However, we did not get it to work because the mobile could not send anything back automatically, a person needed to send something manually. 

The problem was resolved using a computer instead, which can run a server. The server was set up using python code. It received the messages from the Bluetooth and sent back the color the LED should be in, but the lights did not light up. The server was connected to only one Arduino at a time. 


Code for Arduino:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 16); // RX, TX

int r = 4;
int g = 3;
int b = 2;
int test;
char check;
char light;

void setup() {

  Serial.begin(9600);
  
  pinMode(r,OUTPUT);
  pinMode(g,OUTPUT);
  pinMode(b,OUTPUT);
  
  mySerial.begin(9600);
}

void loop()
{
   int sensorValue = analogRead(A0);
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (3 / 1023.0);
    
    char test_sensor;
    if(voltage>=2){
      test_sensor = 'r';
      }
    else if(voltage>=1){
      test_sensor = 'b';
      }
    else if(voltage>=0){
      test_sensor = 'g';
      }
    //char test = mySerial.read();
    
Serial.println(voltage);
    
    if(check != test_sensor){

      digitalWrite(r,LOW);
      digitalWrite(g,LOW);
      digitalWrite(b,LOW);
      
    if(test_sensor == 'r')
    {
      mySerial.print(test_sensor);
      Serial.println(voltage);
      check = test_sensor;
    }
    else if(test_sensor == 'b')
    {
      mySerial.print(test_sensor);
      Serial.println(voltage);
      check = test_sensor;
    }
    else if(test_sensor == 'g')
    {
      mySerial.print(test_sensor);
      Serial.println(voltage);
      check = test_sensor;
    }
    }
      if(mySerial.available()){
        light = mySerial.read();
        digitalWrite(light,HIGH);
      }
}

Code for Server:

# Imports
import time
import serial
from Tkinter import *

# Serial port parameters
serial_speed = 9600
serial_port = '/dev/tty.banana-DevB'

# Test with USB-Serial connection
# serial_port = '/dev/tty.usbmodem1421'

ser = serial.Serial(serial_port, serial_speed, timeout=1)
while 1:
data = ser.readline()
print(data)
ser.write(data)


Written by: Josefine, Carolina, Lennart

Inga kommentarer:

Skicka en kommentar