Close

NodeMCU – měření teploty a posílání na Exosite

Úvodní stránka Fórum Vaše projekty Arduino NodeMCU – měření teploty a posílání na Exosite

  • Toto téma obsahuje celkem 5 odpovědí. Do diskuze (2 diskutující) se naposledy zapojil uživatel johnyhol a poslední změna proběhla před 7 roky.
Aktuálně je na stránce zobrazeno 6 příspěvků - 1. až 6. (celkem z 6)
  • Autor
    Příspěvky
  • #10386
    johnyhol
    Účastník

    Ahoj všem, potřeboval bych poradit jestli už někdo neřešil kombinaci NodeMCU v3 + několik čidel DS18B20 a posílání naměřených hodnot na Exosite. Původně jsem chtěl měření teploty přilepit k mýmu jinýmu projektu: „řídící jednotka pro peletkový kotel“,ale nakonec jsem se rozhodl, že to radši budu řešit odděleně (abych „nerušil“ topící program měřením teploty) a k tomu jsem si vybral právě NodeMCU, protože je levné a pro můj účel naprosto dostačující. Chtěl jsem to otestovat příkladem z Arduina IDE: Exosite-> ESP8266OccupancySensor, ale tady mi pořád vyskakuje chyba kompilace:

    
    Arduino: 1.8.1 (Windows 10), Vývojová deska: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"
    Volby pro sestavení se změnily; sestavuji vše znovu 
    In file included from C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:29:0:
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.h:27:7: error: redefinition of 'class WiFiUDP'
     class WiFiUDP : public UDP {
           ^
    In file included from C:\Program Files (x86)\Arduino\libraries\WiFi\src\utility/wifi_drv.h:26:0,
                     from C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:26:
    C:\Users\janho\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiUdp.h:32:7: error: previous definition of 'class WiFiUDP'
     class WiFiUDP : public UDP, public SList<WiFiUDP> {
           ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In constructor 'WiFiUDP::WiFiUDP()':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:35:22: error: class 'WiFiUDP' does not have any field named '_sock'
     WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL) {}
                          ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual uint8_t WiFiUDP::begin(uint16_t)':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:45:9: error: '_sock' was not declared in this scope
             _sock = sock;
             ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:46:9: error: '_port' was not declared in this scope
             _port = port;
             ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::available()':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:56:7: error: '_sock' was not declared in this scope
       if (_sock != NO_SOCKET_AVAIL)
           ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual void WiFiUDP::stop()':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:66:8: error: '_sock' was not declared in this scope
        if (_sock == NO_SOCKET_AVAIL)
            ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:69:26: error: '_sock' was not declared in this scope
        ServerDrv::stopClient(_sock);
                              ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::beginPacket(IPAddress, uint16_t)':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:88:7: error: '_sock' was not declared in this scope
       if (_sock == NO_SOCKET_AVAIL)
           ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:90:7: error: '_sock' was not declared in this scope
       if (_sock != NO_SOCKET_AVAIL)
           ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::endPacket()':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:101:32: error: '_sock' was not declared in this scope
      return ServerDrv::sendUdpData(_sock);
                                    ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual size_t WiFiUDP::write(const uint8_t*, size_t)':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:111:27: error: '_sock' was not declared in this scope
      ServerDrv::insertDataBuf(_sock, buffer, size);
                               ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::read()':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:125:23: error: '_sock' was not declared in this scope
        ServerDrv::getData(_sock, &b);
                           ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::read(unsigned char*, size_t)':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:137:31: error: '_sock' was not declared in this scope
        if (!ServerDrv::getDataBuf(_sock, buffer, &size))
                                   ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::peek()':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:152:22: error: '_sock' was not declared in this scope
       ServerDrv::getData(_sock, &b, 1);
                          ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual IPAddress WiFiUDP::remoteIP()':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:166:25: error: '_sock' was not declared in this scope
      WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
                             ^
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual uint16_t WiFiUDP::remotePort()':
    C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:176:25: error: '_sock' was not declared in this scope
      WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
                             ^
    exit status 1
    Nastala chyba při kompilaci u desky NodeMCU 1.0 (ESP-12E Module).
    Táto zpráva by měla mít víc informacií v
    "Zobrazení podrobného výstupu při kompilaci"
    podle zapnuté volby v Soubor -> Nastavení.
    

    Vypadá to, že je chyba v knihovně, zkoušel jsem to i v práci kde mám myslím Arduino 1.67, ale tam to hází stejnou chybu. Může to někdo vyzkoušet u sebe, případně poradit co s tím? Jenom dodám, že jsem ještě zkoušel nahrát pro kontrolu kód pro měření teploty a posílání na ThingSpeak a to funguje bez problémů.

    Díky za reakce!

    #10388
    Zbyšek Voda
    Správce

    Podle té chyby to opravdu vypadá, že bude chyba v knihovně. Někde tam asi dochází k tomu, že se nějaký soubor vloží dvakrát – odsud také chyba „redefinition of classs…“.

    Ještě pro jistotu prosím pošlete použitý kód, ale tím to asi nebude.

    #10389
    johnyhol
    Účastník

    použil jsem tento kód přímo z IDE (příklady -> Exosite -> ESP8266OccupancySensor):

    
    //*****************************************************************************
    //
    // ESP8266OccupancySensorNoSleepProvision - An example of how to use the Exosite 
    //                                          library on the Sparkfun ESP8266 Thing 
    //                                          with a PIR motion sensor to detect the 
    //                                          occupancy status of a room.
    //
    // Requires the ESP8266 Arduino Library
    //
    // You MUST use the staging version of the ESP8266 library (or the stable version
    // if newer than Aug 10, 2015) so that method strtoul is compatible
    //
    // Copyright (c) 2015 Exosite LLC.  All rights reserved.
    // 
    // Redistribution and use in source and binary forms, with or without 
    // modification, are permitted provided that the following conditions are met:
    
    //  * Redistributions of source code must retain the above copyright notice,
    //    this list of conditions and the following disclaimer.
    //  * Redistributions in binary form must reproduce the above copyright 
    //    notice, this list of conditions and the following disclaimer in the
    //    documentation and/or other materials provided with the distribution.
    //  * Neither the name of Exosite LLC nor the names of its contributors may
    //    be used to endorse or promote products derived from this software 
    //    without specific prior written permission.
    // 
    // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
    // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
    // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
    // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
    // DAMAGES, FOR ANY REASON WHATSOEVER.
    //
    //*****************************************************************************
    
    #include <EEPROM.h>
    #include <SPI.h>
    #include <ESP8266WiFi.h>
    #include <Exosite.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    const int MOTION_PIN = 13;
    const int MOTION_POWER_PIN = 4;
    const int ONEWIRE = 12;
    const int LED_PIN = 5;
    
    /*==============================================================================
    * Configuration Variables
    *
    * Change these variables to your own settings.
    *=============================================================================*/
    const char ssid[]     = "SSID HERE";
    const char password[] = "PASSWORD HERE";
    char macString[18];  // Used to store a formatted version of the MAC Address
    byte macData[WL_MAC_ADDR_LENGTH];
    
    const int REPORT_TIMEOUT = 30000; //milliseconds period for reporting to Exosite.com
    
    // Use these variables to customize what datasources are read and written to.
    String readString = "occupancyint";
    String writeString = "occupancyint=";
    String returnString;
    String writeString5 = "temp=";
    String readString5 = "temp";
    
    /*==============================================================================
    * End of Configuration Variables
    *=============================================================================*/
    WiFiClient client;
    Exosite exosite(&client);
    // Set up which Arduino pin will be used for the 1-wire interface to the sensor
    // Be sure to use OneWire library from Arduino Library Manager, not from the included
    // library in the ESP8266 Board package
    OneWire oneWire(ONEWIRE);
    DallasTemperature DS18B20(&oneWire);
    int motionState = 0;
    int movementCounter; // Use to count number of times movement is detected in the intervals
    float tempF, tempC;
    unsigned long checkPrevTime = 0;
    
    /*==============================================================================
    * setup
    *
    * Arduino setup function.
    *=============================================================================*/
    void setup(){  
      initHardware();
      delay(100);
      connectWifi();
      movementCounter = 0;
    
      exosite.begin();
    
      ESPprovision();
    
      Serial.println("Calibrating...");
      delay(30000); // Calibration time
      Serial.println("Ready to detect motion.");
      delay(50);
    }
    
    /*==============================================================================
    * loop 
    *
    * Arduino loop function.
    *=============================================================================*/
    void loop()
    {
      checkPrevTime = millis();
      while(millis() - checkPrevTime < REPORT_TIMEOUT){
        // Read the state of the motion sensor value
        motionState = digitalRead(MOTION_PIN);
        // Check if the motion sensor is activated
        // If it is, the motionState is LOW (with 10K resistor)
        if (motionState == LOW) {
          movementCounter++;
          Serial.println("Motion Detected.");
          delay(2000);
        }
        else delay(100);
      }
    
      // Read temperature sensor
      DS18B20.requestTemperatures(); // Send the command to get temperatures
      tempC = DS18B20.getTempCByIndex(0);
      Serial.println("Temperature in degrees C is " + String(tempC));
      tempF = DallasTemperature::toFahrenheit(tempC);
      Serial.println("Temperature in degrees F is " + String(tempF));
      
      delay(100);
      reportToExosite();
    }
    
    /*==============================================================================
    * initHardware 
    *
    * Use in setup to initialize pins and Serial port 
    *=============================================================================*/
    void initHardware(){
      Serial.begin(112500);
      delay(10);
      pinMode(MOTION_PIN, INPUT);
      pinMode(MOTION_POWER_PIN, OUTPUT);
      digitalWrite(MOTION_POWER_PIN, HIGH);
      DS18B20.begin();
      EEPROM.begin(40);
      pinMode(LED_PIN, OUTPUT);
      digitalWrite(LED_PIN, LOW);
      delay(1000);
    }
    
    /*==============================================================================
    * connectWifi 
    *
    * Use in setup to connect to local Wifi network
    *=============================================================================*/
    void connectWifi(){
      WiFi.mode(WIFI_STA); // Need for deepSleep
      // Connect to WiFi network
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      
      WiFi.begin(ssid, password);
    
      byte ledStatus = LOW;
      
      while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        // Blink the LED
        digitalWrite(LED_PIN, ledStatus); // Write LED high/low
        ledStatus = (ledStatus == HIGH) ? LOW : HIGH;
        delay(100);
      }
      digitalWrite(LED_PIN, HIGH);
    
      Serial.println("");
      Serial.println("WiFi connected");  
      Serial.print("IP Address: ");
      Serial.println(WiFi.localIP());
    
      delay(100);
      
      // Create MAC Address String in the format FF:FF:FF:FF:FF:FF
      WiFi.macAddress(macData);
      snprintf(macString, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
               macData[5], macData[4], macData[3], macData[2], macData[1], macData[0]);
      // Print Some Useful Info
      Serial.print(F("MAC Address: "));
      Serial.println(macString);
    }
    
    /*==============================================================================
    * ESPprovision
    *
    * Activate the ESP8266 to specified vendor and model and get a CIK
    *=============================================================================*/
    void ESPprovision(){
      byte ledStatus = HIGH;
      while(client.connected()==0){
        if(exosite.provision("exosite", "arduinoyun-generic", macString)) EEPROM.commit();
        digitalWrite(LED_PIN, ledStatus); // Write LED high/low
        ledStatus = (ledStatus == HIGH) ? LOW : HIGH;
        delay(500);
      }
      digitalWrite(LED_PIN, HIGH);
      delay(100);
    }
    
    /*==============================================================================
    * reportToExosite 
    *
    * Write number of motions detected in period as well as awake status to dataports
    *=============================================================================*/
    void reportToExosite(){
      while(!exosite.writeRead(writeString+String(movementCounter)+"&"+writeString5+String(tempF), readString + "&" + readString5, returnString)) {
        Serial.println("Retrying connection");
        delay(1000);
      }
    
      movementCounter = 0;
      delay(1000);
    }
    
    #10391
    johnyhol
    Účastník

    Ale co nechápu, že to s tou stejnou knihovnou maká v tomto kódu(posílání na Thingspeak):

    
    #include <OneWire.h>
    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include <DallasTemperature.h>
    
    #define ONE_WIRE_BUS D7
    
    const char* host = "api.thingspeak.com"; // Your domain  
    String ApiKey = "----";
    String path = "/update?key=" + ApiKey + "&field1=";  
    
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature DS18B20(&oneWire);
    
    const char* ssid = "----";
    const char* pass = "----";
    
    char temperatureString[6];
    
    void setup(void){
      Serial.begin(9600);
      Serial.println("");
      
      WiFi.begin(ssid, pass);
      // Wait for connection
      while (WiFi.status() != WL_CONNECTED) {
        delay(100);
        Serial.print(".");
      }
      
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    
      DS18B20.begin();
       
    
    }
    
    float getTemperature() {
      float temp;
      do {
        DS18B20.requestTemperatures(); 
        temp = DS18B20.getTempCByIndex(0);
        delay(100);
      } while (temp == 85.0 || temp == (-127.0));
      return temp;
    }
    
    void loop() {
    
      float temperature = getTemperature();
    
      dtostrf(temperature, 2, 2, temperatureString);
      // send temperature to the serial console
      Serial.println(temperatureString);
    
      WiFiClient client;
      const int httpPort = 80;
      if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
      }
    
      client.print(String("GET ") + path + temperatureString + " HTTP/1.1\r\n" +
                   "Host: " + host + "\r\n" + 
                   "Connection: keep-alive\r\n\r\n");
      delay(500);
    
    }
    
    #10401
    Zbyšek Voda
    Správce

    Najděte si složku, kde máte staženou knihovnu Exosite a v ní si otevřete soubor Exosite.h v nějakém textovém editoru. Hned na začátku programu najděte řádek #include <WiFi.h> a zakomentujte ho.

    Nejsem si jistý, jestli takto bude program fungovat, ale kód se zkompiluje bez chybových hlášek. Zkuste to a uvidíme 🙂

    #10967
    johnyhol
    Účastník

    Ahoj. Tak můžu (sice s odstupem času) potvrdit, že byl problém opravdu v knihovně. Po zakomentování toho řádku jak radíte to už funguje. Díky moc!

    Narazil jsem ale ještě na jeden problém, nebo spíš bych potřeboval poradit jak udělat zpožděné vypnutí nahřívání bojleru u dalšího zařízení s ESP8266 (tentokrát se jedná o SonOff). Kód pro zapnutí a vypnutí mám hotový (to funguje bez problémů), ale nyní bych ho potřeboval doplnit o to, aby po zapnutí přes Exosite běželo nahřívání řekněme 2h a pak, aby se nahřívání vypnulo a vše se vrátilo do původního stavu a bylo to připravené pro další zapnutí. Zkouším to tu už asi tři večery a pořád bez úspěchu. Vždy to skončí tak, že se to sice po nastavené době vypne (vypne relé a vrátí se stav přepínače na Exosite do polohy vypnuto), ale po dalším zapnutí se jenom na chvíli přepne přepínač na Exosite do polohy zapnuto a po pár vteřinách se opět přepne do polohy vypnuto.
    Tady je kód se zakomentovaným pokusem o zpoždění:

    
    #include <ESP8266WiFi.h>
    #include <Exosite.h>
    
    String cikData = "4b0024b7d0981b76ca9ac9ed61de0de2cf87b704";  // <-- FILL IN YOUR CIK HERE! (https://portals.exosite.com -> Add Device)
    
    const char* ssid = "Holarci";
    const char* pass = "H0larc1*2S";
    
    WiFiClient client;
    Exosite exosite(cikData, &client);
    
    String readString;
    String writeString;
    String returnString;
    String readParam ;
    String writeParam ;
    char buffer[7];
    
    int smycka4 = 0; // pomocna smycka - zapaleni
    int smycka4max = 50; // max.pomocne smycky
    
    byte TstavA;//ALIAS na exosite
    byte TvipA;//ALIAS na exosite
    byte ReleA=12;
    byte LED=13;
    
    void setup(void){
      Serial.begin(9600);
      Serial.println("");
      
      WiFi.begin(ssid, pass);
      
      while (WiFi.status() != WL_CONNECTED) {
        delay(100);
        Serial.print(".");
      }
      
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
      
      pinMode(ReleA,OUTPUT);
      digitalWrite (ReleA,HIGH); 
    
      pinMode(LED,OUTPUT);
      digitalWrite (LED,HIGH); 
      
    } 
     
    void loop() {
    
    //*****************************//
    // cteni stavu vypinace bojler //
    //*****************************//
    Serial.println("Ctu stav vypinace A");  
    readString = "TvipA";
      if(exosite.writeRead(writeString, readString, returnString)){
       Serial.println("OK");
      Serial.print("Stav vypinace A je: ");
    Serial.println(returnString);
      }
    else{
      Serial.println("Error");
      }
    if (returnString == "TvipA=1"){
    TvipA=1;
      }
    else{
    TvipA=0;
      }
    returnString ="";   
     delay(1000);
    
    //*************************//
    // logika zapinani bojleru //
    //*************************//
    if(TvipA==1){
    digitalWrite(ReleA, LOW);
    digitalWrite(LED, LOW);
     Serial.println("Nahrivani bojleru ZAPNUTO");
    TstavA=1;
    ////////////////////////
    // casovac vypnuti -> //
    ////////////////////////
    /*     for (smycka4 = 0;  smycka4 < smycka4max; smycka4 ++){
        delay(1000); 
      Serial.print("Stav: ");
      Serial.print(smycka4);
      Serial.print("s/");
      Serial.print(smycka4max);
      Serial.println("s ");
      }
    writeString = "TvipA=0";*/
    ////////////////////////
    // <- casovac vypnuti //
    ////////////////////////
      }
    else if (TvipA==0){
    digitalWrite(ReleA, HIGH);
    digitalWrite(LED, HIGH);
     Serial.println("Nahrivani bojleru VYPNUTO");
    TstavA=0;
      }
    //**************************//
    // odeslani stavu nahrivani //
    //**************************//
    readParam = "";        
    writeParam = "TstavA="; 
    writeParam += TstavA;    
    if(exosite.writeRead(writeParam, readParam, returnString)) {  
      Serial.println("Exosite OK");  
      if (returnString != "") { 
        Serial.println("Stav nahrivani odeslany");
       }
       }
    else {
     Serial.println("Exosite chyba stav neodeslany");
        }
    delay(1000);
    
    }  
      
    

    Díky za reakce.

Aktuálně je na stránce zobrazeno 6 příspěvků - 1. až 6. (celkem z 6)
  • Pro reakci na toto téma se musíte přihlásit.