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.
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) {
// 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
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.
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.
@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).
@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:
LocalClient in Processing:
RobustClient in Processing:
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:
Hope frogandtoad doesn't mind but I have copied the code and am posting it for easy copy paste.
// server codeimport processsing.net.*;
Server server;
byte[] voltage = {'1','4','.','7','5','.');
voidsetup(){
size(200, 200);
server = new Server(this,4242);
}
voiddraw() {
server.write(voltage);
delay(1000);
}
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','.');
voidsetup(){
@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, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@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, 360, fairly knowledge in PC plus numerous MPU's & 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.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
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.
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
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
We use cookies on the DroneBot Workshop Forums to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.