master
iotcat 5 years ago
parent 7479d4e11d
commit 6e87f91e84
  1. 142
      opt/gateway/LoRa_GateWay_esp32/LoRa_GateWay_esp32.ino
  2. 72
      opt/gateway/LoRa_GateWay_esp32/ovo.h
  3. 8
      opt/node/test/test.ino

@ -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();
}

@ -22,14 +22,14 @@
* @param int millisecond to dealy
* @return void
*/
void setTimeout(auto function,const int delay)
void setTimeout(void (*function)(void),const int delay)
{
static bool on = 1;
if(on == 1){
static unsigned long startTime = millis();
if(millis() - startTime > delay){
function();
(*function)();
on == 0;
}
}
@ -43,81 +43,17 @@ void setTimeout(auto function,const int delay)
* @param int millisecond of interval
* @return void
*/
void setInterval(auto function, const int delay)
void setInterval(void (*function)(void), const int delay)
{
static unsigned long startTime = millis();
if(millis() - startTime > delay){
function();
(*function)();
startTime = millis();
}
}
/**
* Switch between function1 and function2 for delay1 and delay2
*
* @Author yimian
* @param auto function1 (allow Lambda)
* @param auto function2 (allow Lambda)
* @param int millisecond of interval
* @param int millisecond of interval
* @return void
*/
void setSwitch(auto function1, auto function2, const int delay1, const int delay2)
{
static unsigned long startTime = millis();
if(millis() - startTime < delay1){
function1();
}else if(millis() - startTime >= delay1 && millis() - startTime < delay1 + delay2){
function2();
}else if(millis() - startTime >= delay1 + delay2){
startTime = millis();
}
}
/**
* Make direct voltage change to gradually change
*
* @author yimian
* @category ovo
* @package ovo
*/
class slowWrite
{
public:
inline void set(int t_pin, unsigned int t_delay){
pin = t_pin;
delay = t_delay;
};
inline void high(){
startTime = millis();
state = 1;
};
inline void low(){
startTime = millis();
state = 0;
};
inline void run(){
if(state == 1 && millis() - startTime < delay){
analogWrite(pin, ((millis() - startTime) * 255 / delay));Serial.println(((millis() - startTime) * 255 / delay));
}else if(state == 0 && millis() - startTime < delay){
analogWrite(pin, 255-((millis() - startTime) * 255 / delay));Serial.println(255-((millis() - startTime) * 255 / delay));
}else{
state = -1;
}
};
private:
unsigned long startTime;
int state;
int pin;
unsigned int delay;
};

@ -59,18 +59,16 @@ void loop() {
if (packetSize) {
gotPacket(packetSize);
}
LoRa.idle();
delay(1000);
if(LoRa.beginPacket()){
setInterval([&](){
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.print(" ");
LoRa.print(aVal);
LoRa.endPacket();
Serial.println("sent");
}
delay(3000);
},10000);
//delay(500);

Loading…
Cancel
Save