• 10 dec 2017: forum version update. In case of issues use this topic.
  • 30 nov 2017: pilight moved servers. In case of issues use this topic.
Hello There, Guest! Login Register


Arduino Dimmer Protocol
#1
Hello,

I´m new in pilight, but I like it very much.
I would dim a LED-Stripe with an Arduino Uno using PWM.
I tested some Libarys for Arduino an find that the RCSwitch libary and the pollin protocol works as the best. so I modificated the pollin protocol and would put the new arduino_dimmer protocol in /usr/local/lib/pilight/protocols/433.92/arduino_dimmer.c and .h but in my installation was no folder protocols. So I create one and the 433.92 folder to but I can´t move the files in 433.92 because Linux says 433.92 is no directory. I put the protocols direct in the protocols folder and create a new device in the config file with the new protocol. After I would start pilight the errorlog said invalid protocol.

my config
Code:
{
    "devices": {
        "arduino": {
            "protocol": [ "arduino_dimmer" ],
            "id": [{
                "systemcode": 10,
                "unitcode": 1
            }],
            "state": "off",
            "dimlevel": 11
        },
        "arduino2": {
            "protocol": [ "pollin" ],
            "id": [{
                "systemcode": 1,
                "unitcode": 2
            }],
            "state": "off"
        }
    },
    "rules": {},
    "gui": {
        "arduino": {
            "name": "Arduino",
            "group": [ "Pascal´s Zimmer" ],
            "media": [ "all" ],
            "readonly": 0
        },
        "arduino2": {
            "name": "Arduino2",
            "group": [ "Pascal´s Zimmer" ],
            "media": [ "all" ],
            "readonly": 0
        }
    },
    "settings": {
        "log-level": 6,
        "pid-file": "/var/run/pilight.pid",
        "log-file": "/var/log/pilight.log",
        "protocol-root": "/usr/local/lib/pilight/protocols",
        "webserver-enable": 1,
        "webserver-root": "/usr/local/share/pilight/",
        "webserver-http-port": 5006,
        "webserver-cache": 1
    },
    "hardware": {
        "433gpio": {
            "sender": 0,
            "receiver": 1
        }
    },
    "registry": {
        "pilight": {
            "version": {
                "current": "7.0"
            }
        }
    }
}


I tested also the kaku dimmer and switch protocol and the Arduino libary NewRemoteSwitch but the arduino aren´t recieved many dimlevels. It only revied the dimlevel 6,7,10,11,12,13 and 14 i don´t know why. I tested the switch protocol too, but often the arduino don´t react if I change the State with the webgui. Is it possible that I need a bandpass filter for the arduino, if I want to use the kaku protocol?

what should I do to dim my arduino?

Sorry for my english.

Pascal


Attached Files
.zip   arduino_dimmer.zip (Size: 5.88 KB / Downloads: 14)
 
Reply
#2
Can you move your post somewhere else (e.g. electronics) because your question is not about protocol development but about the pilight plugin system.
 
Reply
#3
Can I change the category or must I create a new post and delete the other.
 
Reply
#4
Thread moved to electronics.
 
Reply
#5
Now I´m a bit further with the protocol.
I have deleted pilight and build it manually, but first I change the CMakeExclude, CMakeConfig and put in the 433.92 folder the arduino_dimmer files. after that I could compile it but it shows 2 errors in the arduino_dimmer.c file.
In line 209 and 211 I forgot to change id to sytemcode and unit to unitcode.
Than I could compile pilight without any error.
My Config-file show no error, too.
But while I tested the Protocol, it shows that the dimlevel would not changed.
The protocol has the groups systemcode, unitcode, dimlevel and state

systemcode: 0-2
unitcode: 3-5
dimlevel: 6-10
state: 11

If I change my config file the systemcode and unitcode changed correctly and if I press the switch at the webgui the state changes too, but the dimlevel is always 11111.
I don´t know why the dimlevel didn´t change.

Code:
/*
    Copyright (C) 2014 CurlyMo

    This file is part of pilight.

    pilight is free software: you can redistribute it and/or modify it under the
    terms of the GNU General Public License as published by the Free Software
    Foundation, either version 3 of the License, or (at your option) any later
    version.

    pilight 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with pilight. If not, see    <http://www.gnu.org/licenses/>
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "../../core/pilight.h"
#include "../../core/common.h"
#include "../../core/dso.h"
#include "../../core/log.h"
#include "../protocol.h"
#include "../../core/binary.h"
#include "../../core/gc.h"
#include "arduino_dimmer.h"

#define PULSE_MULTIPLIER    3
#define MIN_PULSE_LENGTH    296
#define MAX_PULSE_LENGTH    306
#define AVG_PULSE_LENGTH    301
#define RAW_LENGTH                50

static int validate(void) {
    if(arduino_dimmer->rawlen == RAW_LENGTH) {
        if(arduino_dimmer->raw[arduino_dimmer->rawlen-1] >= (MIN_PULSE_LENGTH*PULSE_DIV) &&
           arduino_dimmer->raw[arduino_dimmer->rawlen-1] <= (MAX_PULSE_LENGTH*PULSE_DIV)) {
            return 0;
        }
    }

    return -1;
}

static void createMessage(int systemcode, int unitcode, int state, int dimlevel) {
    arduino_dimmer->message = json_mkobject();
    json_append_member(arduino_dimmer->message, "systemcode", json_mknumber(systemcode, 0));
    json_append_member(arduino_dimmer->message, "unitcode", json_mknumber(unitcode, 0));
    if(dimlevel >= 0) {
        state = 1;
        json_append_member(arduino_dimmer->message, "dimlevel", json_mknumber(dimlevel, 0));
    }
    if(state == 0) {
        json_append_member(arduino_dimmer->message, "state", json_mkstring("on"));
    } else {
        json_append_member(arduino_dimmer->message, "state", json_mkstring("off"));
    }
}

static void parseCode(void) {
    int binary[RAW_LENGTH/4], x = 0, i = 0;

    for(x=0;x<arduino_dimmer->rawlen-2;x+=4) {
        if(arduino_dimmer->raw[x+3] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) {
            binary[i++] = 1;
        } else {
            binary[i++] = 0;
        }
    }

    int systemcode = binToDec(binary, 0, 2);
    int unitcode = binToDec(binary, 3, 5);
    int dimlevel = binToDecRev(binary, 6, 10);
    int state = binary[11];
    createMessage(systemcode, unitcode, state, dimlevel);
}

static void createLow(int s, int e) {
    int i;

    for(i=s;i<=e;i+=4) {
        arduino_dimmer->raw[i]=(AVG_PULSE_LENGTH);
        arduino_dimmer->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH);
        arduino_dimmer->raw[i+2]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH);
        arduino_dimmer->raw[i+3]=(AVG_PULSE_LENGTH);
    }
}

static void createHigh(int s, int e) {
    int i;

    for(i=s;i<=e;i+=4) {
        arduino_dimmer->raw[i]=(AVG_PULSE_LENGTH);
        arduino_dimmer->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH);
        arduino_dimmer->raw[i+2]=(AVG_PULSE_LENGTH);
        arduino_dimmer->raw[i+3]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH);
    }
}

static void clearCode(void) {
    createLow(0,47);
}

static void createSystemCode(int systemcode) {
    int binary[255];
    int length = 0;
    int i=0, x=0;

    length = decToBinRev(systemcode, binary);
    for(i=0;i<=length;i++) {
        if(binary[i]==1) {
            x=i*4;
            createHigh(x, x+3);
        }
    }
}

static void createUnitCode(int unitcode) {
    int binary[255];
    int length = 0;
    int i=0, x=0;

    length = decToBinRev(unitcode, binary);
    for(i=0;i<=length;i++) {
        if(binary[i]==1) {
            x=i*4;
            createHigh(12+x, 12+x+3);
        }
    }
}

static void createDimlevel(int dimlevel) {
    int binary[255];
    int length = 0;
    int i=0, x=0;

    length = decToBin(dimlevel, binary);
    for(i=0;i<=length;i++) {
        if(binary[i]==1) {
            x=((length-i)+1)*4;
            createHigh(43-x, 43-(x-3));
        }
    }
}

static void createState(int state) {
    if(state == 1) {
        createHigh(44, 47);
    } else if(state == -1) {
        arduino_dimmer->raw[44]=(AVG_PULSE_LENGTH);
        arduino_dimmer->raw[45]=(AVG_PULSE_LENGTH);
        arduino_dimmer->raw[46]=(AVG_PULSE_LENGTH);
        arduino_dimmer->raw[47]=(AVG_PULSE_LENGTH);
    }
}

static void createFooter(void) {
    arduino_dimmer->raw[48]=(AVG_PULSE_LENGTH);
    arduino_dimmer->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH);
}

static int checkValues(struct JsonNode *code) {
    int dimlevel = -1;
    int max = 15;
    int min = 0;
    double itmp = -1;

    if(json_find_number(code, "dimlevel-maximum", &itmp) == 0)
        max = (int)round(itmp);
    if(json_find_number(code, "dimlevel-minimum", &itmp) == 0)
        min = (int)round(itmp);
    if(json_find_number(code, "dimlevel", &itmp) == 0)
        dimlevel = (int)round(itmp);

    if(min > max) {
        return 1;
    }

    if(dimlevel != -1) {
        if(dimlevel < min || dimlevel > max) {
            return 1;
        } else {
            return 0;
        }
    }
    return 0;
}

static int createCode(struct JsonNode *code) {
    int systemcode = -1;
    int unitcode = -1;
    int state = -1;
    int dimlevel = -1;
    int max = 15;
    int min = 0;
    double itmp = -1;

    if(json_find_number(code, "dimlevel-maximum", &itmp) == 0)
        max = (int)round(itmp);
    if(json_find_number(code, "dimlevel-minimum", &itmp) == 0)
        min = (int)round(itmp);

    if(json_find_number(code, "systemcode", &itmp) == 0)
        systemcode = (int)round(itmp);
    if(json_find_number(code, "unitcode", &itmp) == 0)
        unitcode = (int)round(itmp);
    if(json_find_number(code, "dimlevel", &itmp) == 0)
        dimlevel = (int)round(itmp);
    
    if(json_find_number(code, "off", &itmp) == 0)
        state=0;
    else if(json_find_number(code, "on", &itmp) == 0)
        state=1;

    if(systemcode == -1 || unitcode == -1 || (dimlevel == -1 && state == -1)) {
        logprintf(LOG_ERR, "arduino_dimmer: insufficient number of arguments");
        return EXIT_FAILURE;
    } else if(systemcode > 7 || systemcode < 1) {
        logprintf(LOG_ERR, "arduino_dimmer: invalid id range");
        return EXIT_FAILURE;
    } else if(unitcode > 7 || unitcode < 0) {
        logprintf(LOG_ERR, "arduino_dimmer: invalid unit range");
        return EXIT_FAILURE;
    } else if(dimlevel != -1 && (dimlevel > max || dimlevel < min) ) {
        logprintf(LOG_ERR, "arduino_dimmer: invalid dimlevel range");
        return EXIT_FAILURE;
    } else if(dimlevel >= 0 && state == 0) {
        logprintf(LOG_ERR, "arduino_dimmer: dimlevel and off state cannot be combined");
        return EXIT_FAILURE;
    } else {
        if(unitcode == -1) {
            unitcode = 0;
        }
        if(dimlevel >= 0) {
            state = -1;
        }
        createMessage(systemcode, unitcode, state, dimlevel);
        clearCode();
        createSystemCode(systemcode);
        createUnitCode(unitcode);
        createState(state);
        if(dimlevel > -1) {
            createDimlevel(dimlevel);
        }
        createFooter();
        arduino_dimmer->rawlen = RAW_LENGTH;
    }
    return EXIT_SUCCESS;
}

static void printHelp(void) {
    printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n");
    printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n");
    printf("\t -d --dimlevel=dimlevel\t\tsend a specific dimlevel\n");
    printf("\t -t --on\t\t\tsend an on signal\n");
    printf("\t -f --off\t\t\tsend an off signal\n");
}

#if !defined(MODULE) && !defined(_WIN32)
__attribute__((weak))
#endif
void arduinoDimmerInit(void) {

    protocol_register(&arduino_dimmer);
    protocol_set_id(arduino_dimmer, "arduino_dimmer");
    protocol_device_add(arduino_dimmer, "arduino_dimmer", "Arduino Dimmers");
    arduino_dimmer->devtype = DIMMER;
    arduino_dimmer->hwtype = RF433;
    arduino_dimmer->minrawlen = RAW_LENGTH;
    arduino_dimmer->maxrawlen = RAW_LENGTH;
    arduino_dimmer->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV;
    arduino_dimmer->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV;

    options_add(&arduino_dimmer->options, 'd', "dimlevel", OPTION_HAS_VALUE, DEVICES_VALUE, JSON_NUMBER, NULL, "^([0-9]{1}|[1][0-5])$");
    options_add(&arduino_dimmer->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$");
    options_add(&arduino_dimmer->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^(3[012]?|[012][0-9]|[0-9]{1})$");
    options_add(&arduino_dimmer->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL);
    options_add(&arduino_dimmer->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL);

    options_add(&arduino_dimmer->options, 0, "dimlevel-minimum", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)0, "^([0-9]{1}|[1][0-5])$");
    options_add(&arduino_dimmer->options, 0, "dimlevel-maximum", OPTION_HAS_VALUE, DEVICES_SETTING, JSON_NUMBER, (void *)15, "^([0-9]{1}|[1][0-5])$");
    options_add(&arduino_dimmer->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$");
    options_add(&arduino_dimmer->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$");

    arduino_dimmer->parseCode=&parseCode;
    arduino_dimmer->createCode=&createCode;
    arduino_dimmer->printHelp=&printHelp;
    arduino_dimmer->checkValues=&checkValues;
    arduino_dimmer->validate=&validate;
}

#if defined(MODULE) && !defined(_WIN32)
void compatibility(struct module_t *module) {
    module->name = "arduino_dimmer";
    module->version = "0.1";
    module->reqversion = "1.0";
    module->reqcommit = "84";
}

void init(void) {
    arduinoDimmerInit();
}
#endif
 
Reply
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  List of protocol supported 433mhz remotes ettman8 0 1,005 03-22-2018, 10:48 AM
Last Post: ettman8
  Arduino nano with CC1101 ? Bootscreen 5 3,915 11-04-2016, 03:38 PM
Last Post: curlymo
  What protocol is used by Aukey plugs? droptix 1 2,164 08-28-2015, 02:39 PM
Last Post: pilino1234
  Communication between Raspberry and Arduino pessie 17 10,875 07-31-2014, 01:21 PM
Last Post: curlymo
  Arduino sensor jpoilux 1 3,036 07-02-2014, 07:21 PM
Last Post: curlymo

Forum Jump:


Browsing: 1 Guest(s)