Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "floatToString.h"
- #include "EmonLib.h" // Include Emon Library
- #include <DHT.h>
- #define DHTPIN A0 // what pin we're connected to
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- EnergyMonitor emon1; // Create an instance
- EnergyMonitor emon2; // Create an instance
- EnergyMonitor emon3; // Create an instance
- DHT dht(DHTPIN, DHTTYPE);
- int switchPins[] = {7, 6, 5, 4, 3, 2};
- int switchStatus = 0;
- char StatusText[11];
- char x, tmp[5], hmd[5], o[5], m[5];
- char n[5];
- char cc[15];
- char th[11];
- char sn[26];
- void setup()
- {
- emon1.current(1, 111.1); // Current: input pin, calibration.
- emon2.current(2, 111.1); // Current: input pin, calibration.
- emon3.current(3, 111.1); // Current: input pin, calibration.
- Serial.begin(9600);
- int i=0;
- for(i=0; i<6; i++)
- {
- pinMode(switchPins[i], OUTPUT);
- }
- Wire.begin(4); // join i2c bus with address #4
- Wire.onReceive(receiveEvent); // register event
- Wire.onRequest(requestEvent); // register event
- dht.begin();
- }
- void loop()
- {
- getSensorValue();
- delay(100);
- }
- // function that executes whenever data is received from master
- // this function is registered as an event, see setup()
- void receiveEvent(int howMany)
- {
- x = Wire.read();
- if(x=='1')
- {
- switching(switchPins[0]);
- }
- else if(x=='2')
- {
- switching(switchPins[1]);
- }
- else if(x=='3')
- {
- switching(switchPins[2]);
- }
- else if(x=='4')
- {
- switching(switchPins[3]);
- }
- else if(x=='5')
- {
- switching(switchPins[4]);
- }
- else if(x=='6')
- {
- switching(switchPins[5]);
- }
- }
- void requestEvent()
- {
- if(x=='c')
- {
- Wire.write(cc); // respond with message of 6
- }
- else if(x=='n')
- {
- Wire.write(sn); // respond with message of 6
- }
- else
- {
- checkingStatus(6);
- Wire.write(StatusText); // respond with message of 6
- }
- }
- void switching(int switchPin)
- {
- switchStatus = digitalRead(switchPin); // Read Check Status Pin
- if(switchStatus==HIGH){ digitalWrite(switchPin, LOW); } else{ digitalWrite(switchPin, HIGH); }
- }
- void checkingStatus(int numberofSwitch)
- {
- int i=0;
- for(i=0; i<numberofSwitch; i++)
- {
- switchStatus = digitalRead(switchPins[i]); // Read Check Status Pin
- if(switchStatus==HIGH)
- {
- StatusText[i]='1';
- }
- else
- {
- StatusText[i]='0';
- }
- }
- }
- void getSensorValue()
- {
- sn[0]='\0';
- getCurrent();
- getDHT();
- strncat(sn,th,11);
- strncat(sn,"|",26);
- strncat(sn,cc,26);
- }
- void getDHT()
- {
- th[0]='\0';
- float t = dht.readTemperature();
- float h = dht.readHumidity();
- floatToString(tmp,t,2);
- strncat(th,tmp,20);
- strncat(th,"|",20);
- floatToString(hmd,h,2);
- strncat(th,hmd,20);
- }
- void getCurrent()
- {
- cc[0]='\0'; o[0]='\0'; m[0]='\0'; n[0]='\0';
- float IrmsR = emon1.calcIrms(1480); // Calculate Irms only
- float IrmsS = emon2.calcIrms(1480); // Calculate Irms only
- float IrmsT = emon3.calcIrms(1480); // Calculate Irms only
- floatToString(o,IrmsR,2);
- floatToString(m,IrmsS,2);
- floatToString(n,IrmsT,2);
- strncat(cc,o,20);
- strncat(cc,"|",20);
- strncat(cc,m,20);
- strncat(cc,"|",20);
- strncat(cc,n,20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement