123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919 |
- #include "configGway.h"
- #include "configNode.h"
- #if defined (ARDUINO_ARCH_ESP32) || defined(ESP32)
- #define ESP32_ARCH 1
- #endif
- #include <Esp.h> // ESP8266 specific IDE functions
- #include <string.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <cstdlib>
- #include <sys/time.h>
- #include <cstring>
- #include <string> // C++ specific string functions
- #include <SPI.h> // For the RFM95 bus
- #include <TimeLib.h> // http://playground.arduino.cc/code/time
- #include <DNSServer.h> // Local DNSserver
- #include <ArduinoJson.h>
- #include <FS.h> // ESP8266 Specific
- #include <WiFiUdp.h>
- #include <pins_arduino.h>
- #include <gBase64.h> // https://github.com/adamvr/arduino-base64 (changed the name)
- #include "loraModem.h"
- #include "loraFiles.h"
- #include "oLED.h"
- extern "C" {
- #include "lwip/err.h"
- #include "lwip/dns.h"
- }
- #if _WIFIMANAGER==1
- #include <WiFiManager.h> // Library for ESP WiFi config through an AP
- #endif
- #if (GATEWAYNODE==1) || (_LOCALSERVER==1)
- #include "AES-128_V10.h"
- #endif
- #if ESP32_ARCH==1
- #include "WiFi.h"
- #include <ESPmDNS.h>
- #include <SPIFFS.h>
- #if A_SERVER==1
- #include <ESP32WebServer.h> // Dedicated Webserver for ESP32
- #include <Streaming.h> // http://arduiniana.org/libraries/streaming/
- #endif
- #if A_OTA==1
- #include <ESP32httpUpdate.h> // Not yet available
- #include <ArduinoOTA.h>
- #endif
- #else
- #include <ESP8266WiFi.h> // Which is specific for ESP8266
- #include <ESP8266mDNS.h>
- extern "C" {
- #include "user_interface.h"
- #include "c_types.h"
- }
- #if A_SERVER==1
- #include <ESP8266WebServer.h>
- #include <Streaming.h> // http://arduiniana.org/libraries/streaming/
- #endif
- #if A_OTA==1
- #include <ESP8266httpUpdate.h>
- #include <ArduinoOTA.h>
- #endif
- #endif
- uint8_t debug=1;
- uint8_t pdebug=0xFF;
- #if GATEWAYNODE==1
- #if _GPS==1
- #include <TinyGPS++.h>
- TinyGPSPlus gps;
- HardwareSerial Serial1(1);
- #endif
- #endif
- #if A_SERVER==1
- #if ESP32_ARCH==1
- ESP32WebServer server(A_SERVERPORT);
- #else
- ESP8266WebServer server(A_SERVERPORT);
- #endif
- #endif
- using namespace std;
- byte currentMode = 0x81;
- bool sx1272 = true;
- uint8_t ifreq = 0;
- uint8_t MAC_array[6];
- sf_t sf = _SPREADING;
- sf_t sfi = _SPREADING;
- float lat = _LAT;
- float lon = _LON;
- int alt = _ALT;
- char platform[24] = _PLATFORM;
- char email[40] = _EMAIL;
- char description[64]= _DESCRIPTION;
- IPAddress ntpServer;
- IPAddress ttnServer;
- IPAddress thingServer;
- WiFiUDP Udp;
- time_t startTime = 0;
-
-
- uint32_t eventTime = 0;
- uint32_t sendTime = 0;
- uint32_t doneTime = 0;
- uint32_t statTime = 0;
- uint32_t pulltime = 0;
- #if A_SERVER==1
- uint32_t wwwtime = 0;
- #endif
- #if NTP_INTR==0
- uint32_t ntptimer = 0;
- #endif
- #define TX_BUFF_SIZE 1024
- #define RX_BUFF_SIZE 1024
- #define STATUS_SIZE 512
- #if GATEWAYNODE==1
- uint16_t frameCount=0;
- #endif
- int mutexSPI = 1;
- void ICACHE_RAM_ATTR Interrupt_0();
- void ICACHE_RAM_ATTR Interrupt_1();
- int sendPacket(uint8_t *buf, uint8_t length);
- static void printIP(IPAddress ipa, const char sep, String& response);
- void setupWWW();
- void SerialTime();
- void SerialStat(uint8_t intr);
- void printHexDigit(uint8_t digit);
- int inDecodes(char * id);
- void init_oLED();
- void acti_oLED();
- void addr_oLED();
- void setupOta(char *hostname);
- void initLoraModem();
- void rxLoraModem();
- void writeRegister(uint8_t addr, uint8_t value);
- void cadScanner();
- void stateMachine();
- bool connectUdp();
- int readUdp(int packetSize);
- int sendUdp(IPAddress server, int port, uint8_t *msg, int length);
- void sendstat();
- void pullData();
- #if MUTEX==1
- void ICACHE_FLASH_ATTR CreateMutux(int *mutex);
- bool ICACHE_FLASH_ATTR GetMutex(int *mutex);
- void ICACHE_FLASH_ATTR ReleaseMutex(int *mutex);
- #endif
- void die(const char *s)
- {
- Serial.println(s);
- if (debug>=2) Serial.flush();
- delay(50);
-
-
- abort();
- }
- void gway_failed(const char *file, uint16_t line) {
- Serial.print(F("Program failed in file: "));
- Serial.print(file);
- Serial.print(F(", line: "));
- Serial.println(line);
- if (debug>=2) Serial.flush();
- }
- void ftoa(float f, char *val, int p) {
- int j=1;
- int ival, fval;
- char b[7] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-
- for (int i=0; i< p; i++) { j= j*10; }
- ival = (int) f;
- fval = (int) ((f- ival)*j);
- if (fval<0) fval = -fval;
-
- if ((f<0) && (ival == 0)) strcat(val, "-");
- strcat(val,itoa(ival,b,10));
- strcat(val,".");
-
- itoa(fval,b,10);
- for (int i=0; i<(p-strlen(b)); i++) {
- strcat(val,"0");
- }
-
-
- strcat(val,b);
- }
- int sendNtpRequest(IPAddress timeServerIP) {
- const int NTP_PACKET_SIZE = 48;
- byte packetBuffer[NTP_PACKET_SIZE];
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
-
- packetBuffer[0] = 0b11100011;
- packetBuffer[1] = 0;
- packetBuffer[2] = 6;
- packetBuffer[3] = 0xEC;
-
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
-
- if (!sendUdp( (IPAddress) timeServerIP, (int) 123, packetBuffer, NTP_PACKET_SIZE)) {
- gwayConfig.ntpErr++;
- gwayConfig.ntpErrTime = now();
- return(0);
- }
- return(1);
- }
- time_t getNtpTime()
- {
- gwayConfig.ntps++;
-
- if (!sendNtpRequest(ntpServer))
- {
- if (( debug>=0 ) && ( pdebug & P_MAIN ))
- Serial.println(F("M sendNtpRequest failed"));
- return(0);
- }
-
- const int NTP_PACKET_SIZE = 48;
- byte packetBuffer[NTP_PACKET_SIZE];
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- uint32_t beginWait = millis();
- delay(10);
- while (millis() - beginWait < 1500)
- {
- int size = Udp.parsePacket();
- if ( size >= NTP_PACKET_SIZE ) {
-
- if (Udp.read(packetBuffer, NTP_PACKET_SIZE) < NTP_PACKET_SIZE) {
- break;
- }
- else {
-
- unsigned long secs;
- secs = packetBuffer[40] << 24;
- secs |= packetBuffer[41] << 16;
- secs |= packetBuffer[42] << 8;
- secs |= packetBuffer[43];
-
- return(secs - 2208988800UL + NTP_TIMEZONES * SECS_IN_HOUR);
- }
- Udp.flush();
- }
- delay(100);
- }
- Udp.flush();
-
-
-
- gwayConfig.ntpErr++;
- gwayConfig.ntpErrTime = now();
- #if _DUSB>=1
- if (( debug>=0 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("M getNtpTime:: read failed"));
- }
- #endif
- return(0);
- }
- #if NTP_INTR==1
- void setupTime() {
- setSyncProvider(getNtpTime);
- setSyncInterval(_NTP_INTERVAL);
- }
- #endif
- void setup() {
- char MAC_char[19];
- MAC_char[18] = 0;
- Serial.begin(_BAUDRATE);
- delay(100);
- #if _GPS==1
-
- Serial1.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX);
- #endif
- #ifdef ESP32
- #if _DUSB>=1
- Serial.print(F("ESP32 defined, freq="));
- Serial.print(freqs[0].upFreq);
- Serial.println();
- #endif
- #endif
- #ifdef ARDUINO_ARCH_ESP32
- #if _DUSB>=1
- Serial.println(F("ARDUINO_ARCH_ESP32 defined"));
- #endif
- #endif
- #if _DUSB>=1
- Serial.flush();
- delay(500);
- if (SPIFFS.begin()) {
- Serial.println(F("SPIFFS init success"));
- }
- else {
- }
- #endif
- #if _SPIFF_FORMAT>=1
- #if _DUSB>=1
- if (( debug >= 0 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("M Format Filesystem ... "));
- }
- #endif
- SPIFFS.format();
- #if _DUSB>=1
- if (( debug >= 0 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("Format Done"));
- }
- #endif
- #endif
- delay(500);
-
- readConfig(CONFIGFILE, &gwayConfig);
- Serial.print(F("Assert="));
- #if defined CFG_noassert
- Serial.println(F("No Asserts"));
- #else
- Serial.println(F("Do Asserts"));
- #endif
- #if OLED>=1
- init_oLED();
- #endif
- delay(500);
- yield();
- #if _DUSB>=1
- if (debug>=1) {
- Serial.print(F("debug="));
- Serial.println(debug);
- yield();
- }
- #endif
- WiFi.mode(WIFI_STA);
- WiFi.setAutoConnect(true);
-
-
- WlanReadWpa();
- WiFi.macAddress(MAC_array);
-
- sprintf(MAC_char,"%02x:%02x:%02x:%02x:%02x:%02x",
- MAC_array[0],MAC_array[1],MAC_array[2],MAC_array[3],MAC_array[4],MAC_array[5]);
- Serial.print("MAC: ");
- Serial.print(MAC_char);
- Serial.print(F(", len="));
- Serial.println(strlen(MAC_char));
-
- char hostname[12];
-
- while (WlanConnect(0) <= 0) {
- Serial.print(F("Error Wifi network connect "));
- Serial.println();
- yield();
- }
-
-
- #if ESP32_ARCH==1
- sprintf(hostname, "%s%02x%02x%02x", "esp32-", MAC_array[3], MAC_array[4], MAC_array[5]);
- WiFi.setHostname( hostname );
- #else
- sprintf(hostname, "%s%02x%02x%02x", "esp8266-", MAC_array[3], MAC_array[4], MAC_array[5]);
- wifi_station_set_hostname( hostname );
- #endif
-
- Serial.print(F("Host "));
- #if ESP32_ARCH==1
- Serial.print(WiFi.getHostname());
- #else
- Serial.print(wifi_station_get_hostname());
- #endif
- Serial.print(F(" WiFi Connected to "));
- Serial.print(WiFi.SSID());
- Serial.print(F(" on IP="));
- Serial.print(WiFi.localIP());
- Serial.println();
-
- delay(500);
-
-
- #if defined(_UDPROUTER)
-
- if (!connectUdp()) {
- Serial.println(F("Error connectUdp"));
- }
- #elif defined(_TTNROUTER)
- if (!connectTtn()) {
- Serial.println(F("Error connectTtn"));
- }
- #else
- Serial.print(F("Setup:: No UDP or TCP Connection defined"));
-
- #endif
- delay(200);
-
-
- pinMode(pins.ss, OUTPUT);
- pinMode(pins.rst, OUTPUT);
- pinMode(pins.dio0, INPUT);
- pinMode(pins.dio1, INPUT);
-
-
- #if ESP32_ARCH==1
- SPI.begin(SCK, MISO, MOSI, SS);
- #else
- SPI.begin();
- #endif
- delay(500);
-
-
-
-
- Serial.print(F("Gateway ID: "));
- printHexDigit(MAC_array[0]);
- printHexDigit(MAC_array[1]);
- printHexDigit(MAC_array[2]);
- printHexDigit(0xFF);
- printHexDigit(0xFF);
- printHexDigit(MAC_array[3]);
- printHexDigit(MAC_array[4]);
- printHexDigit(MAC_array[5]);
- Serial.print(F(", Listening at SF"));
- Serial.print(sf);
- Serial.print(F(" on "));
- Serial.print((double)freqs[ifreq].upFreq/1000000);
- Serial.println(" MHz.");
- if (!WiFi.hostByName(NTP_TIMESERVER, ntpServer))
- {
- die("Setup:: ERROR hostByName NTP");
- };
- delay(100);
- #ifdef _TTNSERVER
- if (!WiFi.hostByName(_TTNSERVER, ttnServer))
- {
- die("Setup:: ERROR hostByName TTN");
- };
- delay(100);
- #endif
- #ifdef _THINGSERVER
- if (!WiFi.hostByName(_THINGSERVER, thingServer))
- {
- die("Setup:: ERROR hostByName THING");
- }
- delay(100);
- #endif
-
-
- #if A_OTA==1
- setupOta(hostname);
- #endif
-
-
- #if NTP_INTR==1
- setupTime();
- #else
-
-
-
-
-
- while (timeStatus() == timeNotSet) {
- #if _DUSB>=1
- if (( debug>=0 ) && ( pdebug & P_MAIN ))
- Serial.println(F("M setupTime:: Time not set (yet)"));
- #endif
- delay(500);
- time_t newTime;
- newTime = (time_t)getNtpTime();
- if (newTime != 0) setTime(newTime);
- }
-
- startTime = now();
- #if _DUSB>=1
- Serial.print("Time: "); printTime();
- Serial.println();
- #endif
- writeGwayCfg(CONFIGFILE );
- #if _DUSB>=1
- Serial.println(F("Gateway configuration saved"));
- #endif
- #endif
- #if A_SERVER==1
-
- setupWWW();
- #endif
- delay(100);
-
-
- _state = S_INIT;
- initLoraModem();
-
- if (_cad) {
- _state = S_SCAN;
- sf = SF7;
- cadScanner();
- }
- else {
- _state = S_RX;
- rxLoraModem();
- }
- LoraUp.payLoad[0]= 0;
- LoraUp.payLength = 0;
-
-
- if (pins.dio0 == pins.dio1) {
-
- attachInterrupt(pins.dio0, Interrupt_0, RISING);
- }
-
- else {
-
-
- attachInterrupt(pins.dio0, Interrupt_0, RISING);
- attachInterrupt(pins.dio1, Interrupt_1, RISING);
- }
-
- writeConfig(CONFIGFILE, &gwayConfig);
-
- #if OLED>=1
- acti_oLED();
- addr_oLED();
- #endif
- Serial.println(F("--------------------------------------"));
- }
- void loop ()
- {
- uint32_t uSeconds;
- int packetSize;
- uint32_t nowSeconds = now();
-
-
-
-
-
- stateMachine();
-
-
-
-
-
-
-
- if ( ((nowSeconds - statr[0].tmst) > _MSG_INTERVAL ) &&
- (msgTime <= statr[0].tmst) )
- {
- #if _DUSB>=1
- if (( debug>=1 ) && ( pdebug & P_MAIN )) {
- Serial.print("M REINIT:: ");
- Serial.print( _MSG_INTERVAL );
- Serial.print(F(" "));
- SerialStat(0);
- }
- #endif
-
- if ((_cad) || (_hop)) {
- _state = S_SCAN;
- sf = SF7;
- cadScanner();
- }
- else {
- _state = S_RX;
- rxLoraModem();
- }
- writeRegister(REG_IRQ_FLAGS_MASK, (uint8_t) 0x00);
- writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF);
- msgTime = nowSeconds;
- }
- #if A_SERVER==1
-
-
-
- yield();
- server.handleClient();
- #endif
- #if A_OTA==1
-
-
-
-
- yield();
- ArduinoOTA.handle();
- #endif
-
-
-
-
-
- if (_event == 1) {
- return;
- }
- else yield();
-
-
-
- if (WlanConnect(1) < 0) {
- #if _DUSB>=1
- if (( debug >= 0 ) && ( pdebug & P_MAIN ))
- Serial.println(F("M ERROR reconnect WLAN"));
- #endif
- yield();
- return;
- }
-
-
-
-
-
-
-
- else {
- while( (packetSize = Udp.parsePacket()) > 0) {
- #if _DUSB>=2
- Serial.println(F("loop:: readUdp calling"));
- #endif
-
-
-
- if (readUdp(packetSize) <= 0) {
- #if _DUSB>=1
- if (( debug>0 ) && ( pdebug & P_MAIN ))
- Serial.println(F("M readUdp error"));
- #endif
- break;
- }
-
- else {
-
- }
- }
- }
-
- yield();
-
-
- if ((nowSeconds - statTime) >= _STAT_INTERVAL) {
- #if _DUSB>=1
- if (( debug>=1 ) && ( pdebug & P_MAIN )) {
- Serial.print(F("M STAT:: ..."));
- Serial.flush();
- }
- #endif
- sendstat();
- #if _DUSB>=1
- if (( debug>=1 ) && ( pdebug & P_MAIN )) {
- Serial.println(F(" done"));
- if (debug>=2) Serial.flush();
- }
- #endif
-
-
-
-
-
- #if GATEWAYNODE==1
- if (gwayConfig.isNode) {
-
- yield();
-
-
-
-
- if (sensorPacket() < 0) {
- #if _DUSB>=1
- Serial.println(F("sensorPacket: Error"));
- #endif
- }
- }
- #endif
- statTime = nowSeconds;
- }
-
- yield();
-
-
-
- nowSeconds = now();
- if ((nowSeconds - pulltime) >= _PULL_INTERVAL) {
- #if _DUSB>=1
- if (( debug>=2) && ( pdebug & P_MAIN )) {
- Serial.println(F("M PULL"));
- if (debug>=1) Serial.flush();
- }
- #endif
- pullData();
- startReceiver();
-
- pulltime = nowSeconds;
- }
-
-
-
-
- #if NTP_INTR==0
-
-
- yield();
- nowSeconds = now();
- if (nowSeconds - ntptimer >= _NTP_INTERVAL) {
- yield();
- time_t newTime;
- newTime = (time_t)getNtpTime();
- if (newTime != 0) setTime(newTime);
- ntptimer = nowSeconds;
- }
- #endif
-
- }
|