-->
Page 4 of 4

Re: Newbee: Very general question wrt iot usage of the esp

PostPosted: Tue Feb 23, 2016 2:27 pm
by Arduolli
martinayotte wrote:Unfortunately, we need to see more of your ESP code.
But I think the issue is that you are leaving the client opened, it should be close it and reconnect new client for each transmission.
Also, BTW, I see that you are sending all the 10 timestamps on the same line, you should at least add a separator such a comma or space between each.

Hi martinayotte; thanks for your feedback.
I know that I am missing commas or similar - these got lost during trial and error hunting....
Also, I am not sure about the closing issue, as I send packets of ten - and for each packet the last is received.....

Here is the entire code - I hope this helps to nail it down - thanks again:
Code: Select all#include <Bounce2.h>
#include <Time.h>
#include <ESP8266WiFi.h>
#define REED_PIN 2
#define loopmaxcount 10

const char* ssid = "xxxxxxx";
const char* password = "xxxxxx";

const char* host = "xxxxxx";

int GasCounter = 0;   // counter; 1 equals 0,01 m3
int timestamp[loopmaxcount];      // time stamp array for sending batches of #[loopmaxcount] to server


// Instantiate a Bounce object
Bounce debouncer = Bounce();

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO2
  pinMode(REED_PIN, INPUT_PULLUP);

  // connect to WiFi

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

   // After setting up the reed-switch, setup the Bounce instance :
  debouncer.attach(REED_PIN);
  debouncer.interval(50); // interval in ms
}

int value = 0;

void loop() {
  for(int x = 0;x < loopmaxcount; x++) {  // accumulate batch of timestamps
    // Update the Bounce instance :
    debouncer.update();
    // compare the reedState to its previous state
    if (debouncer.fell()) {
      ++GasCounter;
      Serial.println(GasCounter);
      timestamp[x]=now();
    }
    else {
      x--;
    }
  //  yield();
   delay(100);
  }

 delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int Port = 9999;
  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
    return;
  }

  // This will send the data to the server
 for(int x = 0;x < loopmaxcount; x++) {  // send batch of timestamps server
    client.println(timestamp[x]);
  //    client.println();
 }
  delay(10);

  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}

Re: Newbee: Very general question wrt iot usage of the esp

PostPosted: Tue Feb 23, 2016 3:55 pm
by martinayotte
Ok ! I think I found out !

Doing single client.println() is known to start packet sending right away, not waiting a WiFi buffer been full.

So, it is better to optimize data to be sent in a single string to be sent with only one client.println().

Code: Select allString s = "";
for(int x = 0;x < loopmaxcount; x++) {  // send batch of timestamps server
    s += String(timestamp[x]) + "\n";
 }
client.print(s);

Re: Newbee: Very general question wrt iot usage of the esp

PostPosted: Tue Feb 23, 2016 4:17 pm
by Arduolli
martinayotte wrote:Ok ! I think I found out !

Doing single client.println() is known to start packet sending right away, not waiting a WiFi buffer been full.

So, it is better to optimize data to be sent in a single string to be sent with only one client.println().

Code: Select allString s = "";
for(int x = 0;x < loopmaxcount; x++) {  // send batch of timestamps server
    s += String(timestamp[x]) + "\n";
 }
client.print(s);


GREAT!
Thank you so much - this works!
Have a good evening.