Open margohaux opened 1 year ago
the code works but when i combined the code for my thingspeak and sms it does not work. hope someone can help @techiesms
THINGSPEAK-TTGO-TCALL
// Server details
// The server variable can be just a domain name or it can have a subdomain. It depends on the service you are using
const char server[] = "thingspeak.com"; // domain name: example.com, maker.ifttt.com, etc
const char resource[] = "http://api.thingspeak.com/update?api_key=XXXXXX";
const int port = 80; // server port number
// Your GPRS credentials (leave empty, if not needed) const char apn[] = "internet"; // APN (example: airtelgprs.com) const char gprsUser[] = ""; // GPRS User const char gprsPass[] = ""; // GPRS Password
float voltage = 0; float current = 0; float power = 0; float pf = 0; float energy = 0; float frequency = 0;
// SIM card PIN (leave empty, if not defined) const char simPIN[] = ""; // get the API key from ThingSpeak.com. // If you change the apiKeyValue value, String apiKeyValue = "XXXXXX";
// TTGO T-Call pins
PZEM004Tv30 pzem(PZEM_SERIAL, PZEM_RX_PIN, PZEM_TX_PIN);
PZEM004Tv30 pzem(PZEM_SERIAL);
// Set serial for debug console (to Serial Monitor, default speed 115200)
// Set serial for AT commands (to SIM800 module)
// Configure TinyGSM library
// Define the serial console for debug prints, if needed //#define DUMP_AT_COMMANDS
StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger);
TinyGsm modem(SerialAT);
// I2C for SIM800 (to keep it running when powered from battery) TwoWire I2CPower = TwoWire(0);
// TinyGSM Client for Internet connection TinyGsmClient client(modem);
bool setPowerBoostKeepOn(int en) { I2CPower.beginTransmission(IP5306_ADDR); I2CPower.write(IP5306_REG_SYS_CTL0); if (en) { I2CPower.write(0x37); // Set bit1: 1 enable 0 disable boost keep on } else { I2CPower.write(0x35); // 0x37 is default reg value } return I2CPower.endTransmission() == 0; }
void setup() { // Set serial monitor debugging window baud rate to 115200 SerialMon.begin(115200);
// Start I2C communication I2CPower.begin(I2C_SDA, I2C_SCL, 400000);
// Keep power when running from battery bool isOk = setPowerBoostKeepOn(1); SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));
// Set modem reset, enable, power pins pinMode(MODEM_PWKEY, OUTPUT); pinMode(MODEM_RST, OUTPUT); pinMode(MODEM_POWER_ON, OUTPUT); digitalWrite(MODEM_PWKEY, LOW); digitalWrite(MODEM_RST, HIGH); digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX); delay(3000);
// Restart SIM800 module, it takes quite some time // To skip it, call init() instead of restart() SerialMon.println("Initializing modem..."); modem.restart(); // use modem.init() if you don't need the complete restart
// Unlock your SIM card with a PIN if needed if (strlen(simPIN) && modem.getSimStatus() != 3 ) { modem.simUnlock(simPIN); }
}
void loop() { SerialMon.print("Connecting to APN: "); SerialMon.print(apn); if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { SerialMon.println(" fail"); } else { SerialMon.println(" OK");
SerialMon.print("Connecting to ");
SerialMon.print(server);
if (!client.connect(server, port)) {
SerialMon.println(" fail");
}
else {
SerialMon.println(" OK");
// Convert the value to a char array
voltage = pzem.voltage();
char voltageString[8];
if(voltage != NAN){
dtostrf(voltage, 1, 2, voltageString);
Serial.print("Voltage: ");
Serial.println(voltageString);
Serial.print(voltage);
Serial.println("V");
} else {
Serial.println("Error reading voltage");
}
current = pzem.current();
char currentString[8];
if(current != NAN){
dtostrf(current, 1, 2, currentString);
Serial.print("current: ");
Serial.println(currentString);
Serial.print(current);
Serial.println("A");
} else {
Serial.println("Error reading current");
}
power = pzem.power();
char powerString[8];
if(power != NAN){
dtostrf(power, 1, 2, powerString);
Serial.print("power: ");
Serial.println(powerString);
Serial.print(power);
Serial.println("W");
} else {
Serial.println("Error reading power");
}
energy = pzem.energy();
char energyString[8];
if(energy != NAN){
dtostrf(energy, 1, 2, energyString);
Serial.print("energy: ");
Serial.println(energyString);
Serial.print(energy);
Serial.println("kWh");
} else {
Serial.println("Error reading energy");
}
pf = pzem.pf();
char pfString[8];
if(pf != NAN){
dtostrf(pf, 1, 2, pfString);
Serial.print("pf: ");
Serial.println(pfString);
Serial.print(pf);
} else {
Serial.println("Error reading pf");
}
frequency = pzem.frequency();
char frequencyString[8];
if(frequency != NAN){
dtostrf(frequency, 1, 2, frequencyString);
Serial.print("frequency: ");
Serial.println(frequencyString);
Serial.print(frequency);
Serial.println("Hz");
} else {
Serial.println("Error reading frequency");
}
// Making an HTTP POST request
SerialMon.println("Performing HTTP POST request...");
String httpRequestData = "api_key=" + apiKeyValue + "&field1=" + String(pzem.voltage())
+ "&field2=" + String(pzem.current()) + "&field3=" + String(pzem.power()) + + "&field4=" + String(pzem.energy())
+ "&field5=" + String(pzem.pf()) + "&field6=" + String(pzem.frequency()) + "";
client.print(String("POST ") + resource + " HTTP/1.1\r\n");
client.print(String("Host: ") + server + "\r\n");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(httpRequestData.length());
client.println();
client.println(httpRequestData);
unsigned long timeout = millis();
while (client.connected() && millis() - timeout < 10000L) {
// Print available data (HTTP response from server)
while (client.available()) {
char c = client.read();
SerialMon.print(c);
timeout = millis();
}
}
SerialMon.println();
// Close client and disconnect
client.stop();
SerialMon.println(F("Server disconnected"));
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
}
} delay(30000); }
CONTROL RELAY SMS-TTGO TCALL-ESP32
__// Please select the corresponding model
// #define SIM800L_IP5306_VERSION_20190610
// #define SIM800C_AXP192_VERSION_20200609 // #define SIM800L_IP5306_VERSION_20200811
// Define the serial console for debug prints, if needed
// Set serial for debug console (to the Serial Monitor, default speed 115200)
// Set serial for AT commands (to the module)
// Configure TinyGSM library
StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger);
TinyGsm modem(SerialAT);
char lower = ""; char response = " "; String res = ""; bool new_data = 0; int relay = 0;
char msg = "+cmt: \"+639304633501\""; char ring = "+clip: \"+639304633501\"";
char value1 = "relay on"; char value0 = "relay off";
void setup() {
// Set console baud rate SerialMon.begin(115200); pinMode(relay_pin,OUTPUT); delay(10);
// Start power management if (setupPMU() == false) { Serial.println("Setting power error"); }
// Some start operations setupModem();
// Set GSM module baud rate and UART pins SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(6000);
SerialMon.println("Initializing modem..."); modem.restart(); // modem.init();
String modemInfo = modem.getModemInfo(); SerialMon.print("Modem Info: "); SerialMon.println(modemInfo); delay(1000);
SerialAT.println("AT"); //Once the handshake test is successful, it will back to OK updateSerial();
SerialAT.println("AT+CMGF=1"); // Configuring TEXT mode updateSerial(); SerialAT.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled updateSerial(); }
void loop() { updateSerial(); }
void updateSerial() { delay(500); while (Serial.available()) { SerialAT.write(Serial.read());//Forward what Serial received to Software Serial Port } while (SerialAT.available()) { //Serial.write(SerialAT.read());//Forward what Software Serial received to Serial Port
char add = SerialAT.read();
res = res + add;
delay(1);
new_data = 1;
}
if (new_data) { response = &res[0]; //------------------------------------------------- Converting every character of the String in lower form const int length = strlen( response ); // get the length of the text lower = ( char )malloc( length + 1 ); // allocate 'length' bytes + 1 (for null terminator) and cast to char lower[ length ] = 0; // set the last byte to a null terminator //------------------------------------------------- copy all character bytes to the new buffer using tolower for ( int i = 0; i < length; i++ ) { lower[ i ] = tolower( response[ i ] ); } Serial.println(lower);// printin the String in lower character form Serial.println("\n"); relay_control( lower); response = ""; res = ""; lower = ""; new_data = 0;
} }
void relay_control(char* lower) {
if (strstr(lower, msg)) {
String source = (String)lower;
char* desti = &source[0];
int i = 0;
while (desti[i+2] != '\n' )
i++;
String destin = (String)desti;
source = source.substring(51);
Serial.print("Your message is = ");Serial.print(source);
char* relay1 = &source[0];
if(strstr(relay1, value1))
{
digitalWrite(relay_pin, HIGH);
res = modem.sendSMS(SMS_TARGET, String("Relay is turned on"));
DBG("SMS:", res ? "OK" : "fail");
relay = 1;
return;
}
if(strstr(relay1, value0))
{ digitalWrite(relay_pin, LOW); res = modem.sendSMS(SMS_TARGET, String("Relay is turned off")); DBG("SMS:", res ? "OK" : "fail"); relay = 0; return; } }
if (strstr(lower, ring)) {
bool res = modem.callHangup();
DBG("Hang up:", res ? "OK" : "fail");
if(relay == 1)
{
digitalWrite(relay_pin, LOW);
relay = 0;
res = modem.sendSMS(SMS_TARGET, String("Relay is turned off"));
DBG("SMS:", res ? "OK" : "fail");
return;
}
if(relay == 0)
{
digitalWrite(relay_pin, HIGH);
relay = 1;
res = modem.sendSMS(SMS_TARGET, String("Relay is turned on"));
DBG("SMS:", res ? "OK" : "fail");
return;
}
}
}_
.