thingspeal
Dependencies: ESP8266NodeMCUInterface mbed
main.cpp
00001 #include "mbed.h" 00002 #include "ESP8266.h" 00003 #include "TCPSocketConnection.h" 00004 #include <string> 00005 #include <stdio.h> 00006 #include <stdlib.h> 00007 using namespace std; 00008 00009 DigitalOut myled(LED1); 00010 ESP8266 ark(PA_9,PA_10,D3,115200); 00011 Serial pc(SERIAL_TX, SERIAL_RX); 00012 TCPSocketConnection gm; 00013 AnalogIn vr(A0); 00014 00015 char* generatePostRequest(float _value) 00016 { 00017 char post[] = "GET /update?key=7577C0JKGHRPAQ6Q&field1="; 00018 char msg[50] = ""; 00019 sprintf(msg,"%f\r\n\r\n",_value*500); 00020 00021 char* postRequest = strcat(post,msg); 00022 return postRequest; 00023 } 00024 00025 int main() 00026 { 00027 pc.baud(115200); 00028 int i=0; 00029 while(1) { 00030 if(i==0) { 00031 hardwareInit: 00032 pc.printf("intializing hardware,...\r\n"); 00033 if(ark.init()) { 00034 pc.printf("hardware intialized!\r\n"); 00035 i=1; 00036 nustConnection: 00037 pc.printf("connecting to NUST,...\r\n"); 00038 if(ark.connect("NUST","nust008tech")) { 00039 00040 pc.printf("NUST Connected!\r\n"); 00041 if(ark.is_connected()) { 00042 pc.printf("connection confirmed!\r\n"); 00043 pc.printf("IP Adress: %s\r\n",ark.getIPAddress()); 00044 hostConnection: 00045 pc.printf("connecting to Host,...\r\n"); 00046 if(gm.connect("184.106.153.149",80)==0) { 00047 pc.printf("Host connected.\r\n"); 00048 00049 //pc.printf(generatePostRequest(vr.read())); 00050 //char getRequest[] = "http://api.thingspeak.com/update?key=3BCEJZ4D93E63PNN&field1=429"; 00051 //char getRequest[500]="POST /api.php/phy HTTP/1.1\r\nHOST: www.maz4579.esy.es\r\nConnection: keep-alive\r\nAccept: */*\r\n{\"id\":\"5\",\"statement\":\"is the id of this question is 4?\",\"A\":\"no\",\"B\":\"no\",\"C\":\"no\",\"D\":\"yes\",\"correct\":\"C\",\"isvalid\":\"False\"}\r\n\r\n"; 00052 char* getRequest = generatePostRequest(vr.read()); 00053 //strcpy(getRequest,generatePostRequest((float)vr.read())); 00054 //char getRequest[] = "GET /update?key=3BCEJZ4D93E63PNN&field1=429\r\n\r\n"; 00055 //char getRequest[] = "GET /testwifi/index.html HTTP/1.1\r\nHost: www.adafruit.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"; 00056 sendGETRequest: 00057 pc.printf("sending GET request,...\r\n"); 00058 if(gm.send_all(getRequest,strlen(getRequest))>0) { 00059 pc.printf("GOT!\r\n"); 00060 00061 char getResponce[5000]= {}; 00062 pc.printf("reading responce of GET request!\r\n"); 00063 int resBytes = gm.receive_all(getResponce,5000); 00064 if(resBytes > 0) { 00065 pc.printf("Responce Received!\r\n"); 00066 pc.printf("Responce:\r\n%s\r\nResponce Read Sucessfull.\r\n",getResponce); 00067 } else if(resBytes==0) { 00068 pc.printf("Empty Responce!\r\n"); 00069 //goto sendGETRequest; 00070 } else { 00071 pc.printf("Resopnce NOT Received!\r\n"); 00072 goto sendGETRequest; 00073 } 00074 00075 } else { 00076 pc.printf("GET request failed!\r\n"); 00077 goto sendGETRequest; 00078 } 00079 } else { 00080 pc.printf("Host Connection Failed\r\n"); 00081 goto hostConnection; 00082 } 00083 } else { 00084 pc.printf("sonething went wrong with connection. not connected \r\n"); 00085 goto nustConnection; 00086 } 00087 } else { 00088 pc.printf("conenction fail!\r\n"); 00089 goto nustConnection; 00090 } 00091 } else { 00092 pc.printf("hardware not intialized!\r\n"); 00093 goto hardwareInit; 00094 } 00095 } 00096 wait(1); 00097 } 00098 }
Generated on Fri Jul 15 2022 06:00:22 by
1.7.2