![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
project
Dependencies: mbed HTTPClient LM75B mbed-rtos EthernetInterface MMA7660
main.cpp@2:f7645c6a85f3, 2021-08-16 (annotated)
- Committer:
- steveshun
- Date:
- Mon Aug 16 16:06:35 2021 +0000
- Revision:
- 2:f7645c6a85f3
- Parent:
- 1:21d76b260bc6
- Child:
- 3:e68b56ffa127
Lab 6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steveshun | 2:f7645c6a85f3 | 1 | #include "mbed.h" |
steveshun | 2:f7645c6a85f3 | 2 | #include "HTTPClient.h" |
steveshun | 2:f7645c6a85f3 | 3 | #include "LM75B.h" |
steveshun | 2:f7645c6a85f3 | 4 | #include "MMA7660.h" |
steveshun | 2:f7645c6a85f3 | 5 | #include "EthernetInterface.h" |
omar1646 | 1:21d76b260bc6 | 6 | |
omar1646 | 1:21d76b260bc6 | 7 | |
steveshun | 2:f7645c6a85f3 | 8 | LM75B sensor(D14,D15); |
steveshun | 2:f7645c6a85f3 | 9 | MMA7660 accel(PTE25 , PTE24); |
steveshun | 2:f7645c6a85f3 | 10 | char buffer[256]; |
steveshun | 2:f7645c6a85f3 | 11 | char resp[1024]; |
steveshun | 2:f7645c6a85f3 | 12 | char val[4][16]; |
steveshun | 2:f7645c6a85f3 | 13 | int ret = 0; |
eduvanceIoT | 0:34d3f68b920e | 14 | |
omar1646 | 1:21d76b260bc6 | 15 | Serial pc(USBTX,USBRX); |
steveshun | 2:f7645c6a85f3 | 16 | //Ethernet Interface |
steveshun | 2:f7645c6a85f3 | 17 | EthernetInterface eth; |
steveshun | 2:f7645c6a85f3 | 18 | //HTTP Client for interfacing to web services |
steveshun | 2:f7645c6a85f3 | 19 | HTTPClient http; |
omar1646 | 1:21d76b260bc6 | 20 | #define IP "184.106.153.149" // thingspeak.com IP Address |
eduvanceIoT | 0:34d3f68b920e | 21 | |
steveshun | 2:f7645c6a85f3 | 22 | //http client init |
steveshun | 2:f7645c6a85f3 | 23 | HTTPMap map; |
steveshun | 2:f7645c6a85f3 | 24 | HTTPText inText(resp, 1024); |
omar1646 | 1:21d76b260bc6 | 25 | //String thingtweetAPIKey = "TTEVLP931ODJ5GMT"; |
omar1646 | 1:21d76b260bc6 | 26 | |
omar1646 | 1:21d76b260bc6 | 27 | |
steveshun | 2:f7645c6a85f3 | 28 | int main () { |
steveshun | 2:f7645c6a85f3 | 29 | //ethernet init |
steveshun | 2:f7645c6a85f3 | 30 | eth.init(); //Use DHCP |
steveshun | 2:f7645c6a85f3 | 31 | ret= eth.connect(); |
omar1646 | 1:21d76b260bc6 | 32 | |
steveshun | 2:f7645c6a85f3 | 33 | if (!ret){ |
steveshun | 2:f7645c6a85f3 | 34 | pc.printf("\r\nConnected, IP: %s, MASK: %s, GW: %s", |
steveshun | 2:f7645c6a85f3 | 35 | eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); |
steveshun | 2:f7645c6a85f3 | 36 | } else { |
steveshun | 2:f7645c6a85f3 | 37 | pc.printf("\r\nError eth.connect() - ret = %d", ret); |
steveshun | 2:f7645c6a85f3 | 38 | } |
steveshun | 2:f7645c6a85f3 | 39 | |
steveshun | 2:f7645c6a85f3 | 40 | while(1){ |
steveshun | 2:f7645c6a85f3 | 41 | pc.printf("\r\nWriting to thingspeak"); |
steveshun | 2:f7645c6a85f3 | 42 | map.put("api_key","xxx"); /* Fill your thingspeak API KEY*/ |
steveshun | 2:f7645c6a85f3 | 43 | sprintf(val[0],"%4.1f",sensor.read()); |
steveshun | 2:f7645c6a85f3 | 44 | map.put("field1", val[0]); |
steveshun | 2:f7645c6a85f3 | 45 | pc.printf("\r\nPosting Data..."); |
steveshun | 2:f7645c6a85f3 | 46 | ret = http.post("https://api.thingspeak.com/update", map, &inText); //writing data to Thingspeak |
steveshun | 2:f7645c6a85f3 | 47 | if (!ret){ |
steveshun | 2:f7645c6a85f3 | 48 | pc.printf("\r\nPOST successfully - read %d characters", strlen(resp)); |
steveshun | 2:f7645c6a85f3 | 49 | pc.printf("\r\nResult: %s\n", resp); |
steveshun | 2:f7645c6a85f3 | 50 | }else{ |
steveshun | 2:f7645c6a85f3 | 51 | pc.printf("\r\nError Connecting to ethernet port - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode()); |
omar1646 | 1:21d76b260bc6 | 52 | } |
eduvanceIoT | 0:34d3f68b920e | 53 | } |
omar1646 | 1:21d76b260bc6 | 54 | } |