0

I'm having a hard time debugging this piece of code, and i'm hoping someone can point out where i am making the mistake.

My serial output is:

connected
GetData()
got data...
next stage...


I've tested with a simple network client that loops sending a hello world, and it works fine. Every time this code halts after attempting to send a value to the client. I know that getData() is causing the issue, but i cannot work out why, or how to best resolve it. (apologies for the code dump, i want to be thorough)

#include <LiquidCrystal.h>
#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
dht11 DHT11;
#define DHT11PIN 8

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server(192,168,1,100);
IPAddress ip(192,168,1,50);
EthernetClient client;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int trigPin = 6;
const int echoPinOne = 7;
const int echoPinTwo = 8;
const int echoPinThree = 9;
const int echoPinFour = 10;



void setup() { 
  lcd.begin(16, 2);
  Serial.begin(9600);
  delay(1000);

  //Network
 if (Ethernet.begin(mac) == 0) {
    Serial.println("DHCP FAIL");
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  Serial.println("connecting...");
  lcd.setCursor(0, 0);
  lcd.print("connecting");
  connect();  
}

void connect(){

    if (client.connect(server, 8080)) {
    Serial.println("connected");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("connected");

    client.println("hello server");
    client.println();
    delay(500);
    lcd.clear();
  } 
  else {
    Serial.println("connection failed");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Connection Failure");
  }
  delay(500);
}

void loop() {



   //network
  if(!client.available()){
    Serial.println("lost connection!-restart");
    lcd.println("Disconnection");
    client.stop();
    connect();
  }
  else {

    int inp[5];   
    getData(inp);
    Serial.println("next stage...");
    char *output = (char *) &inp[0];   
    client.println(output);

    client.println("first");

    Serial.println("sent first...");

    //here repeats the same process for the other sensors - omitted for clear reading 

  }
   //end network

   delay(500);
}




void getData(){


Serial.println("GetData()");
long durationThree, inchesThree, cmThree = 0;
long durationOne, inchesOne, cmOne = 0;
long durationTwo, inchesTwo, cmTwo = 0;
long durationFour, inchesFour, cmFour = 0;



//SENSOR ONE
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPinOne, INPUT);
durationOne = pulseIn(echoPinOne, HIGH);


delay(100);

//SENSOR TWO
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPinTwo, INPUT);
durationTwo = pulseIn(echoPinTwo, HIGH);

delay(100);

//SENSOR THREE
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);


pinMode(echoPinThree, INPUT);
durationThree = pulseIn(echoPinThree, HIGH);


delay(100);


//SENSOR FOUR
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);


pinMode(echoPinFour, INPUT);
durationFour = pulseIn(echoPinFour, HIGH);


//DISTANCE CALCULATION
cmOne = microsecondsToCentimeters(durationOne);
cmTwo = microsecondsToCentimeters(durationTwo);
cmThree = microsecondsToCentimeters(durationThree);
cmFour = microsecondsToCentimeters(durationFour);
//Serial.println("sending...");
//char *out = (char *) &cmOne;
//Serial.println(out);
//client.println(out);
//Serial.println("sent!");      used this to test - same issue

Serial.println("got data...");
inputBuf[0] = cmOne;
inputBuf[1] = cmTwo;
inputBuf[2] = cmThree;
inputBuf[3] = cmFour;

}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
} 
0

The code you posted is performing incompatible casts and by thus is sending garbage to client.println.

There is no automatic conversion in C/C++ from an array of integers (int inp[]) or longs, to a C style string char *. A C Style string should end with null terminator \0, and the one in your code doesn't. For example, in this line:

char *output = (char *) &inp[0]; 

The inp[0] contains the first integer. The expression &inp[0] contains the address of that integer in memory.

Thus the cast to (char *) will yield a malformed C style string, since it will not end with a null terminator, and more important, will not contain the data you expect it would (this cast is saying - "hey, compiler, note that the address of that int is actually the address of a C style string").

You can, however, utilize the String type constructor that receives int/long, and send the duration in order, something similar to:

        void getData() 
        {
            ... // rest of the code here
            client.println(String(cmOne));
            client.println(String(cmTwo));
            ... // etc.
        }
  • I believe this is what i've been looking for! i'll test later tonight when i get to the project but i'm 95% sure this is correct :) thank you! – Simon. Jan 19 '15 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.