ผมส่งค่าจาก Sensor 4 ขึ้น netpie ทั้ง 3 sensor ปกติ แต่ค่า GPS ไม่ขึ้นครับ
แต่ถ้าเขียน code ให้ส่งค่า GPS อย่างเดียว สามารถส่งค่าขึ้นได้ครับ ไม่ทราบว่ามันมีเงื่อนไขอะไรไหมตรงไหนผมผิดไปขอความช่วยเหลือหน่อยครับ
code
#include <ESP8266WiFi.h>
#include "TinyGPS++.h"
#include "SoftwareSerial.h"
#include <MicroGear.h>
#include "DHT.h"
#include <MCP3008.h>
#include <time.h>
const char* ssid = "********";
const char* password = "*********";
SoftwareSerial serial_connection(D7, D8);
TinyGPSPlus gps;
#define DHTPIN D1
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define APPID "Gas"
#define KEY "nmGDsgKbcs9HNaI"
#define SECRET "AnhBaAT5R00BvI6FLUREIvZnB"
#define ALIAS "NODEMCU"
#define CS_PIN D4
#define CLOCK_PIN D5
#define MOSI_PIN D3
#define MISO_PIN D6
MCP3008 adc(CLOCK_PIN, MOSI_PIN, MISO_PIN, CS_PIN);
int dustPin=A0;
int dustVal=0;
int ledPower=D2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
WiFiClient client;
MicroGear microgear(client);
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
Serial.println("Connected to NETPIE...");
/* Set the alias of this microgear ALIAS */
microgear.setAlias(ALIAS);
}
void setup() {
microgear.on(CONNECTED,onConnected);
Serial.begin(115200);
Serial.println("GPS Start");
serial_connection.begin(9600);//This opens up communications to the GPS
Serial.println("Starting...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
microgear.init(KEY,SECRET,ALIAS);
microgear.connect(APPID);
dht.begin();
}
void Temphum()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(h)) {
return;
}
microgear.publish("/Temp",int(t));
microgear.publish("/Hum",int(h));
Serial.println("TempHum Send");
}
void Gps()
{
while(serial_connection.available())//While there are characters to come from the GPS
{
gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
}
if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
{
microgear.publish("/Lat",gps.location.lat(), 6);
microgear.publish("/Long",gps.location.lng(), 6);
}
}
void Gas()
{
float vol;
float sensorValue = adc.readADC(0);
vol=(float)sensorValue/1024*100.0;
microgear.publish("/Gas",int(vol));
}
void Dust()
{
digitalWrite(ledPower,LOW);// power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin);// read the dust value via pin 5 on the sensor
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH);// turn the LED off
delayMicroseconds(offTime);
microgear.publish("/Dust",float(dustVal));
}
void loop() {
if (microgear.connected()) {
microgear.loop();
Gps();
Temphum();
Gas();
Dust();
delay(1000);
}
else
{
microgear.connect(APPID);
}
}
จากโคดดูแล้วไม่น่าจะมีปัญหา อาจจะติดปัญหาบางอย่างต้องลอง debug โดคเพิ่มเติม ให้เช็คที่คำสั่ง gps น่าจะมาจาก gps ยังรับสัญญาณไม่ได้ครับ