use Dussolved_oxygen_sensor_on_wifi
Dependencies: watersenor_and_temp_code MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of DS1820_IDW01M1 by
Diff: main.cpp
- Revision:
- 3:30d9b8d9adc9
- Parent:
- 2:eab5b5c8271d
- Child:
- 4:3642f63079ec
diff -r eab5b5c8271d -r 30d9b8d9adc9 main.cpp --- a/main.cpp Thu Nov 02 04:38:10 2017 +0000 +++ b/main.cpp Fri Nov 03 01:57:41 2017 +0000 @@ -4,6 +4,7 @@ #include "TCPSocket.h" #include "MQTTClient.h" #include "MQTTWiFi.h" +#include "DS1820.h" // MQTT use #define MQTT_MAX_PACKET_SIZE 250 @@ -12,7 +13,8 @@ #define BROKER_URL "192.168.20.149"; #define MQTT_PORT 1883 //MQTT use Topic -#define TOPIC "1" +#define TOPIC1 "1" +#define TOPIC2 "3" #define SUB_TOPIC "LED" @@ -22,7 +24,6 @@ DigitalOut myled(LED1); BME280 bmpSensor; - int connack_rc = 0; // MQTT connack return code const char * ip_addr = ""; char *host_addr = ""; @@ -34,9 +35,16 @@ int retryAttempt = 0; char subscription_url[MQTT_MAX_PAYLOAD_SIZE]; + +DS1820 ds1820(PA_9); +Serial serial(USBTX, USBRX); + + MQTT::Message message; -MQTTString TopicName={TOPIC}; -MQTT::MessageData MsgData(TopicName, message); +MQTTString TopicName1={TOPIC1}; +MQTTString TopicName2={TOPIC2}; +MQTT::MessageData MsgData1(TopicName1, message); +MQTT::MessageData MsgData2(TopicName2, message); void subscribe_LED(char* msg){ int value = atoi(msg); @@ -76,7 +84,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("Topic1: %s\n\r", TOPIC1); + LOG("Topic2: %s\n\r", TOPIC2); //need subscrie LOG("=====================================\n\r"); netConnecting = true; @@ -139,15 +148,12 @@ wait(timeout); } } -int publish (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack){ +int publish (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){ MQTT::Message message; - int data = 1; - char *pubTopic = TOPIC; + char *pubTopic = TOPIC1; char buf[MQTT_MAX_PAYLOAD_SIZE]; - - printf("Temp = %d\n", data); sprintf(buf,"%d",data); message.qos = MQTT::QOS0; @@ -164,11 +170,11 @@ { myled =0; /* code */ + const char *ssid = SSID; const char *seckey = PASSW; //use SpwfSAInterface connect AP SpwfSAInterface spwf(D8,D2, false); - printf("\r\nX-NUCLEO-IDW01M1 mbed \n"); printf("\r\nconnecting to AP\n"); //connect to Wifi @@ -182,23 +188,43 @@ MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); attemptConnect(&client, &ipstack); - - + ds1820.begin(); int count = 0; // tyeld.start(); while (true) { - if (++count == 100) - { // Publish a message every second - if (publish(&client, &ipstack) != 0) { - - attemptConnect(&client, &ipstack); // if we have lost the connection - } - count = 0; - } -// int start = tyeld.read_ms(); - client.yield(10); // allow the MQTT client to receive messages -// printf ("tyeld: %d\n\r",tyeld.read_ms()-start); + int databuffer; + + //if(ds1820.begin()) + //{ + //ds1820.startConversion(); // start temperature conversion + //client.yield(1); // let DS1820 complete the temperature conversion + //serial.printf("temp test = %d \r\n", ds1820.read()); // read temperature + //wait(1); + //databuffer = ds1820.read()*10; + //ds1820.startConversion(); // start temperature conversion + //client.yield(1); // let DS1820 complete the temperature conversion + //} + //else + //{ + // serial.printf("No DS1820 sensor found!\r\n"); + // databuffer = 0; + //} - } + if (++count == 3) + { // Publish a message every second + if (publish(&client, &ipstack,databuffer) != 0) + { + attemptConnect(&client, &ipstack); // if we have lost the connection + } + count = 0; + } + else + { + serial.printf("count: %d \n",count); + } +// int start = tyeld.read_ms(); + client.yield(10); // allow the MQTT client to receive messages +// printf ("tyeld: %d\n\r",tyeld.read_ms()-start); + } } \ No newline at end of file