Solved: Question about libraries

To the Spark Community,

first of all I want to say “Hello”.
Therefore: Hello to the community.

So, back to business :slight_smile: I´ll try to keep this as short as possible…:). If i say something completely wrong trust me that is not my intention. I try to learn something new, thus it is possible that I make mistakes! I need some help. Help that I can read on and do some “further investigations” on how it is possible to port my current Android Nano v3 project to my ordert Spark Core.

Short project describtion:
The current project is mainly used as a, aehm, twilight control?! Hope you know what I mean. The Arduino Nano v3 checks the time of a day and if the time is between X and Y and the photo resistor goes under my predefined value a relay is switched on for a given time. Date and time and the photoresistor value is send to a 1602lcd display. Mainly that´s it…
To get this work with Arduino I use 4 libraries. After I have read in the forum I come to the conclusion that I don´t need all of them with the Core again.

Here’s what I’ve found and hopefully understand correctly:

RTClib.h / Wire.h
Both of them are used for a DS1307 RTC module. I don´t think I need them again when I use the Core. Because I can use NTP, right?! I need something like the unixtime to do some timing functions analog to the well known millis() function.

Like int functionunixtime if functionunixtime >= currentunixtime do something

Do I get it? Am I right or wrong?
In advanced - thx for any help on this topic.
Tried to used my best translation skills (and they are not very skilled) for this.
Have mercy on me cheers clyde

I guess you have self-figured out the cool stuff the community has put together which allows you to build your project fast :smiley:

As for pt.h , i will take a look and see if i can port it. If you don’t hear from me in a few hours, just @ me. I’m busy doing more projects and might slip my mind :wink:

EDIT: I quickly tried copying a few files and they didn’t give me any headache. Have to be on the road now! More updates tonight!

1 Like

Hi @clyde

The real-time clock part is pretty easy–the core firmware has features being added right now to get the real time from the cloud connection, so that is coming very soon.

If you want to get started right now, you can use my NTP library described here:

It can do a few things the core firmware doesn’t do yet, like handle daylight-saving or summer time in the US and Europe.

1 Like

Hi kennethlimcp;
thank you for the time to take a look in the protothread lib.

Take your time, my Core is currently not shipped iirc, only ordered :smile:
Therefore no need to hurry, have a safe trip.

cheers clyde

Hi bko,

thx for pointing this out. I think I got it and same as I said to kennethlimcp; currently waiting for my Core ;). But noticed and also thx a lot!

@clyde,

Good news! So like what the PT library author has claimed, it is non-platform specific and you only need to copy 2 file: PT.h and lc.h to get it working.

Maybe the other header files are required for advanced functions but i guess porting is like copying + paste.

So i’m not going to claim any credit for this but if you really would like to see a demo or want it stored for the :spark: core, i can save this library under the SparkCoreLibraries in github for easy reference.

What do you say :wink:

1 Like

keenethlimcp:
Woohooo thank you so much! Really great work and a really great community.

After writing another question to you I give the WebIDE another shot and tadaaa I found the “+” button in the upper left corner. Now I understand what you mean with “copy + paste”! I think it´s enough if you can save it to the SparkCoreLibs.

That helps me a lot, could be that this is not my last question.
Now fingers crossed and waiting for my spark

1 Like

Got my Spark Core and do some tests, but have problems with the protothreads…
Here my test code:

    #include "pt-sem.h"
    #include "lc-switch.h"
    #include "lc.h"
    #include "pt.h"
    static long threadTest = 5000;          // Run every X sec
    static int th = 0;       
    static struct pt pt1;
    
void setup() { PT_INIT( &pt1 ); }

void loop() { thread1( &pt1, th, threadTest ); }    
 
void Gatekeeper() {
Serial.println( "I´am the Gatekeeper are you the Keymaster?" );
}

// Keymaster
      static int thread1( struct pt *pt, int th, long timeout ) {
        static long t1 = 0;
        while(1) {
          PT_WAIT_UNTIL( pt, (millis() - t1) > timeout );
          callGatekeeper();  // There is no Dana, only Zuuul
          t1 = millis();
        }
        PT_END( pt );
      }

But sadly returns a lot of errors? Anyone else who use protothreads? Or do I miss something. I used exact the same code with my arduino nano ATMEL328 and it works like a charm.
My debugging skills are not very impressive; therefore I have to call for help :slight_smile:

@clyde, I compile your example and there seems to be a problem with the macro expansion causing syntax errors. @zachary may know how to get the compiler to produce a macro-expanded version of the code so we can get an idea of where the error occurs.

1 Like

@peekay123:
Once again, thanks for your help. If I can provide you with more details let me know, I will do my best!

Beside that all astonishing help/support!!!

1 Like

Any news on this topic?
Sorry for bring this up, but I need that library.

@peekay123:
If I understand what you say, this is a problem of the cloud IDE - therefore there is a chance when I use a offline compilation program?
Should I try to contact zachary via PM? Think he had a bunch of work to do and I don´t want to disturb him.

i had a go at compiling it locally and got it to work with some changes to your demo code - that code doesn’t even compile for arduino without changes, you’ve missed PT_BEGIN(pt); for a start and callGatekeeper() should be replaced with Gatekeeper()

anyway, this works for me, you need lc.h, lc-switch.h, pt.h and pt-sem.h, as they include each other.

#include "application.h"
#include "pt.h"

static long threadTest = 5000;
static int th = 0;
static struct pt pt1;

void Gatekeeper()
{
    Serial.println("I am the Gatekeeper are you the Keymaster?");
}

static int thread1(struct pt *pt, int th, long timeout)
{
    static long t1 = 0;
    PT_BEGIN(pt);

    while(1)
    {
        PT_WAIT_UNTIL(pt, (millis() - t1) > timeout);
        Gatekeeper();
        t1 = millis();
    }
    
    PT_END(pt);
}


void setup()
{
    PT_INIT(&pt1);
}

void loop()
{
    thread1(&pt1, th, threadTest);
}    
1 Like

sej7278:
Oh dear,…
Shame over me, I copied this from my arduino script… and do not doublechecked it… ;(
I´ve I use your code (the PT_BEGIN) and it compiles in the Spark Cloud IDE!
Wohooo, when I´am home today I will try if it works. But I see no problem now.

Thank you all for your support,… and sorry again taking your time for a mistake I made…:frowning:

1 Like

Tested with my core and some time output :smiley:
Thank you so much :spark:Community! You rock!

// Libs
#include "pt-sem.h"
#include "lc-switch.h"
#include "lc.h"
#include "pt.h"


// Thread Management
static long sleepThread1 = 24 * 60 * 60 * 1000;       // Run every day
static long sleepThread2 = 5 * 1000;                  // Run every 5secs
static int th = 0;                                    // Needed for thread management
static struct pt pt1, pt2;

// Counter Test
long CounterStart;
long CounterEnd;

// Various Variables
int DEBUG  =  1;                        // You wanna debug info set to 1
int myZone = +2;                        // Daylight-saving UTC

/********************************************************************************
                        Setup Part
*********************************************************************************/
void setup() {
  
  Serial.begin(9600);       // Create a serial connection
  PT_INIT( &pt1 );          // Initiate thread time
  PT_INIT( &pt2 );          // Initiate thread timeZone
  Time.zone( myZone );      // Set timezone (run at start)
}
/********************************************************************************
                        Main Loop
*********************************************************************************/
void loop() {
  thread1( &pt1, th, sleepThread1 );
  thread2( &pt2, th, sleepThread2 );
  }


/********************************************************************************
                        Functions
*********************************************************************************/
void funcTimeZone() {
    // Request time synchronization from the Spark Cloud
    Spark.syncTime();         // Request time sync from Spark Cloud
    Time.zone( myZone );      // Set timezone
    if (DEBUG == 1) { Serial.println("DEBUG: Function -timeZone- executed"); }
}


void funcTime() {
  Serial.println("--------------------------------------------------------------------------------");
  // Print UnixTimeStamp
  Serial.print("UnixTime : "); Serial.println(Time.now());
  // Print Date in human readable Format
  Serial.print("HumanDate: "); Serial.print(Time.day()); Serial.print("."); Serial.print(Time.month()); Serial.print("."); Serial.println(Time.year());
  // Print Time in human readable Format
  Serial.print("HumanTime: "); Serial.print(Time.hour()); Serial.print(":"); Serial.print(Time.minute()); Serial.print(":"); Serial.println(Time.second());
  
  CounterStart =  (Time.now());          // Bind to current time
  CounterEnd = CounterStart + 100;       // Add 100s to the time
  Serial.print("CounterStart: "); Serial.println( CounterStart );
  Serial.print("CounterEnd  : "); Serial.println( CounterEnd );
   if (DEBUG == 1) { Serial.println("DEBUG: Function -time- executed"); }
}



/********************************************************************************
                        Threads
*********************************************************************************/
// Thread TimeZone
static int thread1(struct pt *pt, int th, long timeout)
{
    static long t1 = 0;
    PT_BEGIN(pt);

    while(1)
    {
        PT_WAIT_UNTIL(pt, (millis() - t1) > timeout);
        if (DEBUG == 1) { Serial.println("DEBUG: Thread -timeZone- executed"); }
        funcTimeZone();
        t1 = millis();
    }

    PT_END(pt);
}

// Thread Time
static int thread2(struct pt *pt, int th, long timeout)
{
    static long t2 = 0;
    PT_BEGIN(pt);

    while(1)
    {
        PT_WAIT_UNTIL(pt, (millis() - t2) > timeout);
        if (DEBUG == 1) { Serial.println("DEBUG: Thread -time- executed"); }
        funcTime();
        t2 = millis();
    }

    PT_END(pt);
}
1 Like

glad it worked.

out of interest, what did you actually need the threading for though - there’s no button presses or anything expecting input (you could use interrupts for that anyway) and no complex graphical output to maintain the state of…?