So i have a little experiment project where i would like to try and transmit the data from one ESP8266 to another.
The idea is:
I have a signal generator that generates a signal once every second, pulse width of the signal changes every second, and ranges from 100ms to 500ms. This signal is fed to an Arduino pin directly from a signal generator, and this works ok, BUT... What i would like to do is, instead of feeding the signal directly from signal generator to Arduino, i would feed the signal from a generator to an ESP8266 board (server) and transmit it to another ESP8266 board (client). This client board should then just pass that received signal to an output pin and then feed the signal to Arduino pin.
So basically, the same routine as feeding the signal directly from signal generator to Arduino but this time including 2xESP8266 to transfer the signal over WiFi.
Effectively, you are transmitting at the start of the "ON" period and again at the end of the "ON" period. The "OFF" period is derived from that on the receiver side.
I'm not sure about the latencies but you appear to need only an accuracy in the range of 10s of milliseconds, so an HTTP GET could do that. There may be better ways though.
I've managed to do something, basically using button/led philosophy, but instead of a button on the server side i have a signal generator and instead of a LED on the client side i have a PIN output that basically mirrors the signal of server input pin.
The problem is, i get it to work for 2 or 3 pulses and then it just stops for some reason...
Since frequency of my signal is just 1Hz, speed or latency should not be an issue at all.
Server:
Transmit only start of press and end of press.
You are doing a lot of initialisation in the loop() which needs to be done only once at program start.
Have ready built Strings for the ON case and the OFF case.
Receiver:
Json is very heavy weight for just one value. You can do something like :
if (server.arg(“sensor_reading”).toInt() == 1 ) . . .
instead.
You can do the digitalWrite() in handleSentVar() instead of the loop()
I am aware that json is heavy, as you said especially as i am transmitting basically just one bit per second. However, my knowledge on this one is very limited so i am struggling to find alternative solution.
In a meantime, i have tidied up the code a little but nothing radical that would eliminate transmission latency.
OK. I think I see it. The transmitted data is currently a compound JSON format. The modified receiver code expects a simple format argument list consisting of only one argument.
We give it one more try together with the modified receiver code.
Replace this on the sender:
// We now create a URI for the request. Something like /data/?sensor_reading=123
String url = "/data/";
url += "?sensor_reading=";
url += "{\"sensor0_reading\":\"sensor0_value\",\"sensor1_reading\":\"sensor1_value\",\"sensor2_reading\":\"sensor2_value\",\"sensor3_reading\":\"sensor3_value\"}";
with this:
// We now create a URI for the request. Something like /data/?sensor_reading=123
String url = "/data/?sensor_reading=sensor1_value";
Once that works, we can eliminate the return part where the receiver sends a message back to the sender hopefully eliminating half the latency.
OK. Let's try sending only if there is a change of the "button state" . This may not improve much but is a preparation to drop the part where the receiver sends an message back to the sender.
On the sender side, add this block:
// send only on change of sensorValue1
static int sensorValueLast ;
if ( sensorValueLast == sensorValue1 ) return ; // leave
sensorValueLast = sensorValue1 ;
This works! i am now getting random latency of more than 20ms on only a few bits out of 50-60. Average is now around 10ms. Huge improvement.
Thank you!
Removing return message should cut it further i think
OK. The last change meant a transmission occurred only on a change of state
Then this may not bring so much, because the return message should now occur only in a dead period. but let's see
On the sender, comment this out:
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 50000) {
client.stop();
return;
}
}
and on the receiver, comment this out:
server.send(200, "text/html", "Data received"); // return to sender
The next thing after that is to make the HTTP GET Strings constant instead of rebuilding each time. We need 2, one for sensorValue1 = 1 and the other for sensorValue1 = 0
ok, still works, latency about the same, often below 10ms now, 6-8ms most of the time, and just random jumps to ~20ms.
main loop on transmitter is still cluttered as you said, i've tried various "fixes" but so far none worked...
latest code makes the latency dip lower to as low as 4-5ms but also hits ~20-25ms more frequently than the last one, so i think i will test both further and see if there is anything else that can be done, although this is huge improvement and perhaps there is no point of trying to get it lower. It would be amazing to get a consistent latency below 5ms but not sure that is possible, at least with the current code or setup.
thank you for all the help and effort i really appreciate it!
Alek