How to Master Raspi receive FLOAT value from Arduino Slave

339 views
Skip to first unread message

Pham Dinh

unread,
Jul 5, 2016, 7:15:55 AM7/5/16
to Quick2Wire Users
Dear all,

I am using quick2wire library to communicate between Raspi B+ (Master) with Arduino (Slave). Arduino read value Temp/HUM from DHT22 sensor and send to Raspi. I want to send float data. Could you please, help me how can Raspi receive float data from Arduino. Here the code from Arduino:


//Using Wire library of Chuck Todd see more detail http://forum.arduino.cc/index.php?topic=104732.15

#include <Wire.h>
#include <compat/twi.h>
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT22

volatile float t;
volatile float h;

#define I2C_address 0x05

void setup() {
  // put your setup code here, to run once:

  dht.begin();

  Wire.begin(I2C_address);
  Wire.onRequest(SentValue);

}
 void loop(){
        delay(1000);
        t = (float) dht.readTemperature();
        h = (float) dht.readHumidity();
}
 void SentValue(void){
      I2C_writeAnything(t);
      I2C_writeAnything(h);
}

Here the code Raspi:

import sys
sys.path.append('/home/pi/Python/quick2wire-python-api')

from quick2wire.i2c import I2CMater, reading
import time

adddress = 0x05

with I2CMaster () as master:
      while True:
           value = master.transaction(reading(address,10))
           print ("Received: %f " % value)
           time.sleep(1)

////////////////////////////////////////////////
When i run module python3.4.2 shell send  error : TypeError: a float is required

I am beginner with python. Please help me.

Thank you very much.






Andrew Scheller

unread,
Jul 5, 2016, 8:08:26 AM7/5/16
to Quick2Wire Users
Doing a quick search, it looks like you're using the I2C_Anything
library from http://www.gammon.com.au/forum/?id=10896&reply=8#reply8

So you need to write a python-side equivalent of I2C_readAnything to
convert the bytes sent over i2c back into a float value.
https://docs.python.org/2/library/struct.html might help you with
that.

Other approaches would be to convert the float into an integer, by
multiplying it by some suitable multiplier (e.g. 10 or 100) and then
sending and reading the integer value to python, and then dividing (by
10 or 100) to get back to a floating-point number. Or you could
convert the float to a string by using sprintf, and then send the
string bytes over i2c, and assembling the individual bytes back into a
string on the python side.

Or you could simply connect the DHT22 sensor directly to your
RaspberryPi - loads of examples online
https://duckduckgo.com/?q=dht22+raspberry+pi


BTW I wouldn't recommend using the quick2wire code for anything (no
matter which approach you end up going with) - it's effectively an
abandoned dead-end (it's had no updates in over 3 years!).
https://duckduckgo.com/?q=python+smbus+raspberry+pi seems like a much
better-supported alternative.

And you could also try https://www.raspberrypi.org/forums/viewforum.php?f=44

Andrew
> --
> You received this message because you are subscribed to the Google Groups
> "Quick2Wire Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to quick2wire-use...@googlegroups.com.
> Visit this group at https://groups.google.com/group/quick2wire-users.
> For more options, visit https://groups.google.com/d/optout.

Pham Dinh

unread,
Jul 6, 2016, 10:51:48 AM7/6/16
to Quick2Wire Users
Dear Andrew Scheller,

Yes, I am using library of Nick Gammon edited by Chuck Todd. I use value temperature of DHT for my goal is send float value via I2C. Arduino to arduino is ok with I2C_anything library. I want to try Arduino to Raspberry. With beginner Python i need few days to try your recommended.

I appreciated your reply. Thank you for your consider. 
Reply all
Reply to author
Forward
0 new messages