diff --git a/CSCE838-Lab4-Node/CSCE838-Lab4-Node.ino b/CSCE838-Lab4-Node/CSCE838-Lab4-Node.ino index 2edda140b34a5ffe0890144b104c9d3c0b43762f..0fc788dd0a518e1495b82cd46a4fa696514b2850 100644 --- a/CSCE838-Lab4-Node/CSCE838-Lab4-Node.ino +++ b/CSCE838-Lab4-Node/CSCE838-Lab4-Node.ino @@ -66,7 +66,7 @@ static volatile unsigned int counterISR = 1; // this interval should not also be increased. // See this spreadsheet for an easy airtime and duty cycle calculator: // https://docs.google.com/spreadsheets/d/1voGAtQAjC1qBmaVuP1ApNKs1ekgUjavHuVQIXyYSvNc -#define TX_INTERVAL 60000 //Delay between each message in millisecond. +#define TX_INTERVAL 5000 //Delay between each message in millisecond. // Pin mapping for SAMD21 const lmic_pinmap lmic_pins = { @@ -180,18 +180,18 @@ static void txdone_func (osjob_t* job) static void tx_func (osjob_t* job) { // say hello - tx("Hello, world!", 14, txdone_func); - SerialUSB.println("Hello world"); + //tx("Hello, world!", 14, txdone_func); + //SerialUSB.println("Hello world"); // Construct packet constructTxPacket((Lab3Packet_t*)&txPacket); // Send the packet - //tx((Lab3Packet_t*)&txPacket, sizeof(txPacket), txdone_func); + tx((Lab3Packet_t*)&txPacket, sizeof(txPacket), txdone_func); // Print the packet - //printPacket((Lab3Packet_t*)&txPacket); - //IsErrorDetect((Lab3Packet_t*)&txPacket); + printPacket((Lab3Packet_t*)&txPacket); + IsErrorDetect((Lab3Packet_t*)&txPacket); // Set up the next packet's packet ID txPacket.PacketID++; @@ -222,7 +222,7 @@ void setup() // this is automatically set to the proper bandwidth in kHz, based on the selected channel uint32_t uBandwidth; - LMIC.freq = NETWORK_FREQUENCY * 1000000;; // originally was 903900000 in the example + LMIC.freq = 903900000; // originally was 903900000 in the example uBandwidth = 125; LMIC.datarate = US915_DR_SF9; // DR4 // originally US915_DR_SF7 LMIC.txpow = 20; // originally was 21 in the example diff --git a/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/ESP-sc-gway.h b/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/ESP-sc-gway.h index 37a7f6ac8e503696e955954896f30c8cb0d86cd8..ddf8afd89eb5f909e393e363c49122eb502a6882 100644 --- a/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/ESP-sc-gway.h +++ b/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/ESP-sc-gway.h @@ -192,8 +192,8 @@ #define _LOCALSERVER 0 // See server definitions for decodes // Gateway Ident definitions -#define _DESCRIPTION "Group2" // Name of the gateway -#define _EMAIL "stabrizchi2@huskers.unl.edu" // Owner +#define _DESCRIPTION "Rodoshi Gateway" // Name of the gateway +#define _EMAIL "rrodoshi2@huskers.unl.edu" // Owner #define _PLATFORM "ESP8266" #define _LAT 52.0 #define _LON 5.0 @@ -260,7 +260,9 @@ struct wpas { // wpas wpa[] = { { "" , "" }, // Reserved for WiFi Manager - { "NU-IoT", "vvfzfjrz" } + { "Rodoshi", "11223344" }, + { "RehRez", "ccnaCCNP" }, + { "ape", "beer" } }; // For asserting and testing the following defines are used. @@ -269,4 +271,4 @@ wpas wpa[] = { #define ASSERT(cond) if(!(cond)) gway_failed(__FILE__, __LINE__) #else #define ASSERT(cond) /**/ -#endif \ No newline at end of file +#endif diff --git a/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/ESP-sc-gway.ino b/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/ESP-sc-gway.ino index 977a5d212663e96025842375edddf8d565cb7d38..015e77aaf34d8bea96c1e5aca3e3f148bf76952b 100644 --- a/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/ESP-sc-gway.ino +++ b/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/ESP-sc-gway.ino @@ -1,3 +1,9 @@ +#include "Esp32MQTTClient.h" +#include <HTTPClient.h> +#include <Arduino.h> + +static const char* connectionString = "HostName=RezoanAhmedNazibHub.azure-devices.net;DeviceId=Node1;SharedAccessKey=+PgtmvZu5RYDc/n/1hCoGzj7i7JyXkBwbDE+rtaBrbk="; + // 1-channel LoRa Gateway for ESP8266 // Copyright (c) 2016, 2017, 2018 Maarten Westenberg version for ESP8266 // Version 5.3.3 @@ -27,9 +33,7 @@ // ---------------------------------------------------------------------------------------- #include "ESP-sc-gway.h" // This file contains configuration of GWay -#include "Esp32MQTTClient.h" -#include <HTTPClient.h> -#include <Arduino.h> + #if defined (ARDUINO_ARCH_ESP32) || defined(ESP32) #define ESP32_ARCH 1 #endif @@ -111,10 +115,6 @@ extern "C" { #endif//ESP_ARCH // ----------- Declaration of vars -------------- -#define SEP_CONNECTION_STRING "HostName=SepIoTHub.azure-devices.net;DeviceId=Node1;SharedAccessKey=dZfUU6HJc+TCioN8sbiCgSb/zVK0bgEOiTLKryj6lPA=" -#define SAM_CONNECTION_STRING "HostName=Sam-Murray-Student-IoT-Hub.azure-devices.net;DeviceId=CSCE838-IoT-Sam-Murray;SharedAccessKey=QakoDPCyhIYVOGhf0vcwS+DrHq8rMRWLyS0JmUK8ak0=" -#define MARCUS_CONNECTION_STRING "HostName=CSCE838-IoT.azure-devices.net;DeviceId=Gateway;SharedAccessKey=45a4Yyi98WLO2d7Dudf+jR5r05T063sLWd1UpyCYsAI=" -static const char* connectionString = SEP_CONNECTION_STRING; uint8_t debug=1; // Debug level! 0 is no msgs, 1 normal, 2 extensive uint8_t pdebug=0xFF; // Allow all atterns (departments) @@ -996,21 +996,22 @@ void sendstat() { // _state is S_INIT // ---------------------------------------------------------------------------- void setup() { - char MAC_char[19]; // XXX Unbelievable MAC_char[18] = 0; - Serial.begin(9600); // As fast as possible for bus + Serial.begin(_BAUDRATE); // As fast as possible for bus delay(100); - + Serial.println("Line number 1004"); #if _GPS==1 // Pins are define in LoRaModem.h together with other pins - Serial1.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX);// PIN 12-TX 15-RX + Serial1.begin(115200, SERIAL_8N1, GPS_TX, GPS_RX);// PIN 12-TX 15-RX + Serial.begin("Line number 1008"); #endif #ifdef ESP32 +Serial.println("Line number 1012"); #if DUSB>=1 - Serial.print(F("ESP32 defined, freq=")); + Serial.println(F("ESP32 defined, freq=")); #if _LFREQ==433 Serial.print(freqs[0]); Serial.print(F(" EU433")); @@ -1022,11 +1023,12 @@ void setup() { #endif #endif #ifdef ARDUINO_ARCH_ESP32 + #if DUSB>=1 Serial.println(F("ARDUINO_ARCH_ESP32 defined")); #endif #endif - +Serial.println("Line number 1031"); #if DUSB>=1 Serial.flush(); @@ -1073,15 +1075,15 @@ void setup() { yield(); } #endif - + Serial.println("Line number 1078"); WiFi.mode(WIFI_STA); WiFi.setAutoConnect(true); //WiFi.begin(); - + Serial.println("Line number 1082"); WlanReadWpa(); // Read the last Wifi settings from SPIFFS into memory WiFi.macAddress(MAC_array); - + Serial.println("Line number 1086"); 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: "); @@ -1267,12 +1269,15 @@ void setup() { addr_oLED(); #endif -if (!Esp32MQTTClient_Init((const uint8_t*)connectionString)){ -Serial.println("Initializing IoT hub failed."); -return;} -else{ -Serial.println("Initializing IoT hub success.");} Serial.println(F("--------------------------------------")); + + if (!Esp32MQTTClient_Init((const uint8_t*)connectionString)){ + Serial.println("Initializing IoT hub failed."); + return; + } + else{ + Serial.println("Initializing IoT hub success."); + } }//setup @@ -1294,195 +1299,15 @@ Serial.println("Initializing IoT hub success.");} // ---------------------------------------------------------------------------- void loop () { - uint32_t uSeconds; // micro seconds - int packetSize; - uint32_t nowSeconds = now(); - - // check for event value, which means that an interrupt has arrived. - // In this case we handle the interrupt ( e.g. message received) - // in userspace in loop(). - // - stateMachine(); // do the state machine - - // After a quiet period, make sure we reinit the modem and state machine. - // The interval is in seconds (about 15 seconds) as this re-init - // is a heavy operation. - // SO it will kick in if there are not many messages for the gateway. - // Note: Be careful that it does not happen too often in normal operation. - // - 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 - - // startReceiver() ?? - 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); // Reset all interrupt flags - msgTime = nowSeconds; - } - -#if A_SERVER==1 - // Handle the Web server part of this sketch. Mainly used for administration - // and monitoring of the node. This function is important so it is called at the - // start of the loop() function. - yield(); - server.handleClient(); -#endif - -#if A_OTA==1 - // Perform Over the Air (OTA) update if enabled and requested by user. - // It is important to put this function early in loop() as it is - // not called frequently but it should always run when called. - // - yield(); - ArduinoOTA.handle(); -#endif - - // I event is set, we know that we have a (soft) interrupt. - // After all necessary web/OTA services are scanned, we will - // reloop here for timing purposes. - // Do as less yield() as possible. - // XXX 180326 - if (_event == 1) { - return; - } - else yield(); - - - // If we are not connected, try to connect. - // We will not read Udp in this loop cycle then - if (WlanConnect(1) < 0) { -#if DUSB>=1 - if (( debug >= 0 ) && ( pdebug & P_MAIN )) - Serial.println(F("M ERROR reconnect WLAN")); -#endif - yield(); - return; // Exit loop if no WLAN connected - } - - // So if we are connected - // Receive UDP PUSH_ACK messages from server. (*2, par. 3.3) - // This is important since the TTN broker will return confirmation - // messages on UDP for every message sent by the gateway. So we have to consume them. - // As we do not know when the server will respond, we test in every loop. - // - else { - while( (packetSize = Udp.parsePacket()) > 0) { -#if DUSB>=2 - Serial.println(F("loop:: readUdp calling")); -#endif - // DOWNSTREAM - // Packet may be PKT_PUSH_ACK (0x01), PKT_PULL_ACK (0x03) or PKT_PULL_RESP (0x04) - // This command is found in byte 4 (buffer[3]) - if (readUdp(packetSize) <= 0) { -#if DUSB>=1 - if (( debug>0 ) && ( pdebug & P_MAIN )) - Serial.println(F("M readUDP error")); -#endif - break; - } - // Now we know we succesfully received message from host - else { - //_event=1; // Could be done double if more messages received - } - } - } - - yield(); // XXX 26/12/2017 - - // stat PUSH_DATA message (*2, par. 4) - // - if ((nowSeconds - statTime) >= _STAT_INTERVAL) { // Wake up every xx seconds -#if DUSB>=1 - if (( debug>=1 ) && ( pdebug & P_MAIN )) { - Serial.print(F("M STAT:: ...")); - Serial.flush(); - } -#endif - sendstat(); // Show the status message and send to server -#if DUSB>=1 - if (( debug>=1 ) && ( pdebug & P_MAIN )) { - Serial.println(F(" done")); - if (debug>=2) Serial.flush(); - } -#endif - - // If the gateway behaves like a node, we do from time to time - // send a node message to the backend server. - // The Gateway nod emessage has nothing to do with the STAT_INTERVAL - // message but we schedule it in the same frequency. - // -#if GATEWAYNODE==1 - if (gwayConfig.isNode) { - // Give way to internal some Admin if necessary - yield(); - - // If the 1ch gateway is a sensor itself, send the sensor values - // could be battery but also other status info or sensor info - - if (sensorPacket() < 0) { -#if DUSB>=1 - Serial.println(F("sensorPacket: Error")); -#endif - } - } -#endif - statTime = nowSeconds; - } - - yield(); - - - // send PULL_DATA message (*2, par. 4) - // - nowSeconds = now(); - if ((nowSeconds - pulltime) >= _PULL_INTERVAL) { // Wake up every xx seconds -#if DUSB>=1 - if (( debug>=2) && ( pdebug & P_MAIN )) { - Serial.println(F("M PULL")); - if (debug>=1) Serial.flush(); - } -#endif -// pullData(); // Send PULL_DATA message to server - startReceiver(); - - pulltime = nowSeconds; - } - - - // If we do our own NTP handling (advisable) - // We do not use the timer interrupt but use the timing - // of the loop() itself which is better for SPI -#if NTP_INTR==0 - // Set the time in a manual way. Do not use setSyncProvider - // as this function may collide with SPI and other interrupts - yield(); // 26/12/2017 - nowSeconds = now(); - if (nowSeconds - ntptimer >= _NTP_INTERVAL) { - yield(); - time_t newTime; - newTime = (time_t)getNtpTime(); - if (newTime != 0) setTime(newTime); - ntptimer = nowSeconds; - } -#endif + uint32_t uSeconds; // micro seconds + int packetSize; + uint32_t nowSeconds = now(); + // check for event value, which means that an interrupt has arrived. + // In this case we handle the interrupt ( e.g. message received) + // in userspace in loop(). + // + stateMachine(); }//loop diff --git a/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/_txRx.ino b/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/_txRx.ino index 75e6f46dfef1c0d0a347502f845dd2238a1cb668..c621cc2f8631d7cb061cda7d4db8645f16e21803 100644 --- a/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/_txRx.ino +++ b/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/_txRx.ino @@ -1,3 +1,25 @@ +#include "Esp32MQTTClient.h" +#include <HTTPClient.h> +#include <Arduino.h> + +// Error codes +#define ERROR_CODE_MISSING_PACKET 0x01 +#define ERROR_CODE_RECEPTION_FAILURE 0x02 +#define ERROR_CODE_WDT_EARLY_WARNING 0x04 +#define ERROR_CODE_INVALID_TEMPERATURE 0x08 +#define ERROR_CODE_MISMATCHED_PACKET_NUM 0x10 + +/** Type Definitions **/ +struct Lab3Packet_t +{ + uint8_t NodeID; + uint16_t PacketID; + uint32_t Timestamp; + float AverageTemperature; + uint8_t ErrorType; +}; + + // 1-channel LoRa Gateway for ESP8266 // Copyright (c) 2016, 2017, 2018 Maarten Westenberg version for ESP8266 // Version 5.3.3 @@ -559,12 +581,41 @@ int buildPacket(uint32_t tmst, uint8_t *buff_up, struct LoraUp LoraUp, bool inte ++buff_index; buff_up[buff_index] = 0; // add string terminator, for safety -#if STAT_LOG == 1 - // Do statistics logging. In first version we might only - // write part of the record to files, later more - - addLog( (unsigned char *)(buff_up), buff_index ); -#endif +//#if STAT_LOG == 1 + // Do statistics logging. In first version we might only + // write part of the record to files, later more + addLog( (unsigned char *)(buff_up), buff_index ); + Serial.println((char *)message); + for(int idx=0;idx<messageLength;idx++) + { + Serial.print(message[idx],HEX); + Serial.print(" "); + } + Serial.println(""); + Serial.println("start sending events."); + char buff[256]; + + struct Lab3Packet_t* packet; + packet = (struct Lab3Packet_t*)message; + String packetString = packetToString(packet, false); + + //Send the in JSON format + String res = "{\"Message\": \""; + //res.concat((char *)message); + res.concat(packetString); + res.concat("\"}"); + // Replace the following line with your data sent to Azure IoTHub + snprintf(buff, 256, res.c_str()); + Serial.println(res); + if (Esp32MQTTClient_SendEvent(buff)) + { + Serial.println("Sending data succeed"); + } + else + { + Serial.println("Failure..."); + } +//#endif #if DUSB>=1 if (( debug>=2 ) && ( pdebug & P_RX )) { @@ -711,3 +762,81 @@ int receivePacket() }//receivePacket + + + +bool IsErrorDetect(struct Lab3Packet_t* packet) +{ + if (packet->ErrorType == 0) + { + return false; + } + + uint32_t errorCode = 0 + | ((uint32_t)packet->ErrorType) << 24 // ErrorType occupies bits 31 downto 24 + | ((uint32_t)packet->NodeID) << 16 // NodeID occupies bits 23 downto 16 + | ((uint32_t)packet->PacketID) // PacketID occupies bits 15 downto 0 + ; + + // Print an error message + printError(errorCode); + + return true; +} + +void printError(uint32_t errorCode) +{ + uint8_t errorType = errorCode >> 24; + uint8_t nodeID = errorCode >> 16; + uint16_t packetID = errorCode; + + Serial.print("Error(s) on Node ID "); + Serial.print(": "); + + if (errorType == 0) + { + Serial.println("No errors"); + return; + } + + Serial.print("Error(s): "); + if (errorType & ERROR_CODE_MISSING_PACKET) + { + Serial.print("{Missing packet} "); + } + if (errorType & ERROR_CODE_RECEPTION_FAILURE) + { + Serial.print("{Reception failure} "); + } + if (errorType & ERROR_CODE_WDT_EARLY_WARNING) + { + Serial.print("{WDT early warning} "); + } + if (errorType & ERROR_CODE_INVALID_TEMPERATURE) + { + Serial.print("{Invalid temperature} "); + } + Serial.println(""); +} + +String packetToString(struct Lab3Packet_t* packet, bool printThisDeviceTag) +{ + String s = String("Node ID = ") + + String(packet->NodeID) + + String(", Packet ID = ") + + String(packet->PacketID) + + String(", Timestamp = ") + + String(packet->Timestamp) + + String(", Average Temperature = ") + + String(packet->AverageTemperature) + + String(", Error Type = 0x") + + String(packet->ErrorType, HEX) + ; + + return s; +} + +void printPacket(struct Lab3Packet_t* packet) +{ + Serial.println(packetToString(packet, true)); +} \ No newline at end of file diff --git a/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/loraModem.h b/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/loraModem.h index 1b93eb41c9d9f34da8785f797d7a1eed6f737d85..4185d068f46c314ae75da327540b2dbbc3c35b2e 100644 --- a/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/loraModem.h +++ b/ESP-1ch-Gateway-v5.0-master/ESP-sc-gway/loraModem.h @@ -242,19 +242,20 @@ struct pins { // MOSI 13 / D7 // CLK 14 / D5 // SS 16 / D0 +//#error "Pin Definitions _PIN_OUT must be 1(HALLARD) or 2 (COMRESULT)" + struct pins { + uint8_t dio0 = 26; + uint8_t dio1 = 33; + uint8_t dio2 = 32; + uint8_t ss = 16; + uint8_t rst = 27; // Reset not used + } pins; + #define SCK 14 + #define MISO 12 + #define MOSI 13 + #define SS 16 + #define DIO0 26 -struct pins { -uint8_t dio0 = 26; -uint8_t dio1 = 33; -uint8_t dio2 = 32; -uint8_t ss = 16; -uint8_t rst = 27; // Reset not used -} pins; -#define SCK 14 -#define MISO 12 -#define MOSI 13 -#define SS 16 -#define DIO0 26 #endif // STATR contains the statictis that are kept by message. @@ -520,4 +521,3 @@ struct LoraUp { #define MGT_RESET 0x15 // Not a LoRa Gateway Spec message #define MGT_SET_SF 0x16 #define MGT_SET_FREQ 0x17 -