Gateway

//Arduino Code 1.8.8


/*RX Lora ESP 32 TTGO T-Beam OLED and Blynk

*/

 

char ssid[] = "3data";//************************WIFI RouterID Radio rum

//char ssid[] = "3data_EXT2.4G";//************************WIFI RouterID Flytbar

const char* pass = "xxxxxxxxxxx";

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Use for Blynk

#define BLYNK_PRINT Serial

//#define BLYNK_DEBUG

#include <SimpleTimer.h>

SimpleTimer timer;

#include <BlynkSimpleEsp32.h>

WidgetLED led1(V15); // Upload LED

#define OFF 0   // For LED

#define ON 1

// SPI LoRa Radio

#include <SPI.h>

#include <LoRa.h>      

#include <U8g2lib.h>  

 

// SPI LoRa Radio

#define LORA_SCK 5        // GPIO5 - SX1276 SCK

#define LORA_MISO 19     // GPIO19 - SX1276 MISO

#define LORA_MOSI 27    // GPIO27 - SX1276 MOSI

#define LORA_CS 18     // GPIO18 - SX1276 CS

#define LORA_RST 14   // GPIO14 - SX1276 RST

#define LORA_IRQ 26  // GPIO26 - SX1276 IRQ (interrupt request)

 

// I2C OLED Display works with SSD1306 driver

#define OLED_SDA 4

#define OLED_SCL 15

#define OLED_RST 16

 

#include <U8g2lib.h>                                                                                                                                                                                                                                                                                                                                                                                     

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); 

 

const int blueLED = 14;

String rssiWiFi = "";

String rssix = "";

String packet = "";

String Snr = "";

String b = "";

String Data = "";

String Node = "";

String Temp = "";

byte mac[6];// the MAC address of your Wifi shield

boolean firstsw1 = true;

//WidgetTable table;

//BLYNK_ATTACH_WIDGET(table, V10);

 

#include <WidgetRTC.h>

WidgetRTC rtc;

#include <TimeLib.h>

 

void(* resetFunc) (void) = 0; //declare reset function @ address 0

int rowIndex = 0;

 

BLYNK_CONNECTED() {

  // Synchronize time on connection

  rtc.begin();

}

String currentTime;

String currentDate;

void clockDisplay()

{

  // You can call hour(), minute(), ... at any time

  // Please see Time library examples for details

  currentTime = String(hour()) + ":" + minute() + ":" + second();

  currentDate = String(day()) + " " + month() + " " + year();

  Serial.print("Current time: ");

  Serial.print(currentTime);

  Serial.print(" ");

  Serial.print(currentDate);

  Serial.println();

 

  // Send time to the App

  Blynk.virtualWrite(V1, currentTime);

  // Send date to the App

  Blynk.virtualWrite(V2, currentDate);

}

void sendEvent() {

 

  // adding 1 row to table every second

  Blynk.virtualWrite(V10, "add", rowIndex,Node + " Temp. "  +  Temp + " C" + " Bat. " + packet + " V",currentTime );

   //Blynk.virtualWrite(V10, "add", rowIndex,Node + " Temp. "  +  Temp + " Bat. " + packet + " V",currentTime );

  //highlighting latest added row in table

  Blynk.virtualWrite(V10, "pick", rowIndex);

 

  rowIndex++;

}

 

void greenligth() {

  // Blink green ligth on the App

  if (led1.getValue()) {

    led1.off();

  } else {

    led1.on();

  }

}

void Resetarduino()

{

  Serial.println("resetting");

   resetFunc(); //call

  delay(1000);

}

 

void printIPmv(){

    Serial.println("");

    Serial.print("Connected to Router ");

    Serial.println(ssid);

    Serial.print("IP address: ");

    Serial.println(WiFi.localIP());

    WiFi.macAddress(mac);

    Serial.print("MAC: ");

    Serial.print(mac[5],HEX);

    Serial.print(":");

    Serial.print(mac[4],HEX);

    Serial.print(":");

    Serial.print(mac[3],HEX);

    Serial.print(":");

    Serial.print(mac[2],HEX);

    Serial.print(":");

    Serial.print(mac[1],HEX);

    Serial.print(":");

    Serial.println(mac[0],HEX);

     // Print the received signal strength:

    rssiWiFi = WiFi.RSSI();

    Serial.print("signal strength (RSSI):");

    Serial.print(rssiWiFi);

    Serial.println(" dBm");

    delay(200);

}

 

BLYNK_READ(V20) // UPTIME

{

  // This command writes Arduino's uptime in seconds to Virtual Pin (20)

  Blynk.virtualWrite(V20, millis() / 2000);

}

 

void draw() {

  u8g2.setFont(u8g2_font_wqy12_t_chinese2); //Codeman38

  u8g2.drawStr(0, 17, "Battery: ");

  u8g2.setCursor(45, 17);

  u8g2.print(packet);  // packet

  u8g2.drawStr(0, 32, "RSSI: ");

  u8g2.setCursor(100, 32);

  u8g2.print(Snr);  // Snr

  u8g2.setCursor(32, 32);

  u8g2.print(rssix);  // rssi LoRa

  u8g2.drawStr(0, 47, "Wifi: ");

  u8g2.drawStr(70, 32, "Snr: ");

  u8g2.setCursor(32, 47);

  u8g2.print(rssiWiFi);  // rssi WiFi

  u8g2.drawStr(0, 62, "Temp: ");

  u8g2.setCursor(32, 62);

  u8g2.print(Temp);  // Temp

  u8g2.print(" C");  

  u8g2.drawStr(70, 62, "Node: "); 

  u8g2.setCursor(100, 62);

  u8g2.print(Node);  // Node

}

 

void setup() {

   //clean table at start

 

   firstsw1 = true;

   //rtc.begin(); //????????

  //setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds

// timer.setInterval(10000, clockDisplay);

   timer.setInterval(10000, readloradata);

   //timer.setInterval(160000, RestartBlynk); //

  //timer.setInterval(160000, Resetarduino); //

  timer.setInterval(30000, datatoblynk); //

  Serial.begin(115200);

  u8g2.begin();

  pinMode(blueLED, OUTPUT); // For LED feedback

  // Very important for SPI pin configuration!

  SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);

// Very important for LoRa Radio pin configuration!

  LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);

  if (!LoRa.begin(866E6)) {

    Serial.println("Starting LoRa failed!");

    while (1);

  }

  // The larger the spreading factor the greater the range but slower data rate

  // Send and receive radios need to be set the same

  LoRa.setSpreadingFactor(12);  // ranges from 6-12, default 7 see API docs

 

  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);

  delay(100);

  Blynk.virtualWrite(V10, "clr");

  greenligth();

  printIPmv();

}

void RestartBlynk()

{

  Blynk.disconnect();

  Blynk.connect();

  Serial.println("Restart Blynk!");

  //clean table at start

  Blynk.virtualWrite(V10, "clr");

}

 

void sendmail()

{

    firstsw1 = false; // Stop mail by testning

    if (firstsw1)

     {

       Serial.println("***SOS***");//****************************************** E-Mail

       String body = String("Vineyard Node ") + Node + " Temp. " + Temp;

       Blynk.email("info@tranekaervin.dk", "Subject:  ***SOS***", body);

       delay(5500);

       Blynk.email("{DEVICE_OWNER_EMAIL}", "{DEVICE_NAME} : Alarm", "Your {DEVICE_NAME} Vineyard near freesing point!");

       firstsw1 = false;

       Serial.println("Email is send");

     }

}

 

void datatoblynk() {


  if (Blynk.connected())

  {

    greenligth();    

    Serial.println("*********Send data to Blynk*********");

    Blynk.virtualWrite(V0, rssix);

    Blynk.virtualWrite(V1, packet);

    Blynk.virtualWrite(V4, Snr);

    Blynk.virtualWrite(V9, Node);      

    switch (Node.toInt())

       {

      case 201:

          Serial.println(" 5p6mj node 1");

          Blynk.virtualWrite(V3, Temp);

           Blynk.virtualWrite(V12, "Vinkl. " + Node + " battery " + packet + " V");

          if (Temp.toInt() <= 2)

            sendmail();

          break;

     case 135:

          Serial.println(" 5p6mj node 2");

          Blynk.virtualWrite(V7, Temp);

          Blynk.virtualWrite(V5, "Skov "+ Node + " battery " + packet + " V");

          if (Temp.toInt() <= 2)

            sendmail();

      break;

      default:

          Serial.println(" new one");

          Blynk.virtualWrite(V8," ");

          Blynk.virtualWrite(V6, "Default "+ Node);

      break;        

      }

    clockDisplay();

    sendEvent();

    delay(500);

    printloradata();

    greenligth();

  }

}

void printloradata()

{

   Serial.println("Received packet");

   Serial.println("Battery " + packet);

   Serial.println("RSSI " + rssix);

   Serial.println ("Snr " + Snr);

   Serial.println("Temp " + Temp);

   Serial.println("Node " + Node);

   delay(500);

   u8g2.clearBuffer();

   u8g2.firstPage();

     do

     {

      draw();

      delay(100);

     }

     while ( u8g2.nextPage() );

   delay(200);

}

 

void readloradata()

{

  // try to parse packet

  int packetSize = LoRa.parsePacket();

  if (packetSize)

  {

    // received a packet

    digitalWrite(blueLED, ON);  // Turn blue LED on

    Data = "";

    while (LoRa.available())

    {

      //packet += (char)LoRa.read(); // Assemble new packet

      b = (char)LoRa.read();

      if (b == "t")//Temperature

      {

        Temp =Data;

        Data = "";

      }

       if (b == "n") //Node

      {

        Node = Data;

        Data = "";

      }

        if (b == "c") //Battery

      {

        packet = Data;

        Data = "";

      }

      //else if (b != "t" || b != "c" ||  b != "n")

      else if (b != "t" & b != "c" &  b != "n")

      {

        Data = Data + b;

      }

    }

    rssix = LoRa.packetRssi();

    Snr = LoRa.packetSnr();

    switch (Temp.toInt()) // A Hack

       {

      case 254:

          Temp = "-1";

      break;

     case 255:

          Temp = "-2";

     break;

      }

   

    delay(200); 

// end Packetsize

}

  digitalWrite(blueLED, OFF); // Turn blue LED off

}

 

void loop()

{

  timer.run();

  Blynk.run();

  //readloradata();

}

Jeg har prøvet forskellige forsøg med en Gateway opsætning, og på nuværende tidspunkt tester jeg


Transciever RFM95W  

ESP 32 TTGO og Raspberry Pi (Afventer LoRa Gateway HAT for Raspberry Pi)

3,6 Volt LiOn Battery

Pi PoE Switch HAT - Power over Ethernet for Raspberry Pi

Charge og controller til Solar panel

Nedenstående kode er testudgaven, som jeg bruger på min Control Receiver