Re: Newbee: Very general question wrt iot usage of the esp
Posted: Tue Feb 23, 2016 2:27 pm
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");
}