Solarator

Dependencies:   ESP8266NodeMCUInterface mbed

Fork of Solarator_0-0-1 by Uzair Akbar

Files at this revision

API Documentation at this revision

Comitter:
UzairAkbar
Date:
Mon Dec 28 20:36:21 2015 +0000
Parent:
0:ed69e8ef8c7f
Commit message:
Solarator

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ed69e8ef8c7f -r 408512add17c main.cpp
--- a/main.cpp	Sun Dec 20 17:30:46 2015 +0000
+++ b/main.cpp	Mon Dec 28 20:36:21 2015 +0000
@@ -6,53 +6,81 @@
 #include <stdlib.h>
 using namespace std;
 
+DigitalIn mybutton(USER_BUTTON);
 DigitalOut myled(LED1);
-ESP8266 ark(PA_9,PA_10,D3,115200);
+ESP8266 wifi(PA_9,PA_10,D3,115200);
 Serial pc(SERIAL_TX, SERIAL_RX);
 TCPSocketConnection gm;
-AnalogIn vr(A0);
+AnalogIn Current(A0);
+AnalogIn Voltage(A1);
 
-char* generatePostRequest(float _value)
+
+char* generatePostRequest(float current, float voltage)
 {
-    char post[] = "GET /update?key=7577C0JKGHRPAQ6Q&field1=";
-    char msg[50] = "";
-    sprintf(msg,"%f\r\n\r\n",_value*500);
+    char post[] = "GET /update?key=";
+    char ThingSpeak_key[] = "3BCEJZ4D93E63PNN";
+    char current_field[] = "&field1=";
+    char voltage_field[] = "&field2=";
+    char coordinates[] = "";
+    char status[] = "";
+    char msg[75] = "";
+    
+    sprintf(msg, "%s%s%s%f%s%f%s%s\r\n\r\n", post, ThingSpeak_key, current_field, current, voltage_field, voltage, coordinates, status);
 
-    char* postRequest = strcat(post,msg);
-    return postRequest;
+    return msg;
 }
 
 int main()
 {
     pc.baud(115200);
-    int i=0;
+    
+    int init_flag=0;
+    float sum_current = 0.0;
+    float sum_voltage = 0.0;
+    float I = 0.0;
+    float V = 0.0;
+    
     while(1) {
-        if(i==0) {
+        if(init_flag==0) {
 hardwareInit:
             pc.printf("intializing hardware,...\r\n");
-            if(ark.init()) {
+            if(wifi.init()) {
                 pc.printf("hardware intialized!\r\n");
-                i=1;
+                init_flag = 1;
 nustConnection:
                 pc.printf("connecting to NUST,...\r\n");
-                if(ark.connect("NUST","nust008tech")) {
+                if(wifi.connect("NUST","nust008tech")) {
 
                     pc.printf("NUST Connected!\r\n");
-                    if(ark.is_connected()) {
+                    if(wifi.is_connected()) {
                         pc.printf("connection confirmed!\r\n");
-                        pc.printf("IP Adress: %s\r\n",ark.getIPAddress());
+                        pc.printf("IP Adress: %s\r\n",wifi.getIPAddress());
 hostConnection:
                         pc.printf("connecting to Host,...\r\n");
+                        while(1)
+                        {
+                        sum_current = 0.0;
+                        sum_voltage = 0.0;    
+                            
                         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";
+                            /////////////////////////////////////////////////////////////////////////////////
+                            for (int i = 0; i < 100; i++)
+                            {
+                                sum_current += Current.read();
+                                sum_voltage += Voltage.read();
+                                wait_us(50);
+                            }
+                            I = sum_current/100;
+                            V = sum_voltage/100;
+                            
+                            I = (I-0.5)*3.3/0.185;
+                            V = V;
+                            
+                            pc.printf("%.4f\n\r", I);
+                            
+                            char* getRequest = generatePostRequest(I, V);
+                            /////////////////////////////////////////////////////////////////////////////////
 sendGETRequest:
                             pc.printf("sending GET request,...\r\n");
                             if(gm.send_all(getRequest,strlen(getRequest))>0) {
@@ -76,10 +104,12 @@
                                 pc.printf("GET request failed!\r\n");
                                 goto sendGETRequest;
                             }
-                        } else {
+                        }else {
                             pc.printf("Host Connection Failed\r\n");
                             goto hostConnection;
                         }
+                        wait(10);
+                        }
                     } else {
                         pc.printf("sonething went wrong with connection. not connected \r\n");
                         goto nustConnection;