Writing sensor data from a client esp to a server esp using the ESPAsync library

Hi,

i've been struggling with with a project of mine that requires data from two HMC5883l compass sensors to be used by two corresponding servo motors.

The hardware im using is:

  • 3x NodeMCU ESP8266
  • 2x HMC5883l magnetometer sensors
  • 2x Sg 90 Servo motors

The method i've ended up at is using three ESP8266 boards overall,

  • Board A - to be the server, has two servo motors, moves them using the sensor readings it receives from Board B & Board C.
  • Board B - to be a client, has the first HMC connected to it, required to send this reading to the Board A.
  • Board C - to be another client, has the second HMC connected to it, required to send this reading to the Board A.

The problem is that, being a beginner to coding, I'm having trouble understanding the right functions and syntax to write the code for what I want to do.

So far, I've been referring to this tutorial

where they show how to set up a client to read from a server:

in the server code:

server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readTemp().c_str());
  });

and

in the client code:

temperature = httpGETRequest(serverNameTemp);

what I need to do is the opposite of this, having two clients write to a server.

from reading through the library documentation

I've understood that the tutorial was using the get method to read values from the server whereas I need to use the post method to write values to the server.

but I've tried to break the get command down and understand the syntax used in the tutorial and modify it into a post command to no avail.

I've had no luck trying the find a "list of functions" that I can refer to, no luck finding examples where a client writes to a server.

I would greatly appreciate some help in understanding the syntaxes for the get and post methods.

Thanks in advance !

for communication between ESP8266 boards have a look at esp-now-esp8266 - a lot simpler than using HTTP GET
not sure why you are using three 8266 boards where one would probably do
are the boards a fair distance apart? how far? remember the range of WiFi is limited

edit: not used the HMC5883l but have used pairs of LSM9DS1 9-axis iNEMO Inertial Module which also uses the I2C interface - no problem to connect to a ESP8266

1 Like

Hey horace !

I had a look at espnow, and did indeed find it much easier to understand, I rewrote the code using espnow this time, ill test it out when i get my hands on the circuit in the morning tomorrow.

Thanks for the help !

And the reason im using so many boards is that the receiver board that has the servos is gonna rest on the study desk and the two sender boards with the HMCs are to be worn by the user.

And the problem with the HMCs is that they have the same I2C address and no way to change it, and the esp8266 only has one set of I2C pins hence i cant set up multiple I2C buses to use both sensors at once. i was considering an I2C multiplexer but it would take too long to deliver and i have multiple esp8266 boards lying around anyway so I figured this was much quicker.

edit: spelling errors

if you have an ESP32 available it has two I2C busses

I don't, unfortunately. I hadn't looked into it previously, but I'll keep it in mind for future projects though.

Thanks a lot !

how many samples/second do you require from the sensors
I recommend that you transmit data in binary not text to reduce packet size

not much really, just once every two seconds

should be no problem

ive been messing around with it for a bit now, and its working, but very inconsistently.
this is the serial monitor on the board that is sending out sensor readings, i suspected it might be connection errors so i tried using the wifi in station mode as well as soft access point, station is inconsistent, softAP doesnt even work. do you happen to know whats causing this ?

don't you want your two boards with the sensors to be servers that are queried by the client with the servos based on the response from the servers?

So when I was using the Async library, this would have been a good method to do it, but ive switched it to mainly using ESPNow(Specifically this tutorial) upon horace's suggestion with more success in terms of just the simplicity of it all.

One reason why ESP-NOW works unreliably is to use fast running loops.
Inserting a function-call

yield();  

made it work reliably

For future postings you can post the content of the serial monitor too as a code-section by
mark = Ctrl-A,
Copy = Ctrl-C,
inserting a code-section by clicking the </>-Button
paste = Ctrl-V

activate the timestamp which gives additional information
image

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.