thingspeal

Dependencies:   ESP8266NodeMCUInterface mbed

Revision:
0:ed69e8ef8c7f
diff -r 000000000000 -r ed69e8ef8c7f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Dec 20 17:30:46 2015 +0000
@@ -0,0 +1,98 @@
+#include "mbed.h"
+#include "ESP8266.h"
+#include "TCPSocketConnection.h"
+#include <string>
+#include <stdio.h>
+#include <stdlib.h>
+using namespace std;
+
+DigitalOut myled(LED1);
+ESP8266 ark(PA_9,PA_10,D3,115200);
+Serial pc(SERIAL_TX, SERIAL_RX);
+TCPSocketConnection gm;
+AnalogIn vr(A0);
+
+char* generatePostRequest(float _value)
+{
+    char post[] = "GET /update?key=7577C0JKGHRPAQ6Q&field1=";
+    char msg[50] = "";
+    sprintf(msg,"%f\r\n\r\n",_value*500);
+
+    char* postRequest = strcat(post,msg);
+    return postRequest;
+}
+
+int main()
+{
+    pc.baud(115200);
+    int i=0;
+    while(1) {
+        if(i==0) {
+hardwareInit:
+            pc.printf("intializing hardware,...\r\n");
+            if(ark.init()) {
+                pc.printf("hardware intialized!\r\n");
+                i=1;
+nustConnection:
+                pc.printf("connecting to NUST,...\r\n");
+                if(ark.connect("NUST","nust008tech")) {
+
+                    pc.printf("NUST Connected!\r\n");
+                    if(ark.is_connected()) {
+                        pc.printf("connection confirmed!\r\n");
+                        pc.printf("IP Adress: %s\r\n",ark.getIPAddress());
+hostConnection:
+                        pc.printf("connecting to Host,...\r\n");
+                        if(gm.connect("184.106.153.149",80)==0) {
+                            pc.printf("Host connected.\r\n");
+
+                            //pc.printf(generatePostRequest(vr.read()));
+                            //char getRequest[] = "http://api.thingspeak.com/update?key=3BCEJZ4D93E63PNN&field1=429";
+                            //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";
+                            char* getRequest = generatePostRequest(vr.read());
+                            //strcpy(getRequest,generatePostRequest((float)vr.read()));
+                            //char getRequest[] = "GET /update?key=3BCEJZ4D93E63PNN&field1=429\r\n\r\n";
+                            //char getRequest[] = "GET /testwifi/index.html HTTP/1.1\r\nHost: www.adafruit.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n";
+sendGETRequest:
+                            pc.printf("sending GET request,...\r\n");
+                            if(gm.send_all(getRequest,strlen(getRequest))>0) {
+                                pc.printf("GOT!\r\n");
+
+                                char getResponce[5000]= {};
+                                pc.printf("reading responce of GET request!\r\n");
+                                int resBytes = gm.receive_all(getResponce,5000);
+                                if(resBytes > 0) {
+                                    pc.printf("Responce Received!\r\n");
+                                    pc.printf("Responce:\r\n%s\r\nResponce Read Sucessfull.\r\n",getResponce);
+                                } else if(resBytes==0) {
+                                    pc.printf("Empty Responce!\r\n");
+                                    //goto sendGETRequest;
+                                } else {
+                                    pc.printf("Resopnce NOT Received!\r\n");
+                                    goto sendGETRequest;
+                                }
+
+                            } else {
+                                pc.printf("GET request failed!\r\n");
+                                goto sendGETRequest;
+                            }
+                        } else {
+                            pc.printf("Host Connection Failed\r\n");
+                            goto hostConnection;
+                        }
+                    } else {
+                        pc.printf("sonething went wrong with connection. not connected \r\n");
+                        goto nustConnection;
+                    }
+                } else {
+                    pc.printf("conenction fail!\r\n");
+                    goto nustConnection;
+                }
+            } else {
+                pc.printf("hardware not intialized!\r\n");
+                goto hardwareInit;
+            }
+        }
+        wait(1);
+    }
+}