Niranjan Ravi
/
ESP8266_Niranjan
Working code from Niranjan
main.cpp@0:8ca71ccb52db, 2016-06-06 (annotated)
- Committer:
- SIT2016
- Date:
- Mon Jun 06 11:45:26 2016 +0000
- Revision:
- 0:8ca71ccb52db
- Child:
- 1:2012c31aee1b
Uploading POT value to local phant server using ESP8266;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SIT2016 | 0:8ca71ccb52db | 1 | #include "mbed.h" |
SIT2016 | 0:8ca71ccb52db | 2 | #include "ESP8266.h" |
SIT2016 | 0:8ca71ccb52db | 3 | |
SIT2016 | 0:8ca71ccb52db | 4 | Serial pc(USBTX,USBRX); |
SIT2016 | 0:8ca71ccb52db | 5 | AnalogIn pot(PTB0); // pir senor input |
SIT2016 | 0:8ca71ccb52db | 6 | ESP8266 wifi(PTE0, PTE1, 115200); // baud rate for wifi |
SIT2016 | 0:8ca71ccb52db | 7 | char snd[255],rcv[1000]; |
SIT2016 | 0:8ca71ccb52db | 8 | char http_cmd[300]; |
SIT2016 | 0:8ca71ccb52db | 9 | |
SIT2016 | 0:8ca71ccb52db | 10 | //#define IP "data.sparkfun.com" |
SIT2016 | 0:8ca71ccb52db | 11 | //#define IP "184.106.153.149" |
SIT2016 | 0:8ca71ccb52db | 12 | #define IP "192.168.0.39" |
SIT2016 | 0:8ca71ccb52db | 13 | |
SIT2016 | 0:8ca71ccb52db | 14 | float val = 0; //global variable |
SIT2016 | 0:8ca71ccb52db | 15 | |
SIT2016 | 0:8ca71ccb52db | 16 | char* Public_Key = "Xb3ovW3grvIMo31jd8p0HmPEba3O"; |
SIT2016 | 0:8ca71ccb52db | 17 | char* Private_Key = "LPZe7qZLg7t0NnWbYD2XHrmB2AMq"; |
SIT2016 | 0:8ca71ccb52db | 18 | |
SIT2016 | 0:8ca71ccb52db | 19 | /************ WiFi INTIALIZATION *********/ |
SIT2016 | 0:8ca71ccb52db | 20 | |
SIT2016 | 0:8ca71ccb52db | 21 | void wifi_initialize(void); |
SIT2016 | 0:8ca71ccb52db | 22 | void wifi_send(void); |
SIT2016 | 0:8ca71ccb52db | 23 | |
SIT2016 | 0:8ca71ccb52db | 24 | int main () { |
SIT2016 | 0:8ca71ccb52db | 25 | |
SIT2016 | 0:8ca71ccb52db | 26 | |
SIT2016 | 0:8ca71ccb52db | 27 | wifi_initialize(); |
SIT2016 | 0:8ca71ccb52db | 28 | |
SIT2016 | 0:8ca71ccb52db | 29 | while (1) { |
SIT2016 | 0:8ca71ccb52db | 30 | |
SIT2016 | 0:8ca71ccb52db | 31 | val = pot.read(); |
SIT2016 | 0:8ca71ccb52db | 32 | pc.printf("\r\nPotentiometer value=%.3f \r\n", val); |
SIT2016 | 0:8ca71ccb52db | 33 | pc.printf("Sending WiFi information\n\r"); |
SIT2016 | 0:8ca71ccb52db | 34 | wifi_send(); |
SIT2016 | 0:8ca71ccb52db | 35 | wait(4); |
SIT2016 | 0:8ca71ccb52db | 36 | } |
SIT2016 | 0:8ca71ccb52db | 37 | } |
SIT2016 | 0:8ca71ccb52db | 38 | |
SIT2016 | 0:8ca71ccb52db | 39 | void wifi_initialize(){ |
SIT2016 | 0:8ca71ccb52db | 40 | |
SIT2016 | 0:8ca71ccb52db | 41 | pc.baud(9600); |
SIT2016 | 0:8ca71ccb52db | 42 | pc.printf("SET mode to AP\r\n"); |
SIT2016 | 0:8ca71ccb52db | 43 | wifi.SetMode(1); // set ESP mode to 1 |
SIT2016 | 0:8ca71ccb52db | 44 | wifi.RcvReply(rcv, 1000); //receive a response from ESP |
SIT2016 | 0:8ca71ccb52db | 45 | pc.printf("%s",rcv); //Print the response onscreen |
SIT2016 | 0:8ca71ccb52db | 46 | pc.printf("Conneting to AP\r\n"); |
SIT2016 | 0:8ca71ccb52db | 47 | wifi.Join("Eduvance_WiFi", "eduvance"); // Your wifi username & Password |
SIT2016 | 0:8ca71ccb52db | 48 | wifi.RcvReply(rcv, 1000); //receive a response from ESP |
SIT2016 | 0:8ca71ccb52db | 49 | pc.printf("%s", rcv); //Print the response onscreen |
SIT2016 | 0:8ca71ccb52db | 50 | wait(8); //waits for response from ESP |
SIT2016 | 0:8ca71ccb52db | 51 | pc.printf("Getting IP\r\n"); //get IP addresss from the connected AP |
SIT2016 | 0:8ca71ccb52db | 52 | wifi.GetIP(rcv); //receive an IP address from the AP |
SIT2016 | 0:8ca71ccb52db | 53 | pc.printf("%s", rcv); |
SIT2016 | 0:8ca71ccb52db | 54 | wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding |
SIT2016 | 0:8ca71ccb52db | 55 | pc.printf("Initializing WiFi\r\n"); |
SIT2016 | 0:8ca71ccb52db | 56 | } |
SIT2016 | 0:8ca71ccb52db | 57 | |
SIT2016 | 0:8ca71ccb52db | 58 | void wifi_send(void){ |
SIT2016 | 0:8ca71ccb52db | 59 | |
SIT2016 | 0:8ca71ccb52db | 60 | strcpy(snd,"AT+CIPMODE=0");//Setting WiFi into MultiChannel mode |
SIT2016 | 0:8ca71ccb52db | 61 | wifi.SendCMD(snd); |
SIT2016 | 0:8ca71ccb52db | 62 | //pc.printf(snd); |
SIT2016 | 0:8ca71ccb52db | 63 | wifi.RcvReply(rcv, 3000); |
SIT2016 | 0:8ca71ccb52db | 64 | pc.printf("%s", rcv); |
SIT2016 | 0:8ca71ccb52db | 65 | |
SIT2016 | 0:8ca71ccb52db | 66 | //WIFI updates the Status to Thingspeak servers// |
SIT2016 | 0:8ca71ccb52db | 67 | strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode |
SIT2016 | 0:8ca71ccb52db | 68 | wifi.SendCMD(snd); |
SIT2016 | 0:8ca71ccb52db | 69 | //pc.printf(snd); |
SIT2016 | 0:8ca71ccb52db | 70 | wifi.RcvReply(rcv, 3000); |
SIT2016 | 0:8ca71ccb52db | 71 | pc.printf("%s", rcv); |
SIT2016 | 0:8ca71ccb52db | 72 | |
SIT2016 | 0:8ca71ccb52db | 73 | sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",8080",IP); //Initiate connection with server |
SIT2016 | 0:8ca71ccb52db | 74 | wifi.SendCMD(snd); |
SIT2016 | 0:8ca71ccb52db | 75 | //pc.printf(snd); |
SIT2016 | 0:8ca71ccb52db | 76 | wifi.RcvReply(rcv, 3000); |
SIT2016 | 0:8ca71ccb52db | 77 | pc.printf("%s", rcv); |
SIT2016 | 0:8ca71ccb52db | 78 | |
SIT2016 | 0:8ca71ccb52db | 79 | sprintf(http_cmd,"GET /input/%s?private_key=%s&val=%0.2f HTTP/1.0\n\n",Public_Key,Private_Key,val); //Post values |
SIT2016 | 0:8ca71ccb52db | 80 | pc.printf("%d",strlen(http_cmd)); |
SIT2016 | 0:8ca71ccb52db | 81 | strcpy(snd,"AT+CIPSEND=4,105"); //Send Number of open connections,Characters to send |
SIT2016 | 0:8ca71ccb52db | 82 | wifi.SendCMD(snd); |
SIT2016 | 0:8ca71ccb52db | 83 | //pc.printf(snd); |
SIT2016 | 0:8ca71ccb52db | 84 | wifi.RcvReply(rcv, 3000); |
SIT2016 | 0:8ca71ccb52db | 85 | pc.printf("%s", rcv); |
SIT2016 | 0:8ca71ccb52db | 86 | |
SIT2016 | 0:8ca71ccb52db | 87 | sprintf(snd,"GET /input/%s?private_key=%s&val=%0.2f HTTP/1.0\n\n",Public_Key,Private_Key,val); //Post values |
SIT2016 | 0:8ca71ccb52db | 88 | strcpy(snd, http_cmd); |
SIT2016 | 0:8ca71ccb52db | 89 | pc.printf("%s",http_cmd); |
SIT2016 | 0:8ca71ccb52db | 90 | wifi.SendCMD(http_cmd); |
SIT2016 | 0:8ca71ccb52db | 91 | wifi.RcvReply(rcv, 3000); |
SIT2016 | 0:8ca71ccb52db | 92 | pc.printf("%s", rcv); |
SIT2016 | 0:8ca71ccb52db | 93 | |
SIT2016 | 0:8ca71ccb52db | 94 | //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server |
SIT2016 | 0:8ca71ccb52db | 95 | //wifi.RcvReply(rcv, 3000); |
SIT2016 | 0:8ca71ccb52db | 96 | //pc.printf("%s", rcv); |
SIT2016 | 0:8ca71ccb52db | 97 | } |