turbidity sensor
Dependencies: watersenor_and_temp_code MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of Dissolved_oxygen_sensor_online by
Revision 5:42113ea1b319, committed 2017-11-06
- Comitter:
- e58136782000
- Date:
- Mon Nov 06 15:04:43 2017 +0000
- Parent:
- 4:3642f63079ec
- Child:
- 6:f037cfd92bde
- Commit message:
- use EZO Dissolved_oxygen;
Changed in this revision
| BME280/BME280.hpp | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/BME280/BME280.hpp Sat Nov 04 00:14:23 2017 +0800
+++ b/BME280/BME280.hpp Mon Nov 06 15:04:43 2017 +0000
@@ -1,28 +1,34 @@
#include "mbed.h"
-
#ifndef BME280_HPP
#define BME280_HPP
-#define BME280_size 14
+#define BME280_size 5
-Serial sensor(PA_11,PA_12);
+Serial sensor(PA_11,PA_12);// Tx,Rx
Serial pc(USBTX,USBRX);
-int AT[3] = {0xA5,0x52,0xF7};
class BME280{
public:
void judgement_data(char buf[]);
- int Temp_read();
+ int data_read();
+ int datareadfuiction(int data);
+ void receive();
private:
- void receive();
+ char buf[BME280_size];
void send_AT();
- char buf[BME280_size];
- int Lux,Temp,Preesure,Hum,Height;
+
+ int data;
};
-void BME280::judgement_data(char buf[]){
- int i=0;
- if(buf[i++]== 0x5A){
+int BME280::datareadfuiction(int data)
+{
+ return buf[data];
+}
+void BME280::judgement_data(char buf[])
+{
+// int i=0;
+
+/* if(buf[i++]== 0x5A){
//pc.printf("first_step\n");
if(buf[i++]== 0x5A){
//pc.printf("second_step\n");
@@ -46,30 +52,25 @@
//pc.printf("Height = %d\n", Height);
}
}
- }
+ }*/
}
void BME280::receive()
{
- for(int i=0;i<BME280_size;i++){
- buf[i] = sensor.getc();
- //pc.putc(buf[i]);
+ pc.printf("start receive \n");
+ for(int i=0;i<BME280_size;i++)
+ {
+ sensor.gets(buf,8);
}
}
-void BME280::send_AT(){
- for(int i=0;i<3;i++){
- sensor.putc(AT[i]);
- //pc.putc(AT[i]);
- }
-}
-int BME280::Temp_read(){
- send_AT();
- //wait(0.2);
+
+int BME280::data_read()
+{
receive();
//wait(0.2);
judgement_data(this->buf);
- return Temp;
+ return data;
}
#endif
--- a/main.cpp Sat Nov 04 00:14:23 2017 +0800
+++ b/main.cpp Mon Nov 06 15:04:43 2017 +0000
@@ -10,18 +10,17 @@
#define MQTT_MAX_PACKET_SIZE 250
#define MQTT_MAX_PAYLOAD_SIZE 300
//Configuration value needed to connect Red-node
-#define BROKER_URL "192.168.20.116";
+#define BROKER_URL "192.168.1.110";
#define MQTT_PORT 1883
//MQTT use Topic
-#define TOPIC1 "1"
-#define TOPIC2 "3"
+#define TOPIC "1"
#define SUB_TOPIC "LED"
//Wifi network
-#define SSID "tnta"
-#define PASSW "tnta2355818"
-
+#define SSID "18-2F"
+#define PASSW "062432779"
+Serial serial(USBTX, USBRX);
DigitalOut myled(LED1);
//BME280 bmpSensor;
int connack_rc = 0; // MQTT connack return code
@@ -35,18 +34,11 @@
int retryAttempt = 0;
char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
-
-
-Serial serial(USBTX, USBRX);
-DS1820 ds1820(PA_8); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
-
+MQTT::Message message;
+MQTTString TopicName={TOPIC};
+MQTT::MessageData MsgData1(TopicName, message);
-
-MQTT::Message message;
-MQTTString TopicName1={TOPIC1};
-MQTTString TopicName2={TOPIC2};
-MQTT::MessageData MsgData1(TopicName1, message);
-MQTT::MessageData MsgData2(TopicName2, message);
+BME280 BME280;
void subscribe_LED(char* msg){
int value = atoi(msg);
@@ -86,8 +78,7 @@
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("Topic1: %s\n\r", TOPIC1);
- LOG("Topic2: %s\n\r", TOPIC2);
+ LOG("Topic1: %s\n\r", TOPIC);
//need subscrie
LOG("=====================================\n\r");
netConnecting = true;
@@ -150,13 +141,13 @@
wait(timeout);
}
}
-int publish (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){
+int publish(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){
MQTT::Message message;
- char *pubTopic = TOPIC1;
+ char *pubTopic = TOPIC;
char buf[MQTT_MAX_PAYLOAD_SIZE];
- printf("Temp = %d\n", data);
+ printf("Dissolved oxygen = %d\n", data);
sprintf(buf,"%d",data);
message.qos = MQTT::QOS0;
message.retained = false;
@@ -168,6 +159,37 @@
return client->publish(pubTopic, message);
}
+int ASCIIChangeInteger(int datain)
+{
+ int databuffer1;
+ if(datain >= 0x30)
+ {
+ if(datain >= 0x3A)
+ {
+ serial.printf("ASCIIChangeInteger datain too big \n");
+ }
+ else
+ {
+ databuffer1 = datain - 0x30;
+ }
+ }
+ else
+ {
+ serial.printf("ASCIIChangeInteger datain too small \n");
+ }
+ serial.printf("ASCIIChangeInteger databuffer1 : %d \n", databuffer1);
+ return databuffer1;
+}
+
+int Conversionfunction(int Tensdigit, int Digits, int TheFirstDecimalPlace, int TheSecondDecimalPlace)
+{
+ int databuffer;
+ serial.printf("dataTensdigit: %d\n",Tensdigit);
+ serial.printf("Digits: %d\n",Digits);
+ databuffer = ASCIIChangeInteger(Tensdigit)*10 + ASCIIChangeInteger(Digits);
+ serial.printf("datanumber: %d\n",databuffer);
+ return databuffer;
+}
int main()
{
myled =0;
@@ -189,24 +211,22 @@
printf("ip: %s\n",ipstack.getWiFi().get_ip_address() );
MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
- attemptConnect(&client, &ipstack);
-
+ attemptConnect(&client, &ipstack);
+ sensor.format(8,Serial::None,1);
int count = 0;
- int databuffer=0;
+ int databuffer3 = 0;
// tyeld.start();
while (true){
- if(!ds1820.begin())
- return -1;
- else
- ds1820.startConversion();
-
+ BME280.receive();
+ serial.printf("data 1 : %d\n",BME280.datareadfuiction(0));
+ serial.printf("data 2 : %d\n",BME280.datareadfuiction(1));
+ serial.printf("data 3 : %d\n",BME280.datareadfuiction(2));
+ serial.printf("data 4 : %d\n",BME280.datareadfuiction(3));
+ serial.printf("data 5 : %d\n",BME280.datareadfuiction(4));
+ databuffer3 = Conversionfunction(BME280.datareadfuiction(3),BME280.datareadfuiction(4),BME280.datareadfuiction(0),BME280.datareadfuiction(1));
if (++count == 3)
{ // Publish a message every second
- serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature
- databuffer = ds1820.read();
- ds1820.startConversion(); // start temperature conversion
- wait(1.0); // let DS1820 complete the temperature conversion
- if (publish(&client, &ipstack,databuffer) != 0)
+ if (publish(&client, &ipstack,databuffer3) != 0)
{
attemptConnect(&client, &ipstack); // if we have lost the connection
}
@@ -220,4 +240,4 @@
//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
+}
