//send a data value to the cloud server for the sensor with the specified id. void ATTDevice::Send(String value, int id) { if(_mqttclient->connected() == false) { #ifdef DEBUG Serial.println(F("Lost broker connection,restarting from send")); #endif MqttConnect(); } char* message_buff = BuildContent(value); #ifdef DEBUG //don't need to write all of this if not debugging. Serial.print(F("Publish to ")); Serial.print(id); Serial.print(": "); Serial.println(message_buff); #endif char* Mqttstring_buff; { String idStr(id); //turn it into a string, so we can easily calculate the nr of characters that the nr requires. int length = _clientId.length() + _deviceId.length() + 33 + idStr.length(); Mqttstring_buff = new char[length]; sprintf(Mqttstring_buff, "client/%s/out/device/%s/asset/%s/state", _clientId.c_str(), _deviceId.c_str(), idStr.c_str()); Mqttstring_buff[length-1] = 0; } _mqttclient->publish(Mqttstring_buff, message_buff); #ifndef FAST_MQTT //some boards like the old arduino ethernet need a little time after sending mqtt data, other boards don't. delay(100); //give some time to the ethernet shield so it can process everything. #endif delete(message_buff); delete(Mqttstring_buff); }
//send a data value to the cloud server for the sensor with the specified id. void ATTDevice::Send(String value, int id) { if(_mqttclient->connected() == false) { Serial.println(F("Lost broker connection,restarting")); MqttConnect(); } char* message_buff = BuildContent(value); #ifdef DEBUG //don't need to write all of this if not debugging. Serial.print(F("Publish to ")); Serial.print(id); Serial.print(" : "); #endif Serial.println(message_buff); //this value is still useful and generated anyway, so no extra cost. char* Mqttstring_buff; { int length = _clientId.length() + _deviceId.length() + 26; Mqttstring_buff = new char[length]; sprintf(Mqttstring_buff, "client/%s/out/asset/%s%c/state", _clientId.c_str(), _deviceId.c_str(), (char)(id + 48)); Mqttstring_buff[length-1] = 0; } _mqttclient->publish(Mqttstring_buff, message_buff); delay(100); //give some time to the ethernet shield so it can process everything. delete(message_buff); delete(Mqttstring_buff); }
/*Stop http processing & make certain that we can receive data from the mqtt server, given the specified username and pwd. This Subscribe function can be used to connect to a fog gateway returns true when successful, false otherwise*/ bool ATTDevice::Subscribe(PubSubClient& mqttclient, const char* username, const char* pwd) { _mqttclient = &mqttclient; _serverName = ""; //no longer need this reference. CloseHTTP(); _mqttUserName = username; _mqttpwd = pwd; return MqttConnect(); }
//connect with the http server and broker void ATTDevice::Subscribe(PubSubClient& mqttclient) { _mqttclient = &mqttclient; _serverName = NULL; //no longer need this reference. #ifdef DEBUG Serial.println(F("Stopping HTTP")); #endif _client->flush(); _client->stop(); _client = NULL; MqttConnect(); }
//check for any new mqtt messages. bool ATTDevice::Process() { if(_mqttclient->connected() == false) { #ifdef DEBUG Serial.println(F("Lost broker connection,restarting from process")); #endif if(MqttConnect() == false) return false; } _mqttclient->loop(); return true; }