Hi everyone,
Currently, I am trying to send a floating point number every two seconds from an Arduino WebServer, running off an Arduino Uno R3 with an Ethernet Shield 2 (W5500), to a python client. Currently, the client can connect to server, but every time the value is grabbed from the server by the client, I get something like this in the terminal:
b'1'
b'.00'
b'1'
b'.00'
....
If I remove the delay(2000), I then obtain:
b'1.001.001.001.001.001.001.001.001.001.001.001.001.001'
b'1.001.001.001.001.001.001.001.001.001.001.001.001.001'
b'1.001.001.001.001.001.001.001.001.001.001.001.001.001'
I would like to get something like:
1.00
1.00
1.00
1.00
Given that I want to simply send 1.00 as one whole character string and not have it displayed in two separate lines, nor as a byte string, what can I do to achieve this? How can floating point numbers be sent via Ethernet?
(The reason why I want this delay is I eventually want to use this to record the number of pulses from a Hall Sensor arriving per second and then send this value to the client via the Ethernet)
Below is my basic Arduino Code for completeness:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x69, 0x13};
IPAddress ip(198, 162, 1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
Serial.begin(9600); //Starting serial monitor
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client)
{
boolean currentLineIsBlank = true;
while (client.connected())
if (client.available())
{
delay(2000);
client.print(1.00);
client.println();
}
}
}
The python code for the client is given below:
import asyncio
import websockets
import socket
import time
import datetime
import struct
starttime = time.time() # start value for timed data acquisition
# setup socket 1
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.connect(("198.162.1.177",80))
s1.send('Hello Server'.encode())
if True:
print('Connected')
def acquire():
data = s1.recv(6000)
dataline_tmc1 = datetime.datetime.today().isoformat() + "," + str(data) + "," + str(0) + "\n" # format and assemble data line
print(dataline_tmc1)
file_name_tmc1 = '{:%Y%m%d}'.format(datetime.datetime.today()) + "-test.csv" # generate file name
with open(file_name_tmc1, "a") as tmc_file1: # append dataline to file
tmc_file1.write(dataline_tmc1)
while True:
acquire()
time.sleep(1.0 - ((time.time() - starttime) % 1.0)) # continue every (1.0 seconds)
s1.close()