Solarator

Dependencies:   ESP8266NodeMCUInterface mbed

Fork of Solarator_0-0-1 by Uzair Akbar

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 DigitalIn mybutton(USER_BUTTON);
00010 DigitalOut myled(LED1);
00011 ESP8266 wifi(PA_9,PA_10,D3,115200);
00012 Serial pc(SERIAL_TX, SERIAL_RX);
00013 TCPSocketConnection gm;
00014 AnalogIn Current(A0);
00015 AnalogIn Voltage(A1);
00016 
00017 
00018 char* generatePostRequest(float current, float voltage)
00019 {
00020     char post[] = "GET /update?key=";
00021     char ThingSpeak_key[] = "3BCEJZ4D93E63PNN";
00022     char current_field[] = "&field1=";
00023     char voltage_field[] = "&field2=";
00024     char coordinates[] = "";
00025     char status[] = "";
00026     char msg[75] = "";
00027     
00028     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);
00029 
00030     return msg;
00031 }
00032 
00033 int main()
00034 {
00035     pc.baud(115200);
00036     
00037     int init_flag=0;
00038     float sum_current = 0.0;
00039     float sum_voltage = 0.0;
00040     float I = 0.0;
00041     float V = 0.0;
00042     
00043     while(1) {
00044         if(init_flag==0) {
00045 hardwareInit:
00046             pc.printf("intializing hardware,...\r\n");
00047             if(wifi.init()) {
00048                 pc.printf("hardware intialized!\r\n");
00049                 init_flag = 1;
00050 nustConnection:
00051                 pc.printf("connecting to NUST,...\r\n");
00052                 if(wifi.connect("NUST","nust008tech")) {
00053 
00054                     pc.printf("NUST Connected!\r\n");
00055                     if(wifi.is_connected()) {
00056                         pc.printf("connection confirmed!\r\n");
00057                         pc.printf("IP Adress: %s\r\n",wifi.getIPAddress());
00058 hostConnection:
00059                         pc.printf("connecting to Host,...\r\n");
00060                         while(1)
00061                         {
00062                         sum_current = 0.0;
00063                         sum_voltage = 0.0;    
00064                             
00065                         if(gm.connect("184.106.153.149",80)==0) {
00066                             pc.printf("Host connected.\r\n");
00067                             /////////////////////////////////////////////////////////////////////////////////
00068                             for (int i = 0; i < 100; i++)
00069                             {
00070                                 sum_current += Current.read();
00071                                 sum_voltage += Voltage.read();
00072                                 wait_us(50);
00073                             }
00074                             I = sum_current/100;
00075                             V = sum_voltage/100;
00076                             
00077                             I = (I-0.5)*3.3/0.185;
00078                             V = V;
00079                             
00080                             pc.printf("%.4f\n\r", I);
00081                             
00082                             char* getRequest = generatePostRequest(I, V);
00083                             /////////////////////////////////////////////////////////////////////////////////
00084 sendGETRequest:
00085                             pc.printf("sending GET request,...\r\n");
00086                             if(gm.send_all(getRequest,strlen(getRequest))>0) {
00087                                 pc.printf("GOT!\r\n");
00088 
00089                                 char getResponce[5000]= {};
00090                                 pc.printf("reading responce of GET request!\r\n");
00091                                 int resBytes = gm.receive_all(getResponce,5000);
00092                                 if(resBytes > 0) {
00093                                     pc.printf("Responce Received!\r\n");
00094                                     pc.printf("Responce:\r\n%s\r\nResponce Read Sucessfull.\r\n",getResponce);
00095                                 } else if(resBytes==0) {
00096                                     pc.printf("Empty Responce!\r\n");
00097                                     //goto sendGETRequest;
00098                                 } else {
00099                                     pc.printf("Resopnce NOT Received!\r\n");
00100                                     goto sendGETRequest;
00101                                 }
00102 
00103                             } else {
00104                                 pc.printf("GET request failed!\r\n");
00105                                 goto sendGETRequest;
00106                             }
00107                         }else {
00108                             pc.printf("Host Connection Failed\r\n");
00109                             goto hostConnection;
00110                         }
00111                         wait(10);
00112                         }
00113                     } else {
00114                         pc.printf("sonething went wrong with connection. not connected \r\n");
00115                         goto nustConnection;
00116                     }
00117                 } else {
00118                     pc.printf("conenction fail!\r\n");
00119                     goto nustConnection;
00120                 }
00121             } else {
00122                 pc.printf("hardware not intialized!\r\n");
00123                 goto hardwareInit;
00124             }
00125         }
00126         wait(1);
00127     }
00128 }