Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repair can not downlink data,localization :shipit: #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions ESP-sc-gway/ESP-sc-gway.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// too much code compiled and loaded on your ESP8266.
//
// ----------------------------------------------------------------------------------------
#define XC_DEAL

#define VERSION "V.5.0.2.H; 171118a nOLED, 15/10"

Expand All @@ -40,7 +41,7 @@
// This parameters contains the default value of SF, the actual version can be set with
// the webserver and it will be stored in SPIFF
// NOTE: The frequency is set in the loraModem.h file and is default 868.100000 MHz.
#define _SPREADING SF9
#define _SPREADING SF12//SF9

// Channel Activity Detection
// This function will scan for valid LoRa headers and determine the Spreading
Expand Down Expand Up @@ -73,7 +74,7 @@
// 1: HALLARD
// 2: COMRESULT pin out
// 3: Other, define your own in loraModem.h
#define _PIN_OUT 1
#define _PIN_OUT 2//1

// Gather statistics on sensor and Wifi status
// 0= No statistics
Expand All @@ -95,15 +96,15 @@
// NOTE: If your node has only one frequency enabled and one SF, you must set this to 1
// in order to receive downlink messages
// NOTE: In all other cases, value 0 works for most gateways with CAD enabled
#define _STRICT_1CH 0
#define _STRICT_1CH 1

// Allows configuration through WifiManager AP setup. Must be 0 or 1
#define WIFIMANAGER 0
#define WIFIMANAGER 1

// Define the name of the accesspoint if the gateway is in accesspoint mode (is
// getting WiFi SSID and password using WiFiManager)
#define AP_NAME "ESP8266-Gway-Things4U"
#define AP_PASSWD "MyPw01!"
#define AP_NAME "zenghi"
#define AP_PASSWD "123456"


// Defines whether the gateway will also report sensor/status value on MQTT
Expand Down Expand Up @@ -143,7 +144,8 @@
#define CONFIGFILE "/gwayConfig.txt"

// Set the Server Settings (IMPORTANT)
#define _LOCUDPPORT 1700 // UDP port of gateway! Often 1700 or 1701 is used for upstream comms
//#define _LOCUDPPORT 1700 // UDP port of gateway! Often 1700 or 1701 is used for upstream comms
#define _LOCUDPPORT 6837 // UDP port of gateway! Often 1700 or 1701 is used for upstream comms

// Timing
#define _MSG_INTERVAL 15
Expand All @@ -154,8 +156,11 @@

// MQTT definitions, these settings should be standard for TTN
// and need not changing
#define _TTNPORT 1700 // Standard port for TTN
#define _TTNSERVER "router.eu.thethings.network"
//#define _TTNPORT 1700 // Standard port for TTN
//#define _TTNSERVER "router.eu.thethings.network"
//#define _TTNSERVER "home.xwqzly.com"
#define _TTNPORT 6837
#define _TTNSERVER "120.39.63.133"

// If you have a second back-end server defined such as Semtech or loriot.io
// your can define _THINGPORT and _THINGSERVER with your own value.
Expand All @@ -167,11 +172,13 @@
//#define _THINGSERVER "yourServer.com" // Server URL of the LoRa-udp.js handler

// Gateway Ident definitions
#define _DESCRIPTION "My ESP Gateway"
#define _EMAIL "whoami@hotmail.com"
#define _DESCRIPTION "wifiadv Gateway"
#define _EMAIL "309966755@qq.com"
#define _PLATFORM "ESP8266"
#define _LAT 52.00
#define _LON 5.900
//#define _LAT 52.00 //52.00
//#define _LON 5.900 //5.900
#define _LAT 26.02
#define _LON 119.410
#define _ALT 00

// ntp
Expand Down Expand Up @@ -218,8 +225,9 @@ struct wpas {
//
wpas wpa[] = {
{ "" , "" }, // Reserved for WiFi Manager
{ "aap", "aapPasswd" },
{ "ape", "apePasswd" }
// { "aap", "aapPasswd" },
// { "zhixingli", "xc87654321xc" },
// { "ape", "apePasswd" }
};

// For asserting and testing the following defines are used.
Expand Down
84 changes: 81 additions & 3 deletions ESP-sc-gway/ESP-sc-gway.ino
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ extern "C" {
SSD1306 display(OLED_ADDR, OLED_SDA, OLED_SCL);// i2c ADDR & SDA, SCL on wemos
#endif

#ifdef XC_DEAL
#include "xc_led.h"
#endif /* XC_DEAL */
int debug=1; // Debug level! 0 is no msgs, 1 normal, 2 extensive

// You can switch webserver off if not necessary but probably better to leave it in.
Expand Down Expand Up @@ -328,7 +331,73 @@ int sendNtpRequest(IPAddress timeServerIP) {
return(1);
}

#ifdef XC_DEAL
const int NTP_PACKET_SIZE = 48; // Fixed size of NTP record
byte packetBuffer[NTP_PACKET_SIZE];
void sendNTPpacket(IPAddress& timeServerIP) {
// Zeroise the buffer.
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;

Udp.beginPacket(timeServerIP, (int) 123); // NTP Server and Port

if ((Udp.write((char *)packetBuffer, NTP_PACKET_SIZE)) != NTP_PACKET_SIZE) {
die("sendNtpPacket:: Error write");
}
else {
// Success
}
Udp.endPacket();
}

// ----------------------------------------------------------------------------
// Get the NTP time from one of the time servers
// Note: As this function is called from SyncINterval in the background
// make sure we have no blocking calls in this function
// ----------------------------------------------------------------------------
time_t getNtpTime()
{
const int NTP_PACKET_SIZE = 48; // Fixed size of NTP record
gwayConfig.ntps++;
WiFi.hostByName(NTP_TIMESERVER, ntpServer); // Get IP address of Timeserver
sendNTPpacket(ntpServer); // Send the request
uint32_t beginWait = millis();
while (millis() - beginWait < 1000)
{
int size = Udp.parsePacket();
if ( size >= NTP_PACKET_SIZE ) {
Udp.read(packetBuffer, NTP_PACKET_SIZE);
// Extract seconds portion.
unsigned long secs;
secs = packetBuffer[40] << 24;
secs |= packetBuffer[41] << 16;
secs |= packetBuffer[42] << 8;
secs |= packetBuffer[43];
Udp.flush();Serial.println(F("getNtpTime:: read OK"));
return secs - 2208988800UL + NTP_TIMEZONES * SECS_PER_HOUR;
// UTC is 1 TimeZone correction when no daylight saving time
}
delay(10); // Wait 10 millisecs, allow kernel to act when necessary
}

Udp.flush();

// If we are here, we could not read the time from internet
// So increase the counter
gwayConfig.ntpErr++;
return 0; // return 0 if unable to get the time
}

#endif /* XC_DEAL */
#ifndef XC_DEAL
// ----------------------------------------------------------------------------
// Get the NTP time from one of the time servers
// Note: As this function is called from SyncINterval in the background
Expand Down Expand Up @@ -382,6 +451,7 @@ time_t getNtpTime()
if (debug>0) Serial.println(F("getNtpTime:: read failed"));
return(0); // return 0 if unable to get the time
}
#endif /* XC_DEAL */

// ----------------------------------------------------------------------------
// Set up regular synchronization of NTP server and the local time.
Expand Down Expand Up @@ -428,8 +498,10 @@ int WlanReadWpa() {
pass.toCharArray(passBuf,pass.length()+1);
Serial.print(F("WlanReadWpa: ")); Serial.print(ssidBuf); Serial.print(F(", ")); Serial.println(passBuf);

#if 1
strcpy(wpa[0].login, ssidBuf); // XXX changed from wpa[0][0] = ssidBuf
strcpy(wpa[0].passw, passBuf);
#endif// endif 0

Serial.print(F("WlanReadWpa: <"));
Serial.print(wpa[0].login); // XXX
Expand Down Expand Up @@ -599,7 +671,7 @@ int readUdp(int packetSize)
uint8_t buff[32]; // General buffer to use for UDP, set to 64
uint8_t buff_down[RX_BUFF_SIZE]; // Buffer for downstream

if (WlanConnect(10) < 0) {
if (WlanConnect(1) < 0) {
#if DUSB>=1
Serial.print(F("readdUdp: ERROR connecting to WLAN"));
if (debug>=2) Serial.flush();
Expand Down Expand Up @@ -835,7 +907,7 @@ int readUdp(int packetSize)
int sendUdp(IPAddress server, int port, uint8_t *msg, int length) {

// Check whether we are conected to Wifi and the internet
if (WlanConnect(3) < 0) {
if (WlanConnect(1) < 0) {
#if DUSB>=1
Serial.print(F("sendUdp: ERROR connecting to WiFi"));
Serial.flush();
Expand Down Expand Up @@ -1149,7 +1221,7 @@ void setup() {
wifi_station_set_hostname( hostname );

// Setup WiFi UDP connection. Give it some time and retry 50 times..
while (WlanConnect(50) < 0) {
while (WlanConnect(2) < 0) {
Serial.print(F("Error Wifi network connect "));
Serial.println();
yield();
Expand All @@ -1174,6 +1246,9 @@ void setup() {
pinMode(pins.rst, OUTPUT);
pinMode(pins.dio0, INPUT); // This pin is interrupt
pinMode(pins.dio1, INPUT); // This pin is interrupt
#ifdef XC_DEAL
// led_init();
#endif /* XC_DEAL */
//pinMode(pins.dio2, INPUT);

// Init the SPI pins
Expand Down Expand Up @@ -1290,6 +1365,9 @@ void setup() {
display.drawString(0, 24, "READY");
display.display();
#endif
#ifdef XC_DEAL
sendstat(); // Show the status message and send to server
#endif /* XC_DEAL */

Serial.println(F("--------------------------------------"));
}//setup
Expand Down
36 changes: 32 additions & 4 deletions ESP-sc-gway/_loraModem.ino
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ void setPow(uint8_t powe)
ASSERT((powe>=2)&&(powe<=15));

uint8_t pac = (0x80 | (powe & 0xF)) & 0xFF;
writeRegister(REG_PAC, (uint8_t)pac); // set 0x09 to pac

// writeRegister(REG_PAC, (uint8_t)pac); // set 0x09 to pac
writeRegister(REG_PAC, (uint8_t)0xff); // set 0x09 to pac
// XXX Power settings for CFG_sx1272 are different

return;
Expand Down Expand Up @@ -824,11 +824,16 @@ void initLoraModem()
{
_state = S_INIT;
// Reset the transceiver chip with a pulse of 10 mSec
#ifndef XC_DEAL
digitalWrite(pins.rst, HIGH);
delayMicroseconds(10000);
#endif /* XC_DEAL */
digitalWrite(pins.rst, LOW);
delayMicroseconds(10000);

#ifdef XC_DEAL
digitalWrite(pins.rst, HIGH);
delayMicroseconds(10000);
#endif /* XC_DEAL */
// 2. Set radio to sleep
opmode(OPMODE_SLEEP); // set register 0x01 to 0x00

Expand All @@ -847,6 +852,16 @@ void initLoraModem()
writeRegister(REG_LNA, (uint8_t) LNA_MAX_GAIN); // 0x0C, 0x23

uint8_t version = readRegister(REG_VERSION); // Read the LoRa chip version id

#if 0
uint8_t count = 1000;
while(count--)
{
uint8_t version1 = readRegister(REG_VERSION); // Read the LoRa chip version id
Serial.println(version1,HEX);
yield();
}
#endif// endif 0
if (version == 0x22) {
// sx1272
#if DUSB>=2
Expand Down Expand Up @@ -890,7 +905,9 @@ void initLoraModem()
writeRegister(REG_PARAMP, (readRegister(REG_PARAMP) & 0xF0) | 0x08); // set PA ramp-up time 50 uSec

// Set 0x4D PADAC for SX1276 ; XXX register is 0x5a for sx1272
writeRegister(REG_PADAC_SX1276, 0x84); // set 0x4D (PADAC) to 0x84
// writeRegister(REG_PADAC_SX1276, 0x84); // set 0x4D (PADAC) to 0x84
writeRegister(REG_PADAC_SX1276, 0x87); // set 0x4D (PADAC) to 0x84

//writeRegister(REG_PADAC, readRegister(REG_PADAC)|0x4);

// Reset interrupt Mask, enable all interrupts
Expand Down Expand Up @@ -982,6 +999,9 @@ void stateMachine()
//_state = S_SCAN;
writeRegister(REG_IRQ_FLAGS, 0xFF ); // Clear ALL interrupts
_event = 0;
#ifdef XC_DEAL
if(_state!=S_TX&&_state!=S_TXDONE)
#endif /* XC_DEAL */
return;
}

Expand Down Expand Up @@ -1161,6 +1181,10 @@ void stateMachine()
_state = S_SCAN;
cadScanner();
writeRegister(REG_IRQ_FLAGS, (uint8_t) 0xFF); // Reset all interrupts
#ifdef XC_DEAL
_event = 1;
Serial.println(F("send ack"));
#endif /* XC_DEAL */
}
break; //S_CAD

Expand Down Expand Up @@ -1366,6 +1390,10 @@ void stateMachine()
}
#endif
}
#ifdef XC_DEAL
_event = 1;
Serial.println(F("TX DONE "));
#endif /* XC_DEAL */
break; // S_TXDONE


Expand Down
Loading