|
|
|
@ -1,12 +1,14 @@ |
|
|
|
|
#include <SPI.h> |
|
|
|
|
#include <LoRa.h> |
|
|
|
|
#include <ESP8266WiFi.h> |
|
|
|
|
#include <WiFi.h> |
|
|
|
|
|
|
|
|
|
#define MQTT_MAX_TRANSFER_SIZE 99999 |
|
|
|
|
|
|
|
|
|
#include <PubSubClient.h> |
|
|
|
|
#include <U8x8lib.h> |
|
|
|
|
//#include "ovo.h"
|
|
|
|
|
#include "ovo.h" |
|
|
|
|
|
|
|
|
|
//#include<ArduinoJson.h>
|
|
|
|
|
#include<ArduinoJson.h> |
|
|
|
|
|
|
|
|
|
// WIFI_LoRa_32 ports
|
|
|
|
|
// GPIO5 -- SX1278's SCK
|
|
|
|
@ -25,25 +27,25 @@ |
|
|
|
|
|
|
|
|
|
//============================
|
|
|
|
|
//CHANGE THIS FOR EACH ARDUINO
|
|
|
|
|
char nodeId[15] ="Gateway"; |
|
|
|
|
String nodeId ="Gateway22"; |
|
|
|
|
//============================
|
|
|
|
|
|
|
|
|
|
const char* ssid = "yimian-iot"; |
|
|
|
|
const char* password = "1234567890."; |
|
|
|
|
const char* mqtt_server = "192.168.4.31";//change this to the mqtt server
|
|
|
|
|
const char* mqtt_server = "192.168.1.102";//change this to the mqtt server
|
|
|
|
|
|
|
|
|
|
char* topicIn="qos/sync";//change this to the outgoing messages
|
|
|
|
|
String cache_mqtt_publish = ""; |
|
|
|
|
char* jsonData; |
|
|
|
|
|
|
|
|
|
//char* jsonData;
|
|
|
|
|
|
|
|
|
|
//const int capacity = JSON_OBJECT_SIZE(14);//station has 14 objects, its the max amount
|
|
|
|
|
//StaticJsonDocument<capacity> jb;
|
|
|
|
|
const int capacity = JSON_OBJECT_SIZE(14);//station has 14 objects, its the max amount
|
|
|
|
|
StaticJsonDocument<capacity> jb; |
|
|
|
|
|
|
|
|
|
WiFiClient espClient; |
|
|
|
|
PubSubClient client(espClient); |
|
|
|
|
|
|
|
|
|
char outMsg[200]; |
|
|
|
|
//long lastMsg = 0;
|
|
|
|
|
long lastMsg = 0; |
|
|
|
|
|
|
|
|
|
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16); |
|
|
|
|
|
|
|
|
@ -79,7 +81,6 @@ Serial.println("LoRa Receiver"); |
|
|
|
|
u8x8.drawString(0, 6, "LoRa failure"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LoRa.setSyncWord(syncWord);//connect to only our network
|
|
|
|
|
|
|
|
|
|
LoRa.onReceive(gotMessage);// set the interrupt function
|
|
|
|
|
//LoRa_rxMode(); LoRa.receive();// put LoRa in Receive mode
|
|
|
|
@ -104,7 +105,7 @@ void gotMessage(int packetSize) |
|
|
|
|
|
|
|
|
|
int i=0; |
|
|
|
|
|
|
|
|
|
char msg[999]; |
|
|
|
|
String msg; |
|
|
|
|
|
|
|
|
|
//String buff;
|
|
|
|
|
//DynamicJsonBuffer jBuffer;
|
|
|
|
@ -113,17 +114,20 @@ void gotMessage(int packetSize) |
|
|
|
|
|
|
|
|
|
while(LoRa.available()) |
|
|
|
|
{ |
|
|
|
|
msg[i]=LoRa.read(); |
|
|
|
|
msg+=(char)LoRa.read(); |
|
|
|
|
i++;
|
|
|
|
|
} |
|
|
|
|
msg[i] = '\0'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Serial.println(msg); |
|
|
|
|
delay(200); |
|
|
|
|
|
|
|
|
|
//sendToNode();
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
//JsonObject& obj = jb.parseObject(msg);
|
|
|
|
|
|
|
|
|
|
cache_mqtt_publish = msg; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*deserializeJson(jb, msg);
|
|
|
|
|
//5.13.1
|
|
|
|
|
DeserializationError err = deserializeJson(jb,msg); |
|
|
|
|
if(err) |
|
|
|
@ -134,7 +138,7 @@ void gotMessage(int packetSize) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char* type=jb["type"]; |
|
|
|
|
char* topicOut; |
|
|
|
|
String topicOut; |
|
|
|
|
//change to string if it doesnt work
|
|
|
|
|
u8x8.drawString(0, 6, "from:");// who are we getting messages from?
|
|
|
|
|
u8x8.drawString(0, 8, type); //show on LCD
|
|
|
|
@ -143,55 +147,55 @@ void gotMessage(int packetSize) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
int nodeId=jb["id"]; |
|
|
|
|
int stat=jb["status"]; |
|
|
|
|
int batLev=jb["batteryLevel"]; |
|
|
|
|
const char* watTemp=jb["temperature"]; |
|
|
|
|
int qos = jb["qos"]; |
|
|
|
|
int switchState=jb["waterSwitch"]; |
|
|
|
|
const char* humid=jb["humidity"]; |
|
|
|
|
String stat=jb["status"]; |
|
|
|
|
String batLev=jb["batteryLevel"]; |
|
|
|
|
String watTemp=jb["temperature"]; |
|
|
|
|
String qos = jb["qos"]; |
|
|
|
|
String switchState=jb["waterSwitch"]; |
|
|
|
|
String humid=jb["humidity"]; |
|
|
|
|
|
|
|
|
|
switch(nodeId) |
|
|
|
|
{ |
|
|
|
|
case 0: |
|
|
|
|
|
|
|
|
|
topicOut="res/node0/status"; |
|
|
|
|
client.publish(topicOut, ""+stat); |
|
|
|
|
client.publish(topicOut.c_str(), stat.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/node0/batteryLevel"; |
|
|
|
|
client.publish(topicOut, ""+batLev); |
|
|
|
|
client.publish(topicOut.c_str(), batLev.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/node0/waterSwitch"; |
|
|
|
|
client.publish(topicOut,""+switchState); |
|
|
|
|
client.publish(topicOut.c_str(),switchState.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/node0/temperature"; |
|
|
|
|
client.publish(topicOut, watTemp); |
|
|
|
|
client.publish(topicOut.c_str(), watTemp.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/node0/humidity"; |
|
|
|
|
client.publish(topicOut,humid); |
|
|
|
|
client.publish(topicOut.c_str(),humid.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="qos/node0"; |
|
|
|
|
client.publish(topicOut,""+qos); |
|
|
|
|
client.publish(topicOut.c_str(),qos.c_str()); |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
case 1: |
|
|
|
|
|
|
|
|
|
topicOut="res/node1/status"; |
|
|
|
|
client.publish(topicOut, ""+stat); |
|
|
|
|
client.publish(topicOut.c_str(), stat.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/node1/batteryLevel"; |
|
|
|
|
client.publish(topicOut, ""+batLev);
|
|
|
|
|
client.publish(topicOut.c_str(), batLev.c_str());
|
|
|
|
|
|
|
|
|
|
topicOut="res/node1/waterSwitch"; |
|
|
|
|
client.publish(topicOut,""+switchState); |
|
|
|
|
client.publish(topicOut.c_str(),switchState.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/node1/temperature"; |
|
|
|
|
client.publish(topicOut, watTemp); |
|
|
|
|
client.publish(topicOut.c_str(), watTemp.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/node1/humidity"; |
|
|
|
|
client.publish(topicOut, humid); |
|
|
|
|
client.publish(topicOut.c_str(), humid.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="qos/node1"; |
|
|
|
|
client.publish(topicOut,""+qos);
|
|
|
|
|
client.publish(topicOut.c_str(),qos.c_str());
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
@ -205,46 +209,46 @@ void gotMessage(int packetSize) |
|
|
|
|
else if(type =="station")
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
int stat =jb["status"]; |
|
|
|
|
int batLev=jb["batterylevel"]; |
|
|
|
|
const char* light =jb["light"]; |
|
|
|
|
const char* temp=jb["temperature"]; |
|
|
|
|
const char* humid =jb["humidity"]; |
|
|
|
|
int rainF=jb["rainfall"]; |
|
|
|
|
int co =jb["co"]; |
|
|
|
|
int nh3 =jb["nh3"];
|
|
|
|
|
const char* airP =jb["airpressure"]; |
|
|
|
|
int qos=jb["qos"]; |
|
|
|
|
String stat =jb["status"]; |
|
|
|
|
String batLev=jb["batterylevel"]; |
|
|
|
|
String light =jb["light"]; |
|
|
|
|
String temp=jb["temperature"]; |
|
|
|
|
String humid =jb["humidity"]; |
|
|
|
|
String rainF=jb["rainfall"]; |
|
|
|
|
String co =jb["co"]; |
|
|
|
|
String nh3 =jb["nh3"];
|
|
|
|
|
String airP =jb["airpressure"]; |
|
|
|
|
String qos=jb["qos"]; |
|
|
|
|
|
|
|
|
|
topicOut="res/station/status";
|
|
|
|
|
client.publish(topicOut, ""+stat); |
|
|
|
|
client.publish(topicOut.c_str(), stat.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/station/batteryLevel";
|
|
|
|
|
client.publish(topicOut, ""+batLev); |
|
|
|
|
client.publish(topicOut.c_str(), batLev.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/station/light";
|
|
|
|
|
client.publish(topicOut,light ); |
|
|
|
|
client.publish(topicOut.c_str(),light.c_str() ); |
|
|
|
|
|
|
|
|
|
topicOut="res/station/temperature";
|
|
|
|
|
client.publish(topicOut,temp ); |
|
|
|
|
client.publish(topicOut.c_str(),temp.c_str() ); |
|
|
|
|
|
|
|
|
|
topicOut="res/station/humidity";
|
|
|
|
|
client.publish(topicOut, humid); |
|
|
|
|
client.publish(topicOut.c_str(), humid.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/station/rainfall";
|
|
|
|
|
client.publish(topicOut, ""+rainF ); |
|
|
|
|
client.publish(topicOut.c_str(), rainF.c_str() ); |
|
|
|
|
|
|
|
|
|
topicOut="res/station/CO";
|
|
|
|
|
client.publish(topicOut, ""+co); |
|
|
|
|
client.publish(topicOut.c_str(), co.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/station/NH3"; |
|
|
|
|
client.publish(topicOut, ""+nh3); |
|
|
|
|
client.publish(topicOut.c_str(), nh3.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="res/station/airPressure"; |
|
|
|
|
// client.publish(topicOut, airP);
|
|
|
|
|
client.publish(topicOut.c_str(), airP.c_str()); |
|
|
|
|
|
|
|
|
|
topicOut="qos/station"; |
|
|
|
|
client.publish(topicOut,""+qos);
|
|
|
|
|
client.publish(topicOut.c_str(), qos.c_str());
|
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
@ -253,12 +257,12 @@ void gotMessage(int packetSize) |
|
|
|
|
|
|
|
|
|
//send to MQTT here...
|
|
|
|
|
|
|
|
|
|
client.publish(topicOut, msg); |
|
|
|
|
//client.publish(topicOut, msg);
|
|
|
|
|
|
|
|
|
|
msg[0]='\0'; |
|
|
|
|
jb.clear();//clear memory for next message
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -268,14 +272,18 @@ void callback(char* topic, byte* payload, unsigned int length) { |
|
|
|
|
Serial.print("Message arrived ["); |
|
|
|
|
Serial.print(topic); |
|
|
|
|
Serial.print("] "); |
|
|
|
|
String s = String(topic) + String("|"); |
|
|
|
|
for (int i = 0; i < length; i++) |
|
|
|
|
{ |
|
|
|
|
Serial.print((char)payload[i]); |
|
|
|
|
outMsg[i]=(char)payload[i]; |
|
|
|
|
s += (char)payload[i]; |
|
|
|
|
} |
|
|
|
|
Serial.println(""); |
|
|
|
|
|
|
|
|
|
sendToNode(s); |
|
|
|
|
|
|
|
|
|
//send to LoRa nodes..
|
|
|
|
|
sendToNode(); |
|
|
|
|
//sendToNode();
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -290,12 +298,12 @@ void LoRa_txMode(){ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sendToNode() |
|
|
|
|
void sendToNode(String s) |
|
|
|
|
{ |
|
|
|
|
LoRa_txMode(); // set tx mode
|
|
|
|
|
delay(200); |
|
|
|
|
LoRa.beginPacket(); // start packet
|
|
|
|
|
LoRa.print("lalala"); // add payload
|
|
|
|
|
LoRa.print(s); // add payload
|
|
|
|
|
LoRa.endPacket(); // finish packet and send it
|
|
|
|
|
delay(200); |
|
|
|
|
LoRa_rxMode(); |
|
|
|
@ -327,6 +335,14 @@ void reconnect() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void f(){ |
|
|
|
|
|
|
|
|
|
if(cache_mqtt_publish != "") {Serial.println("haha"+cache_mqtt_publish); |
|
|
|
|
|
|
|
|
|
client.publish("res/json", cache_mqtt_publish.c_str(), cache_mqtt_publish.length()); |
|
|
|
|
cache_mqtt_publish = ""; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void loop() { |
|
|
|
|
|
|
|
|
@ -335,6 +351,6 @@ void loop() { |
|
|
|
|
reconnect(); |
|
|
|
|
} |
|
|
|
|
client.loop(); |
|
|
|
|
|
|
|
|
|
f(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|