Reply To: MQTT Library und Thingspeak API

Forums The NanoESP & Pretzel Board Forum MQTT Library und Thingspeak API Reply To: MQTT Library und Thingspeak API

#2067
Yunaria
Participant

Nochmal ich…

Das gleichzeitige Übertragen mehrerer Sensordaten / mehrerer Felder funktioniert auch auf diese Art.
Den unschönen Code bitte ich zu entschuldigen.

Die relevanten Zeilen:

mqtt_msg newData;

void setup() 
{

newData = {"channels/210619/publish/" + ThingSpeakKEY, "0"};

if (mqtt.connect(0, "mqtt.thingspeak.com", 1883, ThingSpeakKEY))
	{
   		Serial.println("Mit ThingSpeak MQTT verbunden");
 	}
else 
  {
    Serial.println("Verbinden mit ThingSpeak MQTT gescheitert");
  }

}

void loop() 
{
  sendValues();
  delay(15000); 
}

void sendValues() 
{
  newData.value = String("field1=" + String(getDHT11Temp()) + "&field2=" + String(getDHT11Hum()) + "&field3=" + String(getDS18B20()) + "&field4=" + String(getLDR()) + "&field5=" + String(getPing())); 

  mqtt.publish(0, &newData);
  
  delay(500);
}

//--------------------------------Sensoren----------------------------------------------------

float getDHT11Temp()
{
    float DHTtemp = dht.readTemperature();
    
	return DHTtemp;
}

int getDHT11Hum()
{
    float DHThum = dht.readHumidity();
    return DHThum;
}

double getDS18B20()
{
	sensors.requestTemperatures(); 
    double SensorDS = sensors.getTempCByIndex(0);
    return SensorDS;
}

int getPing()
{
	int pingZeit=0;
	do
	{
	    pingZeit = nanoesp.ping("www.google.at");
	} 
	while (pingZeit<=0);

 return pingZeit;
}

double getLDR()
{
	double LDRinput = analogRead(LDR);
	LDRinput = LDRinput/10;
	return LDRinput;
}

LG Yunaria

Attachments: