Created
May 15, 2018 13:23
-
-
Save utkarshgpta/cbc7a899d3c6e1072db4b5f55e87b5a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
/* Create object named SIM900 of the class SoftwareSerial */ | |
SoftwareSerial SIM900(8, 7); | |
void setup() { | |
SIM900.begin(9600); /* Define baud rate for software serial communication */ | |
Serial.begin(9600); /* Define baud rate for serial communication */ | |
Serial.println("GSM Powering Up"); | |
delay(5000); | |
SIM900.println("AT+CGATT=1"); | |
delay(2000); | |
ShowSerialData(); | |
/* Configure bearer profile 1 */ | |
Serial.println("Set connection type GPRS"); | |
SIM900.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""); /* Connection type GPRS */ | |
delay(1000); | |
ShowSerialData(); | |
Serial.println("Set APN of provider"); | |
SIM900.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\""); /* APN of the provider */ | |
delay(1000); | |
ShowSerialData(); | |
Serial.println("Open GPRS context"); | |
SIM900.println("AT+SAPBR=1,1"); /* Open GPRS context */ | |
delay(1000); | |
ShowSerialData(); | |
Serial.println("Querying GPRS context"); | |
SIM900.println("AT+SAPBR=2,1"); /* Query the GPRS context */ | |
delay(1000); | |
ShowSerialData(); | |
} | |
void loop() { | |
SubmitHttpRequest(); | |
delay(2000); | |
} | |
void ShowSerialData() | |
{ | |
while(SIM900.available()!=0) /* If data is available on serial port */ | |
Serial.write(char (SIM900.read())); /* Print character received on to the serial monitor */ | |
} | |
void SubmitHttpRequest() | |
{ | |
Serial.println("Initialize HTTP service"); | |
SIM900.println("AT+HTTPINIT"); /* Initialize HTTP service */ | |
delay(3000); | |
ShowSerialData(); | |
Serial.println("Set HTTP Parameters"); | |
SIM900.println("AT+HTTPPARA=\"CID\",1"); /* Set parameters for HTTP session */ | |
delay(1000); | |
ShowSerialData(); | |
int CurrIn = 1; | |
int TempIn = 3; | |
int VoltIn = 0; | |
int ACSoffset = 2500; | |
int mVperAmp = 66; | |
float maxcurr=0; | |
float maxvolt=0; | |
float temp=0; | |
float voltage = 0; | |
int i=0; | |
while(i<=1000){ | |
float RawValue = analogRead(CurrIn); | |
float CURR_Voltage = (RawValue / 1024.0) * 5000; // Gets you mV | |
float current = ((CURR_Voltage - ACSoffset) / mVperAmp); | |
if(current>maxcurr) | |
maxcurr=current; | |
float tempvout = analogRead(TempIn); | |
temp = temp + ((tempvout*500)/1023); | |
/*Voltage Reading starts*/ | |
voltage = ((((analogRead(VoltIn))*5/1024)+.7))*4*9.15; | |
if(voltage>maxvolt) | |
maxvolt = voltage; | |
delay(1); | |
i=i+1; | |
} | |
i=0; | |
float data_voltage = maxvolt*0.707; | |
float data_current = maxcurr*0.707; | |
float data_temperature = temp/1000.0; | |
Serial.print("Voltage:\t"); | |
Serial.println(data_voltage); | |
Serial.print("Current:\t"); | |
Serial.println(data_current); | |
Serial.print("Temperature:\t"); | |
Serial.println(data_temperature); | |
Serial.println(); | |
maxcurr=0; | |
maxvolt=0; | |
temp=0; | |
Serial.println("Set URL"); | |
String url = "sensor-data-visualization.herokuapp.com/post_data?"; | |
url = url + "current=" + String(data_current) + "&voltage=" + String(data_voltage) + "&temperature=" + String(data_temperature) + "&power=" + String(data_voltage*data_current); | |
Serial.println(url); | |
String get_command = "AT+HTTPPARA=\"URL\"," + url; | |
SIM900.println(get_command); /* Set parameters for HTTP session */ | |
delay(2000); | |
ShowSerialData(); | |
Serial.println("Send GET request"); | |
SIM900.println("AT+HTTPACTION=0"); /* Start GET session */ | |
delay(4000); | |
ShowSerialData(); | |
Serial.println("Terminate HTTP service"); | |
SIM900.println("AT+HTTPTERM"); /* Terminate HTTP service */ | |
delay(2000); | |
ShowSerialData(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment