Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Gadjet
#39498 Hi,
I'm trying this sketch with IDE 1.6.4 but it will not compile, I get no errors and have waited 30 min's but it starts and then stalls at approx. 25%.

I've commented out a few lines to do with the PubSubClient and it compiles until I un-comment the line
Code: Select allPubSubClient client(server);

Then it will not get past 25% .

I've compiled stand alone DHT22 code and ESP8266 code but this get stuck.

any ideas ??
User avatar
By Gadjet
#39556 Hi,
I tried it on another PC and now I get some error messages, I don't get why I'm getting the error messages, they look pretty terminal.

Anyone any suggestions?

Code: Select allArduino: 1.6.5 (Windows 8.1), Board: "SparkFun ESP8266 Thing, 80 MHz, Serial, 115200"

Build options changed, rebuilding all

DHT_to_MQTT:10: error: no matching function for call to 'PubSubClient::PubSubClient(IPAddress&)'
DHT_to_MQTT.ino:10:27: note: candidates are:
In file included from DHT_to_MQTT.ino:2:0:
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:103:4: note: PubSubClient::PubSubClient(const char*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&, Stream&)
    PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:103:4: note:   candidate expects 5 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:102:4: note: PubSubClient::PubSubClient(const char*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&)
    PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:102:4: note:   candidate expects 4 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:101:4: note: PubSubClient::PubSubClient(const char*, uint16_t, Client&, Stream&)
    PubSubClient(const char*, uint16_t, Client& client, Stream&);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:101:4: note:   candidate expects 4 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:100:4: note: PubSubClient::PubSubClient(const char*, uint16_t, Client&)
    PubSubClient(const char*, uint16_t, Client& client);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:100:4: note:   candidate expects 3 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:99:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&, Stream&)
    PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:99:4: note:   candidate expects 5 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:98:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&)
    PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:98:4: note:   candidate expects 4 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:97:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, Client&, Stream&)
    PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:97:4: note:   candidate expects 4 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:96:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, Client&)
    PubSubClient(uint8_t *, uint16_t, Client& client);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:96:4: note:   candidate expects 3 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:95:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&, Stream&)
    PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:95:4: note:   candidate expects 5 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:94:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&)
    PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:94:4: note:   candidate expects 4 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:93:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&, Stream&)
    PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:93:4: note:   candidate expects 4 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:92:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&)
    PubSubClient(IPAddress, uint16_t, Client& client);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:92:4: note:   candidate expects 3 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:91:4: note: PubSubClient::PubSubClient(Client&)
    PubSubClient(Client& client);
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:91:4: note:   no known conversion for argument 1 from 'IPAddress' to 'Client&'
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:90:4: note: PubSubClient::PubSubClient()
    PubSubClient();
    ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:90:4: note:   candidate expects 0 arguments, 1 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:70:7: note: constexpr PubSubClient::PubSubClient(const PubSubClient&)
 class PubSubClient {
       ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:70:7: note:   no known conversion for argument 1 from 'IPAddress' to 'const PubSubClient&'
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:70:7: note: constexpr PubSubClient::PubSubClient(PubSubClient&&)
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:70:7: note:   no known conversion for argument 1 from 'IPAddress' to 'PubSubClient&&'
DHT_to_MQTT.ino: In function 'void loop()':
DHT_to_MQTT:28: error: no matching function for call to 'PubSubClient::publish(const char [18], String)'
DHT_to_MQTT.ino:28:51: note: candidates are:
In file included from DHT_to_MQTT.ino:2:0:
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:117:12: note: boolean PubSubClient::publish(const char*, const char*)
    boolean publish(const char* topic, const char* payload);
            ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:117:12: note:   no known conversion for argument 2 from 'String' to 'const char*'
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:118:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)
    boolean publish(const char* topic, const char* payload, boolean retained);
            ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:118:12: note:   candidate expects 3 arguments, 2 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:119:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
            ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:119:12: note:   candidate expects 3 arguments, 2 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:120:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
            ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:120:12: note:   candidate expects 4 arguments, 2 provided
DHT_to_MQTT:29: error: no matching function for call to 'PubSubClient::publish(const char [15], String)'
DHT_to_MQTT.ino:29:48: note: candidates are:
In file included from DHT_to_MQTT.ino:2:0:
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:117:12: note: boolean PubSubClient::publish(const char*, const char*)
    boolean publish(const char* topic, const char* payload);
            ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:117:12: note:   no known conversion for argument 2 from 'String' to 'const char*'
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:118:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)
    boolean publish(const char* topic, const char* payload, boolean retained);
            ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:118:12: note:   candidate expects 3 arguments, 2 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:119:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
            ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:119:12: note:   candidate expects 3 arguments, 2 provided
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:120:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
            ^
C:\Users\Phil\Dropbox\Arduino\libraries\arduino_472362\src/PubSubClient.h:120:12: note:   candidate expects 4 arguments, 2 provided
no matching function for call to 'PubSubClient::PubSubClient(IPAddress&)'

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
User avatar
By Marmachine
#64772 Well... thanks for sharing!
I'm no expert either :geek: but working on a similar project with Domoticz.
I use a Wemos D1 r2 with DHT22 and BH1750 (lux) sensors which values i send to Domoticz using GET method.

I had my test setup running pretty stabile, so when i put it all together in a box, placed my sensors outside and guess what... i see it locking :? Has happened already 3 times after about 18 hours of operating and i don't have a good indication of what's going on. I figured that it might have to do something with the Wifi signal, but anyway i am still looking. :?:

Here's my code
Code: Select all#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BH1750.h>

// ========================= LED PIN ========================
#define LED_PIN D5 // Led pin

// ======================= DHT11 / 22 =======================
#define DHTPIN D3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// ==================== lightmeter BH1750 ===================
// BH1750 Light Sensor connection: VCC to 5v+ | GND to GND | SCL to pin SCL/D1 | SDA to pin SDA/D2 | ADD not used
BH1750 lightMeter;

// ========================== Wifi ==========================
const char* ssid      = "ssid";
const char* password  = "password";

// ============= Domoticz host address and port =============
const char* host = "192.168.0.123";     // NOTE: There should not be http:// before the address!!!
const int httpPort = 80;

// =============== Domoticz devices to update ===============
const char* domoticz_temp_idx = "133";
const char* domoticz_hum_idx = "167";
const char* domoticz_lux_idx = "168";

// ==========================================================

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

  // starting DHT sensor
  dht.begin();
  // starting lightMeter BH1750
  lightMeter.begin();
 
  WiFi.begin(ssid, password);
 
  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.print("Device IP: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("=============================================");
 
  Serial.println("");
}

// ==========================================================

void loop(){

  float hum = dht.readHumidity();
  float temp = dht.readTemperature();
  int hum_status = 0;
 
  if (isnan(hum) || isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // === BH1750 ===
 
  uint16_t lux = lightMeter.readLightLevel();
  int luxnum = lux;

  if (isnan(lux)) {
    Serial.println("Failed to read from LUX sensor!");
    return;
  }

  // === TEMPERATURE ===
  Serial.print("Sensor read temperature: ");
  Serial.print(temp);
  Serial.println(" degrees Celsius");
  Serial.print(temp);
  Serial.println(" degrees Celsius");

  // Define a new client
  WiFiClient client;
  unsigned long timeout = millis();
 
  // Get Connected to the Server
  if (!client.connect(host, httpPort)) {
    Serial.println("[connection failed]");
    return;
  }

  // LED turns on when we connect
  digitalWrite(LED_PIN, HIGH);

  /* example url for domoticz temperature device:
  http://192.168.0.123/json.htm?type=command&param=udevice&idx=133&nvalue=0&svalue=24
  */

  // Create a URI for the request
  String url = "/json.htm?type=command&param=udevice&idx=";
  url += domoticz_temp_idx;
  url += "&nvalue=0&svalue=";
  url += temp;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);

  Serial.println();
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

  // set the timeout value
  int TIMEOUT1 = millis() + 5000;

  // Read all the lines of the reply from server and print them to Serial
  while(client.available() == 0){
    // check for timeout
    if (TIMEOUT1 - millis() < 0) {
        Serial.println(">>> CLIENT TIMEOUT!");

        // Before we exit, turn the LED off.
        digitalWrite(LED_PIN, LOW);
       
        client.stop();
        return;
    }
   
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  // Disconnect from the server
  client.stop();

  // Before we exit, turn the LED off.
  digitalWrite(LED_PIN, LOW);

  // ------------------
  Serial.println();

  Serial.println("Waiting 20 secs");
  // 20 seconds pause
  delay(20000);

  Serial.println();
  // ------------------

 
  // === HUMIDITY ===
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.println(" Percent");

  /* -- Mapping for Humidity_status
   * ----------------------------------------------------------------------------------------------------
                -- 0    = Normal
                -- 1    <> 46-70%   = Comfortable
                -- 2    < 46        = Dry
                -- 3    > 70%       = Wet
   * ---------------------------------------------------------------------------------------------------- */

     if (hum > 0){
        if ((hum >= 46) and (hum <= 70)) {
          hum_status = 1; /* comfortable */
        } else if (hum < 46) {
          hum_status = 2; /* dry */
        } else if (hum > 70)
          hum_status = 3; /* wet */
     }

  // Get Connected to the Server
  if (!client.connect(host, httpPort)) {
    Serial.println("[connection failed]");
    return;
  }

  // LED turns on when we connect
  digitalWrite(LED_PIN, HIGH);
 
  /* example url for domoticz humidity device:
  http://192.168.0.123/json.htm?type=command&param=udevice&idx=IDX&nvalue=HUM&svalue=0
  */

  // We now create a URI for the request
  String url_hum = "/json.htm?type=command&param=udevice&idx=";
  url_hum += domoticz_hum_idx;
  url_hum += "&nvalue=";
  url_hum += hum; // Humidity value
  url_hum += "&svalue=";
  url_hum += hum_status; // Humidity_status can be: 0=Normal | 1=Comfortable | 2=Dry | 3=Wet
 
  Serial.print("Requesting URL: ");
  Serial.println(url_hum);

  Serial.println();
 
  // This will send the request to the server
  client.print(String("GET ") + url_hum + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

  // set the timeout value
  int TIMEOUT2 = millis() + 5000;

  // Read all the lines of the reply from server and print them to Serial
  while(client.available() == 0){
    // check for timeout
    if (TIMEOUT2 - millis() < 0) {
        Serial.println(">>> CLIENT TIMEOUT!");

        // Before we exit, turn the LED off.
        digitalWrite(LED_PIN, LOW);
       
        client.stop();
        return;
    }
   
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  // Disconnect from the server
  client.stop();

  // Before we exit, turn the LED off.
  digitalWrite(LED_PIN, LOW);

  // ------------------
  Serial.println();

  Serial.println("Waiting 20 secs");
  // 20 seconds pause
  delay(20000);

  Serial.println();
  // ------------------

 
  // === LUX ===
  Serial.print("Lux: ");
  Serial.println(lux);

  // Get Connected to the Server
  if (!client.connect(host, httpPort)) {
    Serial.println("[connection failed]");
    return;
  }

  // LED turns on when we connect
  digitalWrite(LED_PIN, HIGH);

  /* example url for domoticz humidity device:
  http://192.168.0.123/json.htm?type=command&param=udevice&idx=IDX&svalue=VALUE
  */

  // We now create a URI for the request
  String url_lux = "/json.htm?type=command&param=udevice&idx=";
  url_lux += domoticz_lux_idx;
  url_lux += "&svalue=";
  url_lux += lux;
 
  Serial.print("Requesting URL: ");
  Serial.println(url_lux);

  Serial.println();
 
  // This will send the request to the server
  client.print(String("GET ") + url_lux + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

  // set the timeout value
  int TIMEOUT3 = millis() + 5000;

  // Read all the lines of the reply from server and print them to Serial
  while(client.available() == 0){
    // check for timeout
    if (TIMEOUT3 - millis() < 0) {
        Serial.println(">>> CLIENT TIMEOUT!");

        // Before we exit, turn the LED off.
        digitalWrite(LED_PIN, LOW);
       
        client.stop();
        return;
    }
   
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  // Disconnect from the server
  client.stop();

  // Before we exit, turn the LED off.
  digitalWrite(LED_PIN, LOW);
 
  Serial.println();

  Serial.println("Waiting 20 secs");
  // 20 seconds pause
  delay(20000);
 
  Serial.println();

}