Kanał - ATNEL tech-forum
Wszystkie działy
Najnowsze wątki

KURS HOME ASSISTANT

Chcesz zautomatyzować swój dom bez skomplikowanego kodowania?
Zastanawiasz się nad wyborem sprzętu, oprogramowania i aplikacji?
Od czego zacząć przygodę z HA? Co będzie najlepsze na start?

Nasz kurs Home Assistant nauczy Cię krok po kroku, jak łatwo zautomatyzować swój dom i oszczędzić na rachunkach za prąd i ogrzewanie. Bez chmur, bez zbędnych abonamentów. Twoja przygoda z Home Assistant zaczyna się tutaj!

↓↓↓

    Szanujemy Twoją prywatność. Możesz wypisać się w dowolnym momencie.




    Teraz jest 13 lip 2025, o 19:29


    Strefa czasowa: UTC + 1





    Utwórz nowy wątek Odpowiedz w wątku  [ Posty: 1 ] 
    Autor Wiadomość
    PostNapisane: 25 maja 2016, o 21:32 
    Offline
    Nowy

    Dołączył(a): 06 mar 2016
    Posty: 19
    Pomógł: 0

    Mam problem z poniższym kodem na Arduino Uno/Leonadrdo/czy też module ESP8266. Kod napisałem w oparciu o oficjalny przykład jaki znalazłem na stronie:

    http://playground.arduino.cc/Code/DatabaseLibrary


    Przebieg programu jest następujący:

    Pierwsze uruchomienie kodu wywołuje funkcję selectAll() . W ramach tej funkcji są dwie instrukcje if , else. W przypadku warunku IF sprawdzam czy w pamięci EEPROM
    istnieje już jakaś baza. W tym przebiegu programu baza jest pusta ponieważ nigdy wcześniej nie były zapisane do pamięci EEPROM - z tego powodu warunek instrukcji if będzie niepoprawny i wykona się część instrukcji else, w której osadzona jest funkcja insertdb();. Funkcja ta zapisujące dane do bazy w pamięci EEPROM i tak się właśnie dzieje.

    W dalszym przebiegu wywołuje się funkcja selectAll2() która pokazuje co jest w bazie.

    Kolejne uruchomienie po uprzednim odłączeniu prądu od Arduino powinno pokazać mi już jednak jakieś dane które wcześniej zapisałem do pamięci EEPROM. Niestety, ale się tak nie dzieje warunek instrukcji if nie jest spełniony i cały czas wykonuje instrukcja else gdzie znajduje się funkcja insertdb();

    Z jakiegoś powodu dane nie utrwalają się w pamięci, albo mam nieprawidłowy warunek. Proszę więc kolegów o pomoc.

    Kod:
    //
    // EEPROM DB example
    //
    // This example creates a database, adds 10 records, and reads the 10 records
    // displaying the results. 
    //
    #include <Arduino.h>
    #include <EEPROM.h>
    #include <DB.h>
    #include <string.h>

    DB db;

    #define MY_TBL 1

    struct MyRec {
      char date[11];
      char time[9];
      int temperature;
    } myrec;


    void insertdb (){

     Serial.println("EEPROM DB Library Demo");
      Serial.println();

      randomSeed(analogRead(0));

      Serial.print("Creating Table...");
      db.create(MY_TBL,sizeof(myrec));
      db.open(MY_TBL);
      Serial.println("DONE");

      Serial.print("Creating records...");
      for (int recno = 1; recno <= 5; recno++)
      {
        int m = random(1, 12);
        int d = random(1, 31);
        int h = random(1, 12);
        int i = random(59);
        int s = random(59);

        sprintf(myrec.date, "2009-%02d-%02d", m, d);
        sprintf(myrec.time, "%02d:%02d:%02d", h, i, s);
        myrec.temperature = random(-100, 100);
        Serial.print("CREATING RECNUM: "); Serial.println(recno);
        Serial.print("WRITING Date: "); Serial.println(myrec.date);
        Serial.print("WRITING Time: "); Serial.println(myrec.time);
        Serial.print("WRITING Temperature: "); Serial.println(myrec.temperature);
        db.append(DB_REC myrec);
        Serial.println("DONE");
      }
      Serial.println();
      Serial.println("Reading records from EEPROM...");
      Serial.println();
    }


    void setup()
    {
      Serial.begin(9600);

    }

    void loop()
    {

       delay(5000);
       selectAll();
       delay(10000);
       selectAll2(); 
    }

    void selectAll2(){

    if (db.nRecs()) {
     
          Serial.println("kolejne sprawdzenie");
          for (int i = 1; i <= db.nRecs(); i++)
          {
            db.read(i, DB_REC myrec);
            Serial.print("Recnum: "); Serial.println(i);
            Serial.print("Date: "); Serial.println(myrec.date);
            Serial.print("Time: "); Serial.println(myrec.time);
            Serial.print("Temperature: "); Serial.println(myrec.temperature);
            Serial.println("-----"); 
          }
      }
     
    }



    void selectAll()
    {
      if (db.nRecs()) {
          Serial.println("komunikat pierwszego sprawdzenia");
          delay(5000);
          Serial.println("pierwsze sprawdzenie");
          for (int i = 1; i <= db.nRecs(); i++)
          {
            db.read(i, DB_REC myrec);
            Serial.print("Recnum: "); Serial.println(i);
            Serial.print("Date: "); Serial.println(myrec.date);
            Serial.print("Time: "); Serial.println(myrec.time);
            Serial.print("Temperature: "); Serial.println(myrec.temperature);
            Serial.println("-----"); 
          }
      }
      else {
        insertdb();
      }

     
    }




    Dodaje też kod biblioteki DB.cpp:

    Kod:
    // DB.cpp
    /*
      Database library for Arduino
      Written by Madhusudana das
     
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
      License as published by the Free Software Foundation; either
      version 2.1 of the License, or (at your option) any later version.

      This library is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public
      License along with this library; if not, write to the Free Software
      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */

    #include "Arduino.h"
    #include "DB.h"

    /**************************************************/
    // private functions
    int DB::writeHead()
    {
        byte * p = (byte*)(void*)&DB_head;
       int ee = DB_head_ptr;
        int i;
        for (i = 0; i < (int)sizeof(DB_head); i++)
          EEPROM.write(ee++, *p++);
        return i;
    }

    int DB::readHead()
    {
        byte* p = (byte*)(void*)&DB_head;
       int ee = DB_head_ptr;
        int i;
        for (i = 0; i < (int)sizeof(DB_head); i++)
          *p++ = EEPROM.read(ee++);
        return i;
    }

    int DB::EEPROM_dbWrite(int ee, const byte* p)
    {
        int i;
        for (i = 0; i < DB_head.rec_size; i++)
          EEPROM.write(ee++, *p++);
        return i;
    }

    int DB::EEPROM_dbRead(int ee, byte* p)

        int i;
        for (i = 0; i < DB_head.rec_size; i++)
          *p++ = EEPROM.read(ee++);
        return i;
    }

    /**************************************************/
    // public functions

    void DB::create(int head_ptr, byte recsize)
    {
      DB_head_ptr = head_ptr;
      DB_head.n_recs   = 0;
      DB_head.rec_size = recsize;
      writeHead();
    }

    void DB::open(int head_ptr)
    {
      DB_head_ptr = head_ptr;
      DB_tbl_ptr  = head_ptr + DB_HEAD_SIZE;
      readHead();
    }
    //other operations commit DB_head edits to EEPROM so no need for a DB_close


    boolean DB::write(byte recno, const DB_Rec rec)
    {
      DB_error = DB_OK;
      if (recno>0 && recno<=DB_head.n_recs+1)
        EEPROM_dbWrite(DB_tbl_ptr+((recno-1)*DB_head.rec_size), rec);
      else
        DB_error = DB_RECNO_OUT_OF_RANGE;
      return DB_error==DB_OK;
    }


    boolean DB::read(byte recno, DB_Rec rec)
    {
      DB_error = DB_OK;
      if (recno>0 && recno<=DB_head.n_recs)
        EEPROM_dbRead(DB_tbl_ptr+((recno-1)*DB_head.rec_size), rec);
      else
        DB_error = DB_RECNO_OUT_OF_RANGE;
      return DB_error==DB_OK;
    }


    boolean DB::deleteRec(byte recno)
    {
      DB_error = DB_OK;
      if (recno<0 || recno>DB_head.n_recs)
      {  Serial.println("recno out of range");
        DB_error = DB_RECNO_OUT_OF_RANGE;
        return false;
      }
      DB_Rec rec = (byte*)malloc(DB_head.rec_size);
      for (int i=recno+1; i<=DB_head.n_recs; i++)
      {
        read(i,rec);
        write(i-1,rec);
      } 
      free(rec);
      DB_head.n_recs--;
      EEPROM.write(DB_head_ptr,DB_head.n_recs);
      return true;
    }


    boolean DB::insert(byte recno, DB_Rec rec)
    {
      DB_error = DB_OK;
      if (recno<0 || recno>DB_head.n_recs)
      {  Serial.println("recno out of range");
        DB_error = DB_RECNO_OUT_OF_RANGE;
        return false;
      }
      DB_Rec buf = (byte*)malloc(DB_head.rec_size);
      for (int i=DB_head.n_recs; i>=recno; i--)
      {
        read(i,buf);
        write(i+1,buf);
      }
      free(buf);
      write(recno,rec); 
      DB_head.n_recs++;
      EEPROM.write(DB_head_ptr,DB_head.n_recs);
      return true;
    }

    void DB::append(DB_Rec rec)
    {
      DB_error = DB_OK;
      DB_head.n_recs++;
      write(DB_head.n_recs,rec);
      EEPROM.write(DB_head_ptr,DB_head.n_recs);
    }

    byte DB::nRecs()
    {
      return DB_head.n_recs;
    }


    oraz plik DB.h:

    Kod:
    /*
      DB.h
      Database library for Arduino
      Written by Madhusudana das
     
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
      License as published by the Free Software Foundation; either
      version 2.1 of the License, or (at your option) any later version.

      This library is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public
      License along with this library; if not, write to the Free Software
      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */

    #ifndef DB_PROM
    #define DB_PROM

    #include "EEPROM.h"

    struct DB_Header
    {
      byte n_recs;
      byte rec_size;
    };

    // slightly padded for the time being
    #define DB_HEAD_SIZE 4

    // DB_error values
    #define DB_OK 0
    #define DB_RECNO_OUT_OF_RANGE 1

    #define DB_REC (byte*)(void*)&

    typedef byte* DB_Rec;

    class DB {
      public:
        void    create(int head_ptr, byte recsize);
        void    open(int head_ptr);
        boolean write(byte recno, const DB_Rec rec);
        boolean read(byte recno, DB_Rec rec);
        boolean deleteRec(byte recno);                   // delete is a reserved word
        boolean insert(byte recno, const DB_Rec rec);
        void    append(DB_Rec rec);
       byte   nRecs();
        DB_Header DB_head;
        byte DB_error;
      private:
        int writeHead();
        int readHead();
        int EEPROM_dbWrite(int ee, const byte* p);
        int EEPROM_dbRead(int ee, byte* p);
        int DB_head_ptr;
        int DB_tbl_ptr;
    };

    extern DB db;

    #endif



    Góra
     Zobacz profil  
    cytowanie selektywne  Cytuj  
    Wyświetl posty nie starsze niż:  Sortuj wg  
    Utwórz nowy wątek Odpowiedz w wątku  [ Posty: 1 ] 

    Strefa czasowa: UTC + 1


    Kto przegląda forum

    Użytkownicy przeglądający ten dział: Brak zidentyfikowanych użytkowników i 2 gości


    Nie możesz rozpoczynać nowych wątków
    Nie możesz odpowiadać w wątkach
    Nie możesz edytować swoich postów
    Nie możesz usuwać swoich postów
    Nie możesz dodawać załączników

    Szukaj:
    Skocz do:  
    Sitemap
    Technologię dostarcza phpBB® Forum Software © phpBB Group phpBB3.PL
    phpBB SEO