100% found this document useful (1 vote)
119 views

STM32 ESP8266 Code

This Arduino sketch includes libraries for real-time clock, I2C communication, and JSON parsing. It defines parameters for collecting sensor data from a device and transmitting it via GPRS to a server. The sketch sets up the real-time clock, defines sensors to collect voltage, current, power, and other readings, and transmits the data as a JSON object to the server on a periodic schedule defined by the INTERVAL variable.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
119 views

STM32 ESP8266 Code

This Arduino sketch includes libraries for real-time clock, I2C communication, and JSON parsing. It defines parameters for collecting sensor data from a device and transmitting it via GPRS to a server. The sketch sets up the real-time clock, defines sensors to collect voltage, current, power, and other readings, and transmits the data as a JSON object to the server on a periodic schedule defined by the INTERVAL variable.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <RTClib.

h> // Download the library from


https://github.com/adafruit/RTClib
#include <Wire.h> // Library for I2C Communication with DS3231 Module
#include <ArduinoJson.h>
StaticJsonBuffer<200> jsonBuffer;

#define BOARD_USART3_TX_PIN PB10 // Connect to TX3/RX3


#define BOARD_USART3_RX_PIN PB11 // Connect to TX3/RX3

RTC_DS3231 rtc;

/********************Parameters Definations Start********************/


char START [2] ="*";
//char DATATYPE [2];
char DEVICEID[12] = "BYNARK1234";
char TIMEDATE[32];
float VOLTAGE;
float CURRENT;
float PF;
float ACTPOWER;
float APPRPOWER;
float FREQUENCY;
float ACTENERGY;
float APPRENERGY;
int RELAYPIN = PB9;
int RELAY;
int LOAD;
int DOOR= PB8;
int CCMSONOFF;
int MAINTAIN;
int METERREAD;
char SECHDULE[20];
const int OffHour = 15;
const int OffMin = 43;
const int OnHour = 15;
const int OnMin = 50;
int SIGNAL;
int INTERVAL = 2;
char THRSVOLT;
int UNDERVOLTAGE = 180;
int OVERVOLTAGE = 280;
int LOADLIMIT = 5;
char MOBILENO[12];
char FIRMWARE[6] = "v1.1";
char HARDWARE[6] = "v1.0";
char ICCID[24];
char IMEI[20];
char API [60];
char METERTYPE [4];
char END[2] = "#";

/********************Parameters Definations ENDS********************/

void setup()
{
Serial3.begin(9600); // the GPRS baud rate
Serial.begin(9600);
Serial.println("Initializing..........");
Wire.begin();
DynamicJsonBuffer jsonBuffer;

if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower())
{
Serial.println("RTC lost power, lets set the time!");

// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//rtc.adjust(DateTime(2020, 3, 14, 11, 50, 0));

// Following line sets the RTC with an explicit date & time
// for example to set January 27 2017 at 12:56 you would call:
// rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
}
delay(5000);
DateTime now = rtc.now();
sprintf(TIMEDATE, "%02d-%02d-%02d %02d:%02d:%02d", now.year(), now.month(),
now.day(), now.hour(), now.minute(), now.second());
Serial.print("Date/Time: ");
Serial.println(TIMEDATE);

pinMode(RELAYPIN, OUTPUT);
digitalWrite(RELAYPIN, HIGH);
delay(5000);

if ( ((now.hour() * 100 + now.minute()) >= (OffHour * 100 + OffMin)) &&


((now.hour() * 100 + now.minute()) <= (OnHour * 100 + OnMin)) )
{
digitalWrite(RELAYPIN, LOW);
RELAY = digitalRead(RELAYPIN); // read the status of RELAYPIN
Serial.print("Contractor: ");
Serial.println(RELAY);
}

/********************RELAY ON Logic in Evening********************/

else
{
digitalWrite(RELAYPIN, HIGH);
RELAY = digitalRead(RELAYPIN); // read the status of RELAYPIN
Serial.print("Contractor: ");
Serial.println(RELAY);
{
if( (VOLTAGE < UNDERVOLTAGE) && (VOLTAGE > OVERVOLTAGE) )
digitalWrite(RELAYPIN, LOW);
RELAY = digitalRead(RELAYPIN); // read the status of RELAYPIN
Serial.print("Contractor: ");
Serial.println(RELAY);
}
}
}

void loop()
{
/********************D PACKET STARTS********************/
Serial.println("");
Serial.println("************************************************************");

Serial.print("Start Packet: ");


Serial.println(START);
Serial.println("Data Type: C");
Serial.print("CCMS ID: ");
Serial.println(DEVICEID);

DateTime now = rtc.now();


sprintf(TIMEDATE, "%02d-%02d-%02d %02d:%02d:%02d", now.year(), now.month(),
now.day(), now.hour(), now.minute(), now.second());
Serial.print("Date/Time: ");
Serial.println(TIMEDATE);

VOLTAGE = 220.58;
Serial.print("Voltage: ");
Serial.println(VOLTAGE);

CURRENT = 3.5;
Serial.print("Current: ");
Serial.println(CURRENT);

PF = 4;
Serial.print("Power Factor: ");
Serial.println(PF);

ACTPOWER = 500;
Serial.print("Active Power: ");
Serial.println(ACTPOWER);

APPRPOWER = 870;
Serial.print("Apparant Power: ");
Serial.println(APPRPOWER);

FREQUENCY = 49.5;
Serial.print("Frequency: ");
Serial.println(FREQUENCY);

ACTENERGY = 760;
Serial.print("Active Energy: ");
Serial.println(ACTENERGY);

APPRENERGY = 760;
Serial.print("Apparant Energy: ");
Serial.println(APPRENERGY);

if(VOLTAGE>OVERVOLTAGE)
{
THRSVOLT = 'O';
Serial.print("Threshold Voltage: ");
Serial.println(THRSVOLT);
}
else if(VOLTAGE<UNDERVOLTAGE)
{
THRSVOLT = 'U';
Serial.print("Threshold Voltage: ");
Serial.println(THRSVOLT);
}
else
{
THRSVOLT = 'N';
Serial.print("Threshold Voltage: ");
Serial.println(THRSVOLT);
}

pinMode(DOOR, OUTPUT);
int DOORSTATUS = digitalRead(DOOR); // read the status of RELAYPIN
Serial.print("DOOR STATUS: ");
Serial.println(DOORSTATUS);

sprintf(SECHDULE, "%02d:%02d/%02d:%02d", OnHour, OnMin, OffHour, OffMin );

/********************RELAY Logic Starts********************/


/********************RELAY OFF Logic in Morning********************/

if ( ((now.hour() * 100 + now.minute()) >= (OffHour * 100 + OffMin)) &&


((now.hour() * 100 + now.minute()) <= (OnHour * 100 + OnMin)) )
{
digitalWrite(RELAYPIN, HIGH);
RELAY = digitalRead(RELAYPIN); // read the status of RELAYPIN
Serial.print("Contractor: ");
Serial.println(RELAY);
}

/********************RELAY ON Logic in Evening********************/

else
{
digitalWrite(RELAYPIN, LOW);
RELAY = digitalRead(RELAYPIN); // read the status of RELAYPIN
Serial.print("Contractor: ");
Serial.println(RELAY);
}
/********************RELAY Logic Ends********************/

CCMSONOFF= RELAY;
Serial.print("CCMS ON/OFF: ");
Serial.println(CCMSONOFF);

Serial.print("SECHDULE ON/OFF: ");


Serial.println(SECHDULE);

Serial.print("PUSH DATA INTERVAL: ");


Serial.println(INTERVAL);

Serial.print("END PACKET: ");


Serial.println(END);

Serial.print("Signal Strength: ");


Serial.println(SIGNAL);

/****************************************************************/
/********************GSM Communication Starts********************/

delay(1000);
if (Serial3.available())
Serial.write(Serial3.read());

Serial.println();
Serial3.println("AT");
delay(1000);

Serial3.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
delay(3000);
ShowSerialData();

Serial3.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");//APN
delay(3000);
ShowSerialData();

Serial3.println("AT+SAPBR=1,1");
delay(3000);
ShowSerialData();

Serial3.println("AT+SAPBR=2,1");
delay(3000);
ShowSerialData();

Serial3.println("AT+CSQ");
SIGNAL = Serial3.print("AT+CSQ");
Serial.print("Signal Strength: ");
Serial.println(SIGNAL);
delay(3000);
ShowSerialData();

Serial3.println("AT+HTTPINIT");
delay(4000);
ShowSerialData();

Serial3.println("AT+HTTPPARA=\"CID\",1");
delay(4000);
ShowSerialData();

/********************Send Data via JSON Starts********************/

StaticJsonBuffer<200> jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
object.set("deviceID",DEVICEID);
object.set("timedate",TIMEDATE);
object.set("Sechdule",SECHDULE);
object.set("relaystate",RELAY);

object.printTo(Serial);
Serial.println(" ");
String sendtoserver;
object.prettyPrintTo(sendtoserver);
delay(4000);

Serial3.println("AT+HTTPPARA=\"URL\",\"http://120.138.9.117:8080/APCOMMONCM/gasserv
ice1.php\"");//Public server address
delay(4000);
ShowSerialData();

Serial3.println("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
delay(4000);
ShowSerialData();

Serial3.println("AT+HTTPDATA=" + String(sendtoserver.length()) + ",100000");


Serial.println(sendtoserver);
delay(6000);
ShowSerialData();

Serial3.println(sendtoserver);
delay(4000);
ShowSerialData;

Serial3.println("AT+HTTPACTION=1");
delay(4000);
ShowSerialData();

Serial3.println("AT+HTTPREAD");
delay(4000);
ShowSerialData();

Serial3.println("AT+HTTPTERM");
delay(4000);
ShowSerialData;
delay((INTERVAL*60-60)*1000); //Formula to set the delay between
data transmission

/********************GSM Communication Stops********************/

void ShowSerialData()
{
while (Serial3.available() != 0)
Serial.write(Serial3.read());
delay(1000);

You might also like