Hi , Im hoping someone could please give me some guidence.
i have the following code and having a problem of trying to correct a compile error . basically i use the GSM library and the arduinomqttClient library my version of code worked well when using mkr1010 and wifi version but now i need to create GSM version. i have a compile error
"request for member 'setUsernamePassword' in 'mqttClient', which is of non-class type 'MqttClient(GPRS)'"
my error is this line of code mqttClient.setUsernamePassword(SECRET_MQTT_USER,SECRET_MQTT_PASS);
and related to config of this part of code MqttClient mqttClient(GPRS);
author : Bradley Douglas, south africa
Date 2019/07/01
*/
#include <MKRGSM.h>
#include <ArduinoMqttClient.h>
#include "arduino_secrets.h"
//for uptime counter
long Day=0;
int Hour =0;
int Minute=0;
int Second=0;
int HighMillis=0;
int Rollover=0;
long secsUp ;
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
// initialize the library instance
GPRS gprs;
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSMServer server(80); // port 80 (http default)
// timeout
const unsigned long __TIMEOUT__ = 10*1000;
//declare readings
float analog1 = 0.00;
float analog2 = 0.00;
bool digin1 = 0;
bool digin2 = 0;
bool digout1 = 0;
bool digout2 = 0;
float batt_voltage = 0.0;
//pin configuration
const int digin1_pin = 0;
const int digin2_pin = 1;
const int digout1_pin = 2;
const int digout2_pin = 3;
const int analog1_pin = A1;
const int analog2_pin = A2;
//analog scaling calibration
int analog1_offset = 4;
int analog1_gain = 1023;
int analog2_offset = 4;
int analog2_gain = 1023;
//analog smoothing 1
const int numReadings1 = 10;
int analog1_readings[numReadings1]; // the readings from the analog input
int analog1_readIndex = 0; // the index of the current reading
int analog1_total = 0; // the running total
int analog1_average = 0; // the average
//analog smoothing 2
const int numReadings2 = 10;
int analog2_readings[numReadings2]; // the readings from the analog input
int analog2_readIndex = 0; // the index of the current reading
int analog2_total = 0; // the running total
int analog2_average = 0; // the average
const int ledPin = 13;
MqttClient mqttClient(GPRS);
//broker config
const char broker[] = SECRET_MQTT_SERVER;
int port = 10405;
//topic config
const char topic1[] = "GPRS/analog1";
const char topic2[] = "GPRS/analog2";
const char topic3[] = "GPRS/digin1";
const char topic4[] = "GPRS/digin2";
const char topic5[] = "GPRS/digout1";
const char topic6[] = "GPRS/digout2";
const char topic7[] = "GPRS/Batt_V";
const char topic8[] = "GPRS/uptimedays";
const char topicstatus[] = "GPRS/status";
// mqtt send interval
const long postingtime = 10000;
unsigned long currentmqttMillis = 0;
unsigned long previousmqttMillis;
//add blink function to use easlily
void blink( int numBlink = 1, unsigned int speed = 100 ) {
int i;
while ( numBlink-- ) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(speed); // wait
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(speed); // wait
}
}
//***********************************************************************************************
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
// connection state
}
boolean notConnected = true;
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("Connected to GPRS network");
// start server
server.begin();
//Get IP.
IPAddress LocalIP = gprs.getIPAddress();
Serial.println("Server IP address=");
Serial.print(LocalIP);
//********************************************************************************************************************************************
// You can provide a unique client ID, if not set the library uses Arduino-millis()
// Each client must have a unique client ID
// mqttClient.setId("clientId");
// You can provide a username and password for authentication
mqttClient.setUsernamePassword(SECRET_MQTT_USER,SECRET_MQTT_PASS);
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
//***********************************************************************************************************************************************
//configure mode for port pins
pinMode(digin1_pin, INPUT);
pinMode(digin2_pin, INPUT);
pinMode(analog1_pin, INPUT);
pinMode(analog2_pin, INPUT);
pinMode(digout1_pin, OUTPUT);
pinMode(digout2_pin, OUTPUT);
//analog averaging 1 initialize all the readings to 0:
for (int thisReading1 = 0; thisReading1 < numReadings1; thisReading1++) {
analog1_readings[thisReading1] = 0;
}
//analog averaging 2 initialize all the readings to 0:
for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++) {
analog2_readings[thisReading2] = 0;
}
}
//*************************************************************************************************
void loop(){
//****************************************************************************************************************************
//digital config
digin1 = digitalRead(digin1_pin);
digin2 = digitalRead(digin2_pin);
//*********************************************
/
//send data to mqtt
currentmqttMillis = millis();
if ((currentmqttMillis - previousmqttMillis >= postingtime) )
{
// wificheck();
attachgprs ()
MQTTsend();
printloop();
blink(1);
previousmqttMillis = currentmqttMillis;
uptime();
print_Uptime();
}
}
//*******************************************************************************************************
void printloop() // can print debug stuff here for values
{
Serial.println(" ");
Serial.print("Digital Input 1: ");
Serial.println(digin1);
Serial.print("Digital Input 2: ");
Serial.println(digin2);
Serial.print("Digital Output 1: ");
Serial.println(digout1);
Serial.print("Digital Output 2: ");
Serial.println(digout2);
Serial.print("Analog input 1: ");
Serial.println(analog1);
Serial.print("Analog input 2: ");
Serial.println(analog2);
Serial.print("Battery Voltage: ");
Serial.println(batt_voltage);
Serial.println(" ");
}
//**************************************************************************************************************
void MQTTsend() {
// call poll() regularly to allow the library to send MQTT keep alives which
// avoids being disconnected by the broker
mqttClient.poll();
Serial.println(" ");
Serial.print("Sending message to broker");
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topicstatus);
mqttClient.print("OK");
mqttClient.endMessage();
mqttClient.beginMessage(topic1);
mqttClient.print(analog1);
mqttClient.endMessage();
mqttClient.beginMessage(topic2);
mqttClient.print(analog2);
mqttClient.endMessage();
mqttClient.beginMessage(topic3);
mqttClient.print(digin1);
mqttClient.endMessage();
mqttClient.beginMessage(topic4);
mqttClient.print(digin2);
mqttClient.endMessage();
mqttClient.beginMessage(topic5);
mqttClient.print(digout1);
mqttClient.endMessage();
mqttClient.beginMessage(topic6);
mqttClient.print(digout2);
mqttClient.endMessage();
mqttClient.beginMessage(topic7);
mqttClient.print(batt_voltage);
mqttClient.endMessage();
mqttClient.beginMessage(topic8);
mqttClient.print(Day);
mqttClient.endMessage();
}