201 lines
4.7 KiB
C++
201 lines
4.7 KiB
C++
// OLED
|
|
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
|
|
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
|
|
|
|
// LORA
|
|
#include <SPI.h>
|
|
#include <LoRa.h>
|
|
|
|
// MQTT
|
|
#include <WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
|
|
SSD1306 display(0x3c, 4, 15);
|
|
|
|
//OLED pins to ESP32 GPIOs via this connection:
|
|
//OLED_SDA GPIO4
|
|
//OLED_SCL GPIO15
|
|
//OLED_RST GPIO16
|
|
|
|
|
|
|
|
// WIFI_LoRa_32 ports
|
|
|
|
// GPIO5 SX1278 SCK
|
|
// GPIO19 SX1278 MISO
|
|
// GPIO27 SX1278 MOSI
|
|
// GPIO18 SX1278 CS
|
|
// GPIO14 SX1278 RESET
|
|
// GPIO26 SX1278 IRQ(Interrupt Request)
|
|
|
|
#define SS 18
|
|
#define RST 14
|
|
#define DI0 26
|
|
// #define BAND 429E6
|
|
|
|
// LoRa Settings
|
|
#define BAND 434500000.00
|
|
#define spreadingFactor 9
|
|
// #define SignalBandwidth 62.5E3
|
|
#define SignalBandwidth 31.25E3
|
|
|
|
#define codingRateDenominator 8
|
|
|
|
|
|
|
|
// WiFi Network Credentials
|
|
const char *ssid = "SoS IOT";
|
|
const char *password = "godisr8n";
|
|
|
|
// MQTTCredentials
|
|
const char *MQTT_USER = "simon";
|
|
const char *MQTT_PASS = "bajsa123";
|
|
|
|
IPAddress broker(10,0,0,3); // IP address of your MQTT broker eg. 192.168.1.50
|
|
const char *ID = "esp_1"; // Name of our device, must be unique
|
|
const char *TOPIC = "test/sensor"; // Topic to subcribe to
|
|
WiFiClient wclient;
|
|
|
|
PubSubClient client(wclient); // Setup MQTT client
|
|
|
|
String data = "";
|
|
|
|
// Connect to WiFi network
|
|
void setup_wifi() {
|
|
Serial.print("\nConnecting to ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password); // Connect to network
|
|
|
|
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println();
|
|
Serial.println("WiFi connected");
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
// Reconnect to client
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Attempt to connect
|
|
if (client.connect(ID,MQTT_USER,MQTT_PASS)) {
|
|
Serial.println("connected");
|
|
Serial.print("Publishing to: ");
|
|
Serial.println(TOPIC);
|
|
Serial.println('\n');
|
|
|
|
} else {
|
|
Serial.println(" try again in 5 seconds");
|
|
// Wait 5 seconds before retrying
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ADC? Battery voltage
|
|
// const uint8_t vbatPin = 34;
|
|
// float VBAT; // battery voltage from ESP32 ADC read
|
|
|
|
void setup() {
|
|
pinMode(16,OUTPUT);
|
|
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
|
|
delay(50);
|
|
digitalWrite(16, HIGH);
|
|
|
|
display.init();
|
|
display.flipScreenVertically();
|
|
display.setFont(ArialMT_Plain_10);
|
|
display.setTextAlignment(TEXT_ALIGN_LEFT);
|
|
|
|
Serial.begin(115200);
|
|
while (!Serial); //if just the the basic function, must connect to a computer
|
|
delay(1000);
|
|
|
|
Serial.println("LoRa Receiver");
|
|
display.drawString(5,5,"LoRa Receiver");
|
|
display.display();
|
|
SPI.begin(5,19,27,18);
|
|
LoRa.setPins(SS,RST,DI0);
|
|
|
|
/*
|
|
pinMode(vbatPin, INPUT);
|
|
VBAT = (120.0/20.0) * (float)(analogRead(vbatPin)) / 1024.0; // LiPo battery voltage in volts
|
|
Serial.println("Vbat = "); Serial.print(VBAT); Serial.println(" Volts");
|
|
*/
|
|
|
|
if (!LoRa.begin(BAND)) {
|
|
display.drawString(5,25,"Starting LoRa failed!");
|
|
while (1);
|
|
}
|
|
Serial.println("LoRa Initial OK!");
|
|
|
|
Serial.print("LoRa Frequency: ");
|
|
Serial.println(BAND);
|
|
|
|
Serial.print("LoRa Spreading Factor: ");
|
|
Serial.println(spreadingFactor);
|
|
LoRa.setSpreadingFactor(spreadingFactor);
|
|
|
|
Serial.print("LoRa Signal Bandwidth: ");
|
|
Serial.println(SignalBandwidth);
|
|
LoRa.setSignalBandwidth(SignalBandwidth);
|
|
|
|
LoRa.setCodingRate4(codingRateDenominator);
|
|
|
|
display.drawString(5,25,"LoRa Initializing OK!");
|
|
display.display();
|
|
|
|
setup_wifi(); // Connect to network
|
|
client.setServer(broker, 1883);
|
|
}
|
|
|
|
void loop() {
|
|
if (!client.connected()) // Reconnect if connection is lost
|
|
{
|
|
reconnect();
|
|
}
|
|
client.loop();
|
|
// try to parse packet
|
|
int packetSize = LoRa.parsePacket();
|
|
if (packetSize) {
|
|
// received a packets
|
|
Serial.print("Received packet. ");
|
|
|
|
display.clear();
|
|
display.setFont(ArialMT_Plain_16);
|
|
display.drawString(3, 0, "Received packet ");
|
|
display.display();
|
|
|
|
// read packet
|
|
while (LoRa.available()) {
|
|
data = LoRa.readString();
|
|
Serial.print(data);
|
|
display.drawString(20,22, data);
|
|
display.display();
|
|
}
|
|
|
|
// print RSSI of packet
|
|
Serial.print(" with RSSI ");
|
|
Serial.println(LoRa.packetRssi());
|
|
Serial.print(" with SNR ");
|
|
Serial.println(LoRa.packetSnr());
|
|
// display.drawString(0, 45, "RSSI: ");
|
|
// display.drawString(50, 45, (String)LoRa.packetRssi());
|
|
|
|
display.drawString(0, 45, (String)LoRa.packetRssi() + "dB (" + (String)LoRa.packetSnr() +"dB)");
|
|
|
|
display.display();
|
|
client.publish(TOPIC, data.c_str() );
|
|
Serial.println((String)TOPIC + " => " + data + ";");
|
|
}
|
|
}
|