I2C Pi Master and Ardiuno Slave
Ardiuno Slave 14.
#include <Wire.h>
const int ledPin = 13; // onboard LED
int ValueA3 = 0;
static_assert(LOW == 0, "Expecting LOW to be 0");
int Checking_In = 1;
void setup() {
Serial.begin(9600);
Wire.begin(0x14); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Wire.onRequest(requestEvent); // send date to Pi
Wire.onRequest(ard_checkingIn); // send date to Pi
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // turn it off
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent() {
while (Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
digitalWrite(ledPin, c);
}
}
void requestEvent()
{
}
void ard_checkingIn()
{
Wire.write(Checking_In);
}
Pi Master
#! /usr/bin/env python3
#include <iostream>
#include <wiringPiI2C.h>
import time
from smbus import SMBus
from array import *
import I2C_LCD_driver_24
import I2C_LCD_driver_27
ard_addr = bytearray([0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c])
dev_name = ['14', '15', '16', '17', '18', '19', '1a', '1b', '1c']
number = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0])
check_in = [0, 0, 0, 0, 0, 0, 0, 0, 0]
lcd_24_screen_format = [ [1,0,1,2,1,3], [2,0,2,2,2,3], [3,0,3,2,3,3], [4,0,4,2,4,3], [1,5,1,7,1,8], [2,5,2,7,2,8],
[3,5,3,7,3,8], [4,5,4,7,4,8], [1,10,1,12,1,13] ]
bus = SMBus(1) # indicates /dev/ic2-1
lcd_24 = I2C_LCD_driver_24.lcd()
lcd_27 = I2C_LCD_driver_27.lcd()
cnt = 0
check_in_answer = ""
while True:
lcd_24.lcd_clear()
while cnt < 9:
check_in[cnt] = bus.read_byte(ard_addr[cnt])
print("Arduino", dev_name[cnt], "reporting in", check_in[cnt])
bus.write_byte(ard_addr[cnt], 0x1) # switch it on
time.sleep(2)
bus.write_byte(ard_addr[cnt], 0x0) # switch it on
str_addr = "{}".format(dev_name[cnt])
str_check_in = "{}".format(check_in[cnt])
lcd_24.lcd_display_string(str_addr, lcd_24_screen_format[cnt][0], lcd_24_screen_format[cnt][1])
lcd_24.lcd_display_string(": ", lcd_24_screen_format[cnt][2], lcd_24_screen_format[cnt][3])
if (str_check_in == "1"):
check_in_answer = "Y"
elif (str_check_in == "0"):
check_in_answer = "N"
lcd_24.lcd_display_string(check_in_answer, lcd_24_screen_format[cnt][4], lcd_24_screen_format[cnt][5])
cnt +=1
time.sleep(3)
cnt = 0
print(" ")