#include <WiFi.h>
#include <PubSubClient.h>
//#include "UbidotsESPMQTT.h"
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define WIFISSID "ZyXEL_KEENETIC_620" // Put your WifiSSID here
#define PASSWORD "09101939" // Put your wifi password here
#define TOKEN "BBFF-LamC2X9MfgFypdT0UohUiTCoT1iGHh" // Put your Ubidots' TOKEN
#define MQTT_CLIENT_NAME "ESP32_Health_Station" // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string;
#define VARIABLE_LABEL1 "Temperature" // Assing the variable label
#define DEVICE_LABEL "esp32"
#define SERVER "things.ubidots.com"
#define TOPIC "/v1.6/devices/esp32"
//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define BAND 433E6
//OLED pins
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Replace with your network credentials
// Variables to save date and time
// Initialize variables to get and save LoRa data
int rssi;
String loRaMessage;
String temperature;
String readingID;
char mqttBroker[] = "industrial.api.ubidots.com";
char payload[100];
char topic1[150];
// Space to store values to send
char str_Temperature[10];
WiFiClient ubidots;
PubSubClient client(ubidots);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
Serial.write(payload, length);
Serial.println(topic1);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attemp to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("Connected");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
// Replaces placeholder with DHT values
String processor(const String& var){
//Serial.println(var);
if(var == "TEMPERATURE"){
return temperature;
}
else if (var == "RRSI"){
return String(rssi);
}
return String();
}
//------------------Initialize OLED Module-------------------------------------------//
void initOLED(){
//reset OLED display via software
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
//initialize OLED
Wire.begin(OLED_SDA, OLED_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print("LORA RECEIVER");
}
//----------------------Initialize LoRa Module-----------------------------------------//
void initLoRA(){
int counter;
//SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
//setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
while (!LoRa.begin(BAND) && counter < 10) {
Serial.print(".");
counter++;
delay(500);
}
if (counter == 10) {
// Increment readingID on every new reading
Serial.println("Starting LoRa failed!");
}
Serial.println("LoRa Initialization OK!");
display.setCursor(0,10);
display.clearDisplay();
display.print("LoRa Initializing OK!");
display.display();
delay(2000);
}
//----------------------Connect to WiFi Function-----------------------------------------//
//-----------------Read LoRa packet and get the sensor readings-----------------------//
void getLoRaData() {
Serial.print("Lora packet received: ");
// Read packet
while (LoRa.available()) {
String LoRaData = LoRa.readString();
// LoRaData format: readingID/temperature&soilMoisture#batterylevel
// String example: 1/27.43&654#95.34
Serial.print(LoRaData);
// Get readingID, temperature and soil moisture
int pos1 = LoRaData.indexOf('/');
int pos2 = LoRaData.indexOf('&');
int pos3 = LoRaData.indexOf('#');
readingID = LoRaData.substring(0, pos1);
temperature = LoRaData.substring(pos1 +1, pos2);
}
// Get RSSI
rssi = LoRa.packetRssi();
Serial.print(" with RSSI ");
Serial.println(rssi);
}
//-----------------------Function to get date and time from NTPClient------------------//
//-------------------------Display Readings on OLED-------------------------------------//
void displayReadings()
{
display.clearDisplay();
display.setCursor(0,0);
display.println("LoRa RECEIVER 1- Pune");
display.setCursor(0,20);
display.setTextSize(1);
display.print("TEMPERATURE");
display.setCursor(70,20);
display.print(":");
display.setCursor(80,20);
display.print(temperature);
display.drawCircle(116,20,1, WHITE);
display.setCursor(121,20);
display.print("C");
display.display();
delay(500);
}
void setup(){
// serial.begin(115200);
Serial.begin(115200);
WiFi.begin(WIFISSID, PASSWORD);
Serial.println();
Serial.print("Waiting for WiFi Connection ..............");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
// Initialize Serial Monitor
initOLED();
initLoRA();
}
void loop() {
if (!client.connected()) {
reconnect();
}
sprintf(topic1, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label
/* 4 is minimum width, 2 is precision; float value is copied onto str_sensor*/
//dtostrf(temperatur, 4, 2, str_Temperatur);
// sprintf(payload, "%s {\"value\": %s}}", payload, str_Temperature); // Adds the value
Serial.println("Publishing data to Ubidots Cloud");
client.publish(topic1, payload);
client.loop();
delay(500);
// Check if there are LoRa packets available
int packetSize = LoRa.parsePacket();
if (packetSize) {
getLoRaData();
displayReadings();
}
}
Thanks for the answer please I have a lot of problems in this code I want to send the temperature value to ubidots please help me
#include <WiFi.h>
#include <PubSubClient.h>
#include "freertos/FreeRTOS.h"
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define WIFISSID "ZyXEL_KEENETIC_620" // Put your WifiSSID here
#define PASSWORD "09101939" // Put your wifi password here
#define TOKEN "BBFF-LamC2X9MfgFypdT0UohUiTCoT1iGHh" // Put your Ubidots' TOKEN
#define MQTT_CLIENT_NAME "ESP32_Health_Station" // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string;
#define VARIABLE_LABEL1 "Temperature"
#define DEVICE_LABEL "esp32" // Assing the variable label
#define SERVER "things.ubidots.com"
#define TOPIC "/v1.6/devices/esp32"
//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
#define BAND 433E6
//OLED pins
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
WiFiClient ubidots;
PubSubClient client(ubidots);
char mqttBroker[] = "industrial.api.ubidots.com";
char payload[100];
char topic1[150];
// Initialize variables to get and save LoRa data
int rssi;
String loRaMessage;
String temperature;
String readingID;
// Space to store values to send
char str_Temperature[10];
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
// Replaces placeholder with DHT values
String processor(const String& var){
//Serial.println(var);
if(var == "TEMPERATURE"){
return temperature;
}
else if (var == "RRSI"){
return String(rssi);
}
return String();
}
//------------------Initialize OLED Module-------------------------------------------//
void initOLED(){
//reset OLED display via software
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
//initialize OLED
Wire.begin(OLED_SDA, OLED_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print("LORA RECEIVER");
}
//----------------------Initialize LoRa Module-----------------------------------------//
void initLoRA(){
int counter;
//SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
//setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
while (!LoRa.begin(BAND) && counter < 10) {
Serial.print(".");
counter++;
delay(500);
}
if (counter == 10) {
// Increment readingID on every new reading
Serial.println("Starting LoRa failed!");
}
Serial.println("LoRa Initialization OK!");
display.setCursor(0,10);
display.clearDisplay();
display.print("LoRa Initializing OK!");
display.display();
delay(2000);
}
//----------------------Connect to WiFi Function-----------------------------------------//
//-----------------Read LoRa packet and get the sensor readings-----------------------//
void getLoRaData() {
Serial.print("Lora packet received: ");
// Read packet
while (LoRa.available()) {
String LoRaData = LoRa.readString();
// LoRaData format: readingID/temperature&soilMoisture#batterylevel
// String example: 1/27.43&654#95.34
Serial.print(LoRaData);
// Get readingID, temperature and soil moisture
int pos1 = LoRaData.indexOf('/');
int pos2 = LoRaData.indexOf('&');
int pos3 = LoRaData.indexOf('#');
readingID = LoRaData.substring(0, pos1);
temperature = LoRaData.substring(pos1 +1, pos2);
}
// Get RSSI
rssi = LoRa.packetRssi();
Serial.print(" with RSSI ");
Serial.println(rssi);
}
//-----------------------Function to get date and time from NTPClient------------------//
//-------------------------Display Readings on OLED-------------------------------------//
void displayReadings()
{
display.clearDisplay();
display.setCursor(0,0);
display.println("LoRa RECEIVER 1- Pune");
display.setCursor(0,20);
display.setTextSize(1);
display.print("TEMPERATURE");
display.setCursor(70,20);
display.print(":");
display.setCursor(80,20);
display.print(temperature);
display.drawCircle(116,20,1, WHITE);
display.setCursor(121,20);
display.print("C");
display.display();
delay(500);
}
void setup(){
// serial.begin(115200);
Serial.begin(115200);
Serial.print(".");
// Initialize Serial Monitor
initOLED();
initLoRA();
}
void connectToWiFi()
{
int TryCount = 0;
while ( WiFi.status() != WL_CONNECTED )
{
TryCount++;
WiFi.disconnect();
WiFi.begin( WIFISSID, PASSWORD );
vTaskDelay( 4000 );
if ( TryCount == 10 )
{
ESP.restart();
}
}
// WiFi.onEvent( WiFiEvent );
}
void connectToMQTT()
{
MQTTclient.setKeepAlive( 90 ); // needs be made before connecting
byte mac[5];
WiFi.macAddress(mac);
String clientID = String(mac[0]) + String(mac[4]) ; // use mac address to create clientID
while ( !MQTTclient.connected() )
{
// boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
MQTTclient.connect( clientID.c_str(), mqtt_username, mqtt_password, NULL , 1, true, NULL );
vTaskDelay( 250 );
}
MQTTclient.setCallback( mqttCallback );
MQTTclient.subscribe( topicOK );
}
void MQTTkeepalive( void *pvParameters )
{
sema_MQTT_KeepAlive = xSemaphoreCreateBinary();
xSemaphoreGive( sema_MQTT_KeepAlive ); // found keep alive can mess with a publish, stop keep alive during publish
// setting must be set before a mqtt connection is made
MQTTclient.setKeepAlive( 90 ); // setting keep alive to 90 seconds makes for a very reliable connection, must be set before the 1st connection is made.
for (;;)
{
//check for a is-connected and if the WiFi 'thinks' its connected, found checking on both is more realible than just a single check
if ( (wifiClient.connected()) && (WiFi.status() == WL_CONNECTED) )
{
xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY ); // whiles MQTTlient.loop() is running no other mqtt operations should be in process
MQTTclient.loop();
xSemaphoreGive( sema_MQTT_KeepAlive );
}
else {
log_i( "MQTT keep alive found MQTT status %s WiFi status %s", String(wifiClient.connected()), String(WiFi.status()) );
if ( !(wifiClient.connected()) || !(WiFi.status() == WL_CONNECTED) )
{
connectToWiFi();
}
connectToMQTT();
}
vTaskDelay( 250 ); //task runs approx every 250 mS
}
vTaskDelete ( NULL );
}
void loop() {
if (!client.connected()) {
reconnect();
}
sprintf(topic1, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label
/* 4 is minimum width, 2 is precision; float value is copied onto str_Temperature*/
//dtostrf(temperatur, 4, 2, str_Temperatur);
// sprintf(payload, "%s {\"value\": %s}}", payload, str_Temperature); // Adds the value
Serial.println("Publishing data to Ubidots Cloud");
client.publish(topic1, payload);
client.loop();
delay(500);
// Check if there are LoRa packets available
int packetSize = LoRa.parsePacket();
if (packetSize) {
getLoRaData();
displayReadings();
}
}نص منسَّق سابقًا
You see you use the word "client" to name your MQTT client object, right?
You might notice I use the word "MQTTclient" as my MQTT pubsubclient object.
If you were to change the name of "MQTTclient", which is how I named the object to 'client' which is what you named your object you may find a solution to this one issue.
Actually, the code I posted is to complicated don't use it. sorry for posting. I've deleted it. Sorry to have led you down the wrong path.
Good luck.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
