Ok first of all. Here is what have I done and tested so far:
-
Arduino master requestFrom Arduino slaves. (Works as long no delays or loops involved in the slaves)
-
Arduino master beginTransmission() to slaves. Slaves then onReceive, do whatever it needs to do. After a period of time, master request from arduino slaves. (Works as long no loop inlvolved)
Master beginTransmission to arduino slave.
Arduino Slave (attached to Grove GPS, and it uses TinyGPS library), starts polling the GPS.
Master hangs and will never get any response or reply. I believe it is because there is a loop involved.
//You can find the parsing codes from TinyGPS sample
//This is just the part the looping part
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial2.available())
{
char c = Serial2.read();
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
4.Master beginTransmission to arduino slave.
Arduino Slave (attached to the official GSM shield), initialize the GSM shield.
Master hangs and will never get any response or reply. I believe it is because there is a loop involved, and also because GSM does not immediately initialize, it needs some time.
- I understand that requestFrom, requires the slaves to syn with the master clock. That is why I also tried sending 'something' to slave first, and wait for a while before do a requestFrom.
//master
Serial.print("Slave 1: ");
Wire.beginTransmission(BLUNO_SLAVE);
Wire.write(1);
Wire.endTransmission();
delay(2000);
Wire.requestFrom(BLUNO_SLAVE, 25); //char occupy 1 byte
readData();
Serial.print("Master millis: "); Serial.println(millis());
//slave junk data
//If I uncomment the switch chunk, and comment out the millis, its gonna work just fine.
#include <Wire.h>
boolean bDoSomething = false;
int bytesReceived = 0;
void setup() {
Serial.begin(115200);
Wire.begin(0x09); //slave address
Serial.println("Serials initialized");
Wire.onRequest(returnData);
Wire.onReceive(doSomething);
}
void loop() {
}
void returnData() {
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;) {
}
Wire.write(millis());
/*
switch(bytesReceived) {
case 0:
Wire.write("0 bytes received");
break;
case 1:
Wire.write("1 byte received");
break;
case 2:
Wire.write("2 bytes received");
break;
case 3:
Wire.write("3 bytes received");
break;
case 4:
Wire.write("4 bytes received");
break;
default:
Wire.write("received master's request");
break;
}
*/
}
//I don't care how many bytes it sends as of now
void doSomething(int numBytes) {
//bytesReceived = numBytes;
/*
while(Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
*/
bytesReceived = Wire.read();
}
The combination of master and slave below, with loop works:
/*
* begin transmission to each slave.
* send some data to slave, and end transmission.
* On receive, slave do something depending on the command from the master.
* master set a timer. After x timer, master request the data from the slave.
*/
#include <Wire.h>
/***************************************************CONSTANTS */
#define BLUNO_SLAVE 0x09
#define BLUNO_SLAVE_2 0X0A
#define BLUNO_SLAVE_3 0x0B
/**************************************************************/
void setup() {
Serial.begin(115200);
Wire.begin();
Serial.println("Initialized as Master");
//each call to requestFrom, RESETs the read buffer index
Serial.print("Slave 1: ");
Wire.beginTransmission(BLUNO_SLAVE);
Wire.write(1);
Wire.endTransmission();
delay(6000);
Wire.requestFrom(BLUNO_SLAVE, 25); //char occupy 1 byte
readData();
Serial.print("Master millis: "); Serial.println(millis());
}
void loop () {
}
void readData () {
while(Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
}
//slave
#include <Wire.h>
boolean bDoSomething = false;
int bytesReceived = 0;
char *data;
void setup() {
Serial.begin(115200);
Wire.begin(0x09); //slave address
Serial.println("Serials initialized");
Wire.onRequest(returnData);
Wire.onReceive(doSomething);
}
void loop() {
}
void returnData() {
int i=0;
String someString = "ayam";
while(i<5) {
delay(1000);
i++;
}
someString += i;
data = (char *) realloc (data, 5); //reallocate path memory
someString.toCharArray(data, 6);
Wire.write(data);
}
//I don't care how many bytes it sends as of now
void doSomething(int numBytes) {
bytesReceived = Wire.read();
}
The slave below, does not work however even though I called slave3 exactly as what I did for for slave 1.
#include <Wire.h>
#include <GSM.h> // Include the GSM library
/***********************************************GSM VARIABLES*/
/*********************************************GPRS VARIABLES*/
/************************************************************/
void returnData() {
if(bGsmSetup)
Wire.write("GSM has been setup!!");
else
Wire.write("GSM initialzation failed");
}
//I don't care how many bytes it sends as of now
void doSomething(int numBytes) {
Serial.println("Setting up GSM..");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected) {
Serial.print("not connected");
if(gsmAccess.begin(PINNUMBER)!=GSM_READY) { //PINNUMBER
Serial.println("GSM ERROR");
while(true);
} else {
notConnected = false;
bGsmSetup = true;
}
}
Serial.println("GSM has been setup.");
}
void setup() {
Serial.begin(115200);
Wire.begin(0x0B); //slave address
Serial.println("Serials initialized");
Wire.onRequest(returnData);
Wire.onReceive(doSomething);
}
void loop() {
}