Dieses Programm verarbeitet die einzelnen Messwerte der Wettermesseinheit (Windstärke, Niederschlag, Windrichtung, Temperatur und Luftfeuchtigkeit). Diese können über ein LCD Display abgelesen oder über das Internet aberufen werden.
Dependencies: SHTx TextLCD mbed lwip WeatherMeters
main.cpp@0:07d31130dd79, 2012-06-27 (annotated)
- Committer:
- candyshop
- Date:
- Wed Jun 27 15:34:03 2012 +0000
- Revision:
- 0:07d31130dd79
Wetterstation FB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
candyshop | 0:07d31130dd79 | 1 | #include "mbed.h" |
candyshop | 0:07d31130dd79 | 2 | #include "lwip/opt.h" |
candyshop | 0:07d31130dd79 | 3 | #include "lwip/stats.h" |
candyshop | 0:07d31130dd79 | 4 | #include "lwip/sys.h" |
candyshop | 0:07d31130dd79 | 5 | #include "lwip/pbuf.h" |
candyshop | 0:07d31130dd79 | 6 | #include "lwip/udp.h" |
candyshop | 0:07d31130dd79 | 7 | #include "lwip/tcp.h" |
candyshop | 0:07d31130dd79 | 8 | #include "lwip/dns.h" |
candyshop | 0:07d31130dd79 | 9 | #include "lwip/dhcp.h" |
candyshop | 0:07d31130dd79 | 10 | #include "lwip/init.h" |
candyshop | 0:07d31130dd79 | 11 | #include "lwip/netif.h" |
candyshop | 0:07d31130dd79 | 12 | #include "netif/etharp.h" |
candyshop | 0:07d31130dd79 | 13 | #include "netif/loopif.h" |
candyshop | 0:07d31130dd79 | 14 | #include "device.h" |
candyshop | 0:07d31130dd79 | 15 | #include "SHTx/sht15.hpp" |
candyshop | 0:07d31130dd79 | 16 | #include "WeatherMeters.h" |
candyshop | 0:07d31130dd79 | 17 | #include "TextLCD.h" |
candyshop | 0:07d31130dd79 | 18 | |
candyshop | 0:07d31130dd79 | 19 | |
candyshop | 0:07d31130dd79 | 20 | |
candyshop | 0:07d31130dd79 | 21 | TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs,e,d0,d1,d2,d3 |
candyshop | 0:07d31130dd79 | 22 | Serial pc(USBTX, USBRX); |
candyshop | 0:07d31130dd79 | 23 | |
candyshop | 0:07d31130dd79 | 24 | |
candyshop | 0:07d31130dd79 | 25 | |
candyshop | 0:07d31130dd79 | 26 | Ethernet ethernet; |
candyshop | 0:07d31130dd79 | 27 | DigitalOut ledLink(p30); |
candyshop | 0:07d31130dd79 | 28 | DigitalOut ledActivity(p29); |
candyshop | 0:07d31130dd79 | 29 | DigitalOut ledStage0 (LED1); |
candyshop | 0:07d31130dd79 | 30 | DigitalOut ledStage1 (LED2); |
candyshop | 0:07d31130dd79 | 31 | DigitalOut ledStage2 (LED3); |
candyshop | 0:07d31130dd79 | 32 | DigitalOut ledTCP80 (LED4); |
candyshop | 0:07d31130dd79 | 33 | |
candyshop | 0:07d31130dd79 | 34 | SHTx::SHT15 sensor(p9, p10); |
candyshop | 0:07d31130dd79 | 35 | WeatherMeters station(p8, p15, p16, Weather_auto); |
candyshop | 0:07d31130dd79 | 36 | |
candyshop | 0:07d31130dd79 | 37 | volatile char stage = 0; |
candyshop | 0:07d31130dd79 | 38 | |
candyshop | 0:07d31130dd79 | 39 | Ticker stage_blinker; |
candyshop | 0:07d31130dd79 | 40 | |
candyshop | 0:07d31130dd79 | 41 | struct netif netif_data; |
candyshop | 0:07d31130dd79 | 42 | |
candyshop | 0:07d31130dd79 | 43 | const char testPage[] = "HTTP/1.1 200 OK\r\n" |
candyshop | 0:07d31130dd79 | 44 | "Content-Type: text/html\r\n" |
candyshop | 0:07d31130dd79 | 45 | "Connection: Close\r\n\r\n" |
candyshop | 0:07d31130dd79 | 46 | "<html>" |
candyshop | 0:07d31130dd79 | 47 | "<head>" |
candyshop | 0:07d31130dd79 | 48 | "<title>mbed test page</title>" |
candyshop | 0:07d31130dd79 | 49 | "<style type='text/css'>" |
candyshop | 0:07d31130dd79 | 50 | "body{font-family:'Arial, sans-serif', sans-serif;font-size:.8em;background-color:#fff;}" |
candyshop | 0:07d31130dd79 | 51 | "</style>" |
candyshop | 0:07d31130dd79 | 52 | "</head>" |
candyshop | 0:07d31130dd79 | 53 | "<body>%s</body></html>\r\n\r\n"; |
candyshop | 0:07d31130dd79 | 54 | |
candyshop | 0:07d31130dd79 | 55 | char buffer[1024]; |
candyshop | 0:07d31130dd79 | 56 | char temp[1024]; |
candyshop | 0:07d31130dd79 | 57 | err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { |
candyshop | 0:07d31130dd79 | 58 | struct netif *netif = &netif_data; |
candyshop | 0:07d31130dd79 | 59 | ledTCP80 = true; |
candyshop | 0:07d31130dd79 | 60 | printf("TCP callback from %d.%d.%d.%d\r\n", ip4_addr1(&(pcb->remote_ip)),ip4_addr2(&(pcb->remote_ip)),ip4_addr3(&(pcb->remote_ip)),ip4_addr4(&(pcb->remote_ip))); |
candyshop | 0:07d31130dd79 | 61 | char *data; |
candyshop | 0:07d31130dd79 | 62 | /* Check if status is ok and data is arrived. */ |
candyshop | 0:07d31130dd79 | 63 | if (err == ERR_OK && p != NULL) { |
candyshop | 0:07d31130dd79 | 64 | /* Inform TCP that we have taken the data. */ |
candyshop | 0:07d31130dd79 | 65 | tcp_recved(pcb, p->tot_len); |
candyshop | 0:07d31130dd79 | 66 | data = static_cast<char *>(p->payload); |
candyshop | 0:07d31130dd79 | 67 | /* If the data is a GET request we can handle it. */ |
candyshop | 0:07d31130dd79 | 68 | if (strncmp(data, "GET ", 4) == 0) { |
candyshop | 0:07d31130dd79 | 69 | printf("Handling GET request...\r\n"); |
candyshop | 0:07d31130dd79 | 70 | printf("Request:\r\n%s\r\n", data); |
candyshop | 0:07d31130dd79 | 71 | |
candyshop | 0:07d31130dd79 | 72 | |
candyshop | 0:07d31130dd79 | 73 | sprintf(temp, "<h1><font color=blue><center><U>RBB Robert Bosch Berufskolleg der Stadt Dortmund</font></U></h1>" |
candyshop | 0:07d31130dd79 | 74 | "<h2><br></h2>" |
candyshop | 0:07d31130dd79 | 75 | "<h2><u><center>Aktuelle Wetter-Messdaten:</u></center></h2>" |
candyshop | 0:07d31130dd79 | 76 | "<table border=1 align=center cellpadding=10>" |
candyshop | 0:07d31130dd79 | 77 | "<tr align=center>" |
candyshop | 0:07d31130dd79 | 78 | "<td><strong>Temperatur</strong></td>" |
candyshop | 0:07d31130dd79 | 79 | "<td> %3.2f °C [ Celsius ]</td>" |
candyshop | 0:07d31130dd79 | 80 | "</tr>" |
candyshop | 0:07d31130dd79 | 81 | "<tr align=center>" |
candyshop | 0:07d31130dd79 | 82 | "<td><strong>Luftfeuchtigkeit</strong></td>" |
candyshop | 0:07d31130dd79 | 83 | "<td> %3.2f %% [ Prozent ]</td>" |
candyshop | 0:07d31130dd79 | 84 | "</tr>" |
candyshop | 0:07d31130dd79 | 85 | "<tr align=center>" |
candyshop | 0:07d31130dd79 | 86 | "<td><strong>Windstärke</strong></td>" |
candyshop | 0:07d31130dd79 | 87 | "<td> %3.2f m/s [ Meter pro Sekunde ]</td>" |
candyshop | 0:07d31130dd79 | 88 | "</tr>" |
candyshop | 0:07d31130dd79 | 89 | "<tr align=center>" |
candyshop | 0:07d31130dd79 | 90 | "<td><strong>Windrichtung</strong></td>" |
candyshop | 0:07d31130dd79 | 91 | "<td> %3.2f ° [ Grad ]</td>" |
candyshop | 0:07d31130dd79 | 92 | "</tr>" |
candyshop | 0:07d31130dd79 | 93 | "<tr align=center>" |
candyshop | 0:07d31130dd79 | 94 | "<td><strong>Niederschlag</strong></td>" |
candyshop | 0:07d31130dd79 | 95 | "<td> %3.2f mm [ Millimeter ]</td>" |
candyshop | 0:07d31130dd79 | 96 | "</tr>" |
candyshop | 0:07d31130dd79 | 97 | "</table>" |
candyshop | 0:07d31130dd79 | 98 | |
candyshop | 0:07d31130dd79 | 99 | "<form align=center method=post action>" |
candyshop | 0:07d31130dd79 | 100 | "<center><br><br>Projektarbeit von Florian Blumgard</center>" |
candyshop | 0:07d31130dd79 | 101 | |
candyshop | 0:07d31130dd79 | 102 | "</form>",sensor.getTemperature(),sensor.getHumidity(),station.get_windspeed(),station.get_windvane(),station.get_raingauge()); |
candyshop | 0:07d31130dd79 | 103 | |
candyshop | 0:07d31130dd79 | 104 | sprintf(buffer, testPage, temp); |
candyshop | 0:07d31130dd79 | 105 | if (tcp_write(pcb, (void *)buffer, strlen(buffer), 1) == ERR_OK) { |
candyshop | 0:07d31130dd79 | 106 | tcp_output(pcb); |
candyshop | 0:07d31130dd79 | 107 | printf("Closing connection...\r\n"); |
candyshop | 0:07d31130dd79 | 108 | tcp_close(pcb); |
candyshop | 0:07d31130dd79 | 109 | } |
candyshop | 0:07d31130dd79 | 110 | } |
candyshop | 0:07d31130dd79 | 111 | else |
candyshop | 0:07d31130dd79 | 112 | { |
candyshop | 0:07d31130dd79 | 113 | printf("Non GET request...\r\nRequest:\r\n%s\r\n", data); |
candyshop | 0:07d31130dd79 | 114 | } |
candyshop | 0:07d31130dd79 | 115 | |
candyshop | 0:07d31130dd79 | 116 | pbuf_free(p); |
candyshop | 0:07d31130dd79 | 117 | } |
candyshop | 0:07d31130dd79 | 118 | |
candyshop | 0:07d31130dd79 | 119 | else { |
candyshop | 0:07d31130dd79 | 120 | /* No data arrived */ |
candyshop | 0:07d31130dd79 | 121 | /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */ |
candyshop | 0:07d31130dd79 | 122 | /* We have to cleanup and destroy out TCPConnection. */ |
candyshop | 0:07d31130dd79 | 123 | printf("Connection closed by client.\r\n"); |
candyshop | 0:07d31130dd79 | 124 | pbuf_free(p); |
candyshop | 0:07d31130dd79 | 125 | } |
candyshop | 0:07d31130dd79 | 126 | /* Don't panic! Everything is fine. */ |
candyshop | 0:07d31130dd79 | 127 | ledTCP80 = false; |
candyshop | 0:07d31130dd79 | 128 | return ERR_OK; |
candyshop | 0:07d31130dd79 | 129 | } |
candyshop | 0:07d31130dd79 | 130 | /* Accept an incomming call on the registered port */ |
candyshop | 0:07d31130dd79 | 131 | err_t accept_callback(void *arg, struct tcp_pcb *npcb, err_t err) { |
candyshop | 0:07d31130dd79 | 132 | LWIP_UNUSED_ARG(arg); |
candyshop | 0:07d31130dd79 | 133 | /* Subscribe a receive callback function */ |
candyshop | 0:07d31130dd79 | 134 | tcp_recv(npcb, &recv_callback); |
candyshop | 0:07d31130dd79 | 135 | /* Don't panic! Everything is fine. */ |
candyshop | 0:07d31130dd79 | 136 | return ERR_OK; |
candyshop | 0:07d31130dd79 | 137 | } |
candyshop | 0:07d31130dd79 | 138 | |
candyshop | 0:07d31130dd79 | 139 | void stageblinker() |
candyshop | 0:07d31130dd79 | 140 | { |
candyshop | 0:07d31130dd79 | 141 | switch (stage) |
candyshop | 0:07d31130dd79 | 142 | { |
candyshop | 0:07d31130dd79 | 143 | case 0: |
candyshop | 0:07d31130dd79 | 144 | ledStage0 = !ledStage0; |
candyshop | 0:07d31130dd79 | 145 | ledStage1 = false; |
candyshop | 0:07d31130dd79 | 146 | ledStage2 = false; |
candyshop | 0:07d31130dd79 | 147 | break; |
candyshop | 0:07d31130dd79 | 148 | case 1: |
candyshop | 0:07d31130dd79 | 149 | ledStage0 = true; |
candyshop | 0:07d31130dd79 | 150 | ledStage1 = !ledStage1; |
candyshop | 0:07d31130dd79 | 151 | ledStage2 = false; |
candyshop | 0:07d31130dd79 | 152 | break; |
candyshop | 0:07d31130dd79 | 153 | case 2: |
candyshop | 0:07d31130dd79 | 154 | ledStage0 = true; |
candyshop | 0:07d31130dd79 | 155 | ledStage1 = true; |
candyshop | 0:07d31130dd79 | 156 | ledStage2 = true; |
candyshop | 0:07d31130dd79 | 157 | stage_blinker.detach(); |
candyshop | 0:07d31130dd79 | 158 | break; |
candyshop | 0:07d31130dd79 | 159 | } |
candyshop | 0:07d31130dd79 | 160 | } |
candyshop | 0:07d31130dd79 | 161 | |
candyshop | 0:07d31130dd79 | 162 | int main() { |
candyshop | 0:07d31130dd79 | 163 | printf("mBed Ethernet Tester 1.0\r\nStarting Up...\r\n"); |
candyshop | 0:07d31130dd79 | 164 | stage = 0; |
candyshop | 0:07d31130dd79 | 165 | struct netif *netif = &netif_data; |
candyshop | 0:07d31130dd79 | 166 | struct ip_addr ipaddr; |
candyshop | 0:07d31130dd79 | 167 | struct ip_addr netmask; |
candyshop | 0:07d31130dd79 | 168 | struct ip_addr gateway; |
candyshop | 0:07d31130dd79 | 169 | Ticker tickFast, tickSlow, tickARP, eth_tick, dns_tick, dhcp_coarse, dhcp_fine; |
candyshop | 0:07d31130dd79 | 170 | stage_blinker.attach_us(&stageblinker, 1000*500); |
candyshop | 0:07d31130dd79 | 171 | |
candyshop | 0:07d31130dd79 | 172 | char *hostname = "my-mbed"; |
candyshop | 0:07d31130dd79 | 173 | |
candyshop | 0:07d31130dd79 | 174 | printf("Configuring device for DHCP...\r\n"); |
candyshop | 0:07d31130dd79 | 175 | /* Start Network with DHCP */ |
candyshop | 0:07d31130dd79 | 176 | IP4_ADDR(&netmask, 255,255,255,255); |
candyshop | 0:07d31130dd79 | 177 | IP4_ADDR(&gateway, 0,0,0,0); |
candyshop | 0:07d31130dd79 | 178 | IP4_ADDR(&ipaddr, 0,0,0,0); |
candyshop | 0:07d31130dd79 | 179 | /* Initialise after configuration */ |
candyshop | 0:07d31130dd79 | 180 | lwip_init(); |
candyshop | 0:07d31130dd79 | 181 | netif->hwaddr_len = ETHARP_HWADDR_LEN; |
candyshop | 0:07d31130dd79 | 182 | device_address((char *)netif->hwaddr); |
candyshop | 0:07d31130dd79 | 183 | netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input); |
candyshop | 0:07d31130dd79 | 184 | netif->hostname = hostname; |
candyshop | 0:07d31130dd79 | 185 | netif_set_default(netif); |
candyshop | 0:07d31130dd79 | 186 | dhcp_start(netif); // <-- Use DHCP |
candyshop | 0:07d31130dd79 | 187 | |
candyshop | 0:07d31130dd79 | 188 | /* Initialise all needed timers */ |
candyshop | 0:07d31130dd79 | 189 | tickARP.attach_us( ðarp_tmr, ARP_TMR_INTERVAL * 1000); |
candyshop | 0:07d31130dd79 | 190 | tickFast.attach_us(&tcp_fasttmr, TCP_FAST_INTERVAL * 1000); |
candyshop | 0:07d31130dd79 | 191 | tickSlow.attach_us(&tcp_slowtmr, TCP_SLOW_INTERVAL * 1000); |
candyshop | 0:07d31130dd79 | 192 | dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000); |
candyshop | 0:07d31130dd79 | 193 | dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000); |
candyshop | 0:07d31130dd79 | 194 | dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); |
candyshop | 0:07d31130dd79 | 195 | stage = 1; |
candyshop | 0:07d31130dd79 | 196 | while (!netif_is_up(netif)) { |
candyshop | 0:07d31130dd79 | 197 | ledLink = ethernet.link(); |
candyshop | 0:07d31130dd79 | 198 | device_poll(); |
candyshop | 0:07d31130dd79 | 199 | } |
candyshop | 0:07d31130dd79 | 200 | |
candyshop | 0:07d31130dd79 | 201 | /* |
candyshop | 0:07d31130dd79 | 202 | while (!(netif->dhcp->state == DHCP_BOUND || netif->dhcp->state == DHCP_PERMANENT)) |
candyshop | 0:07d31130dd79 | 203 | { |
candyshop | 0:07d31130dd79 | 204 | ledLink = ethernet.link(); |
candyshop | 0:07d31130dd79 | 205 | device_poll(); |
candyshop | 0:07d31130dd79 | 206 | //printf("Waiting for DHCP response, state = %d\r\n", netif->dhcp->state); |
candyshop | 0:07d31130dd79 | 207 | //wait_ms(100); |
candyshop | 0:07d31130dd79 | 208 | } |
candyshop | 0:07d31130dd79 | 209 | */ |
candyshop | 0:07d31130dd79 | 210 | stage = 2; |
candyshop | 0:07d31130dd79 | 211 | printf("Interface is up, local IP is %s\r\n", |
candyshop | 0:07d31130dd79 | 212 | inet_ntoa(*(struct in_addr*)&(netif->ip_addr))); |
candyshop | 0:07d31130dd79 | 213 | |
candyshop | 0:07d31130dd79 | 214 | printf("Starting Web Server...\r\n"); |
candyshop | 0:07d31130dd79 | 215 | |
candyshop | 0:07d31130dd79 | 216 | /* Bind a function to a tcp port */ |
candyshop | 0:07d31130dd79 | 217 | struct tcp_pcb *pcb = tcp_new(); |
candyshop | 0:07d31130dd79 | 218 | if (tcp_bind(pcb, IP_ADDR_ANY, 80) == ERR_OK) { |
candyshop | 0:07d31130dd79 | 219 | pcb = tcp_listen(pcb); |
candyshop | 0:07d31130dd79 | 220 | tcp_accept(pcb, &accept_callback); |
candyshop | 0:07d31130dd79 | 221 | } |
candyshop | 0:07d31130dd79 | 222 | |
candyshop | 0:07d31130dd79 | 223 | printf("Waiting for connection...\r\n"); |
candyshop | 0:07d31130dd79 | 224 | while(1) { |
candyshop | 0:07d31130dd79 | 225 | device_poll(); |
candyshop | 0:07d31130dd79 | 226 | ledLink = ethernet.link(); |
candyshop | 0:07d31130dd79 | 227 | |
candyshop | 0:07d31130dd79 | 228 | |
candyshop | 0:07d31130dd79 | 229 | sensor.setOTPReload(false); |
candyshop | 0:07d31130dd79 | 230 | sensor.setResolution(true); |
candyshop | 0:07d31130dd79 | 231 | |
candyshop | 0:07d31130dd79 | 232 | |
candyshop | 0:07d31130dd79 | 233 | sensor.update(); |
candyshop | 0:07d31130dd79 | 234 | |
candyshop | 0:07d31130dd79 | 235 | |
candyshop | 0:07d31130dd79 | 236 | |
candyshop | 0:07d31130dd79 | 237 | // Temperature in celcius |
candyshop | 0:07d31130dd79 | 238 | |
candyshop | 0:07d31130dd79 | 239 | pc.printf("Temperatur [ %3.2f °C ]\r\n", sensor.getTemperature()); |
candyshop | 0:07d31130dd79 | 240 | |
candyshop | 0:07d31130dd79 | 241 | |
candyshop | 0:07d31130dd79 | 242 | |
candyshop | 0:07d31130dd79 | 243 | pc.printf("Luftfeuchtigkeit [ %3.2f %% ]\r\n", sensor.getHumidity()); |
candyshop | 0:07d31130dd79 | 244 | pc.printf("Windstärke: %3.2f\r\n",station.get_windspeed()); |
candyshop | 0:07d31130dd79 | 245 | pc.printf("Windrichtung: %3.2f °\r\n",station.get_windvane()); |
candyshop | 0:07d31130dd79 | 246 | pc.printf("Niederschlag %3.2f\r\n",station.get_raingauge()); |
candyshop | 0:07d31130dd79 | 247 | |
candyshop | 0:07d31130dd79 | 248 | lcd.printf("Temperatur [ %3.2f °C ]\r\n", sensor.getTemperature()); |
candyshop | 0:07d31130dd79 | 249 | |
candyshop | 0:07d31130dd79 | 250 | |
candyshop | 0:07d31130dd79 | 251 | |
candyshop | 0:07d31130dd79 | 252 | lcd.printf("Luftfeuchtigkeit [ %3.2f %% ]\r\n", sensor.getHumidity()); |
candyshop | 0:07d31130dd79 | 253 | lcd.printf("Windstärke: %3.2f\r\n",station.get_windspeed()); |
candyshop | 0:07d31130dd79 | 254 | lcd.printf("Windrichtung: %3.2f °\r\n",station.get_windvane()); |
candyshop | 0:07d31130dd79 | 255 | lcd.printf("Niederschlag %3.2f\r\n",station.get_raingauge()); |
candyshop | 0:07d31130dd79 | 256 | |
candyshop | 0:07d31130dd79 | 257 | |
candyshop | 0:07d31130dd79 | 258 | |
candyshop | 0:07d31130dd79 | 259 | |
candyshop | 0:07d31130dd79 | 260 | |
candyshop | 0:07d31130dd79 | 261 | |
candyshop | 0:07d31130dd79 | 262 | |
candyshop | 0:07d31130dd79 | 263 | |
candyshop | 0:07d31130dd79 | 264 | |
candyshop | 0:07d31130dd79 | 265 | |
candyshop | 0:07d31130dd79 | 266 | |
candyshop | 0:07d31130dd79 | 267 | wait(1); |
candyshop | 0:07d31130dd79 | 268 | |
candyshop | 0:07d31130dd79 | 269 | } |
candyshop | 0:07d31130dd79 | 270 | } |
candyshop | 0:07d31130dd79 | 271 |