Notifications
Clear all

Sending data over the ethernet using server.write()

74 Posts
6 Users
7 Likes
4,860 Views
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

This is what I found when searching for pointers and processing. Java does not support pointers that is a C/C++ thing.  When I type the code in Processing, I get a syntax error=Bad Identifier.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @gameworn

This is what I found when searching for pointers and processing. Java does not support pointers that is a C/C++ thing.  When I type the code in Processing, I get a syntax error=Bad Identifier.

You hadn't mentioned it and I didn't read the Processing manual, I never got the clue it was written in JavaScript.  You'll need to see if and how it handles binary data.  I know JavaScript, in general, can read binary data using constructs like ArrayBuffer, but I would assume your Processing library has wrappers to handle binary data in a more friendly manner.  It would be awfully limiting to restrict to only a byte (values between 0 to 255) and strings.

If you can't find a binary answer, you'll have to resort back to an earlier suggestion by @will to using strings.  JavaScript is very wishy-washy about variables.  They can be of any type at any time.  It should easily convert an incoming string back to a number.

On the Arduino side, something like...

float voltage = 14.48;
char buff[12];
sprintf(buff, "%f", voltage);
Serial.println(buff);    
server.write(buff);

You'll want to make sure the first time that the Arduino compiler can handle the sprintf correctly.  If I recall correctly, it has trouble with float conversions in printf and sprintf function.  

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

Hi All

First things first. I would like to thank all who responded to my request for help. Your professionalism and hard work did not go unnoticed. You continued to offer help no matter how many times I said I did not understand, or that something did not work.

I spent pretty much the last few days and nights and early mornings trying to make this work.

I did find a solution albeit one that would never be taught in a classroom setting.

Here it is: I want to send a battery voltage from Arduino Mega to Processing onto a voltmeter display. Processing will not accept anything larger than -126 to 125 bytes in size and definitely no decimal values. My nominal voltage will be 14.84 Volts. I multiplied this by 100 to get a 4 digit number or 1484. I decided that I would never see a voltage greater than 15.00 V,  or  1500 when multiplied by 100. Since 125 is my max that Processing sees, I divided 1500/125 and got 12(nice even number). At the processing end, I read 125, multiplied  by 12, then multiplied it by 0.01 to get my two digit decimal value back. I ran my power supply down from 15.00 volts to 10.00 volts and checked it against the voltmeter display in Processing. My accuracy is between 0.02 volts  and 0.05 V full scale. At 14.84 volts from my power supply, my voltmeter displays 14.82 volts. 

This is quite an  unorthodox way of doing things, but it worked for this application

Thank You again, to all who took the time to send in their great ideas and formulas


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6968
 

@gameworn If you can handle 125 bytes in size why all the messing around. You only need at most 4 bytes. I don't get it, and also don't get -126 bytes in size. If you meant in VALUE, then it's -128 to +127 signed, 0 to 255 unsigned.

 -126 to 125 bytes in size 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6968
 

@gameworn Can you provide a link to the documentation of what you call 'processing'?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

Hi All

First things first. I would like to thank all who responded to my request for help. Your professionalism and hard work did not go unnoticed. You continued to offer help no matter how many times I said I did not understand, or that something did not work.

I spent pretty much the last few days and nights and early mornings trying to make this work.

I did find a solution albeit one that would never be taught in a classroom setting.

Here it is: I want to send a battery voltage from Arduino Mega to Processing onto a voltmeter display. Processing will not accept anything larger than -126 to 125 bytes in size and definitely no decimal values. My nominal voltage will be 14.84 Volts. I multiplied this by 100 to get a 4 digit number or 1484. I decided that I would never see a voltage greater than 15.00 V,  or  1500 when multiplied by 100. Since 125 is my max that Processing sees, I divided 1500/125 and got 12(nice even number). At the processing end, I read 125, multiplied  by 12, then multiplied it by 0.01 to get my two digit decimal value back. I ran my power supply down from 15.00 volts to 10.00 volts and checked it against the voltmeter display in Processing. My accuracy is between 0.02 volts  and 0.05 V full scale. At 14.84 volts from my power supply, my voltmeter displays 14.82 volts. 

This is quite an  unorthodox way of doing things, but it worked for this application

Thank You again, to all who took the time to send in their great ideas and formulas


   
ReplyQuote
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

Hi you can find Processing at processing.org


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6968
 

@gameworn Did you not see that all the datatypes are there, see pic

Screen Shot 2022 07 28 at 15.34.43

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

Ron, you asked why all the messing around. One reason mostly. The biggest one being me. Being self taught, I did not truly understanding the concepts of what you guys were telling me to do. I have no idea what it means when you say I may need only 4 bytes.  It's my lack of understanding bytes and integers and why you have to break them up. That's it in a nutshell

 

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6968
 

@gameworn Ok, as you can see from my post above, you can write an integer. I think the statement will look like . This example is from https://processing.org/reference/libraries/net/Server_write_.html

@will told you about this documentation about 5 or 6 posts in, why did you not get it?

Now you still need to mess with the number to deal with decimals etc, OR use the String type (see pic) I assume you know how to convert an integer of 1448 into a string of '14.48'

Screen Shot 2022 07 28 at 16.14.18

 

import processing.net.*;

Server myServer;
int port = 5204;
int val = 0;

void setup() {
  size(200, 200);
  // Starts a myServer on port 5204
  myServer = new Server(this, port);
}

void draw() {
  val = val + 1;
  background(val);
  myServer.write(val);
}

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 
Posted by: @gameworn

Ron, you asked why all the messing around. One reason mostly. The biggest one being me. Being self taught, I did not truly understanding the concepts of what you guys were telling me to do.

If you want to learn how to do things, then if you don't understand what somebody is telling you, ASK them to explain. We are used to newbies and can often tell the ones who don't know much about what they are doing by the questions they ask (and how they ask them).

If you don't want to ask here, then you should use Google or a bookstore to get information and instructions on programming. As you've seen so far, it's very rare that you'll find something that so exactly does what you want that you can just copy and paste it.

Use the help that's available here.

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@gameworn

Ron, you asked why all the messing around. One reason mostly. The biggest one being me. Being self taught, I did not truly understanding the concepts of what you guys were telling me to do. I have no idea what it means when you say I may need only 4 bytes.  It's my lack of understanding bytes and integers and why you have to break them up. That's it in a nutshell

So do you understand bytes and integers now?

I am completely self taught but I did start from the beginning with learning how computers actually work at the level of hardware.

 


   
ReplyQuote
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

Okay Thanks for the vote of confidence. I'll stick with you. I am able to communicate from Arduino to Processing  and back to processing from Arduino. Where I am hung up is once I have a value greater than 255 to send to Processing, I get an error. Sending from Processing to Arduino is okay as processing allows me to send data in String or int form. However, writing to Processing is limited to byte or char form.

I copied the examples below from your suggestions. In the first example, no matter what my voltage was set to, buff showed ?.(question mark). It showed up as decimal 63 in Processing.

In the 2nd example,  for a voltage of 14.48V  I was looking at two values, 243 and 4 in Processing. I do not know what these values mean. Is the number 243 too large to be a byte?

How do I break down a 4 digit number into a byte or char that when it does, it  represents the applied voltage?

float voltage = 14.48;
char buff[12];
sprintf(buff, "%f", voltage);
Serial.println(buff);    
server.write(buff);


int voltage = 1448;
server.write((byte*) &voltage, sizeof(voltage));

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@gameworn 

byte 127 to -128

14.48V would be a floating point number.

As a string of 5 characters (char) = "14.48"

 code example:

Arduino code:

void setup() 
{
  //initialize serial communications at a 9600 baud rate
  Serial.begin(9600);
}

String stringOne = "14.48";

void loop()
{
  //send string
  Serial.println(stringOne);
  //wait 100 milliseconds so we don't drive ourselves crazy
  delay(500);
}

 

Processing code:

NOTE: I divided the val by 2 to show the string was really converted to a number not still a string.

import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
float num;


void setup()
{
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {
    val = myPort.readStringUntil('\n');         // read it and store it in val
    if (val != null){
      println(float(val)/2); //print it out in the console
    }
  }
}

 

 

 


   
frogandtoad reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robotbuilder

Posted by: @robotbuilder

@gameworn 

byte 127 to -128

14.48V would be a floating point number.

As a string of 5 characters (char) = "14.48"

 code example:

Arduino code:

void setup() 
{
  //initialize serial communications at a 9600 baud rate
  Serial.begin(9600);
}

String stringOne = "14.48";

void loop()
{
  //send string
  Serial.println(stringOne);
  //wait 100 milliseconds so we don't drive ourselves crazy
  delay(500);
}

 

Processing code:

NOTE: I divided the val by 2 to show the string was really converted to a number not still a string.

import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
float num;


void setup()
{
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {
    val = myPort.readStringUntil('\n');         // read it and store it in val
    if (val != null){
      println(float(val)/2); //print it out in the console
    }
  }
}

 

 

 

Processing (using Java), actually has a readBytes(...) method, but you'll have to loop over the stream to accumulate them all before conversion.  Having said that, clearly using a string is far superior and easier to use, and I always advocate that line of coding wherever possible, and performance is secondary to the outcome.

Now, as the coding nit picker I am... I would suggest using the following:

Float.parseFloat(val)

... instead of

float(val)

...because type casting to float masks the real error, whereas parseFloat is designed with exception handling in mind, and can report what went wrong with the conversion, if you write a little more code and would like a more bullet proof solution.

Cheers


   
ReplyQuote
Page 3 / 5