Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Arduino
- void requestEvent() {
- byte DEV_CNT =4;
- float hodnoty[DEV_CNT];
- hodnoty[0]=10000;
- hodnoty[1]=20;
- hodnoty[2]=1029.50;
- hodnoty[3]=3333.123456789;
- // Buffer to hold temp data, 4 bytes for each device
- byte buffer[DEV_CNT*4];
- // Populate buffer with temp. data
- for(int a = 0; a < DEV_CNT; a++) {
- byte* b = (byte*) &hodnoty[a];
- // 4 bytes for each float
- for(int i = 0; i < 4; i++) {
- buffer[a*4+i] = b[i];
- }
- }
- // Send data over i2c connection
- Wire.write(buffer, DEV_CNT*4);
- }
- Raspberry
- #!/usr/bin/python
- import smbus
- import time
- import struct
- bus = smbus.SMBus(1)
- address = 0x03
- while True:
- try:
- data = bus.read_i2c_block_data(address, 0);
- except:
- print "Error getting data\n"
- continue
- # Example uses 4 sensors.
- # Each float from the Arduino is 4 bytes long.
- print len(data)
- print data
- for i in range(0, 4):
- bytes = data[4*i:4*i+4]
- # Python 2 struct.unpack takes the data to be unpacked
- # in string format, so the bytes need to be joined
- # together first
- print struct.unpack('f', "".join(map(chr, bytes)))[0]
- print
- time.sleep(5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement