Last active
February 10, 2018 20:15
-
-
Save YongHuax/d2a9fad18ddeb5605467929929c33e70 to your computer and use it in GitHub Desktop.
Final_Fyp_LoRa_Receiver_LG01_P_Mqtt
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 <Console.h> | |
#include <Process.h> | |
#define PABOOST true | |
#define BAND 433E6 | |
void uploadData(); | |
void setup() { | |
Bridge.begin(115200); | |
Console.begin(); | |
// while (!Console); | |
Console.println("LoRa Receiver to Mqtt"); | |
if (!LoRa.begin(BAND)) { | |
Console.println("Starting LoRa failed!"); | |
while (1); | |
} | |
LoRa.setSpreadingFactor(9); | |
} | |
void loop() { | |
//initialize Linux Message parameters | |
int pc = 0; //packetcounter | |
String message ; | |
String message2 ; | |
char lorainput ; | |
// Set all values in Linux Message to 0 | |
// int pc2 = 0 ; | |
// try to parse packet | |
int packetSize = LoRa.parsePacket(); // Supposed to be 4 | |
if (packetSize) { | |
// received a packet | |
Console.println("Received packet '"); | |
Console.print("Packet Size = "); | |
Console.println(packetSize); | |
// read packet | |
for( int i = 0 ; i<packetSize;i++){ | |
lorainput = (char)LoRa.read(); | |
message = message + lorainput; | |
//Console.print(messages[i]); | |
} | |
Console.print(" message received " ); | |
Console.println(message); | |
Console.print("' with RSSI "); | |
Console.println(LoRa.packetRssi()); | |
String tempstorage = message ; | |
//String tempstorage2 = "" ; | |
// uploadData(tempstorage,tempstorage2); | |
uploadData(tempstorage); | |
} | |
} | |
void uploadData(String uploaddata1) {//Upload Data to ThingSpeak | |
// void uploadData(String uploaddata1,String uploaddata2) | |
Console.println("Call Linux Command to Send Data"); | |
Process p; // Create a process and call it "p", this process will execute a Linux curl command | |
String upload_url = uploaddata1; | |
// String upload_url2 = uploaddata2; | |
p.begin("/root/testmqtt"); //Script execution.In this example I use the script name is Test. | |
p.addParameter(upload_url); | |
//p.addParameter(upload_url2); | |
p.run(); | |
//Console.print("Feedback from Linux: "); | |
// If there's output from Linux, | |
// send it out the Console: | |
while (p.available()>0) | |
{ | |
char c = p.read(); | |
Console.write(c); | |
} | |
Console.println("Hivemq_Broker"); | |
Console.println("Call Finished"); | |
Console.println("####################################"); | |
Console.println(""); | |
delay(10000) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment