udp to char

pull/1/head
iotcat 5 years ago
parent 3486ece12f
commit 842e6fd1d0
  1. 38
      lora-mqtt.h
  2. 12
      lora-mqtt.ino
  3. 57
      lora-socket.h

@ -1,7 +1,7 @@
#ifndef __LORA_MATT_H__
#define __LORA_MATT_H__
#define MAX_STRINGVEC_SIZE 20
#define MAX_STRINGVEC_SIZE 2
#include "lora-socket.h"
@ -15,6 +15,13 @@ public:
}
inline void core(){
if(isLoopMode){
if(socket.isNewMsg()){
String msg = "", from = "", to = "", type = "";
socket.getNewMsg(msg, from, to, type);
_onReceived(msg, from, to, type);
}
}
socket.core();
}
@ -31,21 +38,39 @@ public:
inline static void onReceived(void (*f)(String, String)){
_f = f;
}
inline bool isNewMsg(){
isLoopMode = true;
return (newSubject == "") ? false : true;
};
inline void getNewMsg(String& subject, String& content){
subject = newSubject;
content = newContent;
clearNewMsg();
}
private:
static LoRaSocket socket;
static StringVec _subjects;
static void (*_f)(String, String);
static String newSubject, newContent;
static bool isLoopMode;
inline static void _onReceived(String msg, String from, String to, String type){
inline void clearNewMsg(){
newSubject = "";
newContent = "";
};
inline static void _onReceived(String msg, String from, String to, String type){
if(msg.indexOf('$') == -1) return;
String subject = msg.substring(0, msg.indexOf('$'));
String content = msg.substring(msg.indexOf('$') + 1, msg.length());
if(_subjects.Find(subject) != -1){
_f(subject, content);
if(_subjects.Find("#") != -1 || _subjects.Find(subject) != -1){
newSubject = subject;
newContent = content;
if(!isLoopMode) _f(subject, content);
}
};
@ -56,6 +81,7 @@ private:
LoRaSocket LoRaMQTT::socket;
StringVec LoRaMQTT::_subjects;
void (*LoRaMQTT::_f)(String, String);
String LoRaMQTT::newSubject = "", LoRaMQTT::newContent = "";
bool LoRaMQTT::isLoopMode = false;
#endif //__LORA_MATT_H__

@ -20,13 +20,19 @@ void setup(){
}
LoRa.receive();
mqtt.ini();
mqtt.subscribe("lalala");
mqtt.onReceived(msg);
mqtt.subscribe("#");
//mqtt.onReceived(msg);
//mqtt.publish("lalala", "aaaaa");
}
void loop(){
if(mqtt.isNewMsg()){
String s, c;
mqtt.getNewMsg(s, c);
Serial.println(s);
Serial.println(c);
mqtt.publish("res/json", "lalla");
}
mqtt.core();
}

@ -33,19 +33,47 @@ class LoRaSocket {
inline static void onReceived(void (*f)(String, String, String, String)){
_f = f;
};
inline bool isNewMsg(){
isLoopMode = true;
return (newType == "") ? false : true;
};
inline const String getNewMsg(){
String msg = newMsg;
clearNewMsg();
return msg;
};
inline void getNewMsg(String& msg, String& from, String& to, String& type){
msg = newMsg;
from = newFrom;
to = newTo;
type = newType;
clearNewMsg();
};
private:
static StringVec tcp_sendingStack, tcp_receiveStack;
static Vector<unsigned int> tcp_sendingTryTimes;
static void(*_f)(String, String, String, String);
static String newMsg, newFrom, newTo, newType;
static bool isLoopMode;
inline void clearNewMsg(){
newMsg = "";
newFrom = "";
newTo = "";
newType = "";
};
inline static void _onReceived(const String& msg, const String& from, const String& to, const String& type){
(*_f)(msg, from, to, type);
newMsg = msg;
newFrom = from;
newTo = to;
newType = type;
if(!isLoopMode) (*_f)(msg, from, to, type);
};
/* LoRa Functions */
static void LoRa_tx_mode();
static void LoRa_rx_mode();
static void send(const String& s);
static void send(const char *s);
static const String receiveMsg();
/* Package Functions */
inline static const String getIPHeader(const String& to = "0.0.0.0"){
@ -153,9 +181,12 @@ void LoRaSocket::getMsg(const String& msg){
}
void LoRaSocket::udp(const String& msg, const String& to){
String fin = "udp|"+ getIPHeader(to) + encode(msg) + "|";
fin += hash(fin);
send(fin);
char *c;
c = (char*)malloc((msg.length()+39)*sizeof(char));
sprintf(c, "udp|%s%s|", getIPHeader(to).c_str(), encode(msg).c_str());
sprintf(c, "%s%s", c, hash(c).c_str());
send(c);
free(c);
};
@ -179,10 +210,12 @@ void LoRaSocket::rtcp(const String& msg){
StringVec LoRaSocket::tcp_sendingStack, LoRaSocket::tcp_receiveStack;
Vector<unsigned int> LoRaSocket::tcp_sendingTryTimes;
void (*LoRaSocket::_f)(String, String, String, String);
String LoRaSocket::newMsg = "", LoRaSocket::newFrom = "", LoRaSocket::newTo = "", LoRaSocket::newType = "";
bool LoRaSocket::isLoopMode = false;
void LoRaSocket::ini() {
LoRa_rx_mode();
}
void LoRaSocket::core() {
@ -205,10 +238,20 @@ void LoRaSocket::LoRa_rx_mode(){
LoRa.receive();
}
void LoRaSocket::send(const char *s){
LoRa_tx_mode();
delay(200);
LoRa.beginPacket();Serial.println(s);
LoRa.print(s);
LoRa.endPacket();
delay(200);
LoRa_rx_mode();
}
void LoRaSocket::send(const String& s){
LoRa_tx_mode();
delay(200);
LoRa.beginPacket();
LoRa.beginPacket();Serial.println(s);
LoRa.print(s);
LoRa.endPacket();
delay(200);

Loading…
Cancel
Save