Measures the output power from a boiler system and transmits it to open energy monitor system
Dependencies: mbed RFM69 MSF_Time ds3231 TextLCD
main.cpp@0:17c0192cb3d7, 2022-01-07 (annotated)
- Committer:
- dswood
- Date:
- Fri Jan 07 12:23:44 2022 +0000
- Revision:
- 0:17c0192cb3d7
Working but still a number of errors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dswood | 0:17c0192cb3d7 | 1 | // Solid Fuel Energy monitor |
dswood | 0:17c0192cb3d7 | 2 | /* Monitor water temperature and flow |
dswood | 0:17c0192cb3d7 | 3 | The energy flow is proportional to the flow of water multiplied by |
dswood | 0:17c0192cb3d7 | 4 | the temperature difference between in and out. Energy flow per second will |
dswood | 0:17c0192cb3d7 | 5 | give you output power. |
dswood | 0:17c0192cb3d7 | 6 | */ |
dswood | 0:17c0192cb3d7 | 7 | #include "mbed.h" |
dswood | 0:17c0192cb3d7 | 8 | #include "TextLCD.h" |
dswood | 0:17c0192cb3d7 | 9 | #include "MSF_Time.h" |
dswood | 0:17c0192cb3d7 | 10 | #include "RFM69.h" |
dswood | 0:17c0192cb3d7 | 11 | #include "bme280.h" |
dswood | 0:17c0192cb3d7 | 12 | #include "ds3231.h" |
dswood | 0:17c0192cb3d7 | 13 | #include "string" |
dswood | 0:17c0192cb3d7 | 14 | char Version[]="Version 1.01"; |
dswood | 0:17c0192cb3d7 | 15 | Serial Mypc(USBTX, USBRX, 115200); //for debugging |
dswood | 0:17c0192cb3d7 | 16 | |
dswood | 0:17c0192cb3d7 | 17 | #define RF_freq RF69_433MHZ |
dswood | 0:17c0192cb3d7 | 18 | #define Col 20 //display cols |
dswood | 0:17c0192cb3d7 | 19 | #define Row 4 // display rows |
dswood | 0:17c0192cb3d7 | 20 | TextLCD lcd(D10, D9, D6, D5, D4, D3, TextLCD::LCD20x4 ); // rs, e, d4-d7,Display type |
dswood | 0:17c0192cb3d7 | 21 | //DigitalOut led1(LED1); // LED pin used for spi interface - it flashes on each access |
dswood | 0:17c0192cb3d7 | 22 | #define My_scl D15 |
dswood | 0:17c0192cb3d7 | 23 | #define My_sda D14 |
dswood | 0:17c0192cb3d7 | 24 | Ds3231 rtc(My_sda, My_scl); |
dswood | 0:17c0192cb3d7 | 25 | MSF_Time MSF(D2,0); |
dswood | 0:17c0192cb3d7 | 26 | //AnalogIn adc_temp(ADC_TEMP); |
dswood | 0:17c0192cb3d7 | 27 | //AnalogIn adc_vref(ADC_VREF); |
dswood | 0:17c0192cb3d7 | 28 | //AnalogIn adc_vbat(ADC_VBAT); |
dswood | 0:17c0192cb3d7 | 29 | AnalogIn adc_Water_Temp(A0); |
dswood | 0:17c0192cb3d7 | 30 | AnalogIn adc_Water_Diff(A1); |
dswood | 0:17c0192cb3d7 | 31 | AnalogIn adc_Room_Temp(A2); |
dswood | 0:17c0192cb3d7 | 32 | //the size of the arrays holding the measured data |
dswood | 0:17c0192cb3d7 | 33 | // TM is ten minutes worth of data |
dswood | 0:17c0192cb3d7 | 34 | // H hourly data, D day, M Month, Y for year. |
dswood | 0:17c0192cb3d7 | 35 | const int SizeOfTM=13,SizeOfH=14,SizeOfD=14,SizeOfW=7,SizeOfM=12,SizeOfY=12; |
dswood | 0:17c0192cb3d7 | 36 | uint64_t TenMins[SizeOfTM],Hour[SizeOfH],Day[SizeOfD],Week[SizeOfW],Month[SizeOfM],Year[SizeOfY]; |
dswood | 0:17c0192cb3d7 | 37 | //The memory location is in the store and read functions but an offset is needed |
dswood | 0:17c0192cb3d7 | 38 | //so each array is sored in a unique location. |
dswood | 0:17c0192cb3d7 | 39 | //Each element of data is 64 bit or 2 words or 8 bytes. So 8 offsets per item. Stored as bytes. |
dswood | 0:17c0192cb3d7 | 40 | const int MemOffsetTM=0; |
dswood | 0:17c0192cb3d7 | 41 | const int MemOffsetH=SizeOfTM*8; |
dswood | 0:17c0192cb3d7 | 42 | const int MemOffsetD=MemOffsetH+(SizeOfH*8); |
dswood | 0:17c0192cb3d7 | 43 | const int MemOffsetW=MemOffsetD+(SizeOfD*8); |
dswood | 0:17c0192cb3d7 | 44 | const int MemOffsetM=MemOffsetW+(SizeOfW*8); |
dswood | 0:17c0192cb3d7 | 45 | const int MemOffsetY=MemOffsetM+(SizeOfM*8); |
dswood | 0:17c0192cb3d7 | 46 | struct TimeRecord { |
dswood | 0:17c0192cb3d7 | 47 | int TM; // ten mins and the only reason I did not just use tm struct |
dswood | 0:17c0192cb3d7 | 48 | int H; // hour |
dswood | 0:17c0192cb3d7 | 49 | int D; // day (week is day divided by seven) |
dswood | 0:17c0192cb3d7 | 50 | int M; // month |
dswood | 0:17c0192cb3d7 | 51 | int Y; // year |
dswood | 0:17c0192cb3d7 | 52 | }; |
dswood | 0:17c0192cb3d7 | 53 | struct TimeRecord Current; |
dswood | 0:17c0192cb3d7 | 54 | time_t MySeconds; |
dswood | 0:17c0192cb3d7 | 55 | InterruptIn Flow_meter(D7); |
dswood | 0:17c0192cb3d7 | 56 | unsigned int Flow_duration=0xffff; |
dswood | 0:17c0192cb3d7 | 57 | Timer Flow_timer; // Measure time between flow pulses |
dswood | 0:17c0192cb3d7 | 58 | Timer Time_Data_timer; // Measure the length of data pulses |
dswood | 0:17c0192cb3d7 | 59 | const float Flow_Detect_Time=10.0; // must be shorter than timer overflow 30s |
dswood | 0:17c0192cb3d7 | 60 | const float Display_time=1.0; //used to calc flow too |
dswood | 0:17c0192cb3d7 | 61 | float TempS,PressureS,HumidityS,LongAvePressureS,ShortAvePressureS,AvePressureS; |
dswood | 0:17c0192cb3d7 | 62 | float Flow,Power,Water_Temp,Room_Temp; |
dswood | 0:17c0192cb3d7 | 63 | uint64_t TotalEnergyJ=0,LastEnergy=0; |
dswood | 0:17c0192cb3d7 | 64 | int Press; |
dswood | 0:17c0192cb3d7 | 65 | float Temp_Diff=0,Temperatur,Pressure,Humidity; |
dswood | 0:17c0192cb3d7 | 66 | bool Flow_Detected=false; |
dswood | 0:17c0192cb3d7 | 67 | Ticker No_Flow_Detect; |
dswood | 0:17c0192cb3d7 | 68 | Ticker Display_Ticker; |
dswood | 0:17c0192cb3d7 | 69 | void Flow_Stopped(void); |
dswood | 0:17c0192cb3d7 | 70 | float Calc_Flow(int fd); |
dswood | 0:17c0192cb3d7 | 71 | float Calc_Power(float Fl, float Td); |
dswood | 0:17c0192cb3d7 | 72 | char Timebuff[20]; |
dswood | 0:17c0192cb3d7 | 73 | char MyBuffer[80]; |
dswood | 0:17c0192cb3d7 | 74 | bool IsTimeSet=false,RTCValid=false; |
dswood | 0:17c0192cb3d7 | 75 | RFM69 MyRadio(D11,D12,D13,PC_5,PC_6); |
dswood | 0:17c0192cb3d7 | 76 | bool Sensing=false; |
dswood | 0:17c0192cb3d7 | 77 | const int nodeID_emonpwr = 7; |
dswood | 0:17c0192cb3d7 | 78 | const int nodeID_emonenv = 19; // emonTx RFM12B node ID |
dswood | 0:17c0192cb3d7 | 79 | const int networkGroup = 210; |
dswood | 0:17c0192cb3d7 | 80 | const int PulsesPerLitre=288; |
dswood | 0:17c0192cb3d7 | 81 | |
dswood | 0:17c0192cb3d7 | 82 | I2C SensorI2C(My_sda,My_scl); //I2C sensor bme280 |
dswood | 0:17c0192cb3d7 | 83 | struct bme280_dev MySensor; |
dswood | 0:17c0192cb3d7 | 84 | int8_t rslt = BME280_OK; |
dswood | 0:17c0192cb3d7 | 85 | struct bme280_data SensorData; |
dswood | 0:17c0192cb3d7 | 86 | uint8_t SensorSettings; |
dswood | 0:17c0192cb3d7 | 87 | int8_t SensorSetup(); |
dswood | 0:17c0192cb3d7 | 88 | int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len); |
dswood | 0:17c0192cb3d7 | 89 | int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len); |
dswood | 0:17c0192cb3d7 | 90 | int8_t SensorReadAll(); |
dswood | 0:17c0192cb3d7 | 91 | void user_delay_ms(uint32_t period); |
dswood | 0:17c0192cb3d7 | 92 | |
dswood | 0:17c0192cb3d7 | 93 | //DigitalOut GreenLED(LED1); |
dswood | 0:17c0192cb3d7 | 94 | struct tm * NowTime; |
dswood | 0:17c0192cb3d7 | 95 | typedef struct { |
dswood | 0:17c0192cb3d7 | 96 | int16_t Power,Flow, TempOut,TempDiff; |
dswood | 0:17c0192cb3d7 | 97 | } PowerPayloadTX; // create structure - a neat way of packaging data for RF comms |
dswood | 0:17c0192cb3d7 | 98 | PowerPayloadTX emonPowerTX; |
dswood | 0:17c0192cb3d7 | 99 | typedef struct { |
dswood | 0:17c0192cb3d7 | 100 | int16_t temperature, pressure, humidity; |
dswood | 0:17c0192cb3d7 | 101 | } EnviromentPayloadTX; |
dswood | 0:17c0192cb3d7 | 102 | EnviromentPayloadTX emonEnvTX; |
dswood | 0:17c0192cb3d7 | 103 | void send_power_rf_data(int ToAddress, int FromAddress); |
dswood | 0:17c0192cb3d7 | 104 | bool Flag_send_power_rf_data=false; |
dswood | 0:17c0192cb3d7 | 105 | void send_enviro_rf_data(int ToAddress, int FromAddress); |
dswood | 0:17c0192cb3d7 | 106 | InterruptIn PageButton(PC_9); |
dswood | 0:17c0192cb3d7 | 107 | Timeout DebounceTimer; |
dswood | 0:17c0192cb3d7 | 108 | int PageNumber=0; |
dswood | 0:17c0192cb3d7 | 109 | const int MaxPages=14; |
dswood | 0:17c0192cb3d7 | 110 | bool Debouncing(); |
dswood | 0:17c0192cb3d7 | 111 | bool DeBounce=false; |
dswood | 0:17c0192cb3d7 | 112 | void PageControl(); |
dswood | 0:17c0192cb3d7 | 113 | void Timed_out(); |
dswood | 0:17c0192cb3d7 | 114 | void Page1(); |
dswood | 0:17c0192cb3d7 | 115 | void Page2(); |
dswood | 0:17c0192cb3d7 | 116 | void Page3(); |
dswood | 0:17c0192cb3d7 | 117 | void Page4(); |
dswood | 0:17c0192cb3d7 | 118 | void Page5(); |
dswood | 0:17c0192cb3d7 | 119 | void Page6(); |
dswood | 0:17c0192cb3d7 | 120 | void Page7(); |
dswood | 0:17c0192cb3d7 | 121 | void Page8(); |
dswood | 0:17c0192cb3d7 | 122 | void Page9(); |
dswood | 0:17c0192cb3d7 | 123 | void Page10(); |
dswood | 0:17c0192cb3d7 | 124 | void Page11(); |
dswood | 0:17c0192cb3d7 | 125 | void Page12(); |
dswood | 0:17c0192cb3d7 | 126 | void Page13(); |
dswood | 0:17c0192cb3d7 | 127 | void Page14(); |
dswood | 0:17c0192cb3d7 | 128 | |
dswood | 0:17c0192cb3d7 | 129 | void Rotate(uint64_t Arr[], int size); |
dswood | 0:17c0192cb3d7 | 130 | bool Set_RTC_Time(time_t epoch_time); |
dswood | 0:17c0192cb3d7 | 131 | void FillTimeRecord(struct TimeRecord *TR); |
dswood | 0:17c0192cb3d7 | 132 | bool FlagWriteDataToEEPROM=false; |
dswood | 0:17c0192cb3d7 | 133 | |
dswood | 0:17c0192cb3d7 | 134 | HAL_StatusTypeDef writeEEPROMByte(uint32_t address, uint8_t data); |
dswood | 0:17c0192cb3d7 | 135 | uint8_t readEEPROMByte(uint32_t address) ; |
dswood | 0:17c0192cb3d7 | 136 | void ConvertJouleTokWh(uint64_t Joule[], float kWh[], int size); |
dswood | 0:17c0192cb3d7 | 137 | void ConvertJouleToKiloJoule(uint64_t Watts[], float kW[], int size); |
dswood | 0:17c0192cb3d7 | 138 | void WriteDataToEEPROM(); |
dswood | 0:17c0192cb3d7 | 139 | void ReadDataFromEEPROM(); |
dswood | 0:17c0192cb3d7 | 140 | void WriteDataToEEPROM(uint64_t Target[], int size, uint32_t address); |
dswood | 0:17c0192cb3d7 | 141 | void ReadDataFromEEPROM(uint64_t Target[], int size, uint32_t address); |
dswood | 0:17c0192cb3d7 | 142 | struct ScreenSt { |
dswood | 0:17c0192cb3d7 | 143 | char Memory[Col+1][Row+1]; |
dswood | 0:17c0192cb3d7 | 144 | int PersistTime[Row+1]; |
dswood | 0:17c0192cb3d7 | 145 | Ticker PersistTicker; |
dswood | 0:17c0192cb3d7 | 146 | }; |
dswood | 0:17c0192cb3d7 | 147 | bool DisplayChanged=false; |
dswood | 0:17c0192cb3d7 | 148 | ScreenSt ScreenMemory; |
dswood | 0:17c0192cb3d7 | 149 | void DisplayRefresh(); |
dswood | 0:17c0192cb3d7 | 150 | void PersistDelay(); |
dswood | 0:17c0192cb3d7 | 151 | void WriteToScreenMemory(char Buff[],int x,int y,int p=0); |
dswood | 0:17c0192cb3d7 | 152 | void ClearScreen(); |
dswood | 0:17c0192cb3d7 | 153 | |
dswood | 0:17c0192cb3d7 | 154 | int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) |
dswood | 0:17c0192cb3d7 | 155 | { |
dswood | 0:17c0192cb3d7 | 156 | int i; |
dswood | 0:17c0192cb3d7 | 157 | char addr[1],Buff[len]; |
dswood | 0:17c0192cb3d7 | 158 | addr[0]= (char)reg_addr; |
dswood | 0:17c0192cb3d7 | 159 | //MySerial.printf("Read ID %d: addr %d: len %d\n\r",dev_id,reg_addr,len); |
dswood | 0:17c0192cb3d7 | 160 | int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ |
dswood | 0:17c0192cb3d7 | 161 | |
dswood | 0:17c0192cb3d7 | 162 | |
dswood | 0:17c0192cb3d7 | 163 | /* |
dswood | 0:17c0192cb3d7 | 164 | * The parameter dev_id can be used as a variable to store the I2C address of the device |
dswood | 0:17c0192cb3d7 | 165 | */ |
dswood | 0:17c0192cb3d7 | 166 | |
dswood | 0:17c0192cb3d7 | 167 | /*SensorI2C |
dswood | 0:17c0192cb3d7 | 168 | * Data on the bus should be like |
dswood | 0:17c0192cb3d7 | 169 | * |------------+---------------------| |
dswood | 0:17c0192cb3d7 | 170 | * | I2C action | Data | |
dswood | 0:17c0192cb3d7 | 171 | * |------------+---------------------| |
dswood | 0:17c0192cb3d7 | 172 | * | Start | - | |
dswood | 0:17c0192cb3d7 | 173 | * | Write | (reg_addr) | |
dswood | 0:17c0192cb3d7 | 174 | * | Stop | - | |
dswood | 0:17c0192cb3d7 | 175 | * | Start | - | |
dswood | 0:17c0192cb3d7 | 176 | * | Read | (reg_data[0]) | |
dswood | 0:17c0192cb3d7 | 177 | * | Read | (....) | |
dswood | 0:17c0192cb3d7 | 178 | * | Read | (reg_data[len - 1]) | |
dswood | 0:17c0192cb3d7 | 179 | * | Stop | - | |
dswood | 0:17c0192cb3d7 | 180 | * |------------+---------------------| |
dswood | 0:17c0192cb3d7 | 181 | */ |
dswood | 0:17c0192cb3d7 | 182 | |
dswood | 0:17c0192cb3d7 | 183 | rslt=SensorI2C.write(dev_id,addr,1,true); |
dswood | 0:17c0192cb3d7 | 184 | rslt+=SensorI2C.read(dev_id,Buff,len); |
dswood | 0:17c0192cb3d7 | 185 | for (i=0; i<len; i++) reg_data[i]=(uint8_t)Buff[i]; |
dswood | 0:17c0192cb3d7 | 186 | return rslt; |
dswood | 0:17c0192cb3d7 | 187 | } |
dswood | 0:17c0192cb3d7 | 188 | int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) |
dswood | 0:17c0192cb3d7 | 189 | { |
dswood | 0:17c0192cb3d7 | 190 | |
dswood | 0:17c0192cb3d7 | 191 | int i; |
dswood | 0:17c0192cb3d7 | 192 | char addr[1],Buff[len]; |
dswood | 0:17c0192cb3d7 | 193 | addr[0]= (char)reg_addr; |
dswood | 0:17c0192cb3d7 | 194 | for (i=0; i<len; i++) Buff[i]=(char)reg_data[i]; |
dswood | 0:17c0192cb3d7 | 195 | int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ |
dswood | 0:17c0192cb3d7 | 196 | //MySerial.printf("Write ID %d: addr %d: len %d\n\r",dev_id,reg_addr,len); |
dswood | 0:17c0192cb3d7 | 197 | /* |
dswood | 0:17c0192cb3d7 | 198 | * The parameter dev_id can be used as a variable to store the I2C address of the device |
dswood | 0:17c0192cb3d7 | 199 | */ |
dswood | 0:17c0192cb3d7 | 200 | |
dswood | 0:17c0192cb3d7 | 201 | /* |
dswood | 0:17c0192cb3d7 | 202 | * Data on the bus should be like |
dswood | 0:17c0192cb3d7 | 203 | * |------------+---------------------| |
dswood | 0:17c0192cb3d7 | 204 | * | I2C action | Data | |
dswood | 0:17c0192cb3d7 | 205 | * |------------+---------------------| |
dswood | 0:17c0192cb3d7 | 206 | * | Start | - | |
dswood | 0:17c0192cb3d7 | 207 | * | Write | (reg_addr) | |
dswood | 0:17c0192cb3d7 | 208 | * | Write | (reg_data[0]) | |
dswood | 0:17c0192cb3d7 | 209 | * | Write | (....) | |
dswood | 0:17c0192cb3d7 | 210 | * | Write | (reg_data[len - 1]) | |
dswood | 0:17c0192cb3d7 | 211 | * | Stop | - | |
dswood | 0:17c0192cb3d7 | 212 | * |------------+---------------------| |
dswood | 0:17c0192cb3d7 | 213 | */ |
dswood | 0:17c0192cb3d7 | 214 | rslt=SensorI2C.write(dev_id,addr,1,true); |
dswood | 0:17c0192cb3d7 | 215 | rslt+=SensorI2C.write(dev_id,Buff,len); |
dswood | 0:17c0192cb3d7 | 216 | return rslt; |
dswood | 0:17c0192cb3d7 | 217 | } |
dswood | 0:17c0192cb3d7 | 218 | |
dswood | 0:17c0192cb3d7 | 219 | int8_t SensorSetup() |
dswood | 0:17c0192cb3d7 | 220 | { |
dswood | 0:17c0192cb3d7 | 221 | uint8_t settings_sel; |
dswood | 0:17c0192cb3d7 | 222 | int8_t rslt; |
dswood | 0:17c0192cb3d7 | 223 | MySensor.dev_id = BME280_I2C_ADDR_PRIM<<1; |
dswood | 0:17c0192cb3d7 | 224 | MySensor.intf = BME280_I2C_INTF; |
dswood | 0:17c0192cb3d7 | 225 | MySensor.read = user_i2c_read; |
dswood | 0:17c0192cb3d7 | 226 | MySensor.write = user_i2c_write; |
dswood | 0:17c0192cb3d7 | 227 | MySensor.delay_ms = user_delay_ms; |
dswood | 0:17c0192cb3d7 | 228 | rslt = bme280_init(&MySensor); |
dswood | 0:17c0192cb3d7 | 229 | /* Recommended mode of operation: Indoor navigation */ |
dswood | 0:17c0192cb3d7 | 230 | MySensor.settings.osr_h = BME280_OVERSAMPLING_1X; |
dswood | 0:17c0192cb3d7 | 231 | MySensor.settings.osr_p = BME280_OVERSAMPLING_16X; |
dswood | 0:17c0192cb3d7 | 232 | MySensor.settings.osr_t = BME280_OVERSAMPLING_2X; |
dswood | 0:17c0192cb3d7 | 233 | MySensor.settings.filter = BME280_FILTER_COEFF_16; |
dswood | 0:17c0192cb3d7 | 234 | MySensor.settings.standby_time = BME280_STANDBY_TIME_62_5_MS; |
dswood | 0:17c0192cb3d7 | 235 | settings_sel = BME280_OSR_PRESS_SEL; |
dswood | 0:17c0192cb3d7 | 236 | settings_sel |= BME280_OSR_TEMP_SEL; |
dswood | 0:17c0192cb3d7 | 237 | settings_sel |= BME280_OSR_HUM_SEL; |
dswood | 0:17c0192cb3d7 | 238 | settings_sel |= BME280_STANDBY_SEL; |
dswood | 0:17c0192cb3d7 | 239 | settings_sel |= BME280_FILTER_SEL; |
dswood | 0:17c0192cb3d7 | 240 | rslt = bme280_set_sensor_settings(settings_sel, &MySensor); |
dswood | 0:17c0192cb3d7 | 241 | //Mypc.printf("result %d\n\r",rslt); |
dswood | 0:17c0192cb3d7 | 242 | rslt += bme280_set_sensor_mode(BME280_NORMAL_MODE, &MySensor); |
dswood | 0:17c0192cb3d7 | 243 | //Mypc.printf("result %d\n\r",rslt); |
dswood | 0:17c0192cb3d7 | 244 | MySensor.delay_ms(70); // Should be ready after this. |
dswood | 0:17c0192cb3d7 | 245 | return rslt; |
dswood | 0:17c0192cb3d7 | 246 | } |
dswood | 0:17c0192cb3d7 | 247 | int8_t SensorReadAll() |
dswood | 0:17c0192cb3d7 | 248 | { |
dswood | 0:17c0192cb3d7 | 249 | int8_t rslt; |
dswood | 0:17c0192cb3d7 | 250 | //MySerial.printf("SensorRealAll\n\r"); |
dswood | 0:17c0192cb3d7 | 251 | rslt = bme280_get_sensor_data(BME280_ALL, &SensorData, &MySensor); |
dswood | 0:17c0192cb3d7 | 252 | //MySerial.printf("result %d\n\r",rslt); |
dswood | 0:17c0192cb3d7 | 253 | return rslt; |
dswood | 0:17c0192cb3d7 | 254 | } |
dswood | 0:17c0192cb3d7 | 255 | void user_delay_ms(uint32_t period) |
dswood | 0:17c0192cb3d7 | 256 | { |
dswood | 0:17c0192cb3d7 | 257 | /* |
dswood | 0:17c0192cb3d7 | 258 | * Return control or wait, |
dswood | 0:17c0192cb3d7 | 259 | * for a period amount of milliseconds |
dswood | 0:17c0192cb3d7 | 260 | */ |
dswood | 0:17c0192cb3d7 | 261 | wait_ms(period); |
dswood | 0:17c0192cb3d7 | 262 | } |
dswood | 0:17c0192cb3d7 | 263 | |
dswood | 0:17c0192cb3d7 | 264 | |
dswood | 0:17c0192cb3d7 | 265 | void PersistDelay() |
dswood | 0:17c0192cb3d7 | 266 | { |
dswood | 0:17c0192cb3d7 | 267 | int i; |
dswood | 0:17c0192cb3d7 | 268 | for (i=0 ; i<Row; i++)if (ScreenMemory.PersistTime[i]>0)ScreenMemory.PersistTime[i]--; |
dswood | 0:17c0192cb3d7 | 269 | } |
dswood | 0:17c0192cb3d7 | 270 | |
dswood | 0:17c0192cb3d7 | 271 | void WriteToScreenMemory(char Buff[],int x,int y,int p) |
dswood | 0:17c0192cb3d7 | 272 | { |
dswood | 0:17c0192cb3d7 | 273 | if (strlen( Buff)==0)return; |
dswood | 0:17c0192cb3d7 | 274 | if (x>Col-1) return; |
dswood | 0:17c0192cb3d7 | 275 | if (y>Row-1)return; |
dswood | 0:17c0192cb3d7 | 276 | int i; |
dswood | 0:17c0192cb3d7 | 277 | for (i=0; i< strlen( Buff); i++) { |
dswood | 0:17c0192cb3d7 | 278 | if ((ScreenMemory.PersistTime[y]==0)||(p>0))ScreenMemory.Memory[x][y]=Buff[i]; |
dswood | 0:17c0192cb3d7 | 279 | if (x<Col-1) x++; |
dswood | 0:17c0192cb3d7 | 280 | else { |
dswood | 0:17c0192cb3d7 | 281 | y++; |
dswood | 0:17c0192cb3d7 | 282 | x=0; |
dswood | 0:17c0192cb3d7 | 283 | } |
dswood | 0:17c0192cb3d7 | 284 | if (p>0) ScreenMemory.PersistTime[y]=p; |
dswood | 0:17c0192cb3d7 | 285 | if (y>Row) return; |
dswood | 0:17c0192cb3d7 | 286 | } |
dswood | 0:17c0192cb3d7 | 287 | DisplayChanged=true; |
dswood | 0:17c0192cb3d7 | 288 | } |
dswood | 0:17c0192cb3d7 | 289 | |
dswood | 0:17c0192cb3d7 | 290 | void DisplayRefresh() |
dswood | 0:17c0192cb3d7 | 291 | { |
dswood | 0:17c0192cb3d7 | 292 | DisplayChanged=false; |
dswood | 0:17c0192cb3d7 | 293 | lcd.locate(0,0); |
dswood | 0:17c0192cb3d7 | 294 | int x,y; |
dswood | 0:17c0192cb3d7 | 295 | for (y=0; y<Row; y++) { |
dswood | 0:17c0192cb3d7 | 296 | for (x=0; x<Col; x++) { |
dswood | 0:17c0192cb3d7 | 297 | lcd.putc(ScreenMemory.Memory[x][y]); |
dswood | 0:17c0192cb3d7 | 298 | } |
dswood | 0:17c0192cb3d7 | 299 | } |
dswood | 0:17c0192cb3d7 | 300 | } |
dswood | 0:17c0192cb3d7 | 301 | |
dswood | 0:17c0192cb3d7 | 302 | void PageControl() |
dswood | 0:17c0192cb3d7 | 303 | { |
dswood | 0:17c0192cb3d7 | 304 | Press++; |
dswood | 0:17c0192cb3d7 | 305 | if (Debouncing()) return; // not the first press within the bounce period |
dswood | 0:17c0192cb3d7 | 306 | PageNumber++; |
dswood | 0:17c0192cb3d7 | 307 | if (PageNumber>(MaxPages-1)) PageNumber=0; |
dswood | 0:17c0192cb3d7 | 308 | ClearScreen(); |
dswood | 0:17c0192cb3d7 | 309 | } |
dswood | 0:17c0192cb3d7 | 310 | void ClearScreen() |
dswood | 0:17c0192cb3d7 | 311 | { |
dswood | 0:17c0192cb3d7 | 312 | sprintf(MyBuffer," "); |
dswood | 0:17c0192cb3d7 | 313 | //12345678901234567890 |
dswood | 0:17c0192cb3d7 | 314 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 315 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 316 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 317 | WriteToScreenMemory(MyBuffer,0,3); //worlds most inelegant clear screen |
dswood | 0:17c0192cb3d7 | 318 | } |
dswood | 0:17c0192cb3d7 | 319 | |
dswood | 0:17c0192cb3d7 | 320 | void Timed_out() |
dswood | 0:17c0192cb3d7 | 321 | { |
dswood | 0:17c0192cb3d7 | 322 | DeBounce=false;// debouncing time is over |
dswood | 0:17c0192cb3d7 | 323 | } |
dswood | 0:17c0192cb3d7 | 324 | |
dswood | 0:17c0192cb3d7 | 325 | bool Debouncing() |
dswood | 0:17c0192cb3d7 | 326 | { |
dswood | 0:17c0192cb3d7 | 327 | if (DeBounce) return true; |
dswood | 0:17c0192cb3d7 | 328 | DeBounce=true; |
dswood | 0:17c0192cb3d7 | 329 | DebounceTimer.attach_us(&Timed_out,250000); //250ms period of debounce |
dswood | 0:17c0192cb3d7 | 330 | return false; |
dswood | 0:17c0192cb3d7 | 331 | } |
dswood | 0:17c0192cb3d7 | 332 | |
dswood | 0:17c0192cb3d7 | 333 | |
dswood | 0:17c0192cb3d7 | 334 | void Flow_pulse() |
dswood | 0:17c0192cb3d7 | 335 | { |
dswood | 0:17c0192cb3d7 | 336 | float Energy; |
dswood | 0:17c0192cb3d7 | 337 | if (Flow_Detected) { //Flow was detected before |
dswood | 0:17c0192cb3d7 | 338 | Flow_duration=Flow_timer.read_us(); //How long ago was that? |
dswood | 0:17c0192cb3d7 | 339 | } |
dswood | 0:17c0192cb3d7 | 340 | Flow_timer.reset(); |
dswood | 0:17c0192cb3d7 | 341 | No_Flow_Detect.attach(&Flow_Stopped, Flow_Detect_Time);//Reset our watchdog timer |
dswood | 0:17c0192cb3d7 | 342 | Flow_Detected=true; |
dswood | 0:17c0192cb3d7 | 343 | Energy=165.0*adc_Water_Diff.read()*4026.4/PulsesPerLitre; // 4.0264 kJ/l @ 50C Constant volume |
dswood | 0:17c0192cb3d7 | 344 | TotalEnergyJ+=(uint64_t)Energy; |
dswood | 0:17c0192cb3d7 | 345 | |
dswood | 0:17c0192cb3d7 | 346 | } |
dswood | 0:17c0192cb3d7 | 347 | void Display()// Display and read the temps and calc power,energy |
dswood | 0:17c0192cb3d7 | 348 | { |
dswood | 0:17c0192cb3d7 | 349 | //Mypc.printf("Starting display\n\r"); |
dswood | 0:17c0192cb3d7 | 350 | uint64_t TempEnergy; |
dswood | 0:17c0192cb3d7 | 351 | unsigned int Energy; |
dswood | 0:17c0192cb3d7 | 352 | struct TimeRecord temp; |
dswood | 0:17c0192cb3d7 | 353 | Temp_Diff=165*adc_Water_Diff.read(); //20mV per C |
dswood | 0:17c0192cb3d7 | 354 | Water_Temp=165*adc_Water_Temp.read(); //20mV per C |
dswood | 0:17c0192cb3d7 | 355 | Room_Temp=330*adc_Room_Temp.read(); //10mV per C |
dswood | 0:17c0192cb3d7 | 356 | TempEnergy=TotalEnergyJ-LastEnergy; //Using a TempEnergy to force conversion to 32 bit after the subtraction |
dswood | 0:17c0192cb3d7 | 357 | //Energy=1234;//Fixme delete me |
dswood | 0:17c0192cb3d7 | 358 | LastEnergy=TotalEnergyJ; |
dswood | 0:17c0192cb3d7 | 359 | Energy=TempEnergy; |
dswood | 0:17c0192cb3d7 | 360 | |
dswood | 0:17c0192cb3d7 | 361 | |
dswood | 0:17c0192cb3d7 | 362 | if (Flow_Detected) { |
dswood | 0:17c0192cb3d7 | 363 | Flow=Calc_Flow(Flow_duration); |
dswood | 0:17c0192cb3d7 | 364 | Power=Energy/Display_time; // Power in watts equals energy per second |
dswood | 0:17c0192cb3d7 | 365 | } else { |
dswood | 0:17c0192cb3d7 | 366 | Flow=0; |
dswood | 0:17c0192cb3d7 | 367 | Power=0; |
dswood | 0:17c0192cb3d7 | 368 | } |
dswood | 0:17c0192cb3d7 | 369 | emonPowerTX.Power=Power; |
dswood | 0:17c0192cb3d7 | 370 | emonPowerTX.Flow=Flow*60; //flow in litres per minute --per second is always small and rounded to 0 |
dswood | 0:17c0192cb3d7 | 371 | emonPowerTX.TempOut=Water_Temp*10; |
dswood | 0:17c0192cb3d7 | 372 | emonPowerTX.TempDiff=Temp_Diff*10; |
dswood | 0:17c0192cb3d7 | 373 | Flag_send_power_rf_data=true; |
dswood | 0:17c0192cb3d7 | 374 | MySeconds=time(null); |
dswood | 0:17c0192cb3d7 | 375 | //SizeOfTM=13,SizeOfH=14,SizeOfD=14,SizeOfW=7,SizeOfM=12,SizeOfY=12; |
dswood | 0:17c0192cb3d7 | 376 | if (RTCValid) { |
dswood | 0:17c0192cb3d7 | 377 | FillTimeRecord(&temp); |
dswood | 0:17c0192cb3d7 | 378 | //Mypc.printf("%dm %dh %dd %dw %dm %dy\n\r",temp.TM,temp.H,temp.D,temp.D/7,temp.M,temp.Y); |
dswood | 0:17c0192cb3d7 | 379 | if (int(Current.TM/10)!=int(temp.TM/10)) { |
dswood | 0:17c0192cb3d7 | 380 | //Mypc.printf("Ten min %d %d\n\r",Current.TM,temp.TM); |
dswood | 0:17c0192cb3d7 | 381 | Current.TM=temp.TM; |
dswood | 0:17c0192cb3d7 | 382 | Rotate(TenMins,SizeOfTM); |
dswood | 0:17c0192cb3d7 | 383 | if ((Current.TM==30) || (Current.TM==0)) { |
dswood | 0:17c0192cb3d7 | 384 | FlagWriteDataToEEPROM=true; |
dswood | 0:17c0192cb3d7 | 385 | Mypc.printf("stored data\n\r"); |
dswood | 0:17c0192cb3d7 | 386 | } |
dswood | 0:17c0192cb3d7 | 387 | if (Current.H!=temp.H) { |
dswood | 0:17c0192cb3d7 | 388 | //Mypc.printf("Hour %d %d\n\r",Current.H,temp.H); |
dswood | 0:17c0192cb3d7 | 389 | Rotate(Hour,SizeOfH); |
dswood | 0:17c0192cb3d7 | 390 | Current.H=temp.H; |
dswood | 0:17c0192cb3d7 | 391 | if (Current.D!=temp.D) { |
dswood | 0:17c0192cb3d7 | 392 | //Mypc.printf("Day %d %d\n\r",Current.D,temp.D); |
dswood | 0:17c0192cb3d7 | 393 | Rotate(Day,SizeOfD); |
dswood | 0:17c0192cb3d7 | 394 | if (int(Current.D/7)!=int(temp.D/7)) { |
dswood | 0:17c0192cb3d7 | 395 | //Mypc.printf("Week %d %d\n\r",Current.D/7,temp.D/7); |
dswood | 0:17c0192cb3d7 | 396 | Rotate(Week,SizeOfW); |
dswood | 0:17c0192cb3d7 | 397 | } |
dswood | 0:17c0192cb3d7 | 398 | Current.D=temp.D; |
dswood | 0:17c0192cb3d7 | 399 | if (Current.M!=temp.M) { |
dswood | 0:17c0192cb3d7 | 400 | //Mypc.printf("Month %d %d\n\r",Current.M,temp.M); |
dswood | 0:17c0192cb3d7 | 401 | Current.M=temp.M; |
dswood | 0:17c0192cb3d7 | 402 | Rotate(Month,SizeOfM); |
dswood | 0:17c0192cb3d7 | 403 | if (Current.Y!=temp.Y) { |
dswood | 0:17c0192cb3d7 | 404 | //Mypc.printf("Year %d %d\n\r",Current.Y,temp.Y); |
dswood | 0:17c0192cb3d7 | 405 | Current.Y=temp.Y; |
dswood | 0:17c0192cb3d7 | 406 | Rotate(Year,SizeOfY); |
dswood | 0:17c0192cb3d7 | 407 | } |
dswood | 0:17c0192cb3d7 | 408 | } |
dswood | 0:17c0192cb3d7 | 409 | } |
dswood | 0:17c0192cb3d7 | 410 | } |
dswood | 0:17c0192cb3d7 | 411 | } |
dswood | 0:17c0192cb3d7 | 412 | } |
dswood | 0:17c0192cb3d7 | 413 | TenMins[0]+=Energy; |
dswood | 0:17c0192cb3d7 | 414 | Hour[0]+=Energy; |
dswood | 0:17c0192cb3d7 | 415 | Day[0]+=Energy; |
dswood | 0:17c0192cb3d7 | 416 | Week[0]+=Energy; |
dswood | 0:17c0192cb3d7 | 417 | Month[0]+=Energy; |
dswood | 0:17c0192cb3d7 | 418 | Year[0]+=Energy; |
dswood | 0:17c0192cb3d7 | 419 | switch (PageNumber) { |
dswood | 0:17c0192cb3d7 | 420 | case 0: |
dswood | 0:17c0192cb3d7 | 421 | Page1(); |
dswood | 0:17c0192cb3d7 | 422 | break; |
dswood | 0:17c0192cb3d7 | 423 | case 1: |
dswood | 0:17c0192cb3d7 | 424 | Page2(); |
dswood | 0:17c0192cb3d7 | 425 | break; |
dswood | 0:17c0192cb3d7 | 426 | case 2: |
dswood | 0:17c0192cb3d7 | 427 | Page3(); |
dswood | 0:17c0192cb3d7 | 428 | break; |
dswood | 0:17c0192cb3d7 | 429 | case 3: |
dswood | 0:17c0192cb3d7 | 430 | Page4(); |
dswood | 0:17c0192cb3d7 | 431 | break; |
dswood | 0:17c0192cb3d7 | 432 | case 4: |
dswood | 0:17c0192cb3d7 | 433 | Page5(); |
dswood | 0:17c0192cb3d7 | 434 | break; |
dswood | 0:17c0192cb3d7 | 435 | case 5: |
dswood | 0:17c0192cb3d7 | 436 | Page6(); |
dswood | 0:17c0192cb3d7 | 437 | break; |
dswood | 0:17c0192cb3d7 | 438 | case 6: |
dswood | 0:17c0192cb3d7 | 439 | Page7(); |
dswood | 0:17c0192cb3d7 | 440 | break; |
dswood | 0:17c0192cb3d7 | 441 | case 7: |
dswood | 0:17c0192cb3d7 | 442 | Page8(); |
dswood | 0:17c0192cb3d7 | 443 | break; |
dswood | 0:17c0192cb3d7 | 444 | case 8: |
dswood | 0:17c0192cb3d7 | 445 | Page9(); |
dswood | 0:17c0192cb3d7 | 446 | break; |
dswood | 0:17c0192cb3d7 | 447 | case 9: |
dswood | 0:17c0192cb3d7 | 448 | Page10(); |
dswood | 0:17c0192cb3d7 | 449 | break; |
dswood | 0:17c0192cb3d7 | 450 | case 10: |
dswood | 0:17c0192cb3d7 | 451 | Page11(); |
dswood | 0:17c0192cb3d7 | 452 | break; |
dswood | 0:17c0192cb3d7 | 453 | case 11: |
dswood | 0:17c0192cb3d7 | 454 | Page12(); |
dswood | 0:17c0192cb3d7 | 455 | break; |
dswood | 0:17c0192cb3d7 | 456 | case 12: |
dswood | 0:17c0192cb3d7 | 457 | Page13(); |
dswood | 0:17c0192cb3d7 | 458 | break; |
dswood | 0:17c0192cb3d7 | 459 | case 13: |
dswood | 0:17c0192cb3d7 | 460 | Page14(); |
dswood | 0:17c0192cb3d7 | 461 | break; |
dswood | 0:17c0192cb3d7 | 462 | } |
dswood | 0:17c0192cb3d7 | 463 | } |
dswood | 0:17c0192cb3d7 | 464 | void Page1() |
dswood | 0:17c0192cb3d7 | 465 | { |
dswood | 0:17c0192cb3d7 | 466 | |
dswood | 0:17c0192cb3d7 | 467 | sprintf(MyBuffer,"Flw=%3.2f Pwr=%4.0fW ",Flow*60,Power); |
dswood | 0:17c0192cb3d7 | 468 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 469 | sprintf(MyBuffer,"Water Temp=%3.1f%cC ", Water_Temp,223); |
dswood | 0:17c0192cb3d7 | 470 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 471 | sprintf(MyBuffer,"Water Diff=%3.2f%cC ", Temp_Diff,223); |
dswood | 0:17c0192cb3d7 | 472 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 473 | if (Sensing) sprintf(MyBuffer,"Rm Temp=%3.1f%cC %s", TempS,223,MSF.Valid()? "MSF":" "); |
dswood | 0:17c0192cb3d7 | 474 | else sprintf(MyBuffer,"Sensor Fail %s",MSF.Valid()? "MSF":" "); |
dswood | 0:17c0192cb3d7 | 475 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 476 | } |
dswood | 0:17c0192cb3d7 | 477 | void Page2() |
dswood | 0:17c0192cb3d7 | 478 | { |
dswood | 0:17c0192cb3d7 | 479 | strftime(Timebuff, 20, "%a %d %b %T", localtime(&MySeconds) ); |
dswood | 0:17c0192cb3d7 | 480 | if (Sensing) sprintf(MyBuffer,"Room=%2.1f%cC ",TempS,223); |
dswood | 0:17c0192cb3d7 | 481 | else sprintf(MyBuffer,"Sensor Fail "); |
dswood | 0:17c0192cb3d7 | 482 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 483 | sprintf(MyBuffer,"Humidity=%2.1f%% ",HumidityS); |
dswood | 0:17c0192cb3d7 | 484 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 485 | sprintf(MyBuffer,"Atm Pressure=%-4.1f ",PressureS); |
dswood | 0:17c0192cb3d7 | 486 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 487 | WriteToScreenMemory(Timebuff,0,3); |
dswood | 0:17c0192cb3d7 | 488 | } |
dswood | 0:17c0192cb3d7 | 489 | |
dswood | 0:17c0192cb3d7 | 490 | void Page3() |
dswood | 0:17c0192cb3d7 | 491 | { |
dswood | 0:17c0192cb3d7 | 492 | float kJ[SizeOfTM]; |
dswood | 0:17c0192cb3d7 | 493 | ConvertJouleToKiloJoule(TenMins,kJ,SizeOfTM); |
dswood | 0:17c0192cb3d7 | 494 | sprintf(MyBuffer,"Energy kJ in 10mins"); |
dswood | 0:17c0192cb3d7 | 495 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 496 | sprintf(MyBuffer,"E0=%-6.1f,E1=%-7.1f",kJ[0],kJ[1]); |
dswood | 0:17c0192cb3d7 | 497 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 498 | sprintf(MyBuffer,"E2=%-6.1f,E3=%-7.1f",kJ[2],kJ[3]); |
dswood | 0:17c0192cb3d7 | 499 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 500 | sprintf(MyBuffer,"E6=%-6.1f,E5=%-7.1f",kJ[4],kJ[5]); |
dswood | 0:17c0192cb3d7 | 501 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 502 | } |
dswood | 0:17c0192cb3d7 | 503 | void Page4() |
dswood | 0:17c0192cb3d7 | 504 | { |
dswood | 0:17c0192cb3d7 | 505 | float kJ[SizeOfTM]; |
dswood | 0:17c0192cb3d7 | 506 | ConvertJouleToKiloJoule(TenMins,kJ,SizeOfTM); |
dswood | 0:17c0192cb3d7 | 507 | sprintf(MyBuffer,"kJ/10min E6=%-7.1f",kJ[6]); |
dswood | 0:17c0192cb3d7 | 508 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 509 | sprintf(MyBuffer,"E7=%-6.1f,E8=%-7.1f",kJ[7],kJ[8]); |
dswood | 0:17c0192cb3d7 | 510 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 511 | sprintf(MyBuffer,"E9=%-6.1f,E10=%-6.0f",kJ[9],kJ[10]); |
dswood | 0:17c0192cb3d7 | 512 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 513 | sprintf(MyBuffer,"E11=%-5.0f,E12=%-5.0f",kJ[11],kJ[12]); |
dswood | 0:17c0192cb3d7 | 514 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 515 | } |
dswood | 0:17c0192cb3d7 | 516 | void Page5() |
dswood | 0:17c0192cb3d7 | 517 | { |
dswood | 0:17c0192cb3d7 | 518 | float kWh[SizeOfH]; |
dswood | 0:17c0192cb3d7 | 519 | ConvertJouleTokWh(Hour,kWh,SizeOfH); |
dswood | 0:17c0192cb3d7 | 520 | sprintf(MyBuffer,"kWh/Hr Hr0=%-4.2f",kWh[0]); |
dswood | 0:17c0192cb3d7 | 521 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 522 | sprintf(MyBuffer,"Hr1=%-5.2f,Hr2=%-5.2f",kWh[1],kWh[2]); |
dswood | 0:17c0192cb3d7 | 523 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 524 | sprintf(MyBuffer,"Hr3=%-5.2f,Hr4=%-5.2f",kWh[3],kWh[4]); |
dswood | 0:17c0192cb3d7 | 525 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 526 | sprintf(MyBuffer,"Hr5=%-5.2f,Hr6=%-5.2f",kWh[5],kWh[6]); |
dswood | 0:17c0192cb3d7 | 527 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 528 | } |
dswood | 0:17c0192cb3d7 | 529 | void Page6() |
dswood | 0:17c0192cb3d7 | 530 | { |
dswood | 0:17c0192cb3d7 | 531 | //SizeOfTM=13,SizeOfH=14,SizeOfD=14,SizeOfW=7,SizeOfM=12,SizeOfY=12; |
dswood | 0:17c0192cb3d7 | 532 | float kWh[SizeOfH]; |
dswood | 0:17c0192cb3d7 | 533 | ConvertJouleTokWh(Hour,kWh,SizeOfH); |
dswood | 0:17c0192cb3d7 | 534 | sprintf(MyBuffer,"Hr7=%-5.2f,Hr8=%-5.2f",kWh[7],kWh[8]); |
dswood | 0:17c0192cb3d7 | 535 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 536 | sprintf(MyBuffer,"Hr9=%-5.2f,Hr10=%-5.2f",kWh[9],kWh[10]); |
dswood | 0:17c0192cb3d7 | 537 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 538 | sprintf(MyBuffer,"Hr11=%-5.2f",kWh[11]); |
dswood | 0:17c0192cb3d7 | 539 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 540 | sprintf(MyBuffer,"Hr12=%-5.2f",kWh[12]); |
dswood | 0:17c0192cb3d7 | 541 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 542 | } |
dswood | 0:17c0192cb3d7 | 543 | void Page7() |
dswood | 0:17c0192cb3d7 | 544 | { |
dswood | 0:17c0192cb3d7 | 545 | float kWh[SizeOfD]; |
dswood | 0:17c0192cb3d7 | 546 | ConvertJouleTokWh(Day,kWh,SizeOfD); |
dswood | 0:17c0192cb3d7 | 547 | sprintf(MyBuffer,"kWh/Day Dy0=%-5.2f",kWh[0]); |
dswood | 0:17c0192cb3d7 | 548 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 549 | sprintf(MyBuffer,"Dy1=%-5.2f,Dy2=%-5.2f",kWh[1],kWh[2]); |
dswood | 0:17c0192cb3d7 | 550 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 551 | sprintf(MyBuffer,"Dy3=%-5.2f,Dy4=%-5.2f",kWh[3],kWh[4]); |
dswood | 0:17c0192cb3d7 | 552 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 553 | sprintf(MyBuffer,"Dy5=%-5.2f,Dy6=%-5.2f",kWh[5],kWh[6]); |
dswood | 0:17c0192cb3d7 | 554 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 555 | } |
dswood | 0:17c0192cb3d7 | 556 | void Page8() |
dswood | 0:17c0192cb3d7 | 557 | { |
dswood | 0:17c0192cb3d7 | 558 | float kWh[SizeOfD]; |
dswood | 0:17c0192cb3d7 | 559 | ConvertJouleTokWh(Day,kWh,SizeOfD); |
dswood | 0:17c0192cb3d7 | 560 | sprintf(MyBuffer,"kWh wk2 Dy0=%-5.2f",kWh[7]); |
dswood | 0:17c0192cb3d7 | 561 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 562 | sprintf(MyBuffer,"Dy1=%-5.2f,Dy2=%-5.2f",kWh[8],kWh[9]); |
dswood | 0:17c0192cb3d7 | 563 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 564 | sprintf(MyBuffer,"Dy3=%-5.2f,Dy4=%-5.2f",kWh[10],kWh[11]); |
dswood | 0:17c0192cb3d7 | 565 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 566 | sprintf(MyBuffer,"Dy5=%-5.2f,Dy6=%-5.2f",kWh[12],kWh[13]); |
dswood | 0:17c0192cb3d7 | 567 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 568 | } |
dswood | 0:17c0192cb3d7 | 569 | |
dswood | 0:17c0192cb3d7 | 570 | void Page9() |
dswood | 0:17c0192cb3d7 | 571 | { |
dswood | 0:17c0192cb3d7 | 572 | float kWh[SizeOfW]; |
dswood | 0:17c0192cb3d7 | 573 | ConvertJouleTokWh(Week,kWh,SizeOfW); |
dswood | 0:17c0192cb3d7 | 574 | sprintf(MyBuffer,"kWh/wk wk0=%-5.1f",kWh[0]); |
dswood | 0:17c0192cb3d7 | 575 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 576 | sprintf(MyBuffer,"wk1=%-5.1f,wk2=%-5.1f",kWh[1],kWh[2]); |
dswood | 0:17c0192cb3d7 | 577 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 578 | sprintf(MyBuffer,"wk3=%-5.1f,wk4=%-5.1f",kWh[3],kWh[4]); |
dswood | 0:17c0192cb3d7 | 579 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 580 | sprintf(MyBuffer,"wk5=%-5.1f,wk6=%-5.1f",kWh[5],kWh[6]); |
dswood | 0:17c0192cb3d7 | 581 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 582 | } |
dswood | 0:17c0192cb3d7 | 583 | void Page10() |
dswood | 0:17c0192cb3d7 | 584 | { |
dswood | 0:17c0192cb3d7 | 585 | float kWh[SizeOfM]; |
dswood | 0:17c0192cb3d7 | 586 | ConvertJouleTokWh(Month,kWh,SizeOfM); |
dswood | 0:17c0192cb3d7 | 587 | sprintf(MyBuffer,"kWh/Mth M0=%-5.0f",kWh[0]); |
dswood | 0:17c0192cb3d7 | 588 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 589 | sprintf(MyBuffer,"M1=%-5.0f,M2=%-5.0f",kWh[1],kWh[2]); |
dswood | 0:17c0192cb3d7 | 590 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 591 | sprintf(MyBuffer,"M3=%-5.0f,M4=%-5.0f",kWh[3],kWh[4]); |
dswood | 0:17c0192cb3d7 | 592 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 593 | sprintf(MyBuffer,"M5=%-5.0f,M6=%-5.0f",kWh[5],kWh[6]); |
dswood | 0:17c0192cb3d7 | 594 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 595 | } |
dswood | 0:17c0192cb3d7 | 596 | void Page11() |
dswood | 0:17c0192cb3d7 | 597 | { |
dswood | 0:17c0192cb3d7 | 598 | float kWh[SizeOfM]; |
dswood | 0:17c0192cb3d7 | 599 | ConvertJouleTokWh(Month,kWh,SizeOfM); |
dswood | 0:17c0192cb3d7 | 600 | sprintf(MyBuffer,"kWh/Mth M7=%-5.0f",kWh[7]); |
dswood | 0:17c0192cb3d7 | 601 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 602 | sprintf(MyBuffer,"M8=%-5.0f,M9=%-5.0f",kWh[8],kWh[9]); |
dswood | 0:17c0192cb3d7 | 603 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 604 | sprintf(MyBuffer,"kWh/M10=%-5.0f",kWh[10]); |
dswood | 0:17c0192cb3d7 | 605 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 606 | sprintf(MyBuffer,"kWh/M11=%-5.0f",kWh[11]); |
dswood | 0:17c0192cb3d7 | 607 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 608 | } |
dswood | 0:17c0192cb3d7 | 609 | void Page12() |
dswood | 0:17c0192cb3d7 | 610 | { |
dswood | 0:17c0192cb3d7 | 611 | float kWh[SizeOfY]; |
dswood | 0:17c0192cb3d7 | 612 | ConvertJouleTokWh(Year,kWh,SizeOfY); |
dswood | 0:17c0192cb3d7 | 613 | sprintf(MyBuffer,"kWh/Yr Y0=%-5.0f",kWh[0]); |
dswood | 0:17c0192cb3d7 | 614 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 615 | sprintf(MyBuffer,"Y1=%-5.0f,Y2=%-5.0f",kWh[1],kWh[2]); |
dswood | 0:17c0192cb3d7 | 616 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 617 | sprintf(MyBuffer,"Y3=%-5.0f,Y4=%-5.0f",kWh[3],kWh[4]); |
dswood | 0:17c0192cb3d7 | 618 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 619 | sprintf(MyBuffer,"Y5=%-5.0f,Y6=%-5.0f",kWh[5],kWh[6]); |
dswood | 0:17c0192cb3d7 | 620 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 621 | } |
dswood | 0:17c0192cb3d7 | 622 | void Page13() |
dswood | 0:17c0192cb3d7 | 623 | { |
dswood | 0:17c0192cb3d7 | 624 | float kWh[SizeOfY]; |
dswood | 0:17c0192cb3d7 | 625 | ConvertJouleTokWh(Year,kWh,SizeOfY); |
dswood | 0:17c0192cb3d7 | 626 | sprintf(MyBuffer,"kWh/Yr Y7=%-5.0f",kWh[7]); |
dswood | 0:17c0192cb3d7 | 627 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 628 | sprintf(MyBuffer,"Y8=%-5.0f,Y9=%-5.0f",kWh[8],kWh[9]); |
dswood | 0:17c0192cb3d7 | 629 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 630 | sprintf(MyBuffer,"Y10=%-5.0f",kWh[10]); |
dswood | 0:17c0192cb3d7 | 631 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 632 | sprintf(MyBuffer,"Y11=%-5.0f",kWh[11]); |
dswood | 0:17c0192cb3d7 | 633 | WriteToScreenMemory(MyBuffer,0,3); |
dswood | 0:17c0192cb3d7 | 634 | } |
dswood | 0:17c0192cb3d7 | 635 | void Page14() |
dswood | 0:17c0192cb3d7 | 636 | { |
dswood | 0:17c0192cb3d7 | 637 | strftime(Timebuff, 20, "%a %d %b %T", localtime(&MySeconds) ); |
dswood | 0:17c0192cb3d7 | 638 | sprintf(MyBuffer,"Atm Pressure=%f-4.1",PressureS); |
dswood | 0:17c0192cb3d7 | 639 | WriteToScreenMemory(MyBuffer,0,0); |
dswood | 0:17c0192cb3d7 | 640 | sprintf(MyBuffer,"Shrt term rise=%f3.3",AvePressureS-ShortAvePressureS); |
dswood | 0:17c0192cb3d7 | 641 | WriteToScreenMemory(MyBuffer,0,1); |
dswood | 0:17c0192cb3d7 | 642 | sprintf(MyBuffer,"Lng term rise=%f3.3",ShortAvePressureS-LongAvePressureS); |
dswood | 0:17c0192cb3d7 | 643 | WriteToScreenMemory(MyBuffer,0,2); |
dswood | 0:17c0192cb3d7 | 644 | //WriteToScreenMemory(Timebuff,0,3); |
dswood | 0:17c0192cb3d7 | 645 | |
dswood | 0:17c0192cb3d7 | 646 | WriteToScreenMemory(Version,0,3); |
dswood | 0:17c0192cb3d7 | 647 | } |
dswood | 0:17c0192cb3d7 | 648 | |
dswood | 0:17c0192cb3d7 | 649 | void Rotate(uint64_t Arr[],int size) |
dswood | 0:17c0192cb3d7 | 650 | { |
dswood | 0:17c0192cb3d7 | 651 | for (int i=size-1; i>0 ; i--) { |
dswood | 0:17c0192cb3d7 | 652 | Arr[i]=Arr[i-1]; |
dswood | 0:17c0192cb3d7 | 653 | } |
dswood | 0:17c0192cb3d7 | 654 | Arr[0]=0; |
dswood | 0:17c0192cb3d7 | 655 | } |
dswood | 0:17c0192cb3d7 | 656 | void Flow_Stopped() |
dswood | 0:17c0192cb3d7 | 657 | { |
dswood | 0:17c0192cb3d7 | 658 | No_Flow_Detect.detach(); |
dswood | 0:17c0192cb3d7 | 659 | Flow_Detected=false; |
dswood | 0:17c0192cb3d7 | 660 | } |
dswood | 0:17c0192cb3d7 | 661 | |
dswood | 0:17c0192cb3d7 | 662 | float Calc_Flow(int fd) |
dswood | 0:17c0192cb3d7 | 663 | { |
dswood | 0:17c0192cb3d7 | 664 | //Calculate flow from time in microseconds between pulses |
dswood | 0:17c0192cb3d7 | 665 | //288 pulses per litre |
dswood | 0:17c0192cb3d7 | 666 | if (fd==0) { //infinite flow WOW |
dswood | 0:17c0192cb3d7 | 667 | return 123456; |
dswood | 0:17c0192cb3d7 | 668 | } else { |
dswood | 0:17c0192cb3d7 | 669 | return (1000000/(fd*PulsesPerLitre*Display_time)); //Flow in litres per second |
dswood | 0:17c0192cb3d7 | 670 | } |
dswood | 0:17c0192cb3d7 | 671 | } |
dswood | 0:17c0192cb3d7 | 672 | |
dswood | 0:17c0192cb3d7 | 673 | void send_power_rf_data(int ToAddress, int FromAddress) |
dswood | 0:17c0192cb3d7 | 674 | { |
dswood | 0:17c0192cb3d7 | 675 | MyRadio.sleep(false); //wakeup |
dswood | 0:17c0192cb3d7 | 676 | MyRadio.setNodeID(FromAddress); |
dswood | 0:17c0192cb3d7 | 677 | int i = 0; |
dswood | 0:17c0192cb3d7 | 678 | while (!MyRadio.canSend() && i<10) { |
dswood | 0:17c0192cb3d7 | 679 | MyRadio.receiveDone(); |
dswood | 0:17c0192cb3d7 | 680 | i++; |
dswood | 0:17c0192cb3d7 | 681 | } |
dswood | 0:17c0192cb3d7 | 682 | MyRadio.send(ToAddress, &emonPowerTX, sizeof emonPowerTX); |
dswood | 0:17c0192cb3d7 | 683 | MyRadio.sleep(true); |
dswood | 0:17c0192cb3d7 | 684 | |
dswood | 0:17c0192cb3d7 | 685 | } |
dswood | 0:17c0192cb3d7 | 686 | |
dswood | 0:17c0192cb3d7 | 687 | void send_enviro_rf_data(int ToAddress, int FromAddress) |
dswood | 0:17c0192cb3d7 | 688 | { |
dswood | 0:17c0192cb3d7 | 689 | MyRadio.sleep(false); //wakeup |
dswood | 0:17c0192cb3d7 | 690 | MyRadio.setNodeID(FromAddress); |
dswood | 0:17c0192cb3d7 | 691 | int i = 0; |
dswood | 0:17c0192cb3d7 | 692 | while (!MyRadio.canSend() && i<10) { |
dswood | 0:17c0192cb3d7 | 693 | MyRadio.receiveDone(); |
dswood | 0:17c0192cb3d7 | 694 | i++; |
dswood | 0:17c0192cb3d7 | 695 | } |
dswood | 0:17c0192cb3d7 | 696 | MyRadio.send(ToAddress, &emonEnvTX, sizeof emonEnvTX); |
dswood | 0:17c0192cb3d7 | 697 | MyRadio.sleep(true); |
dswood | 0:17c0192cb3d7 | 698 | |
dswood | 0:17c0192cb3d7 | 699 | } |
dswood | 0:17c0192cb3d7 | 700 | bool Set_RTC_Time(time_t epoch_time) |
dswood | 0:17c0192cb3d7 | 701 | { |
dswood | 0:17c0192cb3d7 | 702 | //Taken from here https://os.mbed.com/users/brianclaus/code/ds3231/file/b87f3e7258bb/ds3231.cpp/ |
dswood | 0:17c0192cb3d7 | 703 | // Original code by Brian Claus |
dswood | 0:17c0192cb3d7 | 704 | // Many thanks for sharing. |
dswood | 0:17c0192cb3d7 | 705 | bool success = false; |
dswood | 0:17c0192cb3d7 | 706 | //system vars |
dswood | 0:17c0192cb3d7 | 707 | struct tm * sys_time; |
dswood | 0:17c0192cb3d7 | 708 | //RTC vars |
dswood | 0:17c0192cb3d7 | 709 | ds3231_time_t rtc_time = {0,0,0,0,0}; |
dswood | 0:17c0192cb3d7 | 710 | ds3231_calendar_t rtc_calendar = {0,0,0,0}; |
dswood | 0:17c0192cb3d7 | 711 | Mypc.printf("epoch set to-%d\n\r\n",epoch_time); |
dswood | 0:17c0192cb3d7 | 712 | sys_time = localtime(&epoch_time); |
dswood | 0:17c0192cb3d7 | 713 | set_time(epoch_time); |
dswood | 0:17c0192cb3d7 | 714 | //localtime comes back as 24hour |
dswood | 0:17c0192cb3d7 | 715 | |
dswood | 0:17c0192cb3d7 | 716 | rtc_time.mode = 0; |
dswood | 0:17c0192cb3d7 | 717 | rtc_time.hours = sys_time->tm_hour; |
dswood | 0:17c0192cb3d7 | 718 | rtc_time.minutes = sys_time->tm_min; |
dswood | 0:17c0192cb3d7 | 719 | rtc_time.seconds = sys_time->tm_sec; |
dswood | 0:17c0192cb3d7 | 720 | |
dswood | 0:17c0192cb3d7 | 721 | rtc_calendar.day = sys_time->tm_wday + 1; |
dswood | 0:17c0192cb3d7 | 722 | rtc_calendar.date = sys_time->tm_mday; |
dswood | 0:17c0192cb3d7 | 723 | rtc_calendar.month = sys_time->tm_mon + 1; |
dswood | 0:17c0192cb3d7 | 724 | rtc_calendar.year = sys_time->tm_year - 100; |
dswood | 0:17c0192cb3d7 | 725 | |
dswood | 0:17c0192cb3d7 | 726 | success = 0==rtc.set_calendar(rtc_calendar); |
dswood | 0:17c0192cb3d7 | 727 | success &= 0==rtc.set_time(rtc_time); |
dswood | 0:17c0192cb3d7 | 728 | wait_us(250); |
dswood | 0:17c0192cb3d7 | 729 | |
dswood | 0:17c0192cb3d7 | 730 | return success; |
dswood | 0:17c0192cb3d7 | 731 | } |
dswood | 0:17c0192cb3d7 | 732 | |
dswood | 0:17c0192cb3d7 | 733 | void FillTimeRecord(struct TimeRecord *TR) |
dswood | 0:17c0192cb3d7 | 734 | { |
dswood | 0:17c0192cb3d7 | 735 | |
dswood | 0:17c0192cb3d7 | 736 | time_t secs; |
dswood | 0:17c0192cb3d7 | 737 | struct tm *CT; //current time |
dswood | 0:17c0192cb3d7 | 738 | secs=time(null); |
dswood | 0:17c0192cb3d7 | 739 | CT=localtime(&secs); |
dswood | 0:17c0192cb3d7 | 740 | TR->TM=CT->tm_min; |
dswood | 0:17c0192cb3d7 | 741 | TR->H=CT->tm_hour; |
dswood | 0:17c0192cb3d7 | 742 | TR->D=CT->tm_yday; |
dswood | 0:17c0192cb3d7 | 743 | TR->M=CT->tm_mon; |
dswood | 0:17c0192cb3d7 | 744 | TR->Y=CT->tm_year; |
dswood | 0:17c0192cb3d7 | 745 | } |
dswood | 0:17c0192cb3d7 | 746 | void ConvertJouleTokWh(uint64_t Joule[], float kWh[], int size) |
dswood | 0:17c0192cb3d7 | 747 | { |
dswood | 0:17c0192cb3d7 | 748 | // Function to convert Joules to kWh |
dswood | 0:17c0192cb3d7 | 749 | // A lot of energy being transfered. Will this function get hot? |
dswood | 0:17c0192cb3d7 | 750 | float a; |
dswood | 0:17c0192cb3d7 | 751 | int i; |
dswood | 0:17c0192cb3d7 | 752 | for (i=0; i<size; i++) { |
dswood | 0:17c0192cb3d7 | 753 | a=Joule[i]; |
dswood | 0:17c0192cb3d7 | 754 | kWh[i]=a/3600000; |
dswood | 0:17c0192cb3d7 | 755 | } |
dswood | 0:17c0192cb3d7 | 756 | } |
dswood | 0:17c0192cb3d7 | 757 | void ConvertJouleToKiloJoule(uint64_t Joule[], float kJ[], int size) |
dswood | 0:17c0192cb3d7 | 758 | { |
dswood | 0:17c0192cb3d7 | 759 | float a; |
dswood | 0:17c0192cb3d7 | 760 | int i; |
dswood | 0:17c0192cb3d7 | 761 | for (i=0; i<size; i++) { |
dswood | 0:17c0192cb3d7 | 762 | a=Joule[i]; |
dswood | 0:17c0192cb3d7 | 763 | kJ[i]=a/1000; |
dswood | 0:17c0192cb3d7 | 764 | } |
dswood | 0:17c0192cb3d7 | 765 | } |
dswood | 0:17c0192cb3d7 | 766 | void ReadDataFromEEPROM(uint64_t Target[], int size, uint32_t address) |
dswood | 0:17c0192cb3d7 | 767 | { |
dswood | 0:17c0192cb3d7 | 768 | int count; |
dswood | 0:17c0192cb3d7 | 769 | int data; |
dswood | 0:17c0192cb3d7 | 770 | address = address + 0x08080000; |
dswood | 0:17c0192cb3d7 | 771 | data = *((uint32_t *)address);//high 4 bytes |
dswood | 0:17c0192cb3d7 | 772 | Target[0] = data; |
dswood | 0:17c0192cb3d7 | 773 | //Mypc.printf("Address %d high data %d\n\r",address,data); //fixme |
dswood | 0:17c0192cb3d7 | 774 | Target[0] = Target[0]<<32; |
dswood | 0:17c0192cb3d7 | 775 | address+=4; |
dswood | 0:17c0192cb3d7 | 776 | data = *((uint32_t *)address);//low 4 bytes |
dswood | 0:17c0192cb3d7 | 777 | //Mypc.printf("Address %d low data %d\n\r",address,data); //fixme |
dswood | 0:17c0192cb3d7 | 778 | Target[0] |= data; |
dswood | 0:17c0192cb3d7 | 779 | address+=4; |
dswood | 0:17c0192cb3d7 | 780 | for (count=1; count<size; count++) { |
dswood | 0:17c0192cb3d7 | 781 | data = *((uint32_t *)address); |
dswood | 0:17c0192cb3d7 | 782 | //Mypc.printf("Address %d high data %d\n\r",address,data); //fixme |
dswood | 0:17c0192cb3d7 | 783 | Target[count] =data; |
dswood | 0:17c0192cb3d7 | 784 | Target[count] = Target[count]<<32; |
dswood | 0:17c0192cb3d7 | 785 | address+=4; |
dswood | 0:17c0192cb3d7 | 786 | data = *((uint32_t *)address); |
dswood | 0:17c0192cb3d7 | 787 | Target[count] |= data; |
dswood | 0:17c0192cb3d7 | 788 | //Mypc.printf("Address %d low data %d\n\r",address,data); //fixme |
dswood | 0:17c0192cb3d7 | 789 | address+=4; |
dswood | 0:17c0192cb3d7 | 790 | } |
dswood | 0:17c0192cb3d7 | 791 | } |
dswood | 0:17c0192cb3d7 | 792 | void WriteDataToEEPROM(uint64_t Target[], int size, uint32_t address) |
dswood | 0:17c0192cb3d7 | 793 | { |
dswood | 0:17c0192cb3d7 | 794 | int count; |
dswood | 0:17c0192cb3d7 | 795 | uint32_t data; |
dswood | 0:17c0192cb3d7 | 796 | address = address + 0x08080000; |
dswood | 0:17c0192cb3d7 | 797 | HAL_FLASHEx_DATAEEPROM_Unlock(); |
dswood | 0:17c0192cb3d7 | 798 | data= 0xFFFFFFFF&(Target[0]>>32);//high |
dswood | 0:17c0192cb3d7 | 799 | // HAL_FLASHEx_DATAEEPROM_ProgramDoubleWord(address , Target[0]); |
dswood | 0:17c0192cb3d7 | 800 | //data=0x000000A0;//fixme |
dswood | 0:17c0192cb3d7 | 801 | HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, address, data); |
dswood | 0:17c0192cb3d7 | 802 | //Mypc.printf("Address %d high data %d\n\r",address,data); //fixme |
dswood | 0:17c0192cb3d7 | 803 | address+=4; |
dswood | 0:17c0192cb3d7 | 804 | data= 0xFFFFFFFF&Target[0];//low |
dswood | 0:17c0192cb3d7 | 805 | //data= 0x000000A0;//fixme |
dswood | 0:17c0192cb3d7 | 806 | HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, address, data); |
dswood | 0:17c0192cb3d7 | 807 | //Mypc.printf("Address %d low data %d\n\r",address,data); //fixme |
dswood | 0:17c0192cb3d7 | 808 | address+=4; |
dswood | 0:17c0192cb3d7 | 809 | for (count=1; count<size; count++) { |
dswood | 0:17c0192cb3d7 | 810 | data= 0xFFFFFFFF&(Target[count]>>32);//high |
dswood | 0:17c0192cb3d7 | 811 | HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, address, data); |
dswood | 0:17c0192cb3d7 | 812 | //Mypc.printf("Address %d high data %d\n\r",address,data); //fixme |
dswood | 0:17c0192cb3d7 | 813 | address+=4; |
dswood | 0:17c0192cb3d7 | 814 | data= 0xFFFFFFFF&Target[count];//low |
dswood | 0:17c0192cb3d7 | 815 | HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, address, data); |
dswood | 0:17c0192cb3d7 | 816 | //Mypc.printf("Address %d low data %d\n\r",address,data); //fixme |
dswood | 0:17c0192cb3d7 | 817 | address+=4; |
dswood | 0:17c0192cb3d7 | 818 | } |
dswood | 0:17c0192cb3d7 | 819 | HAL_FLASHEx_DATAEEPROM_Lock(); |
dswood | 0:17c0192cb3d7 | 820 | } |
dswood | 0:17c0192cb3d7 | 821 | void ReadDataFromEEPROM() |
dswood | 0:17c0192cb3d7 | 822 | { |
dswood | 0:17c0192cb3d7 | 823 | ReadDataFromEEPROM(TenMins,SizeOfTM,MemOffsetTM); |
dswood | 0:17c0192cb3d7 | 824 | ReadDataFromEEPROM(Hour,SizeOfH,MemOffsetH); |
dswood | 0:17c0192cb3d7 | 825 | ReadDataFromEEPROM(Day,SizeOfD,MemOffsetD); |
dswood | 0:17c0192cb3d7 | 826 | ReadDataFromEEPROM(Week,SizeOfW,MemOffsetW); |
dswood | 0:17c0192cb3d7 | 827 | ReadDataFromEEPROM(Month,SizeOfM,MemOffsetM); |
dswood | 0:17c0192cb3d7 | 828 | ReadDataFromEEPROM(Year,SizeOfY,MemOffsetY); |
dswood | 0:17c0192cb3d7 | 829 | } |
dswood | 0:17c0192cb3d7 | 830 | void WriteDataToEEPROM() |
dswood | 0:17c0192cb3d7 | 831 | { |
dswood | 0:17c0192cb3d7 | 832 | WriteDataToEEPROM(TenMins,SizeOfTM,MemOffsetTM); |
dswood | 0:17c0192cb3d7 | 833 | WriteDataToEEPROM(Hour,SizeOfH,MemOffsetH); |
dswood | 0:17c0192cb3d7 | 834 | WriteDataToEEPROM(Day,SizeOfD,MemOffsetD); |
dswood | 0:17c0192cb3d7 | 835 | WriteDataToEEPROM(Week,SizeOfW,MemOffsetW); |
dswood | 0:17c0192cb3d7 | 836 | WriteDataToEEPROM(Month,SizeOfM,MemOffsetM); |
dswood | 0:17c0192cb3d7 | 837 | WriteDataToEEPROM(Year,SizeOfY,MemOffsetY); |
dswood | 0:17c0192cb3d7 | 838 | |
dswood | 0:17c0192cb3d7 | 839 | } |
dswood | 0:17c0192cb3d7 | 840 | void InitSensor() |
dswood | 0:17c0192cb3d7 | 841 | { |
dswood | 0:17c0192cb3d7 | 842 | DigitalInOut pin_sda(My_sda, PIN_INPUT, PullNone, 1); |
dswood | 0:17c0192cb3d7 | 843 | DigitalInOut pin_scl_in(My_scl, PIN_INPUT, PullNone, 1); |
dswood | 0:17c0192cb3d7 | 844 | // Read and verify if recovery is required |
dswood | 0:17c0192cb3d7 | 845 | if (pin_scl_in == 1) { |
dswood | 0:17c0192cb3d7 | 846 | if (pin_sda == 1) { |
dswood | 0:17c0192cb3d7 | 847 | // Return successfuly as SDA and SCL is high |
dswood | 0:17c0192cb3d7 | 848 | if (SensorSetup()==0) Sensing=true; |
dswood | 0:17c0192cb3d7 | 849 | else Sensing=false; |
dswood | 0:17c0192cb3d7 | 850 | return; |
dswood | 0:17c0192cb3d7 | 851 | } |
dswood | 0:17c0192cb3d7 | 852 | } else { |
dswood | 0:17c0192cb3d7 | 853 | // Return as SCL is low and no access to become master. |
dswood | 0:17c0192cb3d7 | 854 | Mypc.printf("I2c clock is being held low. I2C is cripled\n\r"); |
dswood | 0:17c0192cb3d7 | 855 | return ; |
dswood | 0:17c0192cb3d7 | 856 | } |
dswood | 0:17c0192cb3d7 | 857 | DigitalInOut pin_scl_out(My_scl, PIN_OUTPUT, PullNone, 1); |
dswood | 0:17c0192cb3d7 | 858 | Mypc.printf("I2C busy. Attempt to clock until it has been freed"); |
dswood | 0:17c0192cb3d7 | 859 | int count = 40; |
dswood | 0:17c0192cb3d7 | 860 | while ( (!pin_sda) && (count > 0)) { |
dswood | 0:17c0192cb3d7 | 861 | count--; |
dswood | 0:17c0192cb3d7 | 862 | pin_scl_out=!pin_scl_out; |
dswood | 0:17c0192cb3d7 | 863 | wait_us(5); |
dswood | 0:17c0192cb3d7 | 864 | } |
dswood | 0:17c0192cb3d7 | 865 | if (count==0)Mypc.printf("Clocking has not worked"); |
dswood | 0:17c0192cb3d7 | 866 | if (SensorSetup()==0) Sensing=true; |
dswood | 0:17c0192cb3d7 | 867 | else Sensing=false; |
dswood | 0:17c0192cb3d7 | 868 | |
dswood | 0:17c0192cb3d7 | 869 | } |
dswood | 0:17c0192cb3d7 | 870 | int main() |
dswood | 0:17c0192cb3d7 | 871 | { |
dswood | 0:17c0192cb3d7 | 872 | time_t secs; |
dswood | 0:17c0192cb3d7 | 873 | Mypc.baud(115200); |
dswood | 0:17c0192cb3d7 | 874 | Mypc.printf("Starting main\n\r"); |
dswood | 0:17c0192cb3d7 | 875 | ClearScreen(); |
dswood | 0:17c0192cb3d7 | 876 | // Uncomment WriteDataToEEPROM to zero data fixme |
dswood | 0:17c0192cb3d7 | 877 | //WriteDataToEEPROM(); |
dswood | 0:17c0192cb3d7 | 878 | //Mypc.printf("Stored zeroed data\n\r\n\r"); |
dswood | 0:17c0192cb3d7 | 879 | if(SensorSetup()==0) Sensing=true;// This setup taken from Bosch sensor github |
dswood | 0:17c0192cb3d7 | 880 | else Sensing=false; |
dswood | 0:17c0192cb3d7 | 881 | char buffer[8]; |
dswood | 0:17c0192cb3d7 | 882 | Flow_meter.fall(&Flow_pulse); |
dswood | 0:17c0192cb3d7 | 883 | Display_Ticker.attach(&Display,Display_time); |
dswood | 0:17c0192cb3d7 | 884 | Flow_timer.start(); |
dswood | 0:17c0192cb3d7 | 885 | PageButton.fall(&PageControl); |
dswood | 0:17c0192cb3d7 | 886 | PageButton.mode(PullUp); |
dswood | 0:17c0192cb3d7 | 887 | bool worked=false; |
dswood | 0:17c0192cb3d7 | 888 | int counter; |
dswood | 0:17c0192cb3d7 | 889 | ds3231_cntl_stat_t rtc_control_status = {0,0}; |
dswood | 0:17c0192cb3d7 | 890 | rtc.set_cntl_stat_reg(rtc_control_status); |
dswood | 0:17c0192cb3d7 | 891 | wait_us(250); |
dswood | 0:17c0192cb3d7 | 892 | while (!worked) { |
dswood | 0:17c0192cb3d7 | 893 | worked=MyRadio.initialize(RF_freq,nodeID_emonpwr,networkGroup); |
dswood | 0:17c0192cb3d7 | 894 | } |
dswood | 0:17c0192cb3d7 | 895 | Mypc.printf("Radio up\n\r"); |
dswood | 0:17c0192cb3d7 | 896 | lcd.printf("Radio up "); |
dswood | 0:17c0192cb3d7 | 897 | MyRadio.sleep(true); |
dswood | 0:17c0192cb3d7 | 898 | ReadDataFromEEPROM(); |
dswood | 0:17c0192cb3d7 | 899 | |
dswood | 0:17c0192cb3d7 | 900 | Mypc.printf("Starting sensor\n\r"); |
dswood | 0:17c0192cb3d7 | 901 | lcd.printf("Starting sensor "); |
dswood | 0:17c0192cb3d7 | 902 | InitSensor(); |
dswood | 0:17c0192cb3d7 | 903 | SensorReadAll(); |
dswood | 0:17c0192cb3d7 | 904 | TempS=SensorData.temperature; |
dswood | 0:17c0192cb3d7 | 905 | PressureS=SensorData.pressure/100; |
dswood | 0:17c0192cb3d7 | 906 | HumidityS=SensorData.humidity; |
dswood | 0:17c0192cb3d7 | 907 | LongAvePressureS=PressureS; //init value |
dswood | 0:17c0192cb3d7 | 908 | ShortAvePressureS=PressureS; |
dswood | 0:17c0192cb3d7 | 909 | AvePressureS=PressureS; |
dswood | 0:17c0192cb3d7 | 910 | if ((PressureS>825.0) && (PressureS<1300.0))Sensing=true; |
dswood | 0:17c0192cb3d7 | 911 | else Sensing=false; |
dswood | 0:17c0192cb3d7 | 912 | Mypc.printf("%f- %f- %f \n\r",TempS,PressureS,HumidityS); |
dswood | 0:17c0192cb3d7 | 913 | lcd.printf("%f- %f- %f ",TempS,PressureS,HumidityS); |
dswood | 0:17c0192cb3d7 | 914 | |
dswood | 0:17c0192cb3d7 | 915 | wait(1); |
dswood | 0:17c0192cb3d7 | 916 | //Set_RTC_Time(1533992221); |
dswood | 0:17c0192cb3d7 | 917 | ClearScreen(); |
dswood | 0:17c0192cb3d7 | 918 | Mypc.printf("Starting loop\n\r"); |
dswood | 0:17c0192cb3d7 | 919 | while (true) { |
dswood | 0:17c0192cb3d7 | 920 | //Mypc.printf("Tick\n\r"); |
dswood | 0:17c0192cb3d7 | 921 | /* RTCValid simply check that the time is not old and fairly recent |
dswood | 0:17c0192cb3d7 | 922 | so thr RTC has been set in the past |
dswood | 0:17c0192cb3d7 | 923 | IsTimeSet is true is MSF has been received and was put into the RTC |
dswood | 0:17c0192cb3d7 | 924 | */ |
dswood | 0:17c0192cb3d7 | 925 | if (!RTCValid) { |
dswood | 0:17c0192cb3d7 | 926 | secs=rtc.get_epoch(); |
dswood | 0:17c0192cb3d7 | 927 | wait_us(250); |
dswood | 0:17c0192cb3d7 | 928 | if (secs>1572611760) { |
dswood | 0:17c0192cb3d7 | 929 | set_time(secs); |
dswood | 0:17c0192cb3d7 | 930 | RTCValid=true; // Real time clock has a real time in it |
dswood | 0:17c0192cb3d7 | 931 | FillTimeRecord(&Current); |
dswood | 0:17c0192cb3d7 | 932 | Mypc.printf("%dm %dh %dd %dw %dm %dy\n\r",Current.TM,Current.H,Current.D,Current.D/7,Current.M,Current.Y); |
dswood | 0:17c0192cb3d7 | 933 | } |
dswood | 0:17c0192cb3d7 | 934 | } |
dswood | 0:17c0192cb3d7 | 935 | if ((!IsTimeSet)&&(MSF.Valid())) { |
dswood | 0:17c0192cb3d7 | 936 | secs=MSF.Read(); |
dswood | 0:17c0192cb3d7 | 937 | if (secs>1572611760) { |
dswood | 0:17c0192cb3d7 | 938 | IsTimeSet=Set_RTC_Time(secs); //Set external RTC + internal rtc |
dswood | 0:17c0192cb3d7 | 939 | // I see this is done by Set_RTC_Time //set_time(secs); //Set internal RTC |
dswood | 0:17c0192cb3d7 | 940 | Mypc.printf("Time set %d\n\r",secs); |
dswood | 0:17c0192cb3d7 | 941 | RTCValid=true; // Real time clock has a real time in it |
dswood | 0:17c0192cb3d7 | 942 | FillTimeRecord(&Current); |
dswood | 0:17c0192cb3d7 | 943 | Mypc.printf("%dm %dh %dd %dw %dm %dy\n\r",Current.TM,Current.H,Current.D,Current.D/7,Current.M,Current.Y); |
dswood | 0:17c0192cb3d7 | 944 | } else { |
dswood | 0:17c0192cb3d7 | 945 | RTCValid=false; // Time is not right |
dswood | 0:17c0192cb3d7 | 946 | IsTimeSet=false; |
dswood | 0:17c0192cb3d7 | 947 | Mypc.printf("Time UNset\n\r"); |
dswood | 0:17c0192cb3d7 | 948 | } |
dswood | 0:17c0192cb3d7 | 949 | } |
dswood | 0:17c0192cb3d7 | 950 | secs=time(null);// Read time and set if 02:10 from msf time |
dswood | 0:17c0192cb3d7 | 951 | strftime(buffer,8,"%T",localtime(&secs)); |
dswood | 0:17c0192cb3d7 | 952 | if (strncmp(buffer,"02:10:00",8)==0) { //set the time each day |
dswood | 0:17c0192cb3d7 | 953 | //printf("compare %s",buffer); |
dswood | 0:17c0192cb3d7 | 954 | wait(1); // wait one second to stop this being called twice Assumes RTC clock is not that bad |
dswood | 0:17c0192cb3d7 | 955 | IsTimeSet=false; |
dswood | 0:17c0192cb3d7 | 956 | } |
dswood | 0:17c0192cb3d7 | 957 | if (SensorReadAll()==0) { |
dswood | 0:17c0192cb3d7 | 958 | Sensing=true; |
dswood | 0:17c0192cb3d7 | 959 | TempS=SensorData.temperature; |
dswood | 0:17c0192cb3d7 | 960 | PressureS=SensorData.pressure/100; |
dswood | 0:17c0192cb3d7 | 961 | HumidityS=SensorData.humidity; |
dswood | 0:17c0192cb3d7 | 962 | AvePressureS=(PressureS+AvePressureS)/2; |
dswood | 0:17c0192cb3d7 | 963 | ShortAvePressureS=(ShortAvePressureS*0.9)+(AvePressureS*0.1); |
dswood | 0:17c0192cb3d7 | 964 | LongAvePressureS=(LongAvePressureS*0.99)+(ShortAvePressureS*0.01); |
dswood | 0:17c0192cb3d7 | 965 | } else InitSensor(); |
dswood | 0:17c0192cb3d7 | 966 | |
dswood | 0:17c0192cb3d7 | 967 | //Mypc.printf("tick\n\r"); |
dswood | 0:17c0192cb3d7 | 968 | for (counter=1 ; counter<111; counter++) {//delay + display update if necessary |
dswood | 0:17c0192cb3d7 | 969 | wait_ms(10); |
dswood | 0:17c0192cb3d7 | 970 | if (DisplayChanged)DisplayRefresh(); |
dswood | 0:17c0192cb3d7 | 971 | if (Flag_send_power_rf_data) { |
dswood | 0:17c0192cb3d7 | 972 | Flag_send_power_rf_data=false; |
dswood | 0:17c0192cb3d7 | 973 | send_power_rf_data(0,nodeID_emonpwr); |
dswood | 0:17c0192cb3d7 | 974 | wait_ms(25);// Delay between transmissions - modems are slow |
dswood | 0:17c0192cb3d7 | 975 | } |
dswood | 0:17c0192cb3d7 | 976 | if (FlagWriteDataToEEPROM) { //Do we update eeprom? |
dswood | 0:17c0192cb3d7 | 977 | FlagWriteDataToEEPROM=false; |
dswood | 0:17c0192cb3d7 | 978 | WriteDataToEEPROM(); |
dswood | 0:17c0192cb3d7 | 979 | } |
dswood | 0:17c0192cb3d7 | 980 | } |
dswood | 0:17c0192cb3d7 | 981 | if (Sensing) { |
dswood | 0:17c0192cb3d7 | 982 | emonEnvTX.temperature=10*TempS;// times ten so we can divide by 10 at the other end and get 1 decimal place |
dswood | 0:17c0192cb3d7 | 983 | emonEnvTX.pressure=10*PressureS; |
dswood | 0:17c0192cb3d7 | 984 | emonEnvTX.humidity=10*HumidityS; |
dswood | 0:17c0192cb3d7 | 985 | send_enviro_rf_data(0, nodeID_emonenv); |
dswood | 0:17c0192cb3d7 | 986 | } |
dswood | 0:17c0192cb3d7 | 987 | //Mypc.printf("Long=%f1.3 Short=%f1.3 \n\r",LongAvePressureS,ShortAvePressureS); |
dswood | 0:17c0192cb3d7 | 988 | } |
dswood | 0:17c0192cb3d7 | 989 | } |