Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Re: Decoding ASCII binary over serial on: January 02, 2012, 11:11:16 pm
doubt it. if it helps you, please send me the debugging errors. thanks.
2  Using Arduino / Networking, Protocols, and Devices / Re: Decoding ASCII binary over serial on: January 02, 2012, 10:37:42 pm
Sorry, works on mine. Make sure you have minim installed, of course.
3  Using Arduino / Networking, Protocols, and Devices / Re: Decoding ASCII binary over serial on: January 02, 2012, 09:19:08 pm
Got it!



Yes. Turns out I was just looking at the edges of when the parity changes from 1 to 0 and vice versa. So what I did is, instead of assigning a bit to each spike; each spike, itself was a toggle to switch from 1 to 0 and back. This works very well. I have transmitted HelloWorld, and found out that 44100/300=147 is really the perfect size for correct timing.

Anyways, I'm pretty excited for now. Here is updated Processing code: (I'm a horrible programmer. still crashed half the time, but is a good proof of concept.)
Code:
import ddf.minim.analysis.*;
import ddf.minim.*;
//import ddf.minim.effects.*;


Minim minim;
AudioInput in;
AudioRecorder recorder;
//FFT fft;

int langth = 0;
int[] Sample = new int[300];
int showBar = 0;
int sampleSize = 16384; //16384
float[] samples;
float thresh = 48;
int crossed = 0;
float sampleRate;
int [] rxString = new int[0];
int bitDur = 147;

//at 44111, 300baud on the arduino should be every 147 samples but actually seems to read best at about 98% speed, 144.06.
//300baud = 144.06
//400baud = 111
//275baud = 158.9
//1200baud = 36.75

void setup()
{
  size(1110, 256);
 
  minim = new Minim(this);
 
  in = minim.getLineIn(Minim.STEREO, sampleSize);
  sampleRate = in.sampleRate();

 
  // create an FFT object that has a time-domain buffer
  // the same size as jingle's sample buffer
  // note that this needs to be a power of two
  // and that it means the size of the spectrum
  // will be 512. see the online tutorial for more info.
  //  fft = new FFT(in.bufferSize(), sampleRate);
  //  lpf = new LowPassFS(500 , in.sampleRate());
  //in.addEffect(lpf);
  //in.linAverages(128);

}

void draw()
{
  int readRX = 0;
     int isOne = 1;
     String [] binString = new String[0];
     String joinedBin = "00000000";
          int separatedBin;
String [] splitBin = new String[0];


  background(51);
  noFill();
  beginShape();
  //draw line to show where thresh is
  stroke(255);
  line(0, height - thresh*4, width, height - thresh*4);
  // perform a forward FFT on the samples in jingle's left buffer
  // note that if jingle were a MONO file,
  // this would be the same as using jingle.right or jingle.left
  //  fft.forward(in.mix);
    // draw the line for frequency band i, scaling it by 300 so we can see it a bit better
    //15 = 5600hz
    //8 = 3000hz
    //line(0, height, 0, height - fft.getBand(15)*300);     

  samples = in.left.toArray();  //figure out where the first pulse is and read from there.
  for(int i = 0; i < samples.length; i++){
    if(samples[i]*50 > thresh){
    readRX = i-5;
    break;
    }
  }

  float lineNumber = (sampleSize/bitDur-readRX/bitDur);
  int mapped = int(map(readRX, 0, sampleSize, 0, width));
  line(mapped, 0, mapped, height);

  for(int p=readRX; p < samples.length;){
    float x = 0;
    for(int d = p+6; d < p + bitDur && d < samples.length; d++){
      x = map(d, 0, in.bufferSize(), 0, width);
      stroke(#14CAFA);
      vertex(x, height - samples[int(d)]*200);
 
      //Draw a line at each bit interval. At 300baud, about every 111 samples.
      //THIS SLOWS DOWN EVERYTHING! DEBUGGING ONLY
//      stroke(#9B4949);
//      for(int e = 0; e < lineNumber; e++){
//        float interval = mapped+width/(sampleSize/157.16)*e;
 //       line(interval, 0, interval, height);
 //     }

      if(samples[int(d)-1]*50 < samples[int(d)]*50 && samples[int(d)-1]*50 < thresh && samples[int(d)]*50 >= thresh){
        crossed++;
      }
    }

    if(crossed >= 1 && isOne == 1){
      isOne = 0;
      text(isOne, x-10, 50);
      binString = append(binString, "0");;
     
    }
    else if(crossed >= 1 && isOne == 0){
      isOne = 1;
      text(isOne, x-10, 50);
      binString = append(binString, "1");

    }
    else{
      if(isOne == 1){
         text(isOne, x-10, 50);
         binString = append(binString, "1");
      }
      if(isOne == 0){
        text(isOne, x-10, 50);
        binString = append(binString, "0");
      }

    }
      crossed = 0; //reset crossed for the next bitspace
      p = p+bitDur; //jump to the next bitspace
    }
    endShape();

    for(int f = 1; f < 71;){  //split, format and print binary string
      splitBin = subset(binString, f, 8);
      joinedBin = join(reverse(splitBin), "");
      println((char)unbinary(joinedBin)); 
      f = f+10; //jump 10 bits to the next character
    }
             
             

}

void stop()
{
  // always close Minim audio classes when you finish with them
  in.close();
  minim.stop();
 
  super.stop();
}
4  Using Arduino / Networking, Protocols, and Devices / Re: Decoding ASCII binary over serial on: January 02, 2012, 03:53:20 pm
PaulS, I've searched all day for a way to bitbang at a given baud rate, but short of getting dirty myself, I can't find one. I'd prefer not to write dozens more lines of code just because my serial pin seems to be wonky...

Anyways, you said various bitrates are supported, do you know of code examples for something like this? Unfortunately, there doesn't seem to be a Serial.xxx() command to bit bang a binary string.
5  Using Arduino / Networking, Protocols, and Devices / Re: Decoding ASCII binary over serial on: January 02, 2012, 03:39:17 pm
I've brought it up to 300baud with no problem. I still get the same pattern of pulses.

However, I wonder what makes you so sure, Grumpy_Mike that transmitting via serial pin like this is a bad idea? Earlier in this project I have used a fast-fourier transform to accomplish the same task, but the power needed is just too much and too slow. Below is a new image, at 300baud.

Transmitting 'h'


Can anyone really look at that and say I'm not getting a binary signal?

The audio is being captured at 44100hz. At 300baud, a bit comes out to about 147 samples. Each one of the bit above the spikes in the audio is the result of the amplitude of the signal passing a threshold (the horizontal line through the middle)

I'm not sure I understand about only seeing edges either. The serial pin seems to be outputting using PWM like all the others. I'm not sure of the frequency, but it's in the audible range. Each bit consists of a multitude of wavepeaks.

So, I conclude that I must be the failwhale of the day. However, I can't seem to see what's wrong with all of this.

It's not kosher, this being an Arduino forum smiley but here is the code for Processing if anyone is curious to try it out. It requires the minim audio library.

Code:
import ddf.minim.analysis.*;
import ddf.minim.*;
//import ddf.minim.effects.*;


Minim minim;
AudioInput in;
AudioRecorder recorder;
//FFT fft;

int langth = 0;
int[] Sample = new int[300];
int showBar = 0;
int sampleSize = 16384; //16384
float[] samples;
float thresh = 48;
int crossed = 0;
float sampleRate;
int [] rxString = new int[0];

//at 44111, 300baud on the arduino should be every 147 samples but actually seems to read best at about 98% speed, 144.06.
//300baud = 144.06
//400baud = 111
//275baud = 158.9
//1200baud = 36.75

void setup()
{
  size(1110, 256);
 
  minim = new Minim(this);
 
  in = minim.getLineIn(Minim.STEREO, sampleSize);
  sampleRate = in.sampleRate();

 
  // create an FFT object that has a time-domain buffer
  // the same size as jingle's sample buffer
  // note that this needs to be a power of two
  // and that it means the size of the spectrum
  // will be 512. see the online tutorial for more info.
  //  fft = new FFT(in.bufferSize(), sampleRate);
  //  lpf = new LowPassFS(500 , in.sampleRate());
  //in.addEffect(lpf);
  //in.linAverages(128);

}

void draw()
{
  int readRX = 0;
  background(51);
  noFill();
  beginShape();
  //draw line to show where thresh is
  stroke(255);
  line(0, height - thresh*4, width, height - thresh*4);
  // perform a forward FFT on the samples in jingle's left buffer
  // note that if jingle were a MONO file,
  // this would be the same as using jingle.right or jingle.left
  //  fft.forward(in.mix);
    // draw the line for frequency band i, scaling it by 300 so we can see it a bit better
    //15 = 5600hz
    //8 = 3000hz
    //line(0, height, 0, height - fft.getBand(15)*300);     

  samples = in.left.toArray();  //figure out where the first pulse is and read from there.
  for(int i = 0; i < samples.length; i++){
    if(samples[i]*50 > thresh){
    readRX = i-5;
    break;
    }
  }

  float lineNumber = (sampleSize/144.06-readRX/144.06);
  int mapped = int(map(readRX, 0, sampleSize, 0, width));
  line(mapped, 0, mapped, height);

  for(float p=readRX; p < samples.length;){
    float x = 0;
    for(float d = p+6; d < p + 144.06 && d < samples.length; d++){
      x = map(d, 0, in.bufferSize(), 0, width);
      stroke(#14CAFA);
      vertex(x, height - samples[int(d)]*200);
 
      //Draw a line at each bit interval. At 300baud, about every 111 samples.
      //THIS SLOWS DOWN EVERYTHING! DEBUGGING ONLY
//      stroke(#9B4949);
//      for(int e = 0; e < lineNumber; e++){
//        float interval = mapped+width/(sampleSize/157.16)*e;
 //       line(interval, 0, interval, height);
 //     }

      if(samples[int(d)-1]*50 < samples[int(d)]*50 && samples[int(d)-1]*50 < thresh && samples[int(d)]*50 >= thresh){
        crossed++;
      }
    }
     
    if(crossed >= 1){
      text(0, x-10, 50);
      rxString = append(rxString, 1);
    }
    else{
      text(1, x-10, 50);
      rxString = append(rxString, 0);
    }
       

       

                crossed = 0;
//println(rxString);
      p = p+144.06;
    }
    endShape();
        //String joinedrxString = join(nf(rxString, 0), "");
        //println(unbinary(joinedrxString));
        //println(joinedrxString);
}

void stop()
{
  // always close Minim audio classes when you finish with them
  in.close();
  minim.stop();
 
  super.stop();
}

6  Using Arduino / Networking, Protocols, and Devices / Decoding ASCII binary over serial on: January 02, 2012, 01:40:58 pm
I've built an arduino with a piezo-electric speaker connected to the TX pin, 1. The sketch is meant to broadcast the ASCII character 'A' over TX at 275baud. On the receiving end is a processing sketch that decodes the audio sample into a binary string:



Here is the arduino sketch:

Code:
void setup()
{
  Serial.begin(275);
}

void loop()
{
  Serial.write('A');
  delay(370);
  delayMicroseconds(895);
}

The problem is, the received bits dont match the ASCII binary code for 'A' at all, 01000001. I tried checking for a pattern or a mistake in the receiving (inverted the numbers, read it backwards, looked for omitted bits) but nothing seems to work.

Every character is unique, and each one seems to be 10-bits long. Does anyone have any idea what is behind how arduino encodes ASCII over serial?

Better yet, is there a way to have the TX pin simply bit-bang a binary string at a given baud rate?
7  Using Arduino / Audio / Re: FM Modulated Barcode, Tone() Problem on: December 09, 2011, 02:22:59 pm
Its a piezo speaker. I am able to run the melody example that came with arduino
8  Using Arduino / Audio / Re: FM Modulated Barcode, Tone() Problem on: December 08, 2011, 08:58:09 pm
Nevermind. I thought I had succeeded, but analogWrite() doesn't change frequency, just the percentage at which it works. Again, open to suggestions smiley
9  Using Arduino / Audio / Re: FM Modulated Barcode, Tone() Problem on: December 08, 2011, 08:27:10 pm
Well! It looks like analogWrite(); worked in a pinch. However, I'm curious if this works the same way. analogWrite() changes the timer settings to where millis(); and other functions will not work correctly. Is tone() the same way?
10  Using Arduino / Audio / FM Modulated Barcode, Tone() Problem on: December 08, 2011, 08:14:28 pm
I am trying to transmit a barcode protocol over audio with the piezo connected at pin 8.

Below is a big piece of code, sorry! Most of it is just a couple big arrays.

Anyway, using digitalWrite(8, HIGH); works well, but tone() does not. Any ideas? Are these huge arrays detrimental to Arduinos?

Code:
#include <String.h>

int charValue = 0;
char* input[] = {" ", "!"};
int length = 2;
int pause = 50;
int startValue;

int target[107][6]=
{
    {2,1,2,2,2,2},
    {2,2,2,1,2,2},
    {2,2,2,2,2,1},
    {1,2,1,2,2,3},
    {1,2,1,3,2,2},
    {1,3,1,2,2,2},
    {1,2,2,2,1,3},
    {1,2,2,3,1,2},
    {1,3,2,2,1,2},
    {2,2,1,2,1,3},
    {2,2,1,3,1,2},
    {2,3,1,2,1,2},
    {1,1,2,2,3,2},
    {1,2,2,1,3,2},
    {1,2,2,2,3,1},
    {1,1,3,2,2,2},
    {1,2,3,1,2,2},
    {1,2,3,2,2,1},
    {2,2,3,2,1,1},
    {2,2,1,1,3,2},
    {2,2,1,2,3,1},
    {2,1,3,2,1,2},
    {2,2,3,1,1,2},
    {3,1,2,1,3,1},
    {3,1,1,2,2,2},
    {3,2,1,1,2,2},
    {3,2,1,2,2,1},
    {3,1,2,2,1,2},
    {3,2,2,1,1,2},
    {3,2,2,2,1,1},
    {2,1,2,1,2,3},
    {2,1,2,3,2,1},
    {2,3,2,1,2,1},
    {1,1,1,3,2,3},
    {1,3,1,1,2,3},
    {1,3,1,3,2,1},
    {1,1,2,3,1,3},
    {1,3,2,1,1,3},
    {1,3,2,3,1,1},
    {2,1,1,3,1,3},
    {2,3,1,1,1,3},
    {2,3,1,3,1,1},
    {1,1,2,1,3,3},
    {1,1,2,3,3,1},
    {1,3,2,1,3,1},
    {1,1,3,1,2,3},
    {1,1,3,3,2,1},
    {1,3,3,1,2,1},
    {3,1,3,1,2,1},
    {2,1,1,3,3,1},
    {2,3,1,1,3,1},
    {2,1,3,1,1,3},
    {2,1,3,3,1,1},
    {2,1,3,1,3,1},
    {3,1,1,1,2,3},
    {3,1,1,3,2,1},
    {3,3,1,1,2,1},
    {3,1,2,1,1,3},
    {3,1,2,3,1,1},
    {3,3,2,1,1,1},
    {3,1,4,1,1,1},
    {2,2,1,4,1,1},
    {4,3,1,1,1,1},
    {1,1,1,2,2,4},
    {1,1,1,4,2,2},
    {1,2,1,1,2,4},
    {1,2,1,4,2,1},
    {1,4,1,1,2,2},
    {1,4,1,2,2,1},
    {1,1,2,2,1,4},
    {1,1,2,4,1,2},
    {1,2,2,1,1,4},
    {1,2,2,4,1,1},
    {1,4,2,1,1,2},
    {1,4,2,2,1,1},
    {2,4,1,2,1,1},
    {2,2,1,1,1,4},
    {4,1,3,1,1,1},
    {2,4,1,1,1,2},
    {1,3,4,1,1,1},
    {1,1,1,2,4,2},
    {1,2,1,1,4,2},
    {1,2,1,2,4,1},
    {1,1,4,2,1,2},
    {1,2,4,1,1,2},
    {1,2,4,2,1,1},
    {4,1,1,2,1,2},
    {4,2,1,1,1,2},
    {4,2,1,2,1,1},
    {2,1,2,1,4,1},
    {2,1,4,1,2,1},
    {4,1,2,1,2,1},
    {1,1,1,1,4,3},
    {1,1,1,3,4,1},
    {1,3,1,1,4,1},
    {1,1,4,1,1,3},
    {1,1,4,3,1,1},
    {4,1,1,1,1,3},
    {4,1,1,3,1,1},
    {1,1,3,1,4,1},
    {1,1,4,1,3,1},
    {3,1,1,1,4,1},
    {4,1,1,1,3,1},
    {2,1,1,4,1,2},
    {2,1,1,2,1,4},
    {2,1,1,2,3,2},
    //    {2,3,3,1,1,1,2},
    {2,3,3,1,1,1}
  // This is a hack. the stop sequence is the onlyone
  // that has 7 bars but we check only for the first 6
  // to make all arrays the same length
};

char* res_a[107] = {
  " ",
  "!",
  "\"",
  "#",
  "$",
  "%",
  "&",
  "'",
  "(",
  ")",
  "*",
  "+",
  ",",
  "-",
  ".",
  "/",
  "0",
  "1",
  "2",
  "3",
  "4",
  "5",
  "6",
  "7",
  "8",
  "9",
  ":",
  ";",
  "<",
  "=",
  ">",
  "?",
  "@",
  "A",
  "B",
  "C",
  "D",
  "E",
  "F",
  "G",
  "H",
  "I",
  "J",
  "K",
  "L",
  "M",
  "N",
  "O",
  "P",
  "Q",
  "R",
  "S",
  "T",
  "U",
  "V",
  "W",
  "X",
  "Y",
  "Z",
  "[",
  "\\",
  "]",
  "^",
  "_",
  "NUL",
  "SOH",
  "STX",
  "ETX",
  "EOT",
  "ENQ",
  "ACK",
  "BEL",
  "BS",
  "HT",
  "LF",
  "VT",
  "FF",
  "CR",
  "SO",
  "SI",
  "DLE",
  "DC1",
  "DC2",
  "DC3",
  "DC4",
  "NAK",
  "SYN",
  "ETB",
  "CAN",
  "EM",
  "SUB",
  "ESC",
  "FS",
  "GS",
  "RS",
  "US",
  "FNC_3",
  "FNC_2",
  "SHIFT",
  "CODE_C",
  "CODE_B",
  "FNC 4",
  "FNC_1",
  "Start_A",
  "Start_B",
  "Start_C",
  "Stop"
};



void setup(){

}


void loop(){

 // Create start sequence
  for(int i; i < sizeof(res_a); i++){
   if(res_a[i] == "Start_A"){
     startValue = i;
    }
  }
  delay(pause*11);
  buzz(startValue);

  //Encode and buzz information 
  for(int k = 0; k < length; k++){
    for(int i = 0; i < sizeof(res_a); i++){
      char* res = res_a[i];
      char* inp = input[k];
      if(res == inp){
//      Serial.println(res);
      charValue = i;
      buzz(charValue);
      }
      else{
 //       Serial.println("Wrong Character!");
      }
    }
  }
     
//  checksum();
//  stopbuzz();
//  delay(pause);
     
}
   
   
int checksum(){
  int CSValue = 100;
  buzz(CSValue);

   
   
void stopbuzz(){

  delay(pause*11);


 
   
int buzz(int charValue){     
   for(int i = 0; i<6; i++){
     if((i % 2) == 0){
       if (target[charValue][i] == 1){
        tone(8, 100, pause);
        noTone(8);   
       }
       else if(target[charValue][i] == 2){
        tone(8, 100, pause*2);   
        noTone(8);   
       }
       else if(target[charValue][i] == 3){
        tone(8, 100, pause*3);     
        noTone(8);         
       }
       else if(target[charValue][i] == 4){
        tone(8, 100, pause*4);     
        noTone(8);         
       }
     }

       
     else{
       if (target[charValue][i] == 1){
        tone(8, 200, pause);     
        noTone(8);   

       }
       else if(target[charValue][i] == 2){
        tone(8, 200, pause*2);   
        noTone(8);
       }
       else if(target[charValue][i] == 3){
        tone(8, 200, pause*3);     
        noTone(8);
       }
       else if(target[charValue][i] == 4){
        tone(8, 200, pause*4);   
        noTone(8);
       }
    }
  }   
}






11  Using Arduino / Networking, Protocols, and Devices / Receiving data with SerialIP on: December 03, 2011, 09:59:01 pm
I am working with the SerialIP library to send and receive data from my arduino connected to a computer through a SLIP connection.

I've had ideas of pairing PHP with the serial port for the ability to put the data to be sent in the url (arduinohost/settempto?=56) but I'm not sure if SerialIP has the tools I need or if I should try to delve deeper.

The following reads a thermistor and sends the temperature to a browser:

Code:
/*
 * SerialIP Hello World example.
 *
 * SerialIP is a TCP/IP stack that can be used over a serial port (a bit
 * like a dial-up Internet connection, but without the modem.)  It works with
 * stock Arduinos (no shields required.)  When attached to a PC supporting
 * SLIP, the Arduino can host network servers and access the Internet (if the
 * PC is configured to share its Internet connection of course!)
 *
 * SerialIP uses the fine uIP stack by Adam Dunkels <adam@sics.se>
 *
 * For more information see the SerialIP page on the Arduino wiki:
 *   <http://www.arduino.cc/playground/Code/SerialIP>
 *
 *      -----------------
 *
 * This Hello World example sets up a server at 192.168.5.2 on port 1000.
 * Telnet here to access the service.  The uIP stack will also respond to
 * pings to test if you have successfully established a SLIP connection to
 * the Arduino.
 *
 * SLIP connection set up under Linux:
 *
 *  # modprobe slip
 *  # slattach -L -s 115200 -p slip /dev/ttyUSB0     (see note below)
 *  # ifconfig sl0 192.168.5.1 dstaddr 192.168.5.2
 *
 *  # ping 192.168.5.2
 *  # telnet 192.168.5.2 1000
 *
 * Here 192.168.5.1 is the address you will give to your PC, and it must be
 * unique on your LAN.  192.168.5.2 is the IP you will give the Arduino.  It
 * must also be unique, and must match the address the Arduino is expecting
 * as set by the "myIP" variable below.
 *
 * Note that slattach won't return so you'll need to run ifconfig from
 * another terminal.  You can press Ctrl+C to kill slattach and release the
 * serial port, e.g. to upload another sketch.
 *
 * This example was based upon uIP hello-world by Adam Dunkels <adam@sics.se>
 * Ported to the Arduino IDE by Adam Nielsen <malvineous@shikadi.net>
 */

#include <SerialIP.h>
#include <Stdio.h>
#include <SoftwareSerial.h>
#include <math.h>

#define txPin 2

// The connection_data struct needs to be defined in an external file.
#include "HelloWorldData.h"

int temp;
int read;
char tempchar [3];


SoftwareSerial LCD = SoftwareSerial(0, txPin);
const int LCDdelay=10;  // conservative, 2 actually works

void setup() {

  // Set up the speed of our serial link.
  Serial.begin(115200);

  // Tell SerialIP which serial port to use (some boards have more than one.)
  // Currently this is limited to HardwareSerial ports, until both it and
  // SoftwareSerial inherit from a common base class.
  SerialIP.use_device(Serial);

  // We're going to be handling uIP events ourselves.  Perhaps one day a simpler
  // interface will be implemented for the Arduino IDE, but until then...  
  SerialIP.set_uip_callback(uip_callback);

  // Set the IP address we'll be using.  Make sure this doesn't conflict with
  // any IP addresses or subnets on your LAN or you won't be able to connect to
  // either the Arduino or your LAN...
  IP_ADDR myIP = {192,168,5,2};
  IP_ADDR subnet = {255,255,255,0};
  SerialIP.begin(myIP, subnet);

  // If you'll be making outgoing connections from the Arduino to the rest of
  // the world, you'll need a gateway set up.
  //IP_ADDR gwIP = {192,168,5,1};
  //SerialIP.set_gateway(gwIP);

  // Listen for incoming connections on TCP port 1000.  Each incoming
  // connection will result in the uip_callback() function being called.
  SerialIP.listen(1000);

 pinMode(txPin, OUTPUT);
// pinMode(buttonOne, INPUT);
// pinMode(buttonTwo, INPUT);
  LCD.begin(9600);
  clearLCD();
  goTo(0,0);
  LCD.print("Hello world!");
  delay(1000);
  pinMode(13, OUTPUT);
}

void loop() {
  // Check the serial port and process any incoming data.
//  digitalWrite(13, HIGH);
//  delay(100);

  SerialIP.tick();

// digitalWrite(13, LOW);
//   delay(10);

  // We can do other things in the loop, but be aware that the loop will
  // briefly pause while IP data is being processed.


 // goTo(0,0);
 // clearLCD();
 // LCD.print("Temp: ");
 // LCD.print(temp);
}

void uip_callback(uip_tcp_appstate_t *s)
{
  if (uip_connected()) {

    // We want to store some data in our connections, so allocate some space
    // for it.  The connection_data struct is defined in a separate .h file,
    // due to the way the Arduino IDE works.  (typedefs come after function
    // definitions.)
    connection_data *d = (connection_data *)malloc(sizeof(connection_data));

    // Save it as SerialIP user data so we can get to it later.
    s->user = d;

    // The protosocket's read functions need a per-connection buffer to store
    // data they read.  We've got some space for this in our connection_data
    // structure, so we'll tell uIP to use that.
    PSOCK_INIT(&s->p, d->input_buffer, sizeof(d->input_buffer));

  }

  // Call/resume our protosocket handler.
  handle_connection(s, (connection_data *)s->user);

  // If the connection has been closed, release the data we allocated earlier.
  if (uip_closed()) {
    if (s->user) free(s->user);
    s->user = NULL;
  }
}

// This function is going to use uIP's protosockets to handle the connection.
// This means it must return int, because of the way the protosockets work.
// In a nutshell, when a PSOCK_* macro needs to wait for something, it will
// return from handle_connection so that other work can take place.  When the
// event is triggered, uip_callback() will call this function again and the
// switch() statement (see below) will take care of resuming execution where
// it left off.  It *looks* like this function runs from start to finish, but
// that's just an illusion to make it easier to code :-)
int handle_connection(uip_tcp_appstate_t *s, connection_data *d)
{

  // All protosockets must start with this macro.  Its internal implementation
  // is that of a switch() statement, so all code between PSOCK_BEGIN and
  // PSOCK_END is actually inside a switch block.  (This means for example,
  // that you can't declare variables in the middle of it!)
  PSOCK_BEGIN(&s->p);

  // READ THE TEMPERATURE AND SEND IT TO THE BROWSER.
  read = senRead();
  temp = getTemp(read);
  dtostrf(temp, 2, 0, tempchar);
  PSOCK_SEND_STR(&s->p, tempchar);
  
  PSOCK_SEND_STR(&s->p, " degrees ");
  // Read some returned text into the input buffer we set in PSOCK_INIT.  Data
  // is read until a newline (\n) is received, or the input buffer gets filled
  // up.  (Which, at 16 chars by default, isn't hard!)
  PSOCK_READTO(&s->p, '\n');

  // Since any subsequent PSOCK_* functions would overwrite the buffer, we
  // need to make a copy of it first.  We can't use a local variable for this,
  // because any PSOCK_* macro may make the function return and resume later,
  // thus losing the data in any local variables.  This is why uip_callback
  // has allocated the connection_data structure for us to use instead.  (You
  // can add/remove other variables in this struct to store different data.
  // See the other file in this sketch, serialip_conn.h)
  strncpy(d->name, d->input_buffer, sizeof(d->name));
  // Note that this will misbehave when the input buffer overflows (i.e.
  // more than 16 characters are typed in) but hey, this is supposed to be
  // a simple example.  Fixing this problem will be left as an exercise for
  // the reader :-)

  // Send some more data over the connection.
 // PSOCK_SEND_STR(&s->p, "Hello ");
 // PSOCK_SEND_STR(&s->p, d->name);

  // Disconnect.
  PSOCK_CLOSE(&s->p);

  // All protosockets must end with this macro.  It closes the switch().
  PSOCK_END(&s->p);
}




int senRead(){
  int one = analogRead(0);
  delay(100);
  int two = analogRead(0);
  delay(100);
  int three = analogRead(0);
  delay(100);
  int four = analogRead(0);
  delay(100);
  int five = analogRead(0);
  delay(100);
  int six = analogRead(0);
  delay(100);
  int seven = analogRead(0);
  delay(100);
  int eight = analogRead(0);
  
  return ((one + two + three + four + five + six + seven + eight) / 8);
}

int getTemp(int number){
  
    return (((.1718 * number) + -31.6792));

}




void goTo(int row, int col) {
  LCD.print(0xFE, BYTE);   //command flag
  LCD.print((col + row*64 + 128), BYTE);    //position
  delay(LCDdelay);
}
void clearLCD(){
  LCD.print(0xFE, BYTE);   //command flag
  LCD.print(0x01, BYTE);   //clear command.
  delay(LCDdelay);
}
void backlightOn() {  //turns on the backlight
  LCD.print(0x7C, BYTE);   //command flag for backlight stuff
  LCD.print(100, BYTE);    //light level.
  delay(LCDdelay);
}
void backlightOff(){  //turns off the backlight
  LCD.print(0x7C, BYTE);   //command flag for backlight stuff
  LCD.print(128, BYTE);     //light level for off.
   delay(LCDdelay);
}
void serCommand(){   //a general function to call the command flag for issuing all other commands  
  LCD.print(0xFE, BYTE);
}

I can't wait to hear your responses. I am very novice and I hope I am headed in the right direction! Thank you.
12  Using Arduino / Networking, Protocols, and Devices / Re: SerialIP and the passing of Variables on: December 01, 2011, 02:01:24 pm
It worked! Well done and thank you. The world is now mine smiley
13  Using Arduino / Networking, Protocols, and Devices / Re: SerialIP and the passing of Variables on: December 01, 2011, 11:23:16 am
Thank you! I will try this ASAP; it looks promising. Thanks for your help.

I wishlisted the ethernet shield and had been looking for ways to connect an arduino without it for ages. I think SerialIP needs to be a little more popular.

Ultimately I'm working toward something like this: http://ricardo-dias.com/node/142. It looks much more fun than buying an ethernet shield.
14  Using Arduino / Networking, Protocols, and Devices / Re: SerialIP and the passing of Variables on: December 01, 2011, 10:31:11 am
Ready!

I'm trying to pass temperature data through PSOCK_SEND_STR(&s->p, tempchar);

Code:
/*
 * SerialIP Hello World example.
 *
 * SerialIP is a TCP/IP stack that can be used over a serial port (a bit
 * like a dial-up Internet connection, but without the modem.)  It works with
 * stock Arduinos (no shields required.)  When attached to a PC supporting
 * SLIP, the Arduino can host network servers and access the Internet (if the
 * PC is configured to share its Internet connection of course!)
 *
 * SerialIP uses the fine uIP stack by Adam Dunkels <adam@sics.se>
 *
 * For more information see the SerialIP page on the Arduino wiki:
 *   <http://www.arduino.cc/playground/Code/SerialIP>
 *
 *      -----------------
 *
 * This Hello World example sets up a server at 192.168.5.2 on port 1000.
 * Telnet here to access the service.  The uIP stack will also respond to
 * pings to test if you have successfully established a SLIP connection to
 * the Arduino.
 *
 * SLIP connection set up under Linux:
 *
 *  # modprobe slip
 *  # slattach -L -s 115200 -p slip /dev/ttyUSB0     (see note below)
 *  # ifconfig sl0 192.168.5.1 dstaddr 192.168.5.2
 *
 *  # ping 192.168.5.2
 *  # telnet 192.168.5.2 1000
 *
 * Here 192.168.5.1 is the address you will give to your PC, and it must be
 * unique on your LAN.  192.168.5.2 is the IP you will give the Arduino.  It
 * must also be unique, and must match the address the Arduino is expecting
 * as set by the "myIP" variable below.
 *
 * Note that slattach won't return so you'll need to run ifconfig from
 * another terminal.  You can press Ctrl+C to kill slattach and release the
 * serial port, e.g. to upload another sketch.
 *
 * This example was based upon uIP hello-world by Adam Dunkels <adam@sics.se>
 * Ported to the Arduino IDE by Adam Nielsen <malvineous@shikadi.net>
 */

#include <SerialIP.h>
#include <Stdio.h>

// The connection_data struct needs to be defined in an external file.
#include "HelloWorldData.h"

int temp;
char* tempchar;

void setup() {

  // Set up the speed of our serial link.
  Serial.begin(115200);

  // Tell SerialIP which serial port to use (some boards have more than one.)
  // Currently this is limited to HardwareSerial ports, until both it and
  // SoftwareSerial inherit from a common base class.
  SerialIP.use_device(Serial);

  // We're going to be handling uIP events ourselves.  Perhaps one day a simpler
  // interface will be implemented for the Arduino IDE, but until then... 
  SerialIP.set_uip_callback(uip_callback);

  // Set the IP address we'll be using.  Make sure this doesn't conflict with
  // any IP addresses or subnets on your LAN or you won't be able to connect to
  // either the Arduino or your LAN...
  IP_ADDR myIP = {192,168,5,2};
  IP_ADDR subnet = {255,255,255,0};
  SerialIP.begin(myIP, subnet);

  // If you'll be making outgoing connections from the Arduino to the rest of
  // the world, you'll need a gateway set up.
  //IP_ADDR gwIP = {192,168,5,1};
  //SerialIP.set_gateway(gwIP);

  // Listen for incoming connections on TCP port 1000.  Each incoming
  // connection will result in the uip_callback() function being called.
  SerialIP.listen(1000);
}

void loop() {
  // Check the serial port and process any incoming data.
  SerialIP.tick();
  temp = 32;
  dtostrf(temp, 2, 0, tempchar);
  // We can do other things in the loop, but be aware that the loop will
  // briefly pause while IP data is being processed.
}

void uip_callback(uip_tcp_appstate_t *s)
{
  if (uip_connected()) {

    // We want to store some data in our connections, so allocate some space
    // for it.  The connection_data struct is defined in a separate .h file,
    // due to the way the Arduino IDE works.  (typedefs come after function
    // definitions.)
    connection_data *d = (connection_data *)malloc(sizeof(connection_data));

    // Save it as SerialIP user data so we can get to it later.
    s->user = d;

    // The protosocket's read functions need a per-connection buffer to store
    // data they read.  We've got some space for this in our connection_data
    // structure, so we'll tell uIP to use that.
    PSOCK_INIT(&s->p, d->input_buffer, sizeof(d->input_buffer));

  }

  // Call/resume our protosocket handler.
  handle_connection(s, (connection_data *)s->user);

  // If the connection has been closed, release the data we allocated earlier.
  if (uip_closed()) {
    if (s->user) free(s->user);
    s->user = NULL;
  }
}

// This function is going to use uIP's protosockets to handle the connection.
// This means it must return int, because of the way the protosockets work.
// In a nutshell, when a PSOCK_* macro needs to wait for something, it will
// return from handle_connection so that other work can take place.  When the
// event is triggered, uip_callback() will call this function again and the
// switch() statement (see below) will take care of resuming execution where
// it left off.  It *looks* like this function runs from start to finish, but
// that's just an illusion to make it easier to code :-)
int handle_connection(uip_tcp_appstate_t *s, connection_data *d)
{
  // All protosockets must start with this macro.  Its internal implementation
  // is that of a switch() statement, so all code between PSOCK_BEGIN and
  // PSOCK_END is actually inside a switch block.  (This means for example,
  // that you can't declare variables in the middle of it!)
  PSOCK_BEGIN(&s->p);

  // Send some text over the connection.
  PSOCK_SEND_STR(&s->p, tempchar);

  // Read some returned text into the input buffer we set in PSOCK_INIT.  Data
  // is read until a newline (\n) is received, or the input buffer gets filled
  // up.  (Which, at 16 chars by default, isn't hard!)
  PSOCK_READTO(&s->p, '\n');

  // Since any subsequent PSOCK_* functions would overwrite the buffer, we
  // need to make a copy of it first.  We can't use a local variable for this,
  // because any PSOCK_* macro may make the function return and resume later,
  // thus losing the data in any local variables.  This is why uip_callback
  // has allocated the connection_data structure for us to use instead.  (You
  // can add/remove other variables in this struct to store different data.
  // See the other file in this sketch, serialip_conn.h)
  strncpy(d->name, d->input_buffer, sizeof(d->name));
  // Note that this will misbehave when the input buffer overflows (i.e.
  // more than 16 characters are typed in) but hey, this is supposed to be
  // a simple example.  Fixing this problem will be left as an exercise for
  // the reader :-)

  // Send some more data over the connection.
  PSOCK_SEND_STR(&s->p, "Hello ");
  PSOCK_SEND_STR(&s->p, d->name);

  // Disconnect.
  PSOCK_CLOSE(&s->p);

  // All protosockets must end with this macro.  It closes the switch().
  PSOCK_END(&s->p);
}


My problem is at dtostrf(temp, 2, 0, tempchar);

I've even used sprintf(tempchar, "%d", temp); with the same results, a server that hangs and will not ping upon connecting to the server.

If I do away with sprintf() or dtostrf() altogether and make tempchar = "45"; it passes through just fine!

What a mystery...
15  Using Arduino / Networking, Protocols, and Devices / SerialIP and the passing of Variables on: December 01, 2011, 08:14:51 am
I've been playing with SerialIP for the past few days and have been remarkably successful in connecting it with ubuntu. Lately I have been trying to pass my own data from the arduino and viewing it in the browser using the web server example.

I promise I will get the offending code up soon but my problem is the following:
Any time I try to pass an integer to PSOCK_SEND, the arduino server either hangs or the browser times out and no page is displayed.

I have tried using sprintf() and a couple other means of converting an integer to char* but they all seem to bring the whole thing down.

Does anyone have similar problems?
Pages: [1]