![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ok
main.cpp@0:e1c22ca27f03, 2021-11-12 (annotated)
- Committer:
- kefil_tonouewa
- Date:
- Fri Nov 12 04:07:52 2021 +0000
- Revision:
- 0:e1c22ca27f03
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kefil_tonouewa | 0:e1c22ca27f03 | 1 | /* mbed Microcontroller Library |
kefil_tonouewa | 0:e1c22ca27f03 | 2 | * Copyright (c) 2019 ARM Limited |
kefil_tonouewa | 0:e1c22ca27f03 | 3 | * SPDX-License-Identifier: Apache-2.0 |
kefil_tonouewa | 0:e1c22ca27f03 | 4 | */ |
kefil_tonouewa | 0:e1c22ca27f03 | 5 | |
kefil_tonouewa | 0:e1c22ca27f03 | 6 | #include "mbed.h" |
kefil_tonouewa | 0:e1c22ca27f03 | 7 | #include "platform/mbed_thread.h" |
kefil_tonouewa | 0:e1c22ca27f03 | 8 | #include "ESP8266.h" |
kefil_tonouewa | 0:e1c22ca27f03 | 9 | #include "math.h" |
kefil_tonouewa | 0:e1c22ca27f03 | 10 | |
kefil_tonouewa | 0:e1c22ca27f03 | 11 | Serial pc(USBTX,USBRX); |
kefil_tonouewa | 0:e1c22ca27f03 | 12 | DigitalOut rled(LED1); |
kefil_tonouewa | 0:e1c22ca27f03 | 13 | |
kefil_tonouewa | 0:e1c22ca27f03 | 14 | //wifi UART port and baud rate |
kefil_tonouewa | 0:e1c22ca27f03 | 15 | ESP8266 wifi(PTC17, PTC16, 115200); // Class Variable Pin dexlaration for WIFI |
kefil_tonouewa | 0:e1c22ca27f03 | 16 | |
kefil_tonouewa | 0:e1c22ca27f03 | 17 | //buffers for wifi library |
kefil_tonouewa | 0:e1c22ca27f03 | 18 | char snd[255],resp[9000]; |
kefil_tonouewa | 0:e1c22ca27f03 | 19 | char http_cmd[300], comm[300]; |
kefil_tonouewa | 0:e1c22ca27f03 | 20 | char url_response[9000]; |
kefil_tonouewa | 0:e1c22ca27f03 | 21 | |
kefil_tonouewa | 0:e1c22ca27f03 | 22 | int timeout = 5000; //timeout for wifi commands |
kefil_tonouewa | 0:e1c22ca27f03 | 23 | |
kefil_tonouewa | 0:e1c22ca27f03 | 24 | //SSID and password for connection |
kefil_tonouewa | 0:e1c22ca27f03 | 25 | #define SSID "Mahutin-2.4" |
kefil_tonouewa | 0:e1c22ca27f03 | 26 | #define PASS "12220517" |
kefil_tonouewa | 0:e1c22ca27f03 | 27 | |
kefil_tonouewa | 0:e1c22ca27f03 | 28 | //Remote IP |
kefil_tonouewa | 0:e1c22ca27f03 | 29 | #define IP "184.106.153.149" // IP for thingspeak server. Remains same for al codes using thingspeak |
kefil_tonouewa | 0:e1c22ca27f03 | 30 | //Public and private keys for phant |
kefil_tonouewa | 0:e1c22ca27f03 | 31 | char* Update_Key = "JL9J9L60TE2VXV4K"; |
kefil_tonouewa | 0:e1c22ca27f03 | 32 | char* Read_Key = "GTLNT8B1ERTG0SUQ"; |
kefil_tonouewa | 0:e1c22ca27f03 | 33 | |
kefil_tonouewa | 0:e1c22ca27f03 | 34 | //Wifi init function. Sets the module to connect to wifi accesspoint with ssid and password mentioned above |
kefil_tonouewa | 0:e1c22ca27f03 | 35 | void wifi_initialize(void){ |
kefil_tonouewa | 0:e1c22ca27f03 | 36 | |
kefil_tonouewa | 0:e1c22ca27f03 | 37 | pc.printf("******** Resetting wifi module ********\r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 38 | wifi.reset(); |
kefil_tonouewa | 0:e1c22ca27f03 | 39 | |
kefil_tonouewa | 0:e1c22ca27f03 | 40 | //wait for 5 seconds for response, else display no response receiveed |
kefil_tonouewa | 0:e1c22ca27f03 | 41 | if (wifi.rcvReply(resp, 5000)) |
kefil_tonouewa | 0:e1c22ca27f03 | 42 | pc.printf("%s",resp); |
kefil_tonouewa | 0:e1c22ca27f03 | 43 | else |
kefil_tonouewa | 0:e1c22ca27f03 | 44 | pc.printf("No response"); |
kefil_tonouewa | 0:e1c22ca27f03 | 45 | |
kefil_tonouewa | 0:e1c22ca27f03 | 46 | pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 47 | wifi.SetMode(1); // set transparent mode |
kefil_tonouewa | 0:e1c22ca27f03 | 48 | if (wifi.RcvReply(resp, timeout)) //receive a response from ESP |
kefil_tonouewa | 0:e1c22ca27f03 | 49 | pc.printf("%s",resp); //Print the response onscreen |
kefil_tonouewa | 0:e1c22ca27f03 | 50 | else |
kefil_tonouewa | 0:e1c22ca27f03 | 51 | pc.printf("No response while setting mode. \r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 52 | |
kefil_tonouewa | 0:e1c22ca27f03 | 53 | pc.printf("******** Joining network with SSID and PASS ********\r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 54 | wifi.Join(SSID, PASS); |
kefil_tonouewa | 0:e1c22ca27f03 | 55 | if (wifi.RcvReply(resp, timeout)) |
kefil_tonouewa | 0:e1c22ca27f03 | 56 | pc.printf("%s",resp); |
kefil_tonouewa | 0:e1c22ca27f03 | 57 | else |
kefil_tonouewa | 0:e1c22ca27f03 | 58 | pc.printf("No response while connecting to network \r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 59 | |
kefil_tonouewa | 0:e1c22ca27f03 | 60 | pc.printf("******** Getting IP and MAC of module ********\r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 61 | wifi.GetIP(resp); |
kefil_tonouewa | 0:e1c22ca27f03 | 62 | if (wifi.RcvReply(resp, timeout)) |
kefil_tonouewa | 0:e1c22ca27f03 | 63 | pc.printf("%s",resp); |
kefil_tonouewa | 0:e1c22ca27f03 | 64 | else |
kefil_tonouewa | 0:e1c22ca27f03 | 65 | pc.printf("No response while getting IP \r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 66 | |
kefil_tonouewa | 0:e1c22ca27f03 | 67 | pc.printf("******** Setting WIFI UART passthrough ********\r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 68 | wifi.setTransparent(); |
kefil_tonouewa | 0:e1c22ca27f03 | 69 | if (wifi.RcvReply(resp, timeout)) |
kefil_tonouewa | 0:e1c22ca27f03 | 70 | pc.printf("%s",resp); |
kefil_tonouewa | 0:e1c22ca27f03 | 71 | else |
kefil_tonouewa | 0:e1c22ca27f03 | 72 | pc.printf("No response while setting wifi passthrough. \r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 73 | wait(1); |
kefil_tonouewa | 0:e1c22ca27f03 | 74 | |
kefil_tonouewa | 0:e1c22ca27f03 | 75 | pc.printf("******** Setting single connection mode ********\r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 76 | wifi.SetSingle(); |
kefil_tonouewa | 0:e1c22ca27f03 | 77 | wifi.RcvReply(resp, timeout); |
kefil_tonouewa | 0:e1c22ca27f03 | 78 | if (wifi.RcvReply(resp, timeout)) |
kefil_tonouewa | 0:e1c22ca27f03 | 79 | pc.printf("%s",resp); |
kefil_tonouewa | 0:e1c22ca27f03 | 80 | else |
kefil_tonouewa | 0:e1c22ca27f03 | 81 | pc.printf("No response while setting single connection \r\n"); |
kefil_tonouewa | 0:e1c22ca27f03 | 82 | wait(1); |
kefil_tonouewa | 0:e1c22ca27f03 | 83 | } |
kefil_tonouewa | 0:e1c22ca27f03 | 84 | |
kefil_tonouewa | 0:e1c22ca27f03 | 85 | |
kefil_tonouewa | 0:e1c22ca27f03 | 86 | |
kefil_tonouewa | 0:e1c22ca27f03 | 87 | |
kefil_tonouewa | 0:e1c22ca27f03 | 88 | int main() |
kefil_tonouewa | 0:e1c22ca27f03 | 89 | { |
kefil_tonouewa | 0:e1c22ca27f03 | 90 | |
kefil_tonouewa | 0:e1c22ca27f03 | 91 | wifi_initialize(); // Calls function defined above to initialize the wifi module |
kefil_tonouewa | 0:e1c22ca27f03 | 92 | while (true) { |
kefil_tonouewa | 0:e1c22ca27f03 | 93 | |
kefil_tonouewa | 0:e1c22ca27f03 | 94 | |
kefil_tonouewa | 0:e1c22ca27f03 | 95 | } |
kefil_tonouewa | 0:e1c22ca27f03 | 96 | } |