UTHM idp team 120

Dependencies:   ESP8266 Servo TextLCD mbed

Fork of ACS712HelloWorldDemo by m b

Committer:
tommy1994
Date:
Fri Sep 01 02:44:32 2017 +0000
Revision:
7:2d11e14b5cc4
Parent:
5:a022ca4aaa1e
UTHM idp team 120

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikeb 0:a35011a2fdaa 1 #include <mbed.h>
mikeb 0:a35011a2fdaa 2 #include "ACS712.h"
tommy1994 7:2d11e14b5cc4 3 #include "ESP8266.h"
tommy1994 7:2d11e14b5cc4 4 #include "TextLCD.h"
tommy1994 7:2d11e14b5cc4 5 #define SSID "OnePlus3"
tommy1994 7:2d11e14b5cc4 6 #define PASS "testingforidp"
tommy1994 7:2d11e14b5cc4 7 #define IP "184.106.153.149"
tommy1994 7:2d11e14b5cc4 8
tommy1994 7:2d11e14b5cc4 9 Serial pc(USBTX, USBRX);
tommy1994 7:2d11e14b5cc4 10 AnalogIn voltage(p20);
tommy1994 7:2d11e14b5cc4 11 ESP8266 wifi(p28, p27, 115200);
tommy1994 7:2d11e14b5cc4 12 ACS712 dev(p18);
tommy1994 7:2d11e14b5cc4 13 TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4-d7
tommy1994 7:2d11e14b5cc4 14
tommy1994 7:2d11e14b5cc4 15 float volt,currn,sample,Current,RMScurrn,power,ener,cumuEnergy;
tommy1994 7:2d11e14b5cc4 16 float mess = 0;
tommy1994 7:2d11e14b5cc4 17 char snd[255],resp[1000];
tommy1994 7:2d11e14b5cc4 18 char http_cmd[300], comm[300];
tommy1994 7:2d11e14b5cc4 19
tommy1994 7:2d11e14b5cc4 20 char* Update_Key = "M2ZXTED1OM75Z9YU";
tommy1994 7:2d11e14b5cc4 21 int timeout = 8000;
tommy1994 7:2d11e14b5cc4 22 void send_Wifi();
mikeb 0:a35011a2fdaa 23
nyengele 1:4f9effb20c29 24 int main()
nyengele 1:4f9effb20c29 25 {
tommy1994 7:2d11e14b5cc4 26 pc.baud(115200);
nyengele 1:4f9effb20c29 27 // Connect the sensor analog output pin to mbed's AnalogIn pin
tommy1994 7:2d11e14b5cc4 28
tommy1994 7:2d11e14b5cc4 29
tommy1994 7:2d11e14b5cc4 30 pc.printf("******** Resetting wifi module ********\r\n");
tommy1994 7:2d11e14b5cc4 31 wifi.Reset();
tommy1994 7:2d11e14b5cc4 32
tommy1994 7:2d11e14b5cc4 33 //wait for 5 seconds for response, else display no response receiveed
tommy1994 7:2d11e14b5cc4 34 if (wifi.RcvReply(resp, 5000))
tommy1994 7:2d11e14b5cc4 35 pc.printf("%s",resp);
tommy1994 7:2d11e14b5cc4 36 else
tommy1994 7:2d11e14b5cc4 37 pc.printf("No response");
tommy1994 7:2d11e14b5cc4 38
tommy1994 7:2d11e14b5cc4 39 pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
tommy1994 7:2d11e14b5cc4 40 wifi.SetMode(1); // set transparent mode
tommy1994 7:2d11e14b5cc4 41 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP
tommy1994 7:2d11e14b5cc4 42 pc.printf("%s",resp); //Print the response onscreen
tommy1994 7:2d11e14b5cc4 43 else
tommy1994 7:2d11e14b5cc4 44 pc.printf("No response while setting mode. \r\n");
tommy1994 7:2d11e14b5cc4 45
tommy1994 7:2d11e14b5cc4 46 pc.printf("******** Joining network with SSID and PASS ********\r\n");
tommy1994 7:2d11e14b5cc4 47 wifi.Join(SSID, PASS);
tommy1994 7:2d11e14b5cc4 48 if (wifi.RcvReply(resp, timeout))
tommy1994 7:2d11e14b5cc4 49 pc.printf("%s",resp);
tommy1994 7:2d11e14b5cc4 50 else
tommy1994 7:2d11e14b5cc4 51 pc.printf("No response while connecting to network \r\n");
nyengele 5:a022ca4aaa1e 52
nyengele 1:4f9effb20c29 53 pc.printf("Sensor Log: \n\n\r");
nyengele 1:4f9effb20c29 54
nyengele 1:4f9effb20c29 55 while (1) {
tommy1994 7:2d11e14b5cc4 56
tommy1994 7:2d11e14b5cc4 57 volt = abs((voltage.read()-0.059)*30);
tommy1994 7:2d11e14b5cc4 58 pc.printf("Voltage: %.3f V\n\r",volt);
tommy1994 7:2d11e14b5cc4 59 lcd.printf("%.3fV ",volt);
tommy1994 7:2d11e14b5cc4 60
nyengele 1:4f9effb20c29 61 // Read current from sensor and output to pc terminal
tommy1994 7:2d11e14b5cc4 62 Current = abs(float(dev))*2;
tommy1994 7:2d11e14b5cc4 63 pc.printf("Current: %2.3f A\n\r", Current);
tommy1994 7:2d11e14b5cc4 64 lcd.printf("%2.3fA", Current);
tommy1994 7:2d11e14b5cc4 65
tommy1994 7:2d11e14b5cc4 66 power = abs(volt * Current);
tommy1994 7:2d11e14b5cc4 67 pc.printf("RMSPower: %.3f W\n\r",power);
tommy1994 7:2d11e14b5cc4 68 lcd.locate(0, 1);
tommy1994 7:2d11e14b5cc4 69 lcd.printf("%.3fW ",power);
tommy1994 7:2d11e14b5cc4 70
tommy1994 7:2d11e14b5cc4 71 ener = power * 3600 / 1000;
tommy1994 7:2d11e14b5cc4 72 pc.printf("Energy: %.3f kWh\n\r",ener);
tommy1994 7:2d11e14b5cc4 73 cumuEnergy = cumuEnergy + ener;
tommy1994 7:2d11e14b5cc4 74 pc.printf("Total Energy: %.3f kWh\n\n\r",cumuEnergy);
tommy1994 7:2d11e14b5cc4 75 lcd.printf("%2.3fkWh", cumuEnergy);
tommy1994 7:2d11e14b5cc4 76
tommy1994 7:2d11e14b5cc4 77 if (cumuEnergy > 30){
tommy1994 7:2d11e14b5cc4 78 pc.printf("*******POWER USAGE WARNING!********\n\r");
tommy1994 7:2d11e14b5cc4 79 send_Wifi();
tommy1994 7:2d11e14b5cc4 80 }
tommy1994 7:2d11e14b5cc4 81 wait(0.8);
tommy1994 7:2d11e14b5cc4 82 lcd.cls();
nyengele 1:4f9effb20c29 83 }
tommy1994 7:2d11e14b5cc4 84 }
nyengele 1:4f9effb20c29 85
tommy1994 7:2d11e14b5cc4 86 void send_Wifi(){
tommy1994 7:2d11e14b5cc4 87
tommy1994 7:2d11e14b5cc4 88 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
tommy1994 7:2d11e14b5cc4 89 wifi.setTransparent();
tommy1994 7:2d11e14b5cc4 90 if (wifi.RcvReply(resp, timeout))
tommy1994 7:2d11e14b5cc4 91 pc.printf("%s",resp);
tommy1994 7:2d11e14b5cc4 92 else
tommy1994 7:2d11e14b5cc4 93 pc.printf("No response while setting wifi passthrough. \r\n");
tommy1994 7:2d11e14b5cc4 94 wait(1);
tommy1994 7:2d11e14b5cc4 95
tommy1994 7:2d11e14b5cc4 96 pc.printf("******** Setting single connection mode ********\r\n");
tommy1994 7:2d11e14b5cc4 97 wifi.SetSingle();
tommy1994 7:2d11e14b5cc4 98 wifi.RcvReply(resp, timeout);
tommy1994 7:2d11e14b5cc4 99 if (wifi.RcvReply(resp, timeout))
tommy1994 7:2d11e14b5cc4 100 pc.printf("%s",resp);
tommy1994 7:2d11e14b5cc4 101 else
tommy1994 7:2d11e14b5cc4 102 pc.printf("No response while setting single connection \r\n");
tommy1994 7:2d11e14b5cc4 103 wait(1);
tommy1994 7:2d11e14b5cc4 104 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
tommy1994 7:2d11e14b5cc4 105 wifi.startTCPConn(IP,80); //cipstart
tommy1994 7:2d11e14b5cc4 106 wifi.RcvReply(resp, timeout);
tommy1994 7:2d11e14b5cc4 107 if (wifi.RcvReply(resp, timeout))
tommy1994 7:2d11e14b5cc4 108 pc.printf("%s",resp);
tommy1994 7:2d11e14b5cc4 109 else
tommy1994 7:2d11e14b5cc4 110 pc.printf("No response while starting TCP connection \r\n");
tommy1994 7:2d11e14b5cc4 111 wait(1);
tommy1994 7:2d11e14b5cc4 112
tommy1994 7:2d11e14b5cc4 113 //create link
tommy1994 7:2d11e14b5cc4 114 mess = cumuEnergy;
tommy1994 7:2d11e14b5cc4 115 sprintf(http_cmd,"/update?api_key=%s&field1=%f",Update_Key,mess);
tommy1994 7:2d11e14b5cc4 116 pc.printf(http_cmd);
tommy1994 7:2d11e14b5cc4 117
tommy1994 7:2d11e14b5cc4 118 pc.printf("******** Sending URL to wifi ********\r\n");
tommy1994 7:2d11e14b5cc4 119 wifi.sendURL(http_cmd, comm); //cipsend and get command
tommy1994 7:2d11e14b5cc4 120 if (wifi.RcvReply(resp, timeout))
tommy1994 7:2d11e14b5cc4 121 pc.printf("%s",resp);
tommy1994 7:2d11e14b5cc4 122
tommy1994 7:2d11e14b5cc4 123 pc.printf("No response while sending URL \r\n");
tommy1994 7:2d11e14b5cc4 124 }
tommy1994 7:2d11e14b5cc4 125