Hi friends. First of all my first language is not English, excuse me if my spelling is not correct.
I am working in a LoRa network, sending data from a GP2Y1010AU0F sensor from a node to a master and then upload it to ThinkSpeak, in the code of my node I have no problems since it works correctly for me, the detail is in the master's code, at compile time I get an error saying: // expected initializer before '.' token // this error is in variables. Please give me your kind help to find a solution to my problem.
master code:
//ESP32 LoRa Gateway Code//
//En el siguiente código, realice cambios en el SSID WiFi, la contraseña y la clave API de Thingspeak//
#include <WiFi.h>
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
//define the pins used by the LoRa transceiver module
#define SCLK 5
#define MISO 19
#define MOSI 27
#define CS 18
#define RST 23
#define DIO 26
#define BAND 915E6 //433E6 for Asia, 866E6 for Europe, 915E6 for North America
// Replace with your network credentials
String apiKey = "AIA0FNJK6MGO"; // Enter your Write API key from ThingSpeak
const char *ssid = "Claro_657E94"; // replace with your wifi ssid and wpa2 key
const char *password = "Q7K7X3J2";
const char* server = "api.thingspeak.com";
WiFiClient client;
//Inicializar variables para obtener y guardar datos de LoRa
int rssi;
String loRaMessage;
String dustSensor.getDustDensity();
String dustSensor.getRunningAverage();
String readingID;
// Reemplaza el marcador de posición con valores GP2Y1010AU0F
String processor(const String& var){
//Serial.println(var);
if(var == "DENSIDAD DE POLVO")
{
return dustSensor.getDustDensity();
}
else if(var == "PROMEDIO")
{
return dustSensor.getRunningAverage();
}
else if (var == "RSSI")
{
return String(rssi);
}
return String();
}
void setup() {
Serial.begin(115200);
int counter;
//setup LoRa transceiver module
LoRa.setPins(CS, RST, DIO); //setup LoRa transceiver module
while (!LoRa.begin(BAND) && counter < 10) {
Serial.print(".");
counter++;
delay(2000);
}
if (counter == 10) {
// Increment readingID on every new reading
Serial.println("Starting LoRa failed!");
}
Serial.println("LoRa Initialization OK!");
delay(2000);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(2000);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// Read LoRa packet and get the sensor readings
void loop()
{
int packetSize = LoRa.parsePacket();
if (packetSize)
{
Serial.print("Lora packet received: ");
while (LoRa.available()) // Read packet
{
String LoRaData = LoRa.readString();
Serial.print(LoRaData);
int pos1 = LoRaData.indexOf('/');
int pos2 = LoRaData.indexOf('&');
readingID = LoRaData.substring(0, pos1); // Obtener readingID
dustSensor.getDustDensity() = LoRaData.substring(pos1 +1, pos2); // Obtener densidad de polvo
dustSensor.getRunningAverage() = LoRaData.substring(pos2+1, LoRaData.length()); // Obtener PROMEDIO
}
rssi = LoRa.packetRssi(); // Obtener RSSI
Serial.print(" with RSSI ");
Serial.println(rssi);
}
if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(readingID);
postStr += "&field2=";
postStr += String(dustSensor.getDustDensity());
postStr += "&field3=";
postStr += String(dustSensor.getRunningAverage());
postStr += "&field4=";
postStr += String(rssi);
postStr += "\r\n\r\n\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
//delay(30000);
}
Sensor node:
//Libraria para LoRa
#include <SPI.h>
#include <LoRa.h>
//Libreria y declaracion de pines para sensor de Polvo
#include <GP2YDustSensor.h>
const uint8_t SHARP_LED_PIN = 12; //Sharp Dust/particle sensor Led Pin
const uint8_t SHARP_VO_PIN = 36; //Sharp Dust/particle analog out pin used for reading
GP2YDustSensor dustSensor(GP2YDustSensorType::GP2Y1010AU0F, SHARP_LED_PIN, SHARP_VO_PIN);
//Libraria para la pantalla Oled
#include <U8g2lib.h>
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
//define the pins used by the LoRa transceiver module
#define SCLK 5
#define MISO 19
#define MOSI 27
#define CS 18
#define RST 23
#define DIO 26
#define BAND 915E6 //433E6 for Asia, 866E6 for Europe, 915E6 for North America
//packet counter
int readingID = 0;
int counter = 0;
String LoRaMessage = "";
//Initialize LoRa module
void startLoRA()
{
LoRa.setPins(CS, RST, DIO); //setup LoRa transceiver module
while (!LoRa.begin(BAND) && counter < 10) {
Serial.print(".");
counter++;
delay(500);
}
if (counter == 10)
{
// Increment readingID on every new reading
readingID++;
Serial.println("Starting LoRa failed!");
}
Serial.println("LoRa Initialization OK!");
delay(2000);
}
void sendReadings() {
LoRaMessage = String(readingID) + "/" + String(dustSensor.getDustDensity()) + "&" + String(dustSensor.getRunningAverage()) ;
//Send LoRa packet to receiver
LoRa.beginPacket();
LoRa.print(LoRaMessage);
LoRa.endPacket();
Serial.print("Sending packet: ");
Serial.println(readingID);
readingID++;
Serial.println(LoRaMessage);
}
void setup() {
Serial.begin(115200);
//dustSensor.setBaseline(0.4); // set no dust voltage according to your own experiments
//dustSensor.setCalibrationFactor(1.1); // calibrate against precision instrument
dustSensor.begin();
u8g2.begin();
startLoRA();
}
void loop() {
sendReadings();
delay(500);
//Se imprime en el monitor serial
Serial.print("Dust density: ");
Serial.print(dustSensor.getDustDensity());
Serial.print(" ug/m3; Running average: ");
Serial.print(dustSensor.getRunningAverage());
Serial.println(" ug/m3");
delay(1000);
//Se imprime en la pantalla Oled
u8g2.clearBuffer(); //Limpiar la memoria interna
u8g2.setFont(u8g2_font_pxplusibmvga9_tr); //Elija una fuente adecuada en https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(0,15,"Dust Density "); //Escribe algo en la memoria interna
u8g2.setCursor(0, 31);
u8g2.print(dustSensor.getDustDensity());
u8g2.sendBuffer(); //Transferir la memoria interna a la pantalla
delay(1000);
}
The error is as follows:
Maestro_LoRa:33:18: error: expected initializer before '.' token
String dustSensor.getDustDensity();
^
Maestro_LoRa:34:18: error: expected initializer before '.' token
String dustSensor.getRunningAverage();
^
C:\Users\Darel\Desktop\Importante\Red LoRa 2021\Maestro_LoRa\Maestro_LoRa.ino: In function 'String processor(const String&)':
Maestro_LoRa:43:12: error: 'dustSensor' was not declared in this scope
return dustSensor.getDustDensity();
^~~~~~~~~~
C:\Users\Darel\Desktop\Importante\Red LoRa 2021\Maestro_LoRa\Maestro_LoRa.ino:43:12: note: suggested alternative: 'strerror'
return dustSensor.getDustDensity();
^~~~~~~~~~
strerror
Maestro_LoRa:47:12: error: 'dustSensor' was not declared in this scope
return dustSensor.getRunningAverage();
^~~~~~~~~~
C:\Users\Darel\Desktop\Importante\Red LoRa 2021\Maestro_LoRa\Maestro_LoRa.ino:47:12: note: suggested alternative: 'strerror'
return dustSensor.getRunningAverage();
^~~~~~~~~~
strerror
C:\Users\Darel\Desktop\Importante\Red LoRa 2021\Maestro_LoRa\Maestro_LoRa.ino: In function 'void loop()':
Maestro_LoRa:107:5: error: 'dustSensor' was not declared in this scope
dustSensor.getDustDensity() = LoRaData.substring(pos1 +1, pos2); // Obtener densidad de polvo
^~~~~~~~~~
C:\Users\Darel\Desktop\Importante\Red LoRa 2021\Maestro_LoRa\Maestro_LoRa.ino:107:5: note: suggested alternative: 'strerror'
dustSensor.getDustDensity() = LoRaData.substring(pos1 +1, pos2); // Obtener densidad de polvo
^~~~~~~~~~
strerror
Maestro_LoRa:123:25: error: 'dustSensor' was not declared in this scope
postStr += String(dustSensor.getDustDensity());
^~~~~~~~~~
C:\Users\Darel\Desktop\Importante\Red LoRa 2021\Maestro_LoRa\Maestro_LoRa.ino:123:25: note: suggested alternative: 'strerror'
postStr += String(dustSensor.getDustDensity());
^~~~~~~~~~
strerror
exit status 1
expected initializer before '.' token