• 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


[Fully Supported] LM75 and LM76 temperature sensor
#1
Hi everyone,

I was trying to set up the LM75 temperature sensor (using I2C). I worked on a new protocol based on the description - it compiles without errors, but I wasn't able to fully integrate it.

The C-code I wrote to access the sensor with the raspberry is pretty short and easy:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
int main() {
    int fd = 0;
    fd = wiringPiI2CSetup(0x49);
    int rawtemperature = wiringPiI2CReadReg16(fd, 0x00);
    float temperature = (rawtemperature & 0x00ff) + ((rawtemperature >> 15)?0:0.5);
    printf("%.1lf\n", temperature);
    exit(EXIT_SUCCESS);
}

I guess there are more changes necessary than just adding these few lines in the code a new dummy-protocol? Do I need to create a new Hardware Module?

My protocol lm75.c looks like this so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdint.h>

#include "../../pilight.h"
#include "common.h"
#include "log.h"
#include "threads.h"
#include "protocol.h"
#include "hardware.h"
#include "binary.h"
#include "gc.h"
#include "json.h"
#include "lm75.h"
#include "../pilight/wiringPi.h"
#include <wiringPiI2C.h>


void *lm75Parse(void *param) {

    struct JsonNode *json = (struct JsonNode *)param;
    struct JsonNode *jsettings = NULL;
    int interval = 5;

    if((jsettings = json_find_member(json, "settings"))) {
        json_find_number(jsettings, "interval", &interval);
    }
    json_delete(json);

        int fd = 0;
        fd = wiringPiI2CSetup(0x49);

            if(fd>0) {

                int rawtemperature = wiringPiI2CReadReg16(fd, 0x00);
            
                float newtemperature = (rawtemperature & 0x00ff) + ((rawtemperature >> 15)?0:0.5);


                lm75->message = json_mkobject();
                JsonNode *code = json_mkobject();
                json_append_member(code, "id", json_mkstring(lm75->id));
                json_append_member(code, "temperature", json_mknumber(newtemperature));

                json_append_member(lm75->message, "code", code);
                json_append_member(lm75->message, "origin", json_mkstring("receiver"));
                json_append_member(lm75->message, "protocol", json_mkstring(lm75->id));
                pilight.broadcast(lm75->id, lm75->message);
                json_delete(lm75->message);
                lm75->message = NULL;
            } else {
                logprintf(LOG_DEBUG, "error connecting to lm75 (probably i2c bus error from wiringPiI2CSetup - maybe wrong id? use i2cdetect to find out)");
                sleep(1);
            }


    return (void *)NULL;
}

void lm75InitDev(JsonNode *jdevice) {
    char *output = json_stringify(jdevice, NULL);
    JsonNode *json = json_decode(output);
    threads_register("lm75", &lm75Parse, (void *)json);
    sfree((void *)&output);
}


void lm75Init(void) {
    protocol_register(&lm75);
    protocol_set_id(lm75, "lm75");
    protocol_device_add(lm75, "lm75", "Lm75 temperature sensor");
    lm75->devtype = WEATHER;
    lm75->hwtype = SENSOR;

    options_add(&lm75->options, 't', "temperature", has_value, config_value, "^[0-9]{1,3}$");
    options_add(&lm75->options, 'i', "id", has_value, config_id, ".+");

    protocol_setting_add_number(lm75, "decimals", 1);
    protocol_setting_add_number(lm75, "temperature", 1);
    protocol_setting_add_number(lm75, "interval", 5);

    lm75->initDev=&lm75InitDev;

    wiringPiSetup();
}

Cheers!
 
Reply
#2
What doesn't work?
 
Reply
#3
The problem is, that the data sent out by the sensor does not show up on the webinterface even though pilight-receive receives data every 5 seconds:

pilight-receive:
Code:
{
        "temperature": 225,
        "origin": "receiver",
        "protocol": "lm75"
}

configuration:
Code:
{
        "living": {
                "name": "Living",
                "temperature": {
                     "name": "lm75",
                     "protocol": [ "lm75" ],
                     "id": [{
                        "id": "0x49"
                     }],
                     "temperature": 210
                }
        }


The webserver keeps showing the 210 from the configuration file, not the 225 coming from the sensor.

Thanks in advance!
 
Reply
#4
A few checks:
If you stop the daemon, is the configuration temperature value different from before?
If you start pilight in debug mode: pilight-daemon -D, do you see these message:
Code:
[Jan 12 22:08:23:554270] pilight-daemon: DEBUG: broadcasted: {"origin":"config","type":3,"uuid":"0338-00-00-38-000300","devices":{"woonkamer":[ "klimaat" ]},"values":{"humidity":"473","temperature":"200"}}
 
Reply
#5
I found there are to many errors to explain. I will upload a fixed version myself based on your code. Please test if that one works.
 
Reply
#6
@CurlyMo

Thats fine if you upload a fixed version.Smile, because i have 4x LM75 in use AND some other I2C stuff (PCF8574).
Maybe integrate I2C in pilight ?
So is pilight more and more multy usable.Wink
 
Reply
#7
How would we need to integrate I2C in another way as currently done by this protocol.

Already uploaded a new version yesterday.
 
Reply
#8
ok, i will test and maybe use.
 
Reply
#9
Like to install the lm75 protocoll.
but after new install with:

git clone --depth 5 -b development https://github.com/pilight/pilight.git

after setup.sh error:

Code:
[ 86%] Building C object CMakeFiles/pilight.dir/libs/websockets/output.c.o
[ 87%] Building C object CMakeFiles/pilight.dir/libs/websockets/server.c.o
[ 87%] Built target pilight
Scanning dependencies of target pilight_shared
Linking C shared library libpilight.so
[ 87%] Built target pilight_shared
Scanning dependencies of target pilight-control
[ 89%] Building C object CMakeFiles/pilight-control.dir/control.c.o
Linking C executable pilight-control
libpilight.so: undefined reference to `i2c_smbus_write_byte'
libpilight.so: undefined reference to `i2c_smbus_read_byte'
libpilight.so: undefined reference to `i2c_smbus_write_byte_data'
libpilight.so: undefined reference to `i2c_smbus_write_word_data'
libpilight.so: undefined reference to `i2c_smbus_read_word_data'
libpilight.so: undefined reference to `i2c_smbus_read_byte_data'
collect2: ld returned 1 exit status
make[2]: *** [pilight-control] Error 1
make[1]: *** [CMakeFiles/pilight-control.dir/all] Error 2
make: *** [all] Error 2
root@raspberrypi:~/pilight#

What's wrong ?
 
Reply
#10
If you did ./setup.sh clean and then ./setup.sh you should receive a clear message of what's wrong.
 
Reply
  


Possibly Related Threads...
Thread Author Replies Views Last Post
Lightbulb [Fully Supported] Kaku Door sensor (AMST-606) geerttttt 53 24,387 10-19-2019, 06:26 PM
Last Post: curlymo
  Digoo / Baldr / Nexus / Rubicson temperature/humidity sensor thielj 4 1,377 09-30-2019, 04:48 PM
Last Post: curlymo
  433MHz PIR sensor from Amazon ha_username 0 90 09-29-2019, 11:44 PM
Last Post: ha_username
  [Partially Supported] TFA / Conrad Weather Yves 184 11,593 03-31-2019, 05:22 PM
Last Post: curlymo
  gs-iwds07 window sensor Loggisch 46 12,777 01-02-2019, 09:11 PM
Last Post: curlymo
  [Fully Supported] DHT22 IcedEarth 86 31,628 11-18-2018, 09:33 AM
Last Post: curlymo
  Pearl weatherstation sensor for FWS-686 & FWS-665 chklump 13 4,704 08-12-2018, 05:34 PM
Last Post: fourty2
  TFA 30.3160 Pool Sensor wseifert 4 909 05-30-2018, 09:24 AM
Last Post: wseifert
  [Fully Supported] Arctech Old Martin 111 35,570 03-27-2018, 04:43 PM
Last Post: genfersee
Lightbulb BH1750 i2c digital illuminance sensor marcm 14 3,642 02-25-2018, 08:40 PM
Last Post: morph027

Forum Jump:


Browsing: 1 Guest(s)