1

Dependencies:   HCSR04 awali2

Revision:
100:642bee2ce173
Parent:
91:dab9882e2b49
--- a/main.cpp	Tue Oct 08 14:00:11 2019 +0100
+++ b/main.cpp	Thu Jan 20 09:44:11 2022 +0000
@@ -1,112 +1,191 @@
-/* WiFi Example
- * Copyright (c) 2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+/* EXAMPLE */
+#include "mbed.h"
+#include "hcsr04.h"
+
+//D12 TRIGGER D11 ECHO
+HCSR04 sensor(D12, D11);
+
+
+
+#include "MQTTmbed.h"
+
+#include <MQTTClientMbedOs.h>
+
+#include <cstdio>
+
 
 #include "mbed.h"
+#include <LiquidCrystal_I2C.h>
 
-WiFiInterface *wifi;
+// Set the LCD address to 0x27 for a 16 chars and 2 line display
+LiquidCrystal_I2C lcd(0x4E, 16, 2);
+ 
+
+int main(int argc, char *argv[])
+ {
+   
+   /* EXAMPLE */
+ long distance = sensor.distance();   
+      printf("distance  %d  \n",distance);
+    // initialize the LCD
+    lcd.begin();
+     
+      
+    // initialize the LCD
+    lcd.begin();
 
-const char *sec2str(nsapi_security_t sec)
-{
-    switch (sec) {
-        case NSAPI_SECURITY_NONE:
-            return "None";
-        case NSAPI_SECURITY_WEP:
-            return "WEP";
-        case NSAPI_SECURITY_WPA:
-            return "WPA";
-        case NSAPI_SECURITY_WPA2:
-            return "WPA2";
-        case NSAPI_SECURITY_WPA_WPA2:
-            return "WPA/WPA2";
-        case NSAPI_SECURITY_UNKNOWN:
-        default:
-            return "Unknown";
-    }
-}
+    // Turn on the blacklight and print a message.
+    lcd.backlight();
+    lcd.print("Hello, world!");
+    
+    
+    
+//lcd.printf("Hi mbed World!\n");
+
+ printf("Starting IBM MQTT demo:\n");
+
+ 
+
+ TCPSocket socket;
+
+ NetworkInterface *net = NetworkInterface::get_default_instance();
+
+ if (!net) {
+
+   printf("Error! No network inteface found.\n");
+
+   return 0;
+
+ }
 
-int scan_demo(WiFiInterface *wifi)
-{
-    WiFiAccessPoint *ap;
+ printf("Connecting to the network...\r\n");
+
+ 
+
+ nsapi_size_or_error_t rc = net->connect();
+
+ if (rc != 0) {
+
+   printf("Error! _net->connect() returned: %d\r\n", rc);
 
-    printf("Scan:\n");
+   return -1;
+
+ }
+
+ 
 
-    int count = wifi->scan(NULL,0);
+ rc = socket.open(net);
+
+ if (rc != 0) {
+
+   printf("Error! _socket.open() returned: %d\r\n", rc);
 
-    if (count <= 0) {
-        printf("scan() failed with return value: %d\n", count);
-        return 0;
-    }
+   return -1;
+
+ }
 
-    /* Limit number of network arbitrary to 15 */
-    count = count < 15 ? count : 15;
+ 
+
+ SocketAddress address;
+
+ net->gethostbyname("sandbox.rightech.io",
 
-    ap = new WiFiAccessPoint[count];
-    count = wifi->scan(ap, count);
+                    &address);
+
+ address.set_port(1883);
+
+ 
 
-    if (count <= 0) {
-        printf("scan() failed with return value: %d\n", count);
-        return 0;
-    }
+ printf("Opening connection to remote %s port %d\r\n",
+
+        address.get_ip_address(), address.get_port());
+
+ rc = socket.connect(address);
 
-    for (int i = 0; i < count; i++) {
-        printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
-               sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
-               ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
-    }
-    printf("%d networks available.\n", count);
+ if (rc != 0) {
+
+   printf("Error! _socket.connect() returned: %d\r\n", rc);
+
+   return -1;
+
+ }
+
+ printf("Connected socket\n");
 
-    delete[] ap;
-    return count;
-}
+ 
+
+ MQTTClient client(&socket);
+
+ MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+
+ data.MQTTVersion = 3;
+
+ data.clientID.cstring = "mqtt-rudikrafikov-11r3lh";
+
+ data.username.cstring = "1234";
 
-int main()
-{
-    printf("WiFi example\n");
+ data.password.cstring = "1234";
+
+ if ((rc = client.connect(data)) != 0)
+
+   printf("rc from MQTT connect is %d\r\n", rc);
 
-#ifdef MBED_MAJOR_VERSION
-    printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
-#endif
+ 
+ 
+ 
+ 
+    //{clientId:"vali_client_id",userName:"vali_user_name",password:"fkQiW3AJFcM38ED"}
+ 
+ 
+  
+ 
 
-    wifi = WiFiInterface::get_default_instance();
-    if (!wifi) {
-        printf("ERROR: No WiFiInterface found.\n");
-        return -1;
-    }
+ MQTT::Message message;
+
+ char *topic = "temp";
+
+ char buf[100];
 
-    int count = scan_demo(wifi);
-    if (count == 0) {
-        printf("No WIFI APs found - can't continue further.\n");
-        return -1;
-    }
+ int temp = 33;
+
+ int press = 2;
+
+ int hum = 3;
+
+// sprintf(buf,
+
+//         "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%d,\"Pressure\":"
+
+//         "%d,\"Humidity\":%d}}", temp, press, hum);
+
+// sprintf(buf,
+
+//         "{\"temperature\":  %d}", temp);
+ sprintf(buf,
+
+         "{\"distance\":  %d}", distance);
+
+
 
-    printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
-    int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
-    if (ret != 0) {
-        printf("\nConnection error: %d\n", ret);
-        return -1;
-    }
+ printf("Sending message: \n%s\n", buf);
+
+ message.qos = MQTT::QOS0;
+
+ message.retained = false;
+
+ message.dup = false;
+
+ message.payload = (void *)buf;
 
-    printf("Success\n\n");
-    printf("MAC: %s\n", wifi->get_mac_address());
-    printf("IP: %s\n", wifi->get_ip_address());
-    printf("Netmask: %s\n", wifi->get_netmask());
-    printf("Gateway: %s\n", wifi->get_gateway());
-    printf("RSSI: %d\n\n", wifi->get_rssi());
+ message.payloadlen = strlen(buf);
+
+ rc = client.publish(topic, message);
+
+ printf("Message sent\n");
 
-    wifi->disconnect();
+ printf("Demo concluded successfully \r\n");
 
-    printf("\nDone\n");
-}
+ 
+
+ return 0;
+}
\ No newline at end of file