Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <EmonLib.h>
- #include <XBee.h>
- EnergyMonitor emon1;
- EnergyMonitor emon2;
- LiquidCrystal lcd(11, 10, 9, 8, 7, 6);
- XBee xbee = XBee();
- // we are going to send 10 floats of 4 bytes each
- // realPower1, apparentPower1, Vrms1, Irms1, powerFactor1, realPower2, apparentPower2, Vrms2, Irms2, powerFactor2
- uint8_t payload[40] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- // union to convert float to byte string
- union u_tag {
- uint8_t b[4];
- float fval;
- } u;
- // SH + SL Address of receiving XBee
- XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x40BADA55);
- ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
- ZBTxStatusResponse txStatus = ZBTxStatusResponse();
- void setup() {
- Serial.begin(9600);
- xbee.setSerial(Serial);
- lcd.begin(16, 2);
- lcd.print("Power Monitor");
- pinMode(A0, OUTPUT);
- analogWrite(A0, 125);
- pinMode(A1, INPUT);
- pinMode(A2, INPUT);
- pinMode(A3, INPUT);
- emon1.current(A1, 60.6); // Current: input pin, calibration.
- emon1.voltage(A2, 130.0, 1.7); // Voltage: input pin, calibration, phase_shift
- emon2.current(A3, 60.6); // Current: input pin, calibration.
- emon2.voltage(A2, 130.0, 1.7); // Voltage: input pin, calibration, phase_shift
- delay(100);
- }
- void loop() {
- digitalWrite(LED_BUILTIN, HIGH);
- emon1.calcVI(20, 2000); // Calculate all. No.of wavelengths, time-out
- //emon1.serialprint();
- emon2.calcVI(20, 2000); // Calculate all. No.of wavelengths, time-out
- //emon2.serialprint();
- double realPower = emon1.realPower + emon2.realPower;
- double apparentPower = emon1.apparentPower + emon2.apparentPower;
- double powerFactor = (emon1.powerFactor + emon2.powerFactor) / 2;
- double supplyVoltage = (emon1.Vrms + emon2.Vrms) / 2;
- double irms = emon1.Irms + emon2.Irms;
- digitalWrite(LED_BUILTIN, LOW);
- // display on LCD
- String msg1 = String(realPower) + "W " + String(supplyVoltage) + "V";
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(msg1);
- //String msg2 = String(irms) + "A " + String(powerFactor) + "PF";
- //lcd.setCursor(0, 1);
- //lcd.print(msg2);
- // prepare payload to send
- addToPayload(emon1.realPower, 0);
- addToPayload(emon1.apparentPower, 4);
- addToPayload(emon1.Vrms, 8);
- addToPayload(emon1.Irms, 12);
- addToPayload(emon1.powerFactor, 16);
- addToPayload(emon2.realPower, 20);
- addToPayload(emon2.apparentPower, 24);
- addToPayload(emon2.Vrms, 28);
- addToPayload(emon2.Irms, 32);
- addToPayload(emon2.powerFactor, 36);
- // transmit with xbee
- xbee.send(zbTx);
- String msg2 = String(getFloatFromData(32)) + " " + String(sizeof(payload));
- lcd.setCursor(0, 1);
- lcd.print(msg2);
- // following is to check the return packet that should be sent
- // after sending a tx request, we expect a status response
- // wait up to half second for the status response
- if (xbee.readPacket(500)) {
- // got a response!
- // should be a znet tx status
- if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
- xbee.getResponse().getZBTxStatusResponse(txStatus);
- // get the delivery status, the fifth byte
- if (txStatus.getDeliveryStatus() == SUCCESS) {
- // success. time to celebrate
- }
- else {
- // the remote XBee did not receive our packet. is it powered on?
- }
- }
- }
- else if (xbee.getResponse().isError()) {
- //nss.print("Error reading packet. Error code: ");
- //nss.println(xbee.getResponse().getErrorCode());
- }
- else {
- // local XBee did not provide a timely TX Status Response -- should not happen
- }
- }
- float checkFloat(float f) {
- if (!isnan(f) && !isinf(f) && isfinite(f)) return f;
- else return 0.0;
- }
- void addToPayload(float val, int pos) {
- u.fval = checkFloat(val);
- for (int i = pos; i < pos + 4; i++){
- payload[i] = u.b[i - pos];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement