0

python code:

import smbus
import time

bus = smbus.SMBus(1)

SLAVE_ADDRESS = 0x10

def requestreading():
    # try:
    #
    #     b1 = data[0]
    #     b2 = data[1]
    #     b3 = data[2]
    #     b4 = data[3]
    #     b5 = data[4]
    #     b6 = data[5]
    #     b7 = data[6]
    #
    # except IOError:
    #     b1 = 0
    #     b2 = 0
    #     b3 = 0
    #     b4 = 0
    #     b5 = 0
    #     b6 = 0
    #     b7 = 0
    data = bus.read_i2c_block_data((SLAVE_ADDRESS), 0, 12)
    print(data)

while True:
    #var = input("Press any key for reading: ")
    requestreading()

arduino code:

#include <Wire.h>            // library needed for communication with other device (our case raspberry pi)
#define SLAVE_ADDRESS 0x10   // slave address how rapberry pi will know where to send requests

bool Pause;
// Hospital Laod Specific
// pins being used for loads, placed here for easy pin changing
int Floor1 = 2;       //High Priority
int Floor2 = 3;       //Low Priority
int Floor3 = 5;       //Low Priority
int Floor4 = 7;       //High Priority
//int HVAC   = 9;       //High Priority???
int ACunit = 9;       //High Priority
int Heater = 12;      //Low Priority
const byte interruptPin = 13;        //This is the pin from switchingArduino that tells hosital to get in/out of blackout mode
// Matrix showing what is turned on in intervals of 30 sec real time/ 1 hr simulation time
byte ArrayFloor1 [] =   {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
byte ArrayFloor2 [] =   {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0};
byte ArrayFloor3 [] =   {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
byte ArrayFloor4 [] =   {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
byte ArrayHVAC   [] =   {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
byte ArrayACunit   [] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
byte ArrayHeater   [] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
// Timer Variables
long interval = 1000;               // interval waiting  in milliseconds (Actual time = 30 sec; Sim time = 1 hour)
int hour = 0;                       // keeps track of what hour it is
int oldHour = 0;                    // for testing
unsigned long previousMillis = 0;
//Mode Variables
int HospitalMode = 0;// hospital mode, info comes from switching arduino using digital pin input, 0=normal, 1=blackout
char Season = 'S';// Season mode for HVAC system; info comes from pi using serial input, S=summer, W=winter

// Metering Junk
// pins being used for metering, placed here for easy pin changing
int MeterF1 = A1;//Analog input pin of Floor 1
int MeterF2 = A2;//Analog input pin of Floor 2
int MeterF3 = A3;//Analog input pin of Floor 3
int MeterF4 = A5;//Analog input pin of Floor 4
//int MeterHVAC = A6;//Analog input pin of 'HVAC' system
int MeterHeater = A6;//Analog input pin of Heater
int MeterAC = A7;//Analog input pin of Fan
int MeterHigh = A0;//Analog input of pin on high side voltage all floor meters connected to
//GlobalVariables
float meterResistance = 100.0;// metering resistor value in ohms
float meterResistanceHVAC = 25.0;// metering resistor value in ohms
//Voltage
float VoltF1;// Voltage (V) of Floor 1
float VoltF2;// Voltage (V) of Floor 2
float VoltF3;// Voltage (V) of Floor 3
float VoltF4;// Voltage (V) of Floor 4
//float VoltHVAC;// Voltage (V) of HVAC;***********Only need one resistor right where HVAC system splits to heater/fan since only one ever on at a time?
float VoltH;// Voltage (V) of Heating Element
float VoltAC;// Voltage (V) of Fan element
float VoltHigh;// voltage of hospital bus
//Current
float IF1;// Current (A) of Floor 1
float IF2;// Current (A) of Floor 2
float IF3;// Current (A) of Floor 3
float IF4;// Current (A) of Floor 4
//float IHVAC;// Current (A) of Floor HVAC System
float IH;// Current (A) of Heating Element
float IAC;// Current (A) of Fan element
//Power
float PF1;// Power (W) of Floor 1
float PF2;// Power (W) of Floor 2
float PF3;// Power (W) of Floor 3
float PF4;// Power (W) of Floor 4
float PH;// Power (W) of Heating System
float PAC;// Power (W) of AC System
/////////////////////////////////////////////////////////////////////////////////
int number = 0;// used to choose what data will be sent
int vdata = 0;// data to be sent
int bdata = 0;// blackout
byte tarray [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};//tim on/off
/////////////////////////////////////////////////////////////////////////////////



void setup() {
  Serial.begin(9600);             // Allows us to use the serial monitor in upper right
  ////////////// code added for communication with pi ///////////////////////////
  Wire.begin(SLAVE_ADDRESS);      // initialize i2c slave
  // define callback for i2c communication
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);
  ////////////////////////////////////////////////////////////////////////////////
  pinMode (Floor1, OUTPUT);       // Output to Floor 1
  pinMode (Floor2, OUTPUT);       // Output to Floor 2
  pinMode (Floor3, OUTPUT);       // Output to Floor 3
  pinMode (Floor4, OUTPUT);       // Output to Floor 4
  //pinMode (HVAC, OUTPUT);         // Output to HVAC
  pinMode (ACunit, OUTPUT);       // Output to fan
  pinMode (Heater, OUTPUT);       // Output to heater
  pinMode (MeterF1, INPUT);       // Input Voltage Metering for Floor 1
  pinMode (MeterF2, INPUT);       // Input Voltage Metering for Floor 2
  pinMode (MeterF3, INPUT);       // Input Voltage Metering for Floor 3
  pinMode (MeterF4, INPUT);       // Input Voltage Metering for Floor 4
//  pinMode (MeterHVAC, INPUT);     // Input Voltage Metering for HVAC
  pinMode (interruptPin, INPUT_PULLUP);             // input for if in blackout mode. This should hopefully be from the arduino upstream keeping track of where we are getting power
  //attachInterrupt(digitalPinToInterrupt(interruptPin), mode, CHANGE);
  HospitalMode = digitalRead(3);
}


void recvSeason() {
 //https://forum.arduino.cc/index.php?topic=288234.0
 if (Serial.available() > 0) {
  if (Serial.read() == 'S' || Serial.read() == 'W')
    Season = Serial.read();
  if (Serial.read() == 'P' & Pause == 0)
    Pause = 0;
  if (Serial.read() == 'P' & Pause == 1)
    Pause = 0;
 }
}

void wait() {
  // check to see if it's time to change 'hour'
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    // reset timer
    previousMillis = currentMillis;
    // rotate hour once
    if (Pause == 1)
    {
      //hour = hour;// if paused keep hour the same
        if (hour >= 23)
        hour = 0;
      else
        {
          oldHour=hour; //For testing
          hour = hour + 1;
        }
    }
    else // otherwise keep the clock moving
    {
        if (hour >= 23)
        hour = 0;
      else
        {
          oldHour=hour; //For testing
          hour = hour + 1;
        }
    }
    Serial.print("Hour:");//For testing
    Serial.println(hour); //For testing
  }
}

void NormalOp() {
  // output what is scheduled from arrays
  recvSeason();
  digitalWrite(Floor1, ArrayFloor1[hour]);
  //digitalWrite(Floor2, 1);
  digitalWrite(Floor2, ArrayFloor2[hour]);
  digitalWrite(Floor3, ArrayFloor3[hour]);
  digitalWrite(Floor4, ArrayFloor4[hour]);
//  digitalWrite(HVAC, ArrayHVAC[hour]);
  // Season mode for HVAC system; info comes from pi using serial input, S=summer, W=winter
  if (Season == 'S')
  {
  digitalWrite(ACunit, ArrayACunit[hour]);
  digitalWrite(Heater, ArrayHeater[hour]);
      }
  else //if (Season =='W')
    {
  digitalWrite(ACunit, ArrayACunit[hour]);
  digitalWrite(Heater, ArrayHeater[hour]);
      }
    //  Serial.println("NORMAL OP:");//For testing
  wait();
}

void BlackoutOp() {

  recvSeason();
  //immediately shut off low priority loads
  digitalWrite(Floor2, ArrayFloor2[hour]);
  digitalWrite(Floor3, 0);
  //continue output for high priority
  digitalWrite(Floor1, ArrayFloor1[hour]);
  //digitalWrite(Floor2, ArrayFloor1[hour]);
  digitalWrite(Floor4, ArrayFloor4[hour]);
  // Season mode for HVAC system; info comes from pi using serial input, S=summer, W=winter
  if (Season == 'S')
  {
  digitalWrite(ACunit, ArrayACunit[hour]);
  digitalWrite(Heater, ArrayHeater[hour]);
      }
  else //if (Season =='W')
    {
  digitalWrite(ACunit, ArrayACunit[hour]);
  digitalWrite(Heater, ArrayHeater[hour]);
      }
      Serial.println("BLACKOUT:");//For testing
  wait();

}

void mode(){
  //The interupt pin changed, assign Hospital Mode to whatever the current input of pin 3
  //(our connection to the switchinng arduino) is. Reminder=> 1=blackout;0=normal
  HospitalMode = digitalRead(3);
  //for Testing
  Serial.println("Hospital Mode has Changed!");
}


void getVoltage(){
  // voltage = (reading across meter resistor*maxvoltage/steps)
  VoltHigh = (analogRead(MeterHigh)*5.0/1024.0);
  VoltF1 = (analogRead(MeterF1)*5.0/1024.0);
  VoltF2 = (analogRead(MeterF2)*5.0/1024.0);
  VoltF3 = (analogRead(MeterF3)*5.0/1024.0);
  VoltF4 = (analogRead(MeterF4)*5.0/1024.0);
      //VoltHVAC = (analogRead(MeterHVAC)*5.0/1024.0);
  VoltH = (analogRead(MeterHeater)*5.0/1024.0);
  VoltAC = (analogRead(MeterAC)*5.0/1024.0);
  // current = (voltage across meter)/ resistance
  IF1 = -(VoltF1 - VoltHigh)/meterResistance;
  IF2 = -(VoltF2 - VoltHigh)/meterResistance;
  IF3 = -(VoltF3 - VoltHigh)/meterResistance;
  IF4 = -(VoltF4 - VoltHigh)/meterResistance;
      //IHVAC = -(VoltHVAC - VoltHigh)/meterResistance;
  IH =  -(VoltAC - VoltHigh)/meterResistance;
  IAC = -(VoltH - VoltHigh)/meterResistance;
  // power = current*voltage (DC so only real power)
  PF1 = IF1*VoltF1;
  PF2 = IF2*VoltF2;
  PF3 = IF3*VoltF3;
  PF4 = IF4*VoltF4;
      //PHVAC = IHVAC*VoltF1;
  PH = IH*VoltH;
  PAC = IAC*VoltAC;
  //send to pi here?
    Serial.print("Floor 1 consumes ");
    Serial.print(VoltF1);
    Serial.print(" V, ");
    Serial.print(IF1*1000);
    Serial.print(" mA, & ");
    Serial.print(PF1*1000);
    Serial.println(" mW.");

    Serial.print("Floor 2 consumes ");
    Serial.print(VoltF2);
    Serial.print(" V, ");
    Serial.print(IF2*1000);
    Serial.print(" Amps, & ");
    Serial.print(PF2*1000);
    Serial.println(" mW.");

    Serial.print("Floor 3 consumes ");
    Serial.print(VoltF3);
    Serial.print(" V, ");
    Serial.print(IF3*1000);
    Serial.print(" mA, & ");
    Serial.print(PF3*1000);
    Serial.println(" mW.");

    Serial.print("Floor 4 consumes ");
    Serial.print(VoltF4);
    Serial.print(" V, ");
    Serial.print(IF4*1000);
    Serial.print(" mA, & ");
    Serial.print(PF4*1000);
    Serial.println(" mW.");

    /*Serial.print("HVAC is consuming ");
    Serial.print(VoltHVAC);
    Serial.print(" V, ");
    Serial.print(IHVAC*1000);
    Serial.print(" mA, & ");
    Serial.print(PHVAC*1000);
    Serial.println(" mW.");*/
}
////////////////// functions for retrieving data //////////////////////////////////
void receiveData(int howMany) {
    while (Wire.available()) {
      number = Wire.read();
    }
    if(number == 1) {
      vdata = PF1 + PF2 + PF3 + PF4 + PH + PAC;
    }
    else if(number == 2) {
      vdata = VoltF1;
    }
    else if(number == 3) {
      vdata = VoltF2;
    }
    else if(number == 4) {
      vdata = VoltF3;
    }
    else if(number == 5) {
      vdata = VoltF4;
    }
    else if(number == 6) {
      //vdata = VoltHVAC;
    }
    else if(number == 7) {
      for(int i=0; i<24; i++){
        tarray[i] = ArrayHVAC[i];
      }
    }
    else if(number == 8) {
      for(int i=0; i<24; i++){
        tarray[i] = ArrayFloor1[i];
      }
    }
    else if(number == 9) {
      for(int i=0; i<24; i++){
        tarray[i] = ArrayFloor2[i];
      }
    }
    else if(number == 10) {
      for(int i=0; i<24; i++){
        tarray[i] = ArrayFloor3[i];
      }
    }
    else if(number == 11) {
      for(int i=0; i<24; i++){
        tarray[i] = ArrayFloor4[i];
      }
    }
    else if(number == 12) {
      bdata = HospitalMode;
    }
}

void sendData() {
    if(number >= 1 && number <= 6) {
      Wire.write(vdata);
    }
    else if(number >= 7 && number <= 11) {
      Wire.write(tarray[hour]);
    }
    else if(number == 12) {
      Wire.write(bdata);
    }
}

//////////////////////////////////////////////////////////////////////////////////

void loop() {
  // No Blackout
  Pause = 0;
  while (HospitalMode == 0) {
    NormalOp();
  }
  //Blackout
  while (HospitalMode == 1) {
    BlackoutOp();
  }
}
  • Your Arduino code is a bit complicated. Is it similar to the following? Arduino and Raspberry Pi Working Together (Part 2): Now With I2C dzone.com/articles/… – tlfong01 Apr 13 at 1:02
  • Whenever you work with I2C, make sure you have pull-up resistors on SDA and SCL. Neither the Pi nor most Arduino boards have them (nor should they). – JayEye Apr 13 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.