0

請問您門V1.1.2改了什麼ˊ,原來可以連上MQTT Server(LASS),完全連不上

我升級到V1.1.2就掛點,

退回去V1.0.9就完全正常

您用您的example (lass_basic.ino)

這樣我真的不敢更新

 

===============

/*
This example demonstrate how to upload sensor data to MQTT server of LASS.
It include features:
(1) Connect to WiFi
(2) Retrieve NTP time with WiFiUDP
(3) Get PM 2.5 value from PMS3003 air condition sensor with UART
(4) Connect to MQTT server and try reconnect when disconnect

You can find more information at this site:

https://lass.hackpad.com/LASS-README-DtZ5T6DXLbu

*/

#include <WiFi.h>
#include <PubSubClient.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>

char ssid[] = "TSAO"; // your network SSID (name)
char pass[] = "TSAO1234"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)

char gps_lat[] = "24.7805647"; // device's gps latitude
char gps_lon[] = "120.9933177"; // device's gps longitude

char server[] = "gpssensor.ddns.net"; // the MQTT server of LASS

#define MAX_CLIENT_ID_LEN 10
#define MAX_TOPIC_LEN 50
char clientId[MAX_CLIENT_ID_LEN];
char outTopic[MAX_TOPIC_LEN];

WiFiClient wifiClient;
PubSubClient client(wifiClient);

int status = WL_IDLE_STATUS;

WiFiUDP Udp;
const char ntpServer[] = "pool.ntp.org";

const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
const byte nptSendPacket[ NTP_PACKET_SIZE] = {
0xE3, 0x00, 0x06, 0xEC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x4E, 0x31, 0x34,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
byte ntpRecvBuffer[ NTP_PACKET_SIZE ];

#define LEAP_YEAR(Y) ( ((Y)>0) && !((Y)%4) && ( ((Y)%100) || !((Y)%400) ) )
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0
uint32_t epochSystem = 0; // timestamp of system boot up

SoftwareSerial mySerial(0, 1); // RX, TX
#define pmsDataLen 32
uint8_t serialBuf[pmsDataLen];
int pm10 = 0;
int pm25 = 0;
int pm100 = 0;

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}

// send an NTP request to the time server at the given address
void retrieveNtpTime() {
Serial.println("Send NTP packet");

Udp.beginPacket(ntpServer, 123); //NTP requests are to port 123
Udp.write(nptSendPacket, NTP_PACKET_SIZE);
Udp.endPacket();

if(Udp.parsePacket()) {
Serial.println("NTP packet received");
Udp.read(ntpRecvBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

unsigned long highWord = word(ntpRecvBuffer[40], ntpRecvBuffer[41]);
unsigned long lowWord = word(ntpRecvBuffer[42], ntpRecvBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears;

epochSystem = epoch - millis() / 1000;
}
}

void getCurrentTime(unsigned long epoch, int *year, int *month, int *day, int *hour, int *minute, int *second) {
int tempDay = 0;

*hour = (epoch % 86400L) / 3600;
*minute = (epoch % 3600) / 60;
*second = epoch % 60;

*year = 1970; // epoch starts from 1970
*month = 0;
*day = epoch / 86400;

for (*year = 1970; ; (*year)++) {
tempDay += (LEAP_YEAR(*year) ? 366 : 365);
if (tempDay > *day) {
tempDay -= (LEAP_YEAR(*year) ? 366 : 365);
break;
}
}
tempDay = *day - tempDay; // the days left in a year
for ((*month) = 0; (*month) < 12; (*month)++) {
if ((*month) == 1) {
tempDay -= (LEAP_YEAR(*year) ? 29 : 28);
if (tempDay < 0) {
tempDay += (LEAP_YEAR(*year) ? 29 : 28);
break;
}
} else {
tempDay -= monthDays[(*month)];
if (tempDay < 0) {
tempDay += monthDays[(*month)];
break;
}
}
}
*day = tempDay+1; // one for base 1, one for current day
(*month)++;
}

void reconnectMQTT() {
// Loop until we're reconnected
char payload[300];

unsigned long epoch = epochSystem + millis() / 1000;
int year, month, day, hour, minute, second;
getCurrentTime(epoch, &year, &month, &day, &hour, &minute, &second);

while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
Serial.println("connected");

sprintf(payload, "|ver_format=3|fmt_opt=1|app=Pm25Ameba|ver_app=0.0.1|device_id=%s|tick=%d|date=%4d-%02d-%02d|time=%02d:%02d:%02d|device=Ameba|s_d0=%d|gps_lat=%s|gps_lon=%s|gps_fix=1|gps_num=9|gps_alt=2",
clientId,
millis(),
year, month, day,
hour, minute, second,
pm25,
gps_lat, gps_lon
);

// Once connected, publish an announcement...
client.publish(outTopic, payload);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void retrievePM25Value() {
uint8_t c = 0;
int idx = 0;
memset(serialBuf, 0, pmsDataLen);

while (true) {
while (c != 0x42) {
while (!mySerial.available());
c = mySerial.read();
}
while (!mySerial.available());
c = mySerial.read();
if (c == 0x4d) {
// now we got a correct header)
serialBuf[idx++] = 0x42;
serialBuf[idx++] = 0x4d;
break;
}
}

while (idx != pmsDataLen) {
while(!mySerial.available());
serialBuf[idx++] = mySerial.read();
}

pm10 = ( serialBuf[10] << 8 ) | serialBuf[11];
pm25 = ( serialBuf[12] << 8 ) | serialBuf[13];
pm100 = ( serialBuf[14] << 8 ) | serialBuf[15];
}

void initializeWiFi() {
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// local port to listen for UDP packets
Udp.begin(2390);
}

void initializeMQTT() {
byte mac[6];
WiFi.macAddress(mac);
memset(clientId, 0, MAX_CLIENT_ID_LEN);
sprintf(clientId, "FT1_0%02X%02X", mac[4], mac[5]);
sprintf(outTopic, "LASS/Test/Pm25Ameba/%s", clientId);

Serial.print("MQTT client id:");
Serial.println(clientId);
Serial.print("MQTT topic:");
Serial.println(outTopic);

client.setServer(server, 1883);
client.setCallback(callback);
}

void setup()
{
Serial.begin(9600);

initializeWiFi();
retrieveNtpTime();
initializeMQTT();

mySerial.begin(9600); // PMS 3003 UART has baud rate 9600

// Allow the hardware to sort itself out
delay(1500);
}

void loop()
{
retrievePM25Value();

if (!client.connected()) {
reconnectMQTT();
}
client.loop();

delay(60000); // delay 1 minute for next measurement
}

morris.yeh 回答了