publish sensors data from Arduino uno to Mosquitto broker via usb port

Hi,

I am a beginner in Mosquitto broker. Actually, I want to get my sensors data from arduino and send them to mySQL database via MQTT protocol. So I installed Mosquitto broker in my laptop and I have succeded to subscribe and publish data via command prompt. Now, I want to get these data from my arduino sensors and publish them to Mosquitto broker via USB port (I haven’t Ethernet shield).

can I use this code to publish data? because I testes it and I didn’t get anything as result while executing the command prompt mosquitto>mosquitto_sub -h 127.0.0.1 -t arduino.

#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>

// Pins
const int photocellPin = 1; // the cell and 10K pulldown are connected to a1
// Variables
char* lightC;
unsigned long time;
char message_buffer[100];

// Network Settings
// MAC address of ethernet shield
// Look for it on a sticket at the bottom of the shield.
// Old Arduino Ethernet Shields or clones may not have a dedicated MAC address. Set any hex values here.
byte MAC_ADDRESS = { 0xFE, 0xED, 0xDE, 0xAD, 0xBE, 0xED };

// IP address of MQTT server
byte MQTT_SERVER = { 127, 0, 0, 1 };// 10, 192, 191, 77 { 192, 168, 1, 115 };
// Handles messages arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
}
EthernetClient ethClient;
PubSubClient client(MQTT_SERVER, 1883, callback, ethClient);

void setup()
{
// Initilize serial link for debugging
Serial.begin(9600);

if (Ethernet.begin(MAC_ADDRESS) == 0)
{
Serial.println(“Failed to configure Ethernet using DHCP”);
return;
}
}

void loop()
{
if (!client.connected())
{ client.connect(“sfo-arduino”);
client.publish(“sfo/arduino/alive”, “I’m alive!”);
}

lightC = dtostrf (analogRead(photocellPin),5,2,message_buffer); // TMP36 sensor calibration

Serial.println(lightC);
delay(2000);
// Publish sensor reading every X milliseconds
if (millis() > (time + 60000)) {
time = millis();
client.publish(“arduino”,lightC);
}
// MQTT client loop processing
client.loop();
}

This is the result that I get:

C:\Program Files (x86)\mosquitto>mosquitto -v
1487871736: mosquitto version 1.4.8 (build date 14/02/2016 15:33:31.09) starting

1487871736: Using default config.
1487871736: Opening ipv6 listen socket on port 1883.
1487871736: Opening ipv4 listen socket on port 1883.
1487871774: New connection from 127.0.0.1 on port 1883.
1487871774: New client connected from 127.0.0.1 as mosqsub/11920-PC (c1,
k60).
1487871774: Sending CONNACK to mosqsub/11920-PC (0, 0)
1487871774: Received SUBSCRIBE from mosqsub/11920-PC
1487871774: arduino (QoS 0)
1487871774: mosqsub/11920-PC 0 arduino
1487871774: Sending SUBACK to mosqsub/11920-PC
1487871834: Received PINGREQ from mosqsub/11920-PC

thank you

Since your Arduino's only link to the outside world is via Serial.print() your sketch isnt going to ever work. Did you not wonder how a sketch that starts out initialising an ethernet shield will work without the shield?

You need a simple sketch on the arduino that writes the data to the serial port. You then need some kind of script or program running on your PC that reads that data from the serial port and sends it to MQTT. If you search the internet for "serial port to mqtt" you will find lots of answers- I havent used any of them so cant recommend.