Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos SDFileSystem EthernetInterface DHT11
main.cpp@2:44f331aea307, 2018-11-24 (annotated)
- Committer:
- gjanusz
- Date:
- Sat Nov 24 22:20:03 2018 +0000
- Revision:
- 2:44f331aea307
- Parent:
- 1:baaf95f8d272
- Child:
- 3:b2eae813c2e1
Add DHT11;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| barti19941 | 0:795a02b2bb68 | 1 | #include "mbed.h" |
| barti19941 | 0:795a02b2bb68 | 2 | #include "SDFileSystem.h" |
| barti19941 | 1:baaf95f8d272 | 3 | #include "EthernetInterface.h" |
| barti19941 | 1:baaf95f8d272 | 4 | #include <iostream> |
| barti19941 | 1:baaf95f8d272 | 5 | #include <fstream> |
| gjanusz | 2:44f331aea307 | 6 | #include "DHT11.h" |
| barti19941 | 0:795a02b2bb68 | 7 | |
| barti19941 | 1:baaf95f8d272 | 8 | #define NUMBER_OF_LINES 23 |
| barti19941 | 1:baaf95f8d272 | 9 | #define COLOR_LINE 12 |
| barti19941 | 1:baaf95f8d272 | 10 | #define TEMPERATURE_LINE 17 |
| barti19941 | 1:baaf95f8d272 | 11 | #define PRESSURE_LINE 18 |
| barti19941 | 1:baaf95f8d272 | 12 | #define HUMADITY_LINE 19 |
| barti19941 | 1:baaf95f8d272 | 13 | #define RAINFALL_LINE 20 |
| barti19941 | 1:baaf95f8d272 | 14 | #define TIME_LINE 21 |
| barti19941 | 1:baaf95f8d272 | 15 | |
| barti19941 | 1:baaf95f8d272 | 16 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
| barti19941 | 0:795a02b2bb68 | 17 | Serial pc(USBTX, USBRX); |
| barti19941 | 1:baaf95f8d272 | 18 | string file_content[NUMBER_OF_LINES]; |
| barti19941 | 0:795a02b2bb68 | 19 | |
| gjanusz | 2:44f331aea307 | 20 | |
| gjanusz | 2:44f331aea307 | 21 | DHT11 d(PTD1); |
| gjanusz | 2:44f331aea307 | 22 | |
| gjanusz | 2:44f331aea307 | 23 | void DHT11_read() |
| gjanusz | 2:44f331aea307 | 24 | { |
| gjanusz | 2:44f331aea307 | 25 | int s; |
| gjanusz | 2:44f331aea307 | 26 | s = d.readData(); |
| gjanusz | 2:44f331aea307 | 27 | if (s != DHT11::OK) { |
| gjanusz | 2:44f331aea307 | 28 | pc.printf("Error!\r\n"); |
| gjanusz | 2:44f331aea307 | 29 | } |
| gjanusz | 2:44f331aea307 | 30 | else { |
| gjanusz | 2:44f331aea307 | 31 | pc.printf("T:%d, H:%d\r\n", d.readTemperature(), d.readHumidity()); |
| gjanusz | 2:44f331aea307 | 32 | } |
| gjanusz | 2:44f331aea307 | 33 | } |
| gjanusz | 2:44f331aea307 | 34 | |
| gjanusz | 2:44f331aea307 | 35 | |
| barti19941 | 1:baaf95f8d272 | 36 | void initializeEthernet(void) |
| barti19941 | 1:baaf95f8d272 | 37 | { |
| barti19941 | 1:baaf95f8d272 | 38 | EthernetInterface eth; |
| barti19941 | 1:baaf95f8d272 | 39 | const char *ip = "192.168.1.2"; |
| barti19941 | 1:baaf95f8d272 | 40 | const char *mask = "255.255.255.0"; |
| barti19941 | 1:baaf95f8d272 | 41 | const char *gateway = "192.168.1.1"; |
| barti19941 | 1:baaf95f8d272 | 42 | if(!eth.init(ip,mask,gateway)) |
| barti19941 | 1:baaf95f8d272 | 43 | { |
| barti19941 | 1:baaf95f8d272 | 44 | pc.printf("Ethernet card initialized successfully.\r"); |
| barti19941 | 1:baaf95f8d272 | 45 | eth.connect(); |
| barti19941 | 1:baaf95f8d272 | 46 | pc.printf("IP address: %s\n",eth.getIPAddress()); |
| barti19941 | 1:baaf95f8d272 | 47 | pc.printf("Network mask: %s\n",eth.getNetworkMask()); |
| barti19941 | 1:baaf95f8d272 | 48 | pc.printf("Gateway: %s\n",eth.getGateway()); |
| barti19941 | 1:baaf95f8d272 | 49 | pc.printf("MAC address: %s\n",eth.getMACAddress()); |
| barti19941 | 0:795a02b2bb68 | 50 | } |
| barti19941 | 1:baaf95f8d272 | 51 | else |
| barti19941 | 1:baaf95f8d272 | 52 | pc.printf("Ethernet card could not be initialized.\r"); |
| barti19941 | 1:baaf95f8d272 | 53 | return; |
| barti19941 | 1:baaf95f8d272 | 54 | } |
| barti19941 | 0:795a02b2bb68 | 55 | |
| barti19941 | 1:baaf95f8d272 | 56 | void readHTMLCode(void) |
| barti19941 | 1:baaf95f8d272 | 57 | { |
| barti19941 | 1:baaf95f8d272 | 58 | fstream file; |
| barti19941 | 1:baaf95f8d272 | 59 | int cnt = 0; |
| barti19941 | 1:baaf95f8d272 | 60 | for (int i = 0; i < NUMBER_OF_LINES; i++) |
| barti19941 | 1:baaf95f8d272 | 61 | file_content[i] = " "; |
| barti19941 | 1:baaf95f8d272 | 62 | file.open("/sd/index.html", ios::in); |
| barti19941 | 1:baaf95f8d272 | 63 | if (file.good()) |
| barti19941 | 1:baaf95f8d272 | 64 | { |
| barti19941 | 1:baaf95f8d272 | 65 | pc.printf("The index.html file has been opened. \r"); |
| barti19941 | 1:baaf95f8d272 | 66 | while(!file.eof()) |
| barti19941 | 1:baaf95f8d272 | 67 | { |
| barti19941 | 1:baaf95f8d272 | 68 | getline(file,file_content[cnt]); |
| barti19941 | 1:baaf95f8d272 | 69 | cnt++; |
| barti19941 | 1:baaf95f8d272 | 70 | } |
| barti19941 | 1:baaf95f8d272 | 71 | file.close(); |
| barti19941 | 1:baaf95f8d272 | 72 | pc.printf("The file has been closed. \r"); |
| barti19941 | 0:795a02b2bb68 | 73 | } |
| barti19941 | 1:baaf95f8d272 | 74 | else |
| barti19941 | 1:baaf95f8d272 | 75 | { |
| barti19941 | 1:baaf95f8d272 | 76 | pc.printf("The file could not be opened. \r"); |
| barti19941 | 1:baaf95f8d272 | 77 | return; |
| barti19941 | 1:baaf95f8d272 | 78 | } |
| barti19941 | 1:baaf95f8d272 | 79 | } |
| barti19941 | 0:795a02b2bb68 | 80 | |
| barti19941 | 1:baaf95f8d272 | 81 | void initializeSerialPC(void) |
| barti19941 | 1:baaf95f8d272 | 82 | { |
| barti19941 | 1:baaf95f8d272 | 83 | pc.baud(115200); |
| barti19941 | 1:baaf95f8d272 | 84 | pc.printf("Serial port initialized. \r"); |
| barti19941 | 0:795a02b2bb68 | 85 | } |
| barti19941 | 1:baaf95f8d272 | 86 | |
| barti19941 | 1:baaf95f8d272 | 87 | int main() |
| barti19941 | 1:baaf95f8d272 | 88 | { |
| barti19941 | 1:baaf95f8d272 | 89 | initializeSerialPC(); |
| barti19941 | 1:baaf95f8d272 | 90 | readHTMLCode(); |
| barti19941 | 1:baaf95f8d272 | 91 | initializeEthernet(); |
| barti19941 | 1:baaf95f8d272 | 92 | while(1); |
| barti19941 | 1:baaf95f8d272 | 93 | } |