Okay so i got a small problem, ive got an Arduino Mega speaking to an Raspberry Pi 2 via Serial through the USB, the raspberry sending commands in form of a homemade protocol works great, problem is when i want the arduino to send data back, it for some reason cuts the data into pices of 5 bytes or less when i look at the Raspberry side (This dose not happen when i listen to the serial monitor, then it all looks fine) im useing a python server useing twisted to send info and read info from and to the Arduino.
The Arduino is trying to send a few chars as flags and 2 integers using a timer to make it every second.
Any thoughts?
Arduino
#include <Smartcar.h>
#include <dht.h>
#include <Timer.h>
// Declare the car Object
Car car;
#define DHT11_PIN 7
dht DHT;
// Variable to check if entire message is read
boolean Complete = false; // whether the string is complete
// Default Speed and Angle
int speed=0;
int angle=0;
// Message stored in byteArray
byte byteArray[6];
// Message size counter
int j=0;
int16_t humidCtrl = 0;
int16_t tempCtrl = 0;
// Timer setups
Timer t;
unsigned long lastCommand;
unsigned long timer;
void setup() {
t.every(1000, takeReading);
car.begin(); //initialize the car using the encoders and the gyro
Serial.begin(9600); // Initialize the Serial connection
}
void loop() {
t.update();
// Check time since programs tarted.
timer = millis();
// If the car dosent recive a command for 2 seconds stop
if(!Complete && (timer - lastCommand) > 2000){
speed = 0;
angle = 0;
// Carry out Instructions
car.setSpeed(speed);
car.setAngle(angle);
}
// If Message is complete check command for instructions
if (Complete) {
lastCommand = millis();
if(byteArray[1] == 0x46)
{
speed = byteArray[3];
}
else if(byteArray[1] == 0x42)
{
speed = -byteArray[1];
}
else if(byteArray[1] == 0x49){
speed = 0;
}
if(byteArray[2] == 0x52)
{
angle = byteArray[4];
}
else if(byteArray[2] == 0x4c)
{
angle = -byteArray[4];
}
else if(byteArray[2] == 0x49){
angle = 0;
}
// Carry out Instructions
car.setSpeed(speed);
car.setAngle(angle);
// Reset for new Message
Complete = false;
j=0;
// Empty old message to make space for new
for(int i = 0; i < 6; i++){
byteArray[i] = 0;
}
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
byte CR;
byte LF;
while (Serial.available()) {
// get the new byte:
byte inByte = (byte)Serial.read();
// Store message byte in Array
byteArray[j] = inByte;
// Increment in wait for new byte
j++;
// Look for CRLF message complete
if (inByte == 0x0d){
CR = 0x0d;
}
if (inByte == 0x0a){
LF = 0x0a;
}
if(inByte == 0x04 && CR == 0x0d && LF == 0x0a)
{
// Got CRLF
Complete = true;
}
}
}
void takeReading() {
// Read sensor data and store the data
int chk = DHT.read11(DHT11_PIN);
tempCtrl = DHT.temperature, 1;
humidCtrl = DHT.humidity, 1;
//byte dht[5] = {0x54,(int)tempCtrl,0x45,(int)humidCtrl,0x45}
byte dht[8];
String build = "T";
String build1 = build + tempCtrl;
String build2 = build1 + "H";
String build3 = build2 + humidCtrl;
String build4 = build3 + "E";
build4.getBytes(dht,8);
Serial.write(dht,8);
}
twistedSocketServer.txt (3.89 KB)