I use I2C to connect ESP8266 with Arduino.
Master Code on ESP8266:
#include <Wire.h>
//#include <Time.h> //only on arduino
struct DATA_STRUCTURE {
time_t czas_kontrolny;
float temp_ds, temp_dht, temp_bmp, humi_dht, srednia;
int32_t pressure;
} volatile data;
union u_tag {
byte b[4];
float fval;
time_t time_tval;
int32_t int32_tval;
} u;
char bufor[100];
void setup() {
Serial.begin(9600);
// Wire.begin(); //only on arduino
Wire.begin(0, 2); //only on ESP8266
Wire.setClock(100000);//only on ESP8266
}
void loop() {
delay(500);
int available = Wire.requestFrom(3, sizeof(data));
if (available == sizeof(data))
{
Serial.print("odczyt danych: ");
for (int i = 0; i < 4; i++) u.b[i] = Wire.read();
data.czas_kontrolny = u.time_tval;
for (int i = 0; i < 4; i++) u.b[i] = Wire.read();
data.temp_ds = u.fval;
for (int i = 0; i < 4; i++) u.b[i] = Wire.read();
data.temp_dht = u.fval;
for (int i = 0; i < 4; i++) u.b[i] = Wire.read();
data.temp_bmp = u.fval;
for (int i = 0; i < 4; i++) u.b[i] = Wire.read();
data.humi_dht = u.fval;
for (int i = 0; i < 4; i++) u.b[i] = Wire.read();
data.pressure = u.int32_tval;
data.srednia = (data.temp_bmp + data.temp_ds + data.temp_dht) / 3;
Serial.print("[");
Serial.print(data.czas_kontrolny);
Serial.print("|");
Serial.print(data.temp_ds);
Serial.print("|");
Serial.print(data.temp_dht);
Serial.print("|");
Serial.print(data.temp_bmp);
Serial.print("|");
Serial.print(data.humi_dht);
Serial.print("|");
Serial.print(data.pressure);
Serial.println("]");
}
else
{
Serial.print(F("Blad odczytu - nieprawidlowa ilosc danych: "));
Serial.println(available);
}
}
Slave code on Arduino UNO
#include <Wire.h>
#include <Time.h>
struct DATA_STRUCTURE {
time_t czas_kontrolny;
float temp_ds, temp_dht, temp_bmp, humi_dht;
int32_t pressure;
} data;
char bufor[100];
void setup() {
Wire.begin(3); // join i2c bus with address #8
Wire.onRequest(requestEvent); //mega requesting data
Serial.begin(9600); // start serial for output
Serial.println("START");
}
void loop() {
}
void requestEvent()
{
byte *ArrayOf24Bytes;
data.czas_kontrolny = now();
data.temp_bmp = 24, 70; //for test
data.pressure = 100049;//for test
Serial.print("do wyslania: ");
Serial.print("[");
Serial.print(data.czas_kontrolny);
Serial.print("|");
Serial.print(data.temp_ds);
Serial.print("|");
Serial.print(data.temp_dht);
Serial.print("|");
Serial.print(data.temp_bmp);
Serial.print("|");
Serial.print(data.humi_dht);
Serial.print("|");
Serial.print(data.pressure);
Serial.println("]");
ArrayOf24Bytes = (byte*) & data;
Wire.write(ArrayOf24Bytes, 24);
}
When I use another arduino UNO as master (with additional liblary and change wire.begin function)
Every thing work correct.
But when ESP8266 is as master. Master don't receive correct data.
I check connection witch code from https://www.arduino.cc/en/Tutorial/MasterReader.
and with:
for master on esp8266
void loop() {
Serial.print("SET to:");
Serial.println(bb);
Wire.beginTransmission(3);
Wire.write(bb); //
Wire.endTransmission();
delay(100);
Serial.println();
Serial.print("request: ");
Wire.requestFrom(3, bb); // request MX 32 bytes from slave device #2
for(int i=0;i<bb;i++)
{
char c = Wire.read(); // receive a byte as character
Serial.print(c);
}
delay(500);
bb=bb+1;
if (bb>32) bb=10;
}
And for slave on arduino:
void requestEvent()
{
String inputString = "";
// sprintf (bufor, "x:= %d",x);
// Serial.println(bufor);
// Wire.write( (byte *) bufor, 10);
Serial.print("wyslanie :");
Serial.println(bb);
for (int i=0;i<bb;i++)
{
inputString+='*';
}
sprintf (bufor, "%s",inputString.c_str() );
Serial.print(inputString);
Wire.write( (byte *) bufor, bb);
Serial.println();
}
When I try send integer value is the same problem.
This is part of bigger plan
Thanks
Pawel.