It would be better to write this as ...
server.write( (byte*) &pi, sizeof(pi) );
in case the datatype of variable pi is ever changed.
I stand corrected. You are right!
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
Hi
I'm using TCP/IP to talk between the mega and processing software. The laptop is used to create an ethernet server(not sure if that is the word). The mega is the master and Processing is the slave. To read from processing I use Myclient.read() , and to write, Myclient.write(). However, from the Mega, I use server.write(). All this is carried over an ethernet cable from Mega's ethernet shield to the laptop's RJ45 Ethernet port. I have no trouble sending data to and from the Mega, as long as it contains 3 digits.
Here is what Arduino says about server.write()
Syntax
server.write(data)
server.write(buffer)
server.write(buffer, size)
Parameters
data: the value to write (byte or char) buffer : an array of data (byte or char) to write size : size of the buffer to write (byte)
Ron, I hear what you are saying, but don't now how to split the voltage into 4 bytes and send then over. I don't know the proper syntax. I scoured the web, but my brain pan had too much teflon in it and nothing stuck. I tried this: I multiplied the voltage by 100 to eliminate the decimal point, then did this:
byte battValue = 1448
byte battery[];
battery[0] = battValue/256;
battery[1] = battValue % /256;
server.write(battery,2);
I found this on a forum somewhere, but that is as far as I got. It sent over a value of 230 or something like that to Processing, which means nothing as far as I see it.
This is starting to sound a little suspicious. Your original post says that you're using Processing, but here you specify the Arduino library (possibly Ethernet).
Are you trying to mix and match an Arduino library with the Processing language ?
Anything seems possible when you don't know what you're talking about.
Syntax
server.write(data) server.write(buffer) server.write(buffer, size)
Parameters
Use the third overload version and send a float.
float voltage = 14.48486; server.write((byte*) &voltage, sizeof(voltage));
The Processing software will receive the four byte float value of 14.48486. I have no idea how Processing is told how to accept the data. I'm guessing if you already know how to accept the byte value of 145 and convert it back to 14.5v on the Processing side, you know how to do about any other reception.
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 have been using Game Control Plus which controls an XBox wireless controller which communicates with Processing. I use Processing to install some graphics which I then use to communicate with the Arduino Mega. I use the Mega for controlling my I/O which shows up in Processing's graphic screen. This is all connected with an ethernet cable between my laptop and the Mega's ethernet shield.. Let me clarify things as I myself read my post and got all mixed up.
When I send data from Processing to Arduino Mega. I use client.write(String). I convert everything into strings and convert it back at the Mega to int or float. When I read at processing, I use client.read().
On the Arduino side, when I receive data from Processing, I use client.readString() .
When I send data to to Processing, I use server.write(), but have only needed to send 3 digit numbers indicating data has been received from processing and I send a token number back to processing.
Now I would like to send a 4 digit float back to Processing, which is the Mega's battery voltage including two two decimal places. such as 14.48 Volts. That is where I am stuck
I would like to apologize for the confusion I have caused.
Thank You
This is where I'm at a loss. I don't understand what the third overload version
float voltage = 14.48486; server.write((byte*) &voltage, sizeof(voltage));
What value or expression goes into byte*
What library are you using on the Arduino?
You say you convert everything to strings and send strings from Processing to the Mega. Can you not do the same FROM the Arduino, send data as strings from the Mega and receive them as strings in Processing ?
Anything seems possible when you don't know what you're talking about.
byte battValue = 1448
byte battery[];
battery[0] = battValue/256;
battery[1] = battValue % /256;
server.write(battery,2);
I found this on a forum somewhere, but that is as far as I got. It sent over a value of 230 or something like that to Processing, which means nothing as far as I see it.
This is not going to work. After the first statement setting battValue = 1448; you'll find it is already wrong. Try printing it out. A byte variable can NOT hold the value 1448. It can only hold values between 0 and 255!
Is there some other reason related to Processing software why you can't send the float value as I described above?
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
This is where I'm at a loss. I don't understand what the third overload version
float voltage = 14.48486; server.write((byte*) &voltage, sizeof(voltage));What value or expression goes into byte*
The (byte*) simply tells the compiler that the next argument is to be treated as the address of a byte (instead of the float which is the actual type). This is because the compiler expects the server.write's first argument to be a byte address.
Anything seems possible when you don't know what you're talking about.
This is where I'm at a loss. I don't understand what the third overload version
float voltage = 14.48486; server.write((byte*) &voltage, sizeof(voltage));What value or expression goes into byte*
@gameworm NOTHING, that is called a typecast. it stops the compiler from issuing an error message because voltage is NOT a byte, it's a float.
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.
This is where I'm at a loss. I don't understand what the third overload version
float voltage = 14.48486; server.write((byte*) &voltage, sizeof(voltage));What value or expression goes into byte*
DID YOU TRY IT? Write those two lines of code verbatim.
The third overload sends the actual bytes in a buffer. In this particular case, the buffer is the memory associated with the float variable "voltage".
- (byte*) is simply type casting so the compiler doesn't freak out.
- The & says to look at the address of the variable in the Arduino's memory. This is a very common C/C++ way of using what are called pointers.
- The sizeof(voltage) parameter says to take that number of bytes that are at that location and send them.
They will contain the value 14.48486 on the other side. It's really that simple. There is no magic. There is no Mickey Mouse conversion on either side. If you really want to send an 1448, you will do it like this:
int voltage = 1448; server.write((byte*) &voltage, sizeof(voltage));
As @will pointed out earlier... the second line didn't even have to change when the variable type is changed from a float to an int in line 1.
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
Hi Will
The Server.write data: the value to write (byte or char) buffer : an array of data (byte or char) size of the buffer to write
I am using as libraries #include <servo.h>, #include <SPI.h> , #unclude<Ethernet.h>
After using the formula, I am sending out 13.76V, but receiving 15.4. I need to figure out where this discrepancy comes from. There is a lot to look at.
Thank You for now until I can get this figured out. I will be back.
Good, thanks - now we know what functions are available to you.
Can you please show us YOUR code from your sketch that sends the data from the Arduino and the part that receives it in Processing. Please copy and paste it, do NOT type it in yourself (we get too many typing errors that way).
Anything seems possible when you don't know what you're talking about.
I will send you the code that correctly converts the 3 digit voltage first.
Arduino
value = analogRead(analogInput); //reads the battery voltage at analog input A15 across a resistor voltage
// divider circuit
vout = value * (5./1024.0) * ((R1+R2)/R2);
Vout = vout *0.945; // adjusts for resistor tolerance
voutA = vout * 10; // makes a 3 digit value
server.write(voutA);
Serial.print("The value of voutA is: ");
Serial.println(voutA);
Processing
dataIn = myClient.read();
print("dataIn is: ");
println(dataIn);
if (dataIn > 100 && dataIn <= 250); // dataIn receives values greater than 250 from other inputs
{
battVolts = ((float) dataIn * 0.1); // changes the 3 digit number to a two digit plus one decimal
print("Battery Voltage is:")
println(battVolts); // batt volts is sent to a voltmeter on the processing screen
}
This correctly reads the battery voltage with two numbers and two decimal points eg 14.84. It is then multiplyed by 10 then written to processing as 148. Processing multiplies this by 0.1 to get a voltage value of 14.8. This value gets input intpoa voltmeter on a Processing screen.
After using the formula, I am sending out 13.76V, but receiving 15.4. I need to figure out where this discrepancy comes from. There is a lot to look at.
Arduino Side
float voltage = 13.76; server.write((byte*) &voltage, sizeof(voltage));
Processing Side
float voltage; myClient.readBytes((byte*)&voltage, sizeof(voltage)); println(voltage);
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