everything is data.
rephrase you comments using "request" and "response".
everything is data.
rephrase you comments using "request" and "response".
but why the bytepointer to data.
when you say passed to the sending routine do you mean send this to sending routine and use it in the response.
The way im reading it it almost sounds like you mean send the response back to the master the same way it was send to the slave
Wire.requestFrom(8, sizeof(pinData)); /* request & read data of size 13 from slave */
while (Wire.available()) {
Wire.readBytes((byte*)&pinData, sizeof(pinData));
Serial.print( pinData.gpios[0] );
}
Do not check Wire.available() after requestFrom(), instead check the return value of requestFrom(). The return value with either be the number of bytes requested, or 0 if there is an error. When requestFrom() is successful, you will ALWAYS get the number of bytes requested, regardless of the number of bytes the slave actually sends, because the master generates the clock signal and will clock in bits even if the slave stops sending (the data line will go HIGH if the slave stops sending, the master has no way of telling whether the slave has stopped sending, or is sending all HIGH bits).
If you are concerned with data integrity, add a checksum or CRC byte to the struct. Also, on the nano it would be a good idea to not write data directly into the struct send to the ESP8266, instead have a 2nd copy of the struct to do the work in the main code, then after reading all the ports, calculate the checksum, then disable interrupts, copy the working copy of the struct to the one used in the ISR, and re-enable interrupts. That will prevent sending the struct before you complete reading all the ports.
Might help if you could describe the entire project, I was about to ask if you had considered using an I2C expander instead of a nano, and it is a bit confusing as to why it would be necessary to send the i/o port numbers in the struct, instead of just have a struct large enough for all the ports used.
if you want to transmit messages of different lengths and Wire.readBytes () needs to know the number of bytes in the message, how do you make this work. For example, in one case the message is a 10 byte struct, in another case it's simply an ACK
i suggested using TLVs. the message is the V, value part of the TLV, preceded by a message type and the length (bytes in message.
a send function would be passed the value, size of the message and a pointer to the data
void
send (
byte type,
byte nByte,
byte *p );
it would copy the type, length and message into a transmit buffer of length nByte+2 and transmit it using Wire.write().
a receive function, not knowing what it is receiving would check if anything is available, and then first read 2 bytes, the type/length, and then the remainder of the message
int
receive (
byte *buf,
byte maxLen )
{
if (0 == Wire.available())
return 0;
Wire.readBytes( buf, 2); // read type/length
Wire.readBytes( &buf [2], buf [1]); // read values
return buf [1];
}
presumably these function are used on both the master and the slave. they are generic for sending various types of messages, not just your struct.
presumably a request from the master can be of various length: a simple request with a type, or some message specifying values (e.g. set led 2 ON).
so the slave needs to be able to receive requests of various lengths and the master needs to be able to receive responses of various lengths.
How would i handle requests of various lengths because Wire.requestFrom() requires a length as an argument. Would it be safe to request 100 bytes but only send a couple? I dont think i would want to request a 1000 bytes and only receive a 10 byte struct but write 989 bytes after it into some other memory and corrupt the memory
@notsolowki you are at about 5% of where you need to be to make this work.
Is Serial/UART an option ?
Wire.requestFrom function, then it will read all the bytes that it requests. Neither the Master nor the Slave can shorten or lengthen it.Wire.requestFrom call, there is no such thing as waiting for data or a timeout. The Wire.requestFrom was successful or not.???
presumably you mean Wire.readBytes ((byte*) buf, 100).
no i mean,
master: Wire.requestFrom(8, 128);
slave: Wire.write((byte *)&pinData, 64);
master:Wire.readBytes((byte*)&pinData, sizeof(pinData));
this is assuming the response is only 64bytes and is the size of pinData.
i can change the buffer 32byte restriction in the Wire library.
of course you know the length of a message to transmit.
i thought the question was how to receive a message of unknown length. ??
I have a master/slave sketch but i have not been able to use memcpy to write struct bytes if structs are volatile.
Please help me here. i want to check the received requests to see if (check == 99) if it does i want to copy tempData to pinData which will set the state of all pins in pinData.gpios[ ] to the value of pinData.pins[ ] on the slave
master,
#include <Wire.h>
struct {
uint16_t gpios[8] {9, 4, 5, 6, 7, 8, 9};
uint8_t pins[8];
uint8_t check = 0;
} pinData;
struct {
uint16_t gpios[8] {8, 8, 8, 6, 7, 8, 9};
uint8_t pins[8];
uint8_t check = 0;
} tempData;
unsigned long now = 0;
volatile void handlePins() {
Wire.beginTransmission(8); /* begin with device address 8 */
pinData.check = 99;
Wire.write((byte *)&pinData, sizeof(pinData));
Wire.endTransmission(); /* stop transmitting */
pinData.check = 0;
Wire.requestFrom(8, sizeof(pinData)); /* request & read data from slave */
while (Wire.available()) {
Wire.readBytes((byte*)&tempData, sizeof(tempData));
}
if (tempData.check == 99) {
tempData.check = 0;
memcpy (&pinData, &tempData, sizeof (tempData));
} else {
Serial.println("problem");
}
}
void setup() {
Serial.begin(9600); /* begin serial for debug */
Wire.begin(D1, D2); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}
void loop() {
if (millis() - now >= 1000) {
handlePins();
now = millis();
Serial.println(pinData.gpios[6]);
}
}
slave,
#include <Wire.h>
unsigned long now = 0;
struct {
uint16_t gpios[8] {0, 0, 0, 0, 7, 8, 9};
uint8_t pins[8];
uint8_t check = 0;
} pinData;
struct {
uint16_t gpios[8] {9, 4, 5, 6, 7, 8, 9};
uint8_t pins[8];
uint8_t check = 0;
} tempData;
void setup() {
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(9600); /* start serial for debug */
Serial.println("hi");
for (int i; i <= 6; i++) {
pinData.pins[i] = digitalRead(pinData.gpios[i]);
Serial.println(pinData.gpios[i]);
}
}
void loop() {
if (millis() - now >= 1000) {
for (int i; i <= 6; i++) {
pinData.pins[i] = digitalRead(pinData.gpios[i]);
Serial.println(pinData.gpios[i]);
}
now = millis();
}
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
Wire.readBytes((byte*)&tempData, sizeof(tempData));
if (tempData.check == 99) {
tempData.check = 0;
memcpy (&pinData, &tempData, sizeof (tempData));
} else {
}
}
void requestEvent() { // no blocking code goes here!!
pinData.check = 99;
pinData.gpios[6]++;
Wire.write((byte *)&pinData, sizeof(pinData));
pinData.check = 0;
}
I updated my code to be more functional. how do you think this will work?
master,
#include <Wire.h>
struct {
uint16_t gpios[8] {9, 4, 5, 6, 7, 8, 9};
uint8_t pinStates[8];
uint8_t check = 99;
} pinData;
struct {
uint16_t gpios[8] {8, 8, 8, 6, 7, 8, 9};
uint8_t pinStates[8];
uint8_t check = 0;
} tempData;
unsigned long now = 0;
void handlePins(int a, int b, int c, int d, int e, int f, int g, int h) {
pinData.pinStates[0] = a;
pinData.pinStates[1] = b;
pinData.pinStates[2] = c;
pinData.pinStates[3] = d;
pinData.pinStates[4] = e;
pinData.pinStates[5] = f;
pinData.pinStates[6] = g;
pinData.pinStates[7] = h;
Wire.beginTransmission(8); /* begin with device address 8 */
Wire.write((byte *)&pinData, sizeof(pinData));
Wire.endTransmission(); /* stop transmitting */
Wire.requestFrom(8, sizeof(pinData)); /* request & read data from slave */
while (Wire.available()) {
Wire.readBytes((byte*)&tempData, sizeof(tempData));
}
if (tempData.check == 99) {
tempData.check = 0;
memcpy (&pinData, &tempData, sizeof (tempData));
}
}
void setup() {
Serial.begin(9600); /* begin serial for debug */
Wire.begin(D1, D2); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}
void loop() {
if (millis() - now >= 1000) {
handlePins(0,1,0,1,0,1,0,1);
now = millis();
Serial.println(pinData.gpios[6]);
}
}
slave,
#include <Wire.h>
struct {
uint16_t gpios[8] {0, 0, 0, 0, 7, 8, 9};
uint8_t pinStates[8];
uint8_t check = 99;
} pinData;
struct {
uint16_t gpios[8] {9, 4, 5, 6, 7, 8, 9};
uint8_t pinStates[8];
uint8_t check = 0;
} tempData;
unsigned long now = 0;
void setup() {
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(9600); /* start serial for debug */
Serial.println("hi");
for (int i; i <= 6; i++) {
pinData.pinStates[i] = digitalRead(pinData.gpios[i]);
Serial.println(pinData.gpios[i]);
}
}
void loop() {
if (millis() - now >= 1000) {
for (int i; i <= 6; i++) {
pinData.pinStates[i] = digitalRead(pinData.gpios[i]);
Serial.println(pinData.gpios[i]);
}
now = millis();
}
}
void updatePins() {
for (int i = 0; i <= 6; i++) {
digitalWrite(pinData.gpios[i], pinData.pinStates[i]);
}
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
Wire.readBytes((byte*)&tempData, sizeof(tempData));
if (tempData.check == 99) {
tempData.check = 0;
memcpy (&pinData, &tempData, sizeof (tempData));
updatePins();
}
}
void requestEvent() { // no blocking code goes here!!
Wire.write((byte *)&pinData, sizeof(pinData));
}
This is my idea of a verification of the bytes,
master: handlePins(0, 1, 0, 1, 0, 1, 0, 1);
void handlePins(int a, int b, int c, int d, int e, int f, int g, int h) {
pinData.pinStates[0] = a;
pinData.pinStates[1] = b;
pinData.pinStates[2] = c;
pinData.pinStates[3] = d;
pinData.pinStates[4] = e;
pinData.pinStates[5] = f;
pinData.pinStates[6] = g;
pinData.pinStates[7] = h;
Wire.beginTransmission(8); /* begin with device address 8 */
Wire.write((byte *)&pinData, sizeof(pinData));
Wire.endTransmission(); /* stop transmitting */
Wire.requestFrom(8, sizeof(pinData)); /* request & read data from slave */
while (Wire.available()) {
Wire.readBytes((byte*)&tempData, sizeof(tempData));
}
if (tempData.check == 99) {
tempData.check = 0;
memcpy (&pinData, &tempData, sizeof (tempData));
}else{
handlePins(a, b, c, d, e, f, g, h);
}
}
I just dont understand what you mean about the TLV method. i asked about some verification of the data somehow. i thought your trying to say use TLV as verification. and i am asking what to do with the TLV variables. but now i think your just showing me how to send and receive data.
How about my verification method that checks "check for value of 99" and can you help me improve this if you see any major problems. i also need a method to update a single pin.
idk my codes not working like i thought it was. im about to just give up on the idea and buy a expander board its not worth all the frustration i thought it would be simple.
Have you dropped the text commands ? Good
![]()
You don't need the tempData.
I prefer to check the number of received bytes.
int n = Wire.requestFrom(8, sizeof(pinData)); /* request & read data from slave */
if( n == sizeof(pinData))
{
Wire.readBytes((byte*)&pinData, sizeof(pinData));
}
To keep the requestEvent() short and fast, you update pinData in the loop(). That is very good, but why limit that to once per second ? If you remove the Serial.println(), then you can update it at maximum speed.
void loop()
{
for (int i; i <= 6; i++)
{
pinData.pinStates[i] = digitalRead(pinData.gpios[i]);
}
}
It is even possible to do the updatePins() in the loop(). Add a global variable volatile bool newData = false;. In the onReceive handler, write the new data to the struct and set newData to true. In the loop() check that variable and make it false after using it.
If you change a variable from inside a interrupt routine and use that also in the loop(), then it must be 'volatile'. That means also the struct has to be 'volatile'.
Use the 'howMany' to make it safer:
void receiveEvent(int howMany)
{
if( howMany == sizeof( pinData))
{
Wire.readBytes((byte*)&pinData, sizeof(pinData));
updatePins();
}
}
I have started to write a few basic notes about Arduino in Slave mode.
Nice writeup lots of good information. I have just recently discovered the old 16x4 lcd screens I use have a PCF8574T I2C GPIO expander module attached to the back of them.
I'm at a crossroad here. I feel as if the arduino as slave could be much more reliable but then again maybe not. I found a library for the PCF8574 that makes things really simple.
Too bad there's not an esp with 32 pins on it.
Use the PCF8574. That is easier.
what is this thread about?