Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I2C BRICK adhoc commands for slaves from UNO master.
#include <Wire.h>
const byte _num_chars = 32;
char _received_chars[_num_chars]; // an array to store the received data
boolean _has_new_data = false;
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println("ASSIMILATE IOT ACTOR/SENSOR EEPROM EDITOR");
Serial.println("ensure newline selected in console window");
Serial.println();
Serial.println("ADDRESS 1<ENTER> CONFIRM METADATA RECEIPT N/A (FOR M2M)");
Serial.println("ADDRESS 2 <CMD><ENTER> ACTOR COMMAND");
Serial.println();
Serial.println("ADDRESSES ON BUS:");
scan_i2c_addresses();
Serial.println();
Serial.println("<Arduino is ready>");
}
void scan_i2c_addresses(){
int device_count = 0;
for (byte address = 8; address < 127; address++)
{
Wire.beginTransmission(address);
const byte error = Wire.endTransmission();
if (error == 0)
{
Serial.println(address);
}
}
}
void loop() {
recv_with_end_marker();
send_to_i2c();
}
void recv_with_end_marker() {
static byte ndx = 0;
char end_marker = '\n';
char rc;
while (Serial.available() > 0 && _has_new_data == false) {
rc = Serial.read();
if (rc != end_marker) {
_received_chars[ndx] = rc;
ndx++;
if (ndx >= _num_chars) {
ndx = _num_chars - 1;
}
}
else {
_received_chars[ndx] = '\0'; // terminate the string
ndx = 0;
_has_new_data = true;
}
}
}
void send_to_i2c() {
char param_buf[16];
const String received_string = String(_received_chars);
if (_has_new_data == true) {
int idx1 = received_string.indexOf(' ');
String address = received_string.substring(0, idx1);
int address_int = address.toInt();
if (address_int < 8 || address_int > 127){
Serial.println("INVALID ADDRESS INPUT:");
Serial.println(address);
return;
}
int idx2 = received_string.indexOf(' ', idx1+1);
String code;
if (idx2 == -1){
code = received_string.substring(idx1+1);
}else{
code = received_string.substring(idx1+1, idx2+1);
}
int code_int = code.toInt();
if (code_int < 0 || code_int > 5){
Serial.println("INVALID CODE INPUT:");
Serial.println(code);
return;
}
bool has_parameter = idx2 > -1;
String parameter;
if (has_parameter){
parameter = received_string.substring(idx2 + 1, idx2 + 17); // 16 chars max
if (parameter.length() < 1){
Serial.println("PARTAMETER MIN. LENGTH 1");
_has_new_data = false;
return;
}
}else{
if (code_int > 1){
Serial.println("PARAMETER REQUIRED!");
_has_new_data = false;
return;
}
}
Serial.println();
Serial.print("input orig = ");
Serial.println(received_string);
Serial.print("address = ");
Serial.println(address);
Serial.print("code = ");
Serial.println(code);
Serial.print("parameter = ");
Serial.println(parameter);
// SEND VIA I2C
Wire.beginTransmission(address_int);
Wire.write(code_int);
if (has_parameter){
parameter.trim();
strcpy(param_buf, parameter.c_str());
Wire.write(param_buf);
}
Wire.endTransmission();
Serial.println();
Serial.println("SENT VIA I2C!");
Serial.println();
Serial.println("<Arduino is ready>");
_has_new_data = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.