diff --git a/src/gateway/.gitignore b/src/gateway/.gitignore new file mode 100644 index 0000000..dbe9c82 --- /dev/null +++ b/src/gateway/.gitignore @@ -0,0 +1 @@ +.vscode/ \ No newline at end of file diff --git a/src/gateway/BME280.cpp b/src/gateway/BME280.cpp deleted file mode 100644 index e490da1..0000000 --- a/src/gateway/BME280.cpp +++ /dev/null @@ -1,211 +0,0 @@ -#include"BME280.h" -#include -#include - -void BME280::readTrim() -{ - uint8_t data[32],i=0; - Wire.beginTransmission(BME280_ADDRESS); - Wire.write(0x88); - Wire.endTransmission(); - Wire.requestFrom(BME280_ADDRESS,24); - while(Wire.available()){ - data[i] = Wire.read(); - i++; - } - - Wire.beginTransmission(BME280_ADDRESS); - Wire.write(0xA1); - Wire.endTransmission(); - Wire.requestFrom(BME280_ADDRESS,1); - data[i] = Wire.read(); - i++; - - Wire.beginTransmission(BME280_ADDRESS); - Wire.write(0xE1); - Wire.endTransmission(); - Wire.requestFrom(BME280_ADDRESS,7); - while(Wire.available()){ - data[i] = Wire.read(); - i++; - } - dig_T1 = (data[1] << 8) | data[0]; - dig_T2 = (data[3] << 8) | data[2]; - dig_T3 = (data[5] << 8) | data[4]; - dig_P1 = (data[7] << 8) | data[6]; - dig_P2 = (data[9] << 8) | data[8]; - dig_P3 = (data[11]<< 8) | data[10]; - dig_P4 = (data[13]<< 8) | data[12]; - dig_P5 = (data[15]<< 8) | data[14]; - dig_P6 = (data[17]<< 8) | data[16]; - dig_P7 = (data[19]<< 8) | data[18]; - dig_P8 = (data[21]<< 8) | data[20]; - dig_P9 = (data[23]<< 8) | data[22]; - dig_H1 = data[24]; - dig_H2 = (data[26]<< 8) | data[25]; - dig_H3 = data[27]; - dig_H4 = (data[28]<< 4) | (0x0F & data[29]); - dig_H5 = (data[30] << 4) | ((data[29] >> 4) & 0x0F); - dig_H6 = data[31]; -} - -void BME280::writeReg(uint8_t reg_address, uint8_t data) -{ - Wire.beginTransmission(BME280_ADDRESS); - Wire.write(reg_address); - Wire.write(data); - Wire.endTransmission(); -} - -void BME280::readData() -{ - int i = 0; - uint32_t data[8]; - Wire.beginTransmission(BME280_ADDRESS); - Wire.write(0xF7); - Wire.endTransmission(); - Wire.requestFrom(BME280_ADDRESS,8); - while(Wire.available()){ - data[i] = Wire.read(); - i++; - } - pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); - temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); - hum_raw = (data[6] << 8) | data[7]; -} - -double BME280::getTemp() -{ - double temp_act = 0.0, press_act = 0.0,hum_act=0.0; - signed long int temp_cal; - unsigned long int press_cal,hum_cal; - - readData(); - - temp_cal = calibration_T(temp_raw); - press_cal = calibration_P(pres_raw); - hum_cal = calibration_H(hum_raw); - temp_act = (double)temp_cal / 100.0; - press_act = (double)press_cal / 100.0; - hum_act = (double)hum_cal / 1024.0; - /* Serial.print("TEMP : "); - Serial.print(temp_act); - Serial.print(" DegC PRESS : "); - Serial.print(press_act); - Serial.print(" hPa HUM : "); - Serial.print(hum_act); - Serial.println(" %");*/ - - //delay(1000); - return temp_act; -} - -double BME280::getPress() -{ - double temp_act = 0.0, press_act = 0.0,hum_act=0.0; - signed long int temp_cal; - unsigned long int press_cal,hum_cal; - - readData(); - - temp_cal = calibration_T(temp_raw); - press_cal = calibration_P(pres_raw); - hum_cal = calibration_H(hum_raw); - temp_act = (double)temp_cal / 100.0; - press_act = (double)press_cal / 100.0; - hum_act = (double)hum_cal / 1024.0; - /* Serial.print("TEMP : "); - Serial.print(temp_act); - Serial.print(" DegC PRESS : "); - Serial.print(press_act); - Serial.print(" hPa HUM : "); - Serial.print(hum_act); - Serial.println(" %");*/ - - //delay(1000); - return press_cal; -} - -double BME280::getHum() -{ - double temp_act = 0.0, press_act = 0.0,hum_act=0.0; - signed long int temp_cal; - unsigned long int press_cal,hum_cal; - - readData(); - - temp_cal = calibration_T(temp_raw); - press_cal = calibration_P(pres_raw); - hum_cal = calibration_H(hum_raw); - temp_act = (double)temp_cal / 100.0; - press_act = (double)press_cal / 100.0; - hum_act = (double)hum_cal / 1024.0; - /* Serial.print("TEMP : "); - Serial.print(temp_act); - Serial.print(" DegC PRESS : "); - Serial.print(press_act); - Serial.print(" hPa HUM : "); - Serial.print(hum_act); - Serial.println(" %");*/ - //delay(1000); - return hum_act; -} - - - -signed long int BME280::calibration_T(signed long int adc_T) -{ - - signed long int var1, var2, T; - var1 = ((((adc_T >> 3) - ((signed long int)dig_T1<<1))) * ((signed long int)dig_T2)) >> 11; - var2 = (((((adc_T >> 4) - ((signed long int)dig_T1)) * ((adc_T>>4) - ((signed long int)dig_T1))) >> 12) * ((signed long int)dig_T3)) >> 14; - - t_fine = var1 + var2; - T = (t_fine * 5 + 128) >> 8; - return T; -} - - -unsigned long int BME280::calibration_P(signed long int adc_P) -{ - signed long int var1, var2; - unsigned long int P; - var1 = (((signed long int)t_fine)>>1) - (signed long int)64000; - var2 = (((var1>>2) * (var1>>2)) >> 11) * ((signed long int)dig_P6); - var2 = var2 + ((var1*((signed long int)dig_P5))<<1); - var2 = (var2>>2)+(((signed long int)dig_P4)<<16); - var1 = (((dig_P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long int)dig_P2) * var1)>>1))>>18; - var1 = ((((32768+var1))*((signed long int)dig_P1))>>15); - if (var1 == 0) - { - return 0; - } - P = (((unsigned long int)(((signed long int)1048576)-adc_P)-(var2>>12)))*3125; - if(P<0x80000000) - { - P = (P << 1) / ((unsigned long int) var1); - } - else - { - P = (P / (unsigned long int)var1) * 2; - } - var1 = (((signed long int)dig_P9) * ((signed long int)(((P>>3) * (P>>3))>>13)))>>12; - var2 = (((signed long int)(P>>2)) * ((signed long int)dig_P8))>>13; - P = (unsigned long int)((signed long int)P + ((var1 + var2 + dig_P7) >> 4)); - return P; -} - -unsigned long int BME280::calibration_H(signed long int adc_H) -{ - signed long int v_x1; - - v_x1 = (t_fine - ((signed long int)76800)); - v_x1 = (((((adc_H << 14) -(((signed long int)dig_H4) << 20) - (((signed long int)dig_H5) * v_x1)) + - ((signed long int)16384)) >> 15) * (((((((v_x1 * ((signed long int)dig_H6)) >> 10) * - (((v_x1 * ((signed long int)dig_H3)) >> 11) + ((signed long int) 32768))) >> 10) + (( signed long int)2097152)) * - ((signed long int) dig_H2) + 8192) >> 14)); - v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * ((signed long int)dig_H1)) >> 4)); - v_x1 = (v_x1 < 0 ? 0 : v_x1); - v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1); - return (unsigned long int)(v_x1 >> 12); -} diff --git a/src/gateway/BME280.h b/src/gateway/BME280.h deleted file mode 100644 index 83c0fca..0000000 --- a/src/gateway/BME280.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef BME280_H_ -#define BME280_H_ -#include -#include - - -class BME280 -{ -#define BME280_ADDRESS 0x76 - -private: - - signed long int t_fine; - uint16_t dig_T1; - int16_t dig_T2; - int16_t dig_T3; - uint16_t dig_P1; - int16_t dig_P2; - int16_t dig_P3; - int16_t dig_P4; - int16_t dig_P5; - int16_t dig_P6; - int16_t dig_P7; - int16_t dig_P8; - int16_t dig_P9; - int8_t dig_H1; - int16_t dig_H2; - int8_t dig_H3; - int16_t dig_H4; - int16_t dig_H5; - int8_t dig_H6; - -public: -unsigned long int hum_raw,temp_raw,pres_raw; - -void readTrim(); - -void writeReg(uint8_t reg_address, uint8_t data); - -void readData(); - -double getTemp(); - -double getPress(); - -double getHum(); - -signed long int calibration_T(signed long int adc_T); - -unsigned long int calibration_P(signed long int adc_P); - -unsigned long int calibration_H(signed long int adc_H); -}; -#endif diff --git a/src/gateway/GY30.cpp b/src/gateway/GY30.cpp deleted file mode 100644 index 3fa7160..0000000 --- a/src/gateway/GY30.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include"GY30.h" -#include - -int GY30::getLight() -{ - // put your main code here, to run repeatedly: - int val = 0; - // reset - Wire.beginTransmission(ADDR); - Wire.write(0b00000111); - Wire.endTransmission(); - - Wire.beginTransmission(ADDR); - Wire.write(0b00100000); - Wire.endTransmission(); - // typical read delay 120ms - delay(120); - Wire.requestFrom(ADDR, 2); // 2byte every time - for (val = 0; Wire.available() >= 1; ) { - char c = Wire.read(); - //Serial.println(c, HEX); - val = (val << 8) + (c & 0xFF); - } - val = val / 1.2; - return val; -} - diff --git a/src/gateway/GY30.h b/src/gateway/GY30.h deleted file mode 100644 index b8c65e4..0000000 --- a/src/gateway/GY30.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef GY30_H_ -#define GY30_H_ -#include -#include - -class GY30 -{ - #define ADDR 0b0100011 - - private: - - public: - int getLight(); -}; - -#endif - diff --git a/src/gateway/gateway.ino b/src/gateway/gateway.ino index 08e4f3a..4188aee 100644 --- a/src/gateway/gateway.ino +++ b/src/gateway/gateway.ino @@ -1,165 +1,248 @@ -/* - * @Author: IoTcat (https://iotcat.me) - * @Date: 2019-08-20 09:58:56 - * @Last Modified by: - * @Last Modified time: 2019-08-20 09:58:56 - */ -//#include #include #include -#include -#include "BME280.h" -#include "GY30.h" +#include -//const size_t capacity = JSON_OBJECT_SIZE(15); -//DynamicJsonDocument data(capacity); +#define LORA_SOCKET_IP "1.0.0.1" -#define LORA_SOCKET_IP "1.0.0.5" #include "lora-mqtt.h" + +#define MQTT_MAX_TRANSFER_SIZE 99999 + +#include +#include +#include "ovo.h" + + +// WIFI_LoRa_32 ports +// GPIO5 -- SX1278's SCK +// GPIO19 -- SX1278's MISO +// GPIO27 -- SX1278's MOSI +// GPIO18 -- SX1278's CS +// GPIO14 -- SX1278's RESET +// GPIO26 -- SX1278's IRQ(Interrupt Request) + +#define SS 18 +#define RST 14 +#define DI0 26 +#define Band 433E6 + +//#define syncWord 0xF3 + +//============================ +//CHANGE THIS FOR EACH ARDUINO +String nodeId ="goodGateway"; +//============================ + +const char* ssid = "yimian-iot"; +const char* password = "1234567890."; +const char* mqtt_server = "192.168.3.4";//change this to the mqtt server + +char* topicIn="#";//change this to the outgoing messages +String cache_mqtt_publish = ""; +String cache_mqtt_subject = ""; + + +WiFiClient espClient; +PubSubClient client(espClient); + LoRaMQTT mqtt; +char outMsg[200]; +long lastMsg = 0; -BME280 bme; -GY30 gy; +U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16); -void (* resetFunc)(void) = 0; +void oled_print(const String& s){ + u8x8.drawString(0, 4, " "); + u8x8.drawString((16 - s.length()) / 2, 4, s.c_str()); + } + +void router_print(const String& s){ + u8x8.drawString(0, 7, " "); + u8x8.drawString(0, 6, " "); + if(s.length() <= 16){ + u8x8.drawString((16 - s.length()) / 2, 6, s.c_str()); + return; + } + u8x8.drawString(0, 6, s.substring(0, 16).c_str()); + u8x8.drawString(0, 7, s.substring(16, s.length()).c_str()); + } + +void mode_print(const String& s){ + u8x8.drawString(0, 1, " "); + u8x8.drawString((16 - s.length()) / 2, 1, s.c_str()); + } + + void ini_print(){ + u8x8.drawString(0, 3, "---"); + u8x8.drawString(13, 3, "---"); + u8x8.drawString(0, 5, "---"); + u8x8.drawString(13, 5, "---"); + } void setup() { - uint8_t osrs_t = 1; //Temperature oversampling x 1 - uint8_t osrs_p = 1; //Pressure oversampling x 1 - uint8_t osrs_h = 1; //Humidity oversampling x 1 - uint8_t mode = 3; //Normal mode - uint8_t t_sb = 5; //Tstandby 1000ms - uint8_t filter = 0; //Filter off - uint8_t spi3w_en = 0; //3-wire SPI Disable - - uint8_t ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode; - uint8_t config_reg = (t_sb << 5) | (filter << 2) | spi3w_en; - uint8_t ctrl_hum_reg = osrs_h; - - Serial.begin(115200); - pinMode(A0, INPUT); - pinMode(A1, INPUT); - pinMode(A3, INPUT); - pinMode(A7, INPUT); - - Wire.begin(); - if (!LoRa.begin(433E6)) { - Serial.println("Starting LoRa failed!"); - while (1); + Serial.begin(115200); + + u8x8.begin(); + u8x8.setFont(u8x8_font_chroma48medium8_r); + + LoRa.setPins(SS, RST, DI0); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + oled_print("No WiFi.."); } + - mqtt.ini(); - mqtt.subscribe("#"); - mqtt.onReceived(mqttRes); - - bme.writeReg(0xF2,ctrl_hum_reg); - bme.writeReg(0xF4,ctrl_meas_reg); - bme.writeReg(0xF5,config_reg); - bme.readTrim(); + Serial.println(""); + Serial.println("WiFi connected"); + oled_print("WiFi ok!"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + client.setServer(mqtt_server, 1883);//default port, change it... + client.setCallback(callback); + +Serial.println("LoRa Receiver"); - Serial.println("started.."); + if (!LoRa.begin(Band)) { + Serial.println("Starting LoRa failed!"); + oled_print("No LoRa.."); + } - mqtt.publish("res/json", "lalala"); + mqtt.ini(); + mqtt.subscribe("#"); + mqtt.onReceived(mqttRes); + + Serial.println("Lora Started...\n"); + oled_print("LoRa ok.."); + Serial.print("Connecting to "); + Serial.println(ssid); } -void loop() { - if(millis() > 180000) resetFunc(); - mqtt.core(); -} +void callback(char* topic, byte* payload, unsigned int length) { + + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + String s = ""; + for (int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + s += (char)payload[i]; + } + Serial.println(""); + + router_print(String(topic) + " " + s); + + mqtt.publish(String(topic), s); + + if(String(topic) == "hass/snsr/hall/light"){ + if(s == "1") oled_print("hall light on"); + if(s == "0") oled_print("hall light off"); + } + if(String(topic) == "hass/snsr/din/light"){ + if(s == "1") oled_print("din light on"); + if(s == "0") oled_print("din light off"); + } + if(String(topic) == "hass/snsr/liv/light"){ + if(s == "1") oled_print("liv light on"); + if(s == "0") oled_print("liv light off"); + } + if(String(topic) == "hass/snsr/kit/light"){ + if(s == "1") oled_print("kit light on"); + if(s == "0") oled_print("kit light off"); + } + + if(String(topic) == "hass/ctl/kit/mode/isNight"){ + if(s == "0") mode_print("Day Mode"); + if(s == "1") mode_print("Night Mode"); + } + if(String(topic) == "hass/ctl/kit/mode/isMidnight"){ + if(s == "1") mode_print("MidNight Mode"); + } + + if(String(topic).substring(0, 4) == "peo/"){ + oled_print(String(topic).substring(4, String(topic).length()) + " - num " + s); + } + if(String(topic) == "peo/hall"){ + oled_print("hall - num " + s); + } + if(String(topic) == "peo/din"){ + oled_print("din - num " + s); + } + if(String(topic) == "peo/liv"){ + oled_print("liv - num " + s); + } + if(String(topic) == "peo/kit"){ + oled_print("kit - num " + s); + } + + //send to LoRa nodes.. + //sendToNode(); -void mqttRes(String subject, String content){ - Serial.println(subject); - if(subject == "qos/sync" && content.toInt()>=0 && content.toInt()<100){ - String s; - getData(s, content); - //data = getJson(); - //data["qos"] = content; - //data["type"] = "station"; - //serializeJson(data, dataStr); - Serial.println(s); - mqtt.publish("res/json", s); +} +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + + String clientId = nodeId; + + // Attempt to connect + if (client.connect(clientId.c_str())) { + Serial.println("connected"); + oled_print("MQTT ok.."); + ini_print(); + // Once connected, publish an announcement... + //client.publish(topicOut, "Hello from the Gateway!"); + // ... and resubscribe + client.subscribe(topicIn); + } else { + oled_print("No MQTT.."); + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 3 seconds before retrying + delay(3000); + } } - delay(1000); - //resetFunc(); } -void getData(String& s, const String& content){ - s = "{\"t\":\"station\",\"l\": "; - s += gy.getLight(); - s += ",\"T\":"; - s += bme.getTemp(); - s += ",\"h\":"; - s += bme.getHum(); - s += ",\"a\":"; - s += bme.getPress()/100; - s += ",\"C\":"; - s += analogRead(A0); - s += ",\"N\":"; - s += analogRead(A1); - s += ",\"r\":"; - s += analogRead(A2); - s += ",\"b\":"; - s += (unsigned int)analogRead(A6)*100/1024; - s += ",\"s\":"; - s += 0; - s += ",\"q\":"; - s += content; - s += "}"; -} +void f(){ + + if(cache_mqtt_publish != "") { + client.publish(cache_mqtt_subject.c_str(), cache_mqtt_publish.c_str(), cache_mqtt_publish.length()); + cache_mqtt_publish = ""; + cache_mqtt_subject = ""; + } +} -/* -//DynamicJsonDocument createJson(const size_t capacity, int Light, long int batteryLevel, double Temp, double Hum, double Press, int CO, int NH3, int Rain) -DynamicJsonDocument getJson() -{ - DynamicJsonDocument json(capacity); - int Light = gy.getLight(); - double Temp = bme.getTemp(); - double Hum = bme.getHum(); - double Press = bme.getPress()/100; - int CO = analogRead(A0); - int NH3 = analogRead(A1); - int Rain = analogRead(A2); - long int batteryLevel = analogRead(A6); - batteryLevel = (batteryLevel * 100)/1024; - - Serial.print("Light: "); - Serial.println(Light); - Serial.print("batteryLevel: "); - Serial.println(batteryLevel); - Serial.print("Temperature: "); - Serial.println(Temp); - Serial.print("Humidity: "); - Serial.println(Hum); - Serial.print("Press: "); - Serial.println(Press); - Serial.print("Rain: "); - Serial.println(Rain); - Serial.print("CO: "); - Serial.println(CO); - Serial.print("NH3: "); - Serial.println(NH3); - Serial.println(); - - json["status"] = "0"; - json["batterylevel"] = batteryLevel; - json["light"] = Light; - json["temperature"] = Temp; - json["humidity"] = Hum; - json["rainfall"] = Rain; - json["co"] = CO; - json["nh3"] = NH3; - json["airpressure"] = Press; - return json; +void mqttRes(String subject, String content){ + Serial.println("LoRaMQTT::"+subject+"::"+content); + cache_mqtt_subject = subject; + cache_mqtt_publish = content; } -*/ \ No newline at end of file +void loop() { + + if (!client.connected()) + { + reconnect(); + } + client.loop(); + mqtt.core(); + f(); +} + diff --git a/src/gateway/lora-mqtt.h b/src/gateway/lora-mqtt.h index 43e89c2..0fd0ac0 100644 --- a/src/gateway/lora-mqtt.h +++ b/src/gateway/lora-mqtt.h @@ -1,13 +1,7 @@ -/* - * @Author: IoTcat (https://iotcat.me) - * @Date: 2019-08-20 09:58:50 - * @Last Modified by: - * @Last Modified time: 2019-08-20 09:58:50 - */ #ifndef __LORA_MATT_H__ #define __LORA_MATT_H__ -#define MAX_STRINGVEC_SIZE 2 +#define MAX_STRINGVEC_SIZE 20 #include "lora-socket.h" @@ -21,13 +15,6 @@ 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(); } @@ -44,28 +31,13 @@ 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 void clearNewMsg(){ - newSubject = ""; - newContent = ""; - }; inline static void _onReceived(String msg, String from, String to, String type){ @@ -74,9 +46,7 @@ private: String content = msg.substring(msg.indexOf('$') + 1, msg.length()); if(_subjects.Find("#") != -1 || _subjects.Find(subject) != -1){ - newSubject = subject; - newContent = content; - if(!isLoopMode) _f(subject, content); + _f(subject, content); } }; @@ -87,7 +57,6 @@ 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__ \ No newline at end of file diff --git a/src/gateway/lora-socket.h b/src/gateway/lora-socket.h index 0c9cc71..7c305e5 100644 --- a/src/gateway/lora-socket.h +++ b/src/gateway/lora-socket.h @@ -1,9 +1,3 @@ -/* - * @Author: IoTcat (https://iotcat.me) - * @Date: 2019-08-20 09:58:47 - * @Last Modified by: - * @Last Modified time: 2019-08-20 09:59:57 - */ #ifndef __LORA_SOCKET_H__ #define __LORA_SOCKET_H__ @@ -39,47 +33,19 @@ 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 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){ - newMsg = msg; - newFrom = from; - newTo = to; - newType = type; - if(!isLoopMode) (*_f)(msg, from, to, type); + (*_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"){ @@ -187,12 +153,9 @@ void LoRaSocket::getMsg(const String& msg){ } void LoRaSocket::udp(const String& msg, const String& to){ - 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); + String fin = "udp|"+ getIPHeader(to) + encode(msg) + "|"; + fin += hash(fin); + send(fin); }; @@ -216,8 +179,6 @@ void LoRaSocket::rtcp(const String& msg){ StringVec LoRaSocket::tcp_sendingStack, LoRaSocket::tcp_receiveStack; Vector 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() { @@ -244,16 +205,6 @@ void LoRaSocket::LoRa_rx_mode(){ LoRa.receive(); } -void LoRaSocket::send(const char *s){ - LoRa_tx_mode(); - delay(200); - LoRa.beginPacket(); - LoRa.print(s); - LoRa.endPacket(); - delay(200); - LoRa_rx_mode(); -} - void LoRaSocket::send(const String& s){ LoRa_tx_mode(); delay(200); @@ -269,7 +220,7 @@ const String LoRaSocket::receiveMsg(){ String s = ""; while (LoRa.available()) { s += (char)LoRa.read(); - }Serial.println(s); + } return s; } diff --git a/src/gateway/ovo.h b/src/gateway/ovo.h new file mode 100644 index 0000000..6420b13 --- /dev/null +++ b/src/gateway/ovo.h @@ -0,0 +1,61 @@ + +/** + * ovo-arduino Main File + * + * @category ovo-arduino + * @package ovo.cpp + * @copyright Copyright (c) 2019 EEENeko (https://github.com/eeeneko) + * @license GNU General Public License 2.0 + * @version 0.0.1 + */ + + +#ifndef _OVO_H +#define _OVO_H + + +/** + * Like set Timeout in JS + * + * @Author yimian + * @param auto function (allow Lambda) + * @param int millisecond to dealy + * @return void + */ +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)(); + on == 0; + } + } +} + +/** + * Like set Interval in JS + * + * @Author yimian + * @param auto function (allow Lambda) + * @param int millisecond of interval + * @return void + */ +void setInterval(void (*function)(void), const int delay) +{ + static unsigned long startTime = millis(); + + if(millis() - startTime > delay){ + (*function)(); + startTime = millis(); + } +} + + + + + +#endif + diff --git a/src/gateway/stringVec.h b/src/gateway/stringVec.h index 6874a86..08f4cf2 100644 --- a/src/gateway/stringVec.h +++ b/src/gateway/stringVec.h @@ -1,16 +1,10 @@ -/* - * @Author: IoTcat (https://iotcat.me) - * @Date: 2019-08-20 09:58:43 - * @Last Modified by: - * @Last Modified time: 2019-08-20 09:59:58 - */ #ifndef __STRINGVEC__ #define __STRINGVEC__ #ifndef MAX_STRINGVEC_SIZE -#define MAX_STRINGVEC_SIZE 2 +#define MAX_STRINGVEC_SIZE 10 #endif class StringVec{