![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
1
Diff: main.cpp
- Revision:
- 101:bea6b4cfedac
- Parent:
- 100:642bee2ce173
--- a/main.cpp Thu Jan 20 09:44:11 2022 +0000 +++ b/main.cpp Tue Apr 19 07:21:10 2022 +0000 @@ -1,191 +1,231 @@ -/* 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> - -// 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(); - - // 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; - - } - - 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); - - return -1; - - } - - - - rc = socket.open(net); - - if (rc != 0) { - - printf("Error! _socket.open() returned: %d\r\n", rc); - - return -1; - - } - - - - SocketAddress address; - - net->gethostbyname("sandbox.rightech.io", - - &address); - - address.set_port(1883); - - - - printf("Opening connection to remote %s port %d\r\n", - - address.get_ip_address(), address.get_port()); - - rc = socket.connect(address); - - if (rc != 0) { - - printf("Error! _socket.connect() returned: %d\r\n", rc); - - return -1; - - } - - printf("Connected socket\n"); - - - - MQTTClient client(&socket); - - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; - - data.MQTTVersion = 3; - - data.clientID.cstring = "mqtt-rudikrafikov-11r3lh"; - - data.username.cstring = "1234"; - - data.password.cstring = "1234"; - - if ((rc = client.connect(data)) != 0) - - printf("rc from MQTT connect is %d\r\n", rc); - - - - - - //{clientId:"vali_client_id",userName:"vali_user_name",password:"fkQiW3AJFcM38ED"} - - - - - - MQTT::Message message; - - char *topic = "temp"; - - char buf[100]; - - 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("Sending message: \n%s\n", buf); - - message.qos = MQTT::QOS0; - - message.retained = false; - - message.dup = false; - - message.payload = (void *)buf; - - message.payloadlen = strlen(buf); - - rc = client.publish(topic, message); - - printf("Message sent\n"); - - printf("Demo concluded successfully \r\n"); - - - - return 0; +/* 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> + + + +// Set the LCD address to 0x27 for a 16 chars and 2 line display + //BufferedSerial pc(USBTX, USBRX); + //RawSerial pc(USBTX, USBRX); + // Serial pc2(USBTX, USBRX); +// RawSerial pc(USBTX, USBRX); + EventQueue eventQueue(/* event count */ 50 * EVENTS_EVENT_SIZE); + LowPowerTicker lpTicker; + LiquidCrystal_I2C lcd(0x4E, 16, 2); + + void read_datchik_write_lcd_consol_publish_mqtt() + { + + + /* EXAMPLE */ + long distance = sensor.distance(); + printf("distance %d \n",distance); + // initialize the LCD + lcd.begin(); + + + // initialize the LCD + lcd.begin(); + + // Turn on the blacklight and print a message. + lcd.backlight(); + + // lcd.printstr("distance %d \n",distance); + + + + char displayText[1000]; + +sprintf(displayText,"rasstyanie: %d",distance); +lcd.print(displayText); + + + +//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; + + } + +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); + + // return -1; + + } + + + + + rc = socket.open(net); + + if (rc != 0) { + + printf("Error! _socket.open() returned: %d\r\n", rc); + + //return -1; + + } + + + + SocketAddress address; + + net->gethostbyname("dev.rightech.io", + + &address); + + address.set_port(1883); + + + +printf("Opening connection to remote %s port %d\r\n", + + address.get_ip_address(), address.get_port()); + + rc = socket.connect(address); + + if (rc != 0) { + + printf("Error! _socket.connect() returned: %d\r\n", rc); + +// return -1; + + } + +printf("Connected socket\n"); + + + + MQTTClient client(&socket); + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + + data.MQTTVersion = 3; + + data.clientID.cstring = "ric-demo-room-device-my"; + + data.username.cstring = "111"; + + data.password.cstring = "1234567"; + + if ((rc = client.connect(data)) != 0) + + printf("rc from MQTT connect is %d\r\n", rc); + + + + + + //{clientId:"vali_client_id",userName:"vali_user_name",password:"fkQiW3AJFcM38ED"} + + + + + + MQTT::Message message; + + char *topic = "distance"; + + char buf[100]; + + 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, + + "\%d", distance); + //sprintf(buf, + +// "{\"distance\": %d}", distance); + + + +printf("Sending message: \n%s\n", buf); + + message.qos = MQTT::QOS0; + + message.retained = false; + + message.dup = false; + + message.payload = (void *)buf; + + message.payloadlen = strlen(buf); + + rc = client.publish(topic, message); + +printf("Message sent\n"); + +printf("Demo concluded successfully \r\n"); + + + } + + void tickerIRQ (void) +{ + eventQueue.call(read_datchik_write_lcd_consol_publish_mqtt); +} + + + +int main(int argc, char *argv[]) + { + // pc.baud(115200); + + lpTicker.attach(tickerIRQ, 5); // every 5 second + while(1) + { + eventQueue.dispatch(0); + sleep(); + } + + + + + return 0; } \ No newline at end of file