hello,
i need to send a float value over i2c, from one Arduino to raspberry.
i understand i need to convert it, because only byte or string can be sent.
i wrote this code but it seems it doesn't work ,i dont know what should i do ,
code arduino
#include <Wire.h>
#include "SHTSensor.h"
#include <String.h>
SHTSensor sht;
double Temperature;
double Humidity;
int i;
byte SLAVE_ADRESS = 8;
char temp[10];
char humid[10];
String tmp;
char T_data[5];
int index = 0;
void SendData(){
if (sht.readSample()) {
Serial.println("SHT:");
Humidity=sht.getHumidity();
Serial.println(Humidity,2);
Temperature=sht.getTemperature();
Serial.println(Temperature,2);
}
else {
Serial.println("Error in readSample()\n");
}
tmp = dtostrf(Temperature,5,2,temp);
for(i=0;i++;i<5){
T_data[i] = tmp[i];
}
Wire.write(T_data[index]);
++index;
if(index >= 5){
index=0;
}
}
void setup() {
Wire.begin(SLAVE_ADRESS);
Wire.onRequest(SendData);
Serial.begin(9600);
if (sht.init()) {
Serial.println("init(): success\n");
}
else {
Serial.println("init(): failed\n");
}
}
void loop() {
}
code rasp
import smbus
import time
bus = smbus.SMBus(1)
address = 0x08
while True:
data = ""
for i in range(0,5):
data += chr(bus.read_byte(address))
print(data)
time.sleep(1)
so if you can cheek my codes and tell me what is the problem , i will be great full ,its been a week searching end it doesn't work
thank you