Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By martinayotte
#37324
Petervdpol wrote:Thank you for your time, but I am afraid this solution doesn't do the job:


"previous_millis" is a sketch variable, it is not part of the SDK/core.
I didn't provide the whole code.
Simply add it globally in your sketch as "unsigned long previous_millis = 0;"
User avatar
By DrG
#37420
Petervdpol wrote:Well, since I am a NOOB, you must be mighty... Can you give me a hint how to work your code in mine? Depending where I place it I get errors regarding a " { " (at the top) or at the bottom I get a remark that " MinuteDelay" was not declared in this scope...


Try this -
Code: Select all#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <DHT.h>

const char* ssid = "Yggdrasil";
const char* password = "XXXXXXXXX";
char* topic_t = "openhab/esp8266-1/temp";
char* topic_h = "openhab/esp8266-1/vocht";
char* server = "192.168.X.X";
String clientName = "esp8266-1";

//time peter, idea taken from https://www.safaribooksonline.com/library/view/arduino-cookbook-2nd/9781449321185/ch12.html
const long oneSecond = 1000;  // a second is a thousand milliseconds
const long oneMinute = oneSecond * 60;
const long fiveMinutes = oneMinute * 5;
//time peter

#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE,15);

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

//ADDED CODE HERE
void MinuteDelay(int number){
  for (int i = 0; i < number; ++i)
    {
     
      // use 6 10 second delays for each minute // with a yield before each
      for(int j=0; j<6 ;j++)
      {
        yield();
        delay(10000);
       
      }
    }
  }
// END ADDED CODE


void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}

void setup() {
Serial.begin(115200);
delay(10);
dht.begin();
Serial.println();
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());

Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);


if (client.connect((char*) clientName.c_str())) {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic_t);
Serial.println(topic_h);

}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
}

void loop()

{

float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

static int counter = 0;
String payload ;
payload += t;
//payload += ":";
//payload += h;
String payloadh ;
payloadh += h;

if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payload);

if (client.publish(topic_t, (char*) payload.c_str()))
{
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}

//peter
if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payloadh);

if (client.publish(topic_h, (char*) payloadh.c_str()))
{
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}}
//peter


}
else {
if (client.connect((char*) clientName.c_str())) {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic_t);
Serial.println(topic_h);

}
}
// delay (20000); //WORKS FINE
// delay(5*60*1000); //STOPS AFTER FIRST READING AT STARTUP
//delay(fiveMinutes); // STOPS AFTER FIRST READING AT STARTUP
// ADDED CODE HERE
MinuteDelay(5)        // delay for 5 minutes
// END ADDED CODE
}

User avatar
By crami
#37556 Be careful using delay() !
delay() is an unsigned long integer and the arduino processing languages compiler does not check if you are using your variables in the right manner.

so delay (1000) works fine,
delay(25*4*10) will make your computer hang, because you are multiplying 3 integers which gives a negative long integer !
delay(25L*4L*10L) will probably work.

See http://forum.arduino.cc/index.php?topic=46540.0

cr