watersensor and tmp
Dependencies: DS1820 MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of IDW01M1-MQTT by
Diff: main.cpp
- Revision:
- 2:d2fb91dc221e
- Parent:
- 1:2ee9e5685a1e
- Child:
- 3:0511bd22e20f
diff -r 2ee9e5685a1e -r d2fb91dc221e main.cpp --- a/main.cpp Tue Oct 31 23:56:09 2017 +0800 +++ b/main.cpp Wed Nov 01 07:25:04 2017 +0000 @@ -1,5 +1,5 @@ #include "mbed.h" -#include "BME280.hpp" +#include "DS1820.h" #include "SpwfInterface.h" #include "TCPSocket.h" #include "MQTTClient.h" @@ -9,19 +9,22 @@ #define MQTT_MAX_PACKET_SIZE 250 #define MQTT_MAX_PAYLOAD_SIZE 300 //Configuration value needed to connect Red-node -#define BROKER_URL "192.168.0.10"; +#define BROKER_URL "192.168.20.116"; #define MQTT_PORT 1883 //MQTT use Topic -#define TOPIC "temp" +#define TOPIC0 "1" +#define TOPIC1 "3" #define SUB_TOPIC "LED" //Wifi network -#define SSID "megu megu fire" -#define PASSW "66666667" +#define SSID "tnta" +#define PASSW "tnta2355818" +Serial serial(USBTX, USBRX); DigitalOut myled(LED1); -BME280 bmpSensor; +DS1820 ds1820(PA_9); +DigitalIn sensor_in(D4); int connack_rc = 0; // MQTT connack return code const char * ip_addr = ""; @@ -35,8 +38,10 @@ char subscription_url[MQTT_MAX_PAYLOAD_SIZE]; MQTT::Message message; -MQTTString TopicName={TOPIC}; -MQTT::MessageData MsgData(TopicName, message); +MQTTString TopicName1 = {TOPIC0}; +MQTTString TopicName2 = {TOPIC1}; +MQTT::MessageData MsgData1(TopicName1, message); +MQTT::MessageData MsgData2(TopicName2, message); void subscribe_LED(char* msg){ int value = atoi(msg); @@ -76,7 +81,8 @@ LOG("Nucleo IP ADDRESS: %s\n\r", WiFi.get_ip_address()); LOG("Nucleo MAC ADDRESS: %s\n\r", WiFi.get_mac_address()); LOG("Server Hostname: %s port: %d\n\r", hostname, MQTT_PORT); - LOG("Topic: %s\n\r", TOPIC); + LOG("Topic: %s\n\r", TOPIC0); + LOG("Topic: %s\n\r", TOPIC1); //need subscrie LOG("=====================================\n\r"); netConnecting = true; @@ -139,17 +145,31 @@ wait(timeout); } } -int publish (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack){ +int publish0 (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack){ MQTT::Message message; - int data = bmpSensor.Temp_read(); - char *pubTopic = TOPIC; + char *pubTopic = TOPIC0; + char buf[MQTT_MAX_PAYLOAD_SIZE]; + serial.printf("Temp = %d\n",ds1820.read()); + serial.printf(buf,"%d",ds1820.read()); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf); + printf("Publishing %s\n\r", buf); + return client->publish(pubTopic, message); +} + +int publish1 (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack) +{ + MQTT::Message message; + char *pubTopic = TOPIC1; char buf[MQTT_MAX_PAYLOAD_SIZE]; - - printf("Temp = %d\n", data); - sprintf(buf,"%d",data); + serial.printf("Watersensor = %d\n", sensor_in); + serial.printf(buf,"%d",sensor_in); message.qos = MQTT::QOS0; message.retained = false; message.dup = false; @@ -169,11 +189,12 @@ //use SpwfSAInterface connect AP SpwfSAInterface spwf(D8,D2, false); - printf("\r\nX-NUCLEO-IDW01M1 mbed \n"); - printf("\r\nconnecting to AP\n"); + serial.printf("\r\nX-NUCLEO-IDW01M1 mbed \n"); + serial.printf("\r\nconnecting to AP\n"); //connect to Wifi MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2); //check wifi has got ip_address + serial.printf(ipstack.getWiFi().get_ip_address()); if(ipstack.getWiFi().get_ip_address() == 0){ printf("Connect WiFi is failed!\nPlease check your ssid and passwd is correct"); return 0; @@ -189,12 +210,17 @@ while (true) { if (++count == 100) - { // Publish a message every second - if (publish(&client, &ipstack) != 0) { - + { // Publish a message every second + if (publish0(&client, &ipstack) != 0) + { + attemptConnect(&client, &ipstack); // if we have lost the connection + } + if (publish1(&client, &ipstack) != 0) + { attemptConnect(&client, &ipstack); // if we have lost the connection } count = 0; + wait(5); } // int start = tyeld.read_ms(); client.yield(10); // allow the MQTT client to receive messages @@ -202,5 +228,4 @@ } - return 0; } \ No newline at end of file