BayEOSArduino Library
MeteoShield.h
1 /*
2  * Just a bunch of variables and functions to
3  * handle measurements on UniversalShield
4  *
5  * expects to have a bayeosClient called "client"
6  * and a RTC called "myRTC"
7  channel 1 - CH-Status (0 sleeping - 1 charging - 2 done - 3 error)
8  channel 2 U_LiPo (V)
9  channel 3+4 - uptime/cpu-time (s)
10  channel 5 - RTC-Temperature (°C)
11  channel 6 HMP Moisture (%)
12  channel 7 HMP Temperature (%)
13  channel 8 PAR1 (V)
14  channel 9 PAR2 (V)
15  channel 10 Kippwaage (count)
16  channel 11 Dallas (Currently one sensor per bus)
17  */
18 
19 #define DALLAS1_POWER A0
20 #define DALLAS1_DATA A1
21 #define UPSTEPPER_EN_PIN A3
22 
23 #define MCP_ADDR_OFFSET 3
24 #define ADC_CH_PAR2 1
25 #define ADC_CH_HMP_T 0
26 #define ADC_CH_HMP_H 3
27 #define ADC_CH_PAR1 2
28 
29 #define XBEE_SLEEP_PIN 5
30 float tmp_float ;
31 
32 volatile uint8_t kippevent=0;
33 volatile uint8_t wdcount=0; //set 0 in loop otherwise board will call a reset!!
34 //uint8_t last_kippevent=0;
35 uint8_t count_kippevent=0;
36 uint8_t kippcount=0;
37 unsigned long start_time;
38 unsigned long last_data;
39 
40 DS18B20 ds1=DS18B20(DALLAS1_DATA,0,1);
41 MCP342x mcp = MCP342x();
42 
43 void handleKippevent(){
44  if(count_kippevent){
45  kippcount++;
46  count_kippevent=0;
47  kippevent=0;
48  }
49  if(kippevent && ! count_kippevent){
50  count_kippevent=1;
51  delay(400);
52  }
53 }
54 
55 void measurePAR(void){
56  client.startDataFrame(BayEOS_Float32le);
57  client.addToPayload((uint8_t) 7); //Offset 7
58  //Read PAR1
59  mcp.setConf(MCP_ADDR_OFFSET,1,ADC_CH_PAR1,1,3,3);
60  delay(350);
61  tmp_float=mcp.getData(MCP_ADDR_OFFSET);
62  client.addToPayload(tmp_float);
63  //Read PAR2
64  mcp.setConf(MCP_ADDR_OFFSET,1,ADC_CH_PAR2,1,3,3);
65  delay(350);
66  tmp_float=mcp.getData(MCP_ADDR_OFFSET);
67  client.addToPayload(tmp_float);
68 }
69 
70 
71 void measure(void){
72  last_data=myRTC.now().get();
73  //send buffered frames
74  //only one each time
75 
76 
77  myRTC.convertTemperature();
78  digitalWrite(DALLAS1_POWER,HIGH);
79  //Normally you have to wait 750, but we will do some other stuff meanwhile...
80  ds1.t_conversion();
81  digitalWrite(UPSTEPPER_EN_PIN,HIGH);
82  delay(1250);
83  handleKippevent();
84  delay(1250);
85  handleKippevent();
86 
87 
88 
89  //Start a new data frame
90  client.startDataFrame(BayEOS_Float32le);
91  client.addToPayload((uint8_t) 0); //Offset 0
92  // Charge
93  analogReference(INTERNAL);
94  tmp_float=analogRead(A6);
95  /*
96  * >900 sleeping
97  * >550 charging
98  * >350 done
99  * <350 error
100  */
101  if(tmp_float>900) tmp_float=0;
102  else if(tmp_float>550) tmp_float=1;
103  else if(tmp_float>350) tmp_float=2;
104  else tmp_float=3;
105 
106  client.addToPayload(tmp_float); // Float
107  tmp_float=(1.1 / 1024)*analogRead(A7)*(10+2)/2; //Voltage
108  /*
109  * voltage = tmp_float * (1.1 / 1024)* (10+2)/2; //Voltage divider
110  */
111  client.addToPayload(tmp_float); // Float
112 
113  client.addToPayload((float) (last_data-start_time));
114  client.addToPayload((float) (millis()/1000));
115  client.addToPayload((float) myRTC.getTemperature());
116 
117 
118  //Read HMP
119  mcp.setConf(MCP_ADDR_OFFSET,1,ADC_CH_HMP_H,1,0,0);
120  delay(10);
121  tmp_float = mcp.getData(MCP_ADDR_OFFSET)*100;
122  client.addToPayload(tmp_float); // Float
123 
124  mcp.setConf(MCP_ADDR_OFFSET,1,ADC_CH_HMP_T,1,0,0);
125  delay(10);
126  tmp_float = mcp.getData(MCP_ADDR_OFFSET)*120-40;
127  client.addToPayload(tmp_float); // Float
128 
129  digitalWrite(UPSTEPPER_EN_PIN,LOW);
130 
131  //Read PAR1
132  mcp.setConf(MCP_ADDR_OFFSET,1,ADC_CH_PAR1,1,3,3);
133  delay(350);
134  tmp_float=mcp.getData(MCP_ADDR_OFFSET);
135  client.addToPayload(tmp_float);
136  //Read PAR2
137  mcp.setConf(MCP_ADDR_OFFSET,1,ADC_CH_PAR2,1,3,3);
138  delay(350);
139  tmp_float=mcp.getData(MCP_ADDR_OFFSET);
140  client.addToPayload(tmp_float);
141 
142  tmp_float=kippcount;
143  kippcount=0;
144  client.addToPayload(tmp_float);
145 
146  if(ds1.readChannel(1,&tmp_float))
147  tmp_float=-255;
148  client.addToPayload(tmp_float);
149  //data frame is now complete ...
150  //sending or writing to SD is done in the sketch...
151 
152 
153  digitalWrite(DALLAS1_POWER,LOW);
154 }
155 
156 //****************************************************************
157 // Watchdog Interrupt Service / is executed when watchdog timed out
158 // You have to set this! Otherwise it will call reset!
159 ISR(WDT_vect) {
160  wdcount++;
161  if(wdcount>240){
162  asm volatile (" jmp 0"); //restart programm
163  }
164 }
165 
166 void kippwaage(){
167  kippevent=1;
168 }
169 
170 void initShield(void){
171  attachInterrupt(0, kippwaage, FALLING);
172  Wire.begin();
173  myRTC.begin();
174 
175  pinMode(DALLAS1_POWER, OUTPUT);
176  pinMode(UPSTEPPER_EN_PIN, OUTPUT);
177  digitalWrite(DALLAS1_POWER,HIGH);
178  delay(50);
179  ds1.setAllAddr();
180  start_time=myRTC.now().get();
181 
182 }
uint8_t addToPayload(uint8_t b)
Definition: BayEOS.h:469
void startDataFrame(uint8_t subtype=BayEOS_Float32le, uint8_t checksum=0)
Definition: BayEOS.cpp:12
Definition: DS18B20.h:31
uint8_t readChannel(uint8_t channel, float *f, uint8_t tries=1)
Definition: DS18B20.cpp:147
int setAllAddr(void)
Definition: DS18B20.cpp:89
void t_conversion(void)
Definition: DS18B20.cpp:23
Definition: MCP342x.h:17
void setConf(uint8_t adc_addr, uint8_t conf)
Definition: MCP342x.cpp:91
float getData(byte adc_addr)
Definition: MCP342x.cpp:127