为什么ss.begin(9600)不能将数据发送到mqtt;已激活?

问题描述 投票:0回答:1

为什么当ss.begin(9600)时我不能将数据发送到mqtt;已激活?

  /* code that can be sent */

      #define TINY_GSM_MODEM_SIM800
      #include <TinyGPS++.h>
      #include <SoftwareSerial.h>
      #include <TinyGsmClient.h>
      #include <PubSubClient.h>
      #include "DHT.h"

      #define SensorPin A3  
      #define DHTPIN 4
      #define pompa 7
      SoftwareSerial SerialAT(8,9); // RX, TX

      static const int RXPin = 11, TXPin = 10;
      //static const uint32_t GPSBaud = 9600;
      // The TinyGPS++ object
      TinyGPSPlus gps;

      unsigned long int avgValue;
      float b;
      int buf[10],temp;

      //Network details
      const char apn[]  = "my3g";
      const char user[] = "";
      const char pass[] = "";

      // MQTT details
      const char* broker = "tailor.cloudmqtt.com";
      const char* uss = "*******";
      const char* pwd = "*******";
      const char* topicOut = "/coba";
      const char* topicIn = "/tombol";

      #define DHTTYPE DHT11
      int sensor= A0;

      SoftwareSerial ss(11, 10);
      TinyGsm modem(SerialAT);
      TinyGsmClient client(modem);
      PubSubClient mqtt(client);
      DHT dht(DHTPIN, DHTTYPE);

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

        pinMode(sensor, INPUT);
        pinMode(pompa, OUTPUT);
        digitalWrite(pompa, HIGH);
        Serial.println("System start.");
        modem.restart();
        Serial.println("Modem: " + modem.getModemInfo());
        Serial.println("Searching for telco provider.");
        if(!modem.waitForNetwork())
        {
          Serial.println("fail");
          while(true);
        }
        Serial.println("Connected to telco.");
        Serial.println("Signal Quality: " + String(modem.getSignalQuality()));

        Serial.println("Connecting to GPRS network.");
        if (!modem.gprsConnect(apn, user, pass))
        {
          Serial.println("fail");
          while(true);
        }
        Serial.println("Connected to GPRS: " + String(apn));

        mqtt.setServer(broker,17063);
        mqtt.setCallback(mqttCallback);
        Serial.println("Connecting to MQTT Broker: " + String(broker));
        while(mqttConnect()==false) continue;
        Serial.println();
      //  ss.begin(9600);                                             //can't be send to mqtt broker
        Serial.println("GPS");
      }

      void loop()
      {
      //  if (ss.available() > 0){
      //    gps.encode(ss.read());
      //    if (gps.location.isUpdated()){

            //soil moisture
            int data = analogRead(sensor);
            int nilai = (105-((data/1023.00)*148));
            //DHT 11
            float h = dht.readHumidity();
            float t = dht.readTemperature();
            String H = String(h);
            String T = String(t);

            //DATA PH
            for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
            { 
              buf[i]=analogRead(SensorPin);
              delay(10);
            }
            for(int i=0;i<9;i++)        //sort the analog from small to large
            {
              for(int j=i+1;j<10;j++)
              {
                if(buf[i]>buf[j])
                {
                  temp=buf[i];
                  buf[i]=buf[j];
                  buf[j]=temp;
                }
              }
            }
            avgValue=0;
            for(int i=2;i<8;i++)                      //take the average value of 6 center sample
            avgValue+=buf[i];
            float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
            phValue = 3.5*phValue;                    //convert the millivolt into pH value
            String ph = String(phValue,2);

            double LAT = double(gps.location.lat());
            double LNG = double(gps.location.lng());

            String Lat = String(LAT,6);
            String Long = String(LNG,6);

            // data digabung
            //{"id":123,"Hum":200,"Temp":12,"Soil":123,"Ph":23,"Long":1223,"Lat":2323}
            String A = "{\"Hum\":" + H + ",\"Temp\":" + T + ",\"Soil\":" + nilai + ",\"Ph\":" + ph + ",\"Long\":" + Lat+",\"Lat\":" + Long + "}";
      //      String A = "{\"Hum\":" + H + ",\"Temp\":" + T + ",\"Soil\":" + nilai + ",\"Ph\":" + ph + ",\"Long\":-6.777722,\"Lat\":1.677734}";
            if (isnan(h) || isnan(t)) {
              Serial.println("Failed to read from DHT sensor!");
              return;
            }

            String message = A;
            Serial.println(A);
            while(Serial.available()) message+=(char)Serial.read();
            mqtt.publish(topicOut, message.c_str());

            delay(500);
            if(mqtt.connected())
            {
              mqtt.loop();
            }             
      //    }
      //  }
      }

      boolean mqttConnect()
      {
        if(!mqtt.connect("GsmClientTest",uss, pwd))
        {
          Serial.print(".");
          return false;
        }
        Serial.println("Connected to broker.");
        mqtt.subscribe(topicIn);
        return mqtt.connected();
      }

      void mqttCallback(char* topic, byte* payload, unsigned int len)
      {
        Serial.print("Message receive [");
        Serial.print(topic);
        Serial.print("] : ");
        String msg = "";
        int i = 0;
        while (i < len){
            msg += (char)payload[i++];
        }
        Serial.print(msg);
        if (msg == "on"){
          digitalWrite(pompa, LOW);
        } else if (msg == "off"){
          digitalWrite(pompa, HIGH);
        }
      //  Serial.write(payload, len);
        Serial.println();
      }

"""


//---------------------------------------------------------------------------------------

/* code that can't be sent */
"""
      #define TINY_GSM_MODEM_SIM800
      #include <TinyGPS++.h>
      #include <SoftwareSerial.h>
      #include <TinyGsmClient.h>
      #include <PubSubClient.h>
      #include "DHT.h"

      #define SensorPin A3  
      #define DHTPIN 4
      #define pompa 7
      SoftwareSerial SerialAT(8,9); // RX, TX

      static const int RXPin = 11, TXPin = 10;
      //static const uint32_t GPSBaud = 9600;
      // The TinyGPS++ object
      TinyGPSPlus gps;

      unsigned long int avgValue;
      float b;
      int buf[10],temp;

      //Network details
      const char apn[]  = "my3g";
      const char user[] = "";
      const char pass[] = "";

      // MQTT details
      const char* broker = "tailor.cloudmqtt.com";
      const char* uss = "gvrwasgg";
      const char* pwd = "hqW7Act1FjRd";
      const char* topicOut = "/coba";
      const char* topicIn = "/tombol";

      #define DHTTYPE DHT11
      int sensor= A0;

      SoftwareSerial ss(11, 10);
      TinyGsm modem(SerialAT);
      TinyGsmClient client(modem);
      PubSubClient mqtt(client);
      DHT dht(DHTPIN, DHTTYPE);

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

        pinMode(sensor, INPUT);
        pinMode(pompa, OUTPUT);
        digitalWrite(pompa, HIGH);
        Serial.println("System start.");
        modem.restart();
        Serial.println("Modem: " + modem.getModemInfo());
        Serial.println("Searching for telco provider.");
        if(!modem.waitForNetwork())
        {
          Serial.println("fail");
          while(true);
        }
        Serial.println("Connected to telco.");
        Serial.println("Signal Quality: " + String(modem.getSignalQuality()));

        Serial.println("Connecting to GPRS network.");
        if (!modem.gprsConnect(apn, user, pass))
        {
          Serial.println("fail");
          while(true);
        }
        Serial.println("Connected to GPRS: " + String(apn));

        mqtt.setServer(broker,17063);
        mqtt.setCallback(mqttCallback);
        Serial.println("Connecting to MQTT Broker: " + String(broker));
        while(mqttConnect()==false) continue;
        Serial.println();
      //  ss.begin(9600);
        Serial.println("GPS");
      }

      void loop()
      {
      //  ss.begin(9600);
      //  if (ss.available() > 0){
      //    gps.encode(ss.read());
      //    if (gps.location.isUpdated()){

            //soil moisture
            int data = analogRead(sensor);
            int nilai = (105-((data/1023.00)*148));
            //DHT 11
            float h = dht.readHumidity();
            float t = dht.readTemperature();
            String H = String(h);
            String T = String(t);

            //DATA PH
            for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
            { 
              buf[i]=analogRead(SensorPin);
              delay(10);
            }
            for(int i=0;i<9;i++)        //sort the analog from small to large
            {
              for(int j=i+1;j<10;j++)
              {
                if(buf[i]>buf[j])
                {
                  temp=buf[i];
                  buf[i]=buf[j];
                  buf[j]=temp;
                }
              }
            }
            avgValue=0;
            for(int i=2;i<8;i++)                      //take the average value of 6 center sample
            avgValue+=buf[i];
            float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
            phValue = 3.5*phValue;                    //convert the millivolt into pH value
            String ph = String(phValue,2);

            double LAT = double(gps.location.lat());
            double LNG = double(gps.location.lng());

            String Lat = String(LAT,6);
            String Long = String(LNG,6);

            // data digabung
            //{"id":123,"Hum":200,"Temp":12,"Soil":123,"Ph":23,"Long":1223,"Lat":2323}
            String A = "{\"Hum\":" + H + ",\"Temp\":" + T + ",\"Soil\":" + nilai + ",\"Ph\":" + ph + 
 ",\"Long\":" + Lat+",\"Lat\":" + Long + "}";
      //      String A = "{\"Hum\":" + H + ",\"Temp\":" + T + ",\"Soil\":" + nilai + ",\"Ph\":" + ph + ",\"Long\":-6.777722,\"Lat\":1.677734}";
            if (isnan(h) || isnan(t)) {
              Serial.println("Failed to read from DHT sensor!");
              return;
            }

            String message = A;
            Serial.println(A);
            while(Serial.available()) message+=(char)Serial.read();
            mqtt.publish(topicOut, message.c_str());

            delay(500);
            if(mqtt.connected())
            {
              mqtt.loop();
            }             
      //    }
      //  }
      }

      boolean mqttConnect()
      {
        if(!mqtt.connect("GsmClientTest",uss, pwd))
        {
          Serial.print(".");
          return false;
        }
        Serial.println("Connected to broker.");
        mqtt.subscribe(topicIn);
        return mqtt.connected();
      }

      void mqttCallback(char* topic, byte* payload, unsigned int len)
      {
        Serial.print("Message receive [");
        Serial.print(topic);
        Serial.print("] : ");
        String msg = "";
        int i = 0;
        while (i < len){
            msg += (char)payload[i++];
        }
        Serial.print(msg);
        if (msg == "on"){
          digitalWrite(pompa, LOW);
        } else if (msg == "off"){
          digitalWrite(pompa, HIGH);
        }
      //  Serial.write(payload, len);
        Serial.println();
      }

我想要这个数据

"{\" Hum \ ":" + H + ", \" Temp \ ":" + T + ", \" Soil \ ":" + value + ", \" Ph \ ":" + ph + ", \" Long \ ":" + Lat + ", \" Lat \ ":" + Long + "}"

可以发送到CloudMQTT。

arduino gps mqtt
1个回答
0
投票
salut。j'ai le memeproblèmede connexion,j'aimerai savoir评论utiliser les fonction“ listen();” dans le codedonné和haute par notre amis。怜悯。
© www.soinside.com 2019 - 2024. All rights reserved.