Notifications
Clear all

Sending data over the ethernet using server.write()

74 Posts
6 Users
7 Likes
4,740 Views
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@frogandtoad 

Now, as the coding nit picker I am...

Nit pick away!!  I am no expert on programming so it is all helpful to me as well.  I did see some versions using try catch as well.  I don't know how helpful the code was with regards myServer.write(val); as I have never used a server.

 


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

I am using an ethernet connection between the Arduino and Processing. I cannot use the serial port for communication as the distance between the two will be 100 feet or more. Here is the Arduino, then Processing Communications coding I am using. To talk from Arduino to processing uses server.write() which can transmit bytes or chars

Arduino

Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.begin(115200);
Serial.print("Web Server Address is: ");
Serial.println(Ethernet.localIP());
Serial.println("Hello?");

Serial.print("Web Server Address is: ");
Serial.println(Ethernet.localIP());
Serial.println("Hello again");
}

void loop() {
EthernetClient myClient = server.available();
// Serial.println(varInt);
// if an incoming client connects, there will be bytes available
if (myClient) // read bytes from the incoming client and write them back
{ // to any clients connected to the server;
if(!alreadyConnected)
{

alreadyConnected = true;
}
//var = myClient.readString()

 

 

Processing

// connect to the local machine at port 80
myClient = new Client(this, "192.168.137.211", 90); // This looks for the Ethernet board attached to the arduino mega board. The mega is the master

println("Connected to server");
myClient.write("hello from processing"); // myClient.write is how data is sent to the mega


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

@gameworn 

Not having your setup and not ever sending data over the ethernet I cannot test the code myself. The code you posted is incomplete. Where did the code come from?

processing uses server.write() which can transmit bytes or chars

Then on the Arduino side perhaps you can convert the number to a string. Using a loop extract and send each char one at a time ending with a new line character or null character.

On the Processing side using a loop read each char and append each char to a string until a new line character or null character is received. Convert the string back to a number.

 

 


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

That I can  do. I will let you know how I made out.


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

The code is bits and pieces from all over the place where if I could understand what they were saying and doing , I would include them in my sketch. The code for the Arduino side is shy of 1000 lines and the code for the Processing side is 1324 lines long.  There is an XBox wireless controller that talks to Processing and must be activated first. Then you open the the Arduino Serial monitor which   sends a message to Processing to say all is well and  is able to go.. Then You have to start the Prcessing sketch and away it goes.


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

@gameworn 

@robotbuilder is right... your code is so incomplete, it's hard for anyone to help you, so please in future; post the actual code that does the reading and writing parts that are not working - We are more than willing to help, but you need to help yourself to help us help you first, and it all starts with doing some research, and then presenting your concerns and misunderstandings of your problematic code 😉

Anyway, I don't have an Ethernet shield to test from an Arduino, but that doesn't matter as I have created a local server in Processing to mimic the transfer over Ethernet:

Here is a solution that you can test, and I could have implemented this in many ways, with bytes, strings etc... Below is a local server set up in processing, and two clients also set up in processing.  The server is mimicking the Arduino sending data over Ethernet, and there are two clients listening for that data - The first client is using casting as per @robotbuilder example to turn the data back into a float data type, and the second client using the robust exception handling I spoke about to do the same).

You can test the difference in reporting messages, by simply removing the last byte in the server byte array, or just making it another digit instead of a period character).

Server Code in Processing:

image

LocalClient in Processing:

image

RobustClient in Processing:

image

Hope this helps!

Cheers


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

@gameworn, @robotbuilder

Posted by: @frogandtoad

@gameworn 

@robotbuilder is right... your code is so incomplete, it's hard for anyone to help you, so please in future; post the actual code that does the reading and writing parts that are not working - We are more than willing to help, but you need to help yourself to help us help you first, and it all starts with doing some research, and then presenting your concerns and misunderstandings of your problematic code 😉

Anyway, I don't have an Ethernet shield to test from an Arduino, but that doesn't matter as I have created a local server in Processing to mimic the transfer over Ethernet:

Here is a solution that you can test, and I could have implemented this in many ways, with bytes, strings etc... Below is a local server set up in processing, and two clients also set up in processing.  The server is mimicking the Arduino sending data over Ethernet, and there are two clients listening for that data - The first client is using casting as per @robotbuilder example to turn the data back into a float data type, and the second client using the robust exception handling I spoke about to do the same).

You can test the difference in reporting messages, by simply removing the last byte in the server byte array, or just making it another digit instead of a period character).

Server Code in Processing:

image

LocalClient in Processing:

image

RobustClient in Processing:

image

Hope this helps!

Cheers

Oop's... I forgot that I was using exception handling, so no need to check for the 'null' in the function - Corrected line highlighted below:

image

Cheers


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

@frogandtoad 

FYI @gameworn

Hope frogandtoad doesn't mind but I have copied the code and am posting it for easy copy paste.

 

// server code
import processsing.net.*;

Server server;

byte[] voltage = {'1','4','.','7','5','.');
void setup(){
  size(200, 200);
  server = new Server(this,4242);
}

void draw() {
  server.write(voltage);
  delay(1000);
}

 

// LocalClient
import processing.net.*;

Client client;

String strVoltage;

void setup() {
  size(200, 200);
  client = new Client(this, "127.0.0.1",4242);
}

void draw() {
  if (client.available() > 0) {
    strVoltage = client.readString();
  }
  
  if (strVoltage != null)
    println(float(strVoltage)*2);
    
  delay(1000);
}

 

 

// RobustClient

import processing.net.*;

void setup() {
  size(200, 200);
  client = new Client(this, "127.0.0.1",4242);
}

float getVoltage(Client client) {
  String strVoltage = client.readString();
  
  return Float.parseFloat(strVoltage);
}

void draw() {
  float voltage = 0.0F;
  
  if (client.available() > 0 {
    try {
      voltage = getVoltage(client);
    }
    catch(Exception e) {
      println("Failed to parse incomming data with following error:\n\t + e + "\n");
    }
    finally {
      if (voltage > 0.0F) {
        println(voltage * 2);
      }
    }
  }
}

 

 

 


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

@robotbuilder

Posted by: @robotbuilder

@frogandtoad 

FYI @gameworn

Hope frogandtoad doesn't mind but I have copied the code and am posting it for easy copy paste.

No, I don't mind at all, thanks for reminding me 🙂

Cheers


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

Hi

I understand what is going on here, but the voltage array you are using is a passive one. I need to monitor the battery voltage  in real time.  This is where everything hangs up. Trying to convert that float to a byte and then sending  it over using client.write().

I can send you my entire sketch for Arduino and processing if you like. They are both a work in  progress. 

Thank You

byte[] voltage = {'1','4','.','7','5','.');
void setup(){

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

@gameworn First some administrivia, use the Reply link at the bottom of the message you are replying to, that way the person who sent the message is notified by email that you responded. Also make sure you are subscribed to your own posts.

As far as your 'problem' You CANNOT convert a FLOAT to a BYTE, a FLOAT is 4 bytes while a byte is one byte. 4 does not go into 1.

HOWEVER you can send a BUFFER of SIZE bytes. The problem is getting the float into the BUFFER. I will have to go look for the specifics but someone may jump in before I get there. One of the standard ways of converting a float to a buffer (char array, string) is to use the sprintf function, that is exactly like (f)ormatted print but to a (s)tring hence the "s print f" name. My  best guess is sprintf(myBuffer, "%f") <<< I am 100% sure that is not right in as far as it's an f but hopefully someone will correct quickly and provide the rest of the formatting given what he wants.

 

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: 3 years ago
Posts: 30
Topic starter  

Ron; Yes Thank You for that bit of admin advice

 I will keep up my efforts to get this thing working.


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

@gameworn Check the private message I sent you, it's actually quite simple if I understand what you are doing.

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
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @zander

HOWEVER you can send a BUFFER of SIZE bytes. The problem is getting the float into the BUFFER. I will have to go look for the specifics but someone may jump in before I get there.

I've already shown it once, but I guess, he missed it.  Here is sending both ways via a string that he mentioned he had seen work.  The binary is the Arduino side, but he did not find a way to receive it on the Processing side.  I find it hard to believe that any library purported to be a data receptor would be limited to JUST a byte or string.  Binary data reception is a common and most efficient manner.  I'm not quite willing to to go through that documentation to help find what is being missed.

float pi = PI;

// String representation
char buff[10];
sprintf(buff, "%f", pi);
server.write(buff);

// Binary represenation
server.write((byte*)&pi, sizeof(pi));

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
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

I know both of these methods will send properly.  The problem seems to be on the receiving end.  I've used both of these exact methods using TCP over WiFi on many projects, but when you have control of both the sending side and receiving side, things are a LOT easier.  

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
Page 4 / 5