Last active
June 29, 2018 18:01
-
-
Save GSE-UP/6ecc28c5f9f4786e583182d70982ffb4 to your computer and use it in GitHub Desktop.
LoRa 32 WIFI Sender Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <LoRa.h> | |
#include <Wire.h> //responsible for i2c communication | |
#include "SSD1306Wire.h" | |
#define SS 18 | |
#define RST 14 | |
#define DI0 26 | |
#define current 13 //current value | |
float v=0,p=0,e=0;//voltage, power and energy values | |
SSD1306Wire display(0x3c, 4, 15); | |
void setup() { | |
//set pins as output | |
pinMode(16,OUTPUT);//OLED RST | |
pinMode(25,OUTPUT); | |
digitalWrite(16, LOW);//reset OLED | |
delay(50); | |
digitalWrite(16, HIGH);//while OLED is on, GPIO16 might be HIGH | |
//starts LoRa's display | |
display.init(); | |
display.flipScreenVertically(); | |
display.setFont(ArialMT_Plain_10); | |
SPI.begin(5, 19, 27, 18); | |
LoRa.setPins(SS, RST, DI0); | |
Serial.begin(4800); | |
while (!Serial); | |
//clear LoRa's display | |
display.clear(); | |
//433E6, 868E6, 915E6 | |
if (!LoRa.begin(433E6)) { | |
Serial.println("Starting LoRa failed!"); | |
display.drawString(0, 0, "LoRa Initial failed!"); | |
while (1); | |
} else { | |
display.drawString(0, 0, "LoRa Initial success!"); | |
} | |
//showcase the string on LoRa's display | |
display.display(); | |
} | |
void loop() { | |
//reads output voltage and calculates input voltage | |
v=(analogRead(36)*3.35/4096)*(23700/1700); | |
if(v>3) | |
v+=3; | |
//gets power value | |
p=v*current; | |
//add power to energy | |
e+=((p/1000)/3600);//divide p by 1000 to obtain kW and by 3600s(1h) to get kWh | |
//starts LoRa packet | |
LoRa.beginPacket(); | |
//writes on the packet | |
LoRa.print(v); | |
LoRa.print("V POWER: "); | |
LoRa.print(p); | |
LoRa.print("W ENERGY: "); | |
LoRa.print(e); | |
LoRa.print("kWh"); | |
//ends the packet | |
LoRa.endPacket(); | |
//dlear LoRa's display | |
display.clear(); | |
display.drawString(0, 0, "GSE - SENDER"); | |
display.drawString(0, 10, "Packet: "); | |
String rssi = (String)LoRa.packetRssi(); | |
display.drawString(0, 20, "RSSI:"); | |
display.drawString(50, 20, rssi); | |
display.display();//showcase the string on LoRa's display | |
//pause for 1 seconds | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment