Publishing a value through PubSubClient - arduino

I tried the following code to publish a value using Arduino onto Mosquitto broker. The code appears to be right but then as soon as I compile/verify, the following error is thrown. What's the cause and how do I overcome this problem?
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#define CLIENTID "ArduinoSensor"
#define TOPICNAME "sensor/temperature"
#define POLLINTERVAL 120000
void callback(char topic, byte payload, unsigned int length){
//Do nothing as we are publishing ONLY.
}
byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} ;
byte server [] = { 85, 119, 83, 194 };
EthernetClient ethClient;
PubSubClient arduinoClient(server, 1883, callback, ethClient) ;
unsigned long boardTime = 0 ;
float sensedTemperature = 0;
char charTemperature [20];
void setup(void) {
Serial.begin(9600);
//Connect to the MQTT server - test.mosquitto.org
beginConnection() ;
}
//Initialise MQTT connection
void beginConnection() {
Serial.begin(9600);
Ethernet.begin(mac) ; //using the address assigned through DHCP
int connRC = arduinoClient.connect(CLIENTID) ;
if (!connRC) {
Serial.println(connRC) ;
Serial.println("Could not connect to MQTT Server");
Serial.println("Please reset the arduino to try again");
delay(100);
exit(-1);
}
else {
Serial.println("Connected to MQTT Server...");
}
}
void loop(void) {
boardTime = millis();
if ((boardTime % POLLINTERVAL) == 0) {
getTemp();
dtostrf(sensedTemperature,5,2,charTemperature) ;
arduinoClient.publish(TOPICNAME, charTemperature) ;
}
}
void getTemp() {
// Send the command to get temperatures
delay(100);
sensedTemperature = analogRead(2); //temperature sensor at analog pin 2 on Arduino.
delay(150);
}
Error Printed:
publisher:18: error: invalid conversion from 'void (*)(char, byte, unsigned int) {aka void (*)(char, unsigned char, unsigned int)}' to 'void (*)(char*, uint8_t*, unsigned int) {aka void (*)(char*, unsigned char*, unsigned int)}' [-fpermissive]
PubSubClient arduinoClient(server, 1883, callback, ethClient) ;
C:\Users\Chetan\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:98:4: error: initializing argument 3 of 'PubSubClient::PubSubClient(uint8_t*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&)' [-fpermissive]
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
^
exit status 1
invalid conversion from 'void (*)(char, byte, unsigned int) {aka void (*)(char, unsigned char, unsigned int)}' to 'void (*)(char*, uint8_t*, unsigned int) {aka void (*)(char*, unsigned char*, unsigned int)}' [-fpermissive]
In file included from C:\Users\Chetan\Desktop\publisher\publisher.ino:4:0:
Any suggestions?

The error message is telling you that the signature of the callback function you are passing to the constructor is incorrect.
You have:
void callback(char topic, byte payload, unsigned int length){
It should be:
void callback(char* topic, byte* payload, unsigned int length) {
Note the * for the topic and payload arguments.

Related

Sigfox parse error: Why can it not send my message?

I'm testing the Sigfox communication with a Breakout board BRKWS01 and a Nucleo-32 L432KC. I work with Arduino IDE. I can send a message of 12 bytes. I used 12 "char" to save my data but it did not work. Then I test with 3 "int" but I always receive the error message: "Error: parse error", and I don't know why.
This is the structure of my message:
typedef struct {
unsigned int temperature;
unsigned int latitude;
unsigned int longitude;
} Payload;
Here is my code:
#include "IO_WSSFM10.h"
IO_WSSFM10 mySigfox(10, 11, true);
typedef struct {
unsigned int temperature;
unsigned int latitude;
unsigned int longitude;
} Payload;
unsigned int counter=0;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
mySigfox.begin();
delay(50);
mySigfox.test();
mySigfox.getID();
mySigfox.getPAC();
mySigfox.getTemp();
//mySigfox.setPowerMode(1);
}
// the loop function runs over and over again forever
void loop() {
String sfResponse;
Payload p;
p.temperature = 1075847679;
p.latitude = 441300000;
p.longitude = 41000000;
bool statusS = mySigfox.send(&p, sizeof(p));
if (statusS) counter++;
delay(10000);
/*
bool statusSR = mySigfox.sendReceive(&p, sizeof(p), sfResponse);
if (statusS) {
for (uint8_t i= 0; i<26; ++i) {//RX= 01 02 03 04 05 06 07 08
Serial.println(sfResponse[i]);
counter++;
}
}*/
}
Could you help me please?

Expected primary-expression before 'const'

I am trying to send one UDP packet but get stuck on concatenate between different data type and don't know how to solve it.
The code:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
const char* UdpIPaddress = "192.168.0.240";
unsigned int UdpPort = 4210;
const char* Udpmessage = "Message";
int a=0;
float Temp=29.25;
void setup() {
Serial.begin (115200);
}
void loop() {
if(a==0){
a = a + 1;
const char* sendmessage = const char*(a) + "=" + const char*(Temp) + "=" + Udpmessage;
UdpSend(sendmessage, UdpIPaddress, UdpPort);
}
}
void UdpSend(const char* message, const char * ipaddress, int port){
Serial.println("Send message: ");
Udp.beginPacket(ipaddress, port);
Udp.write(message);
Udp.endPacket();
}
Arduino IDE compiler error: expected primary-expression before 'const'
Please help...
Found the answer of my own question:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
const char* UdpIPaddress = "192.168.0.240";
unsigned int UdpPort = 4210;
char buf[40];
const char* Udpmessage = "Message";
int a=0;
float Temp=29.25;
void setup() {
Serial.begin (115200);
}
void loop() {
if(a==0){
a = a + 1;
sprintf(buf, "%d=%f=%s", a,Temp, Udpmessage);
UdpSend(buf, UdpIPaddress, UdpPort);
}
}
void UdpSend(const char* message, const char * ipaddress, int port){
Serial.println("Send message: ");
Udp.beginPacket(ipaddress, port);
Udp.write(message);
Udp.endPacket();
}
Works perfectly :) :) :)

Simulating RSSI with Cheap RF Modules

My goal is to essentially spoof RSSI (Received Signal Strength Indicator) using a system of counting received packets. The idea is to have something where:
A specific number of packets is sent in a specific time from the transmitter.
Then are received at another unit and the number of packets received is counted.
The number in the counter of the receiver indicates the number of packets received at that time specific in the transmitter.
The fewer packages (counter value) that are received, the farther the sender will be.
I'm having a little trouble implementing the logic in my code however so I'd really appreciate the help. I am using Arduino Pro Mini 5V with NRF24L01+ radios and the RF24 Network library. My code is as follows:
Transmitter:
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <Wire.h>
RF24 radio(8,9);
RF24Network network(radio);
const uint16_t home_node = 00;
const uint16_t distant_node = 01;
struct payload_t { // Structure of our payload
byte ID;
};
void setup(void) {
Serial.begin(115200);
SPI.begin();
radio.begin();
network.begin(/*channel*/ 92, /*node address*/ distant_node);
}
void loop(void) {
byte ID = 1;
for (int i = 0; i < 50; i++)
{
payload_t payload = {ID};
RF24NetworkHeader header(/*to node*/ home_node);
bool ok = network.write(header,&payload,sizeof(payload));
if (ok)
Serial.println("ok.");
else
Serial.println("failed.");
delay (300);
}
delay(15000);
}
Receiver:
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <Wire.h>
RF24 radio(8,9);
RF24Network network(radio);
const uint16_t home_node = 00;
const uint16_t distant_node = 01;
struct payload_t {
byte ID;
};
//const unsigned long interval = 3000;
//unsigned long last_sent;
int count = 0;
void setup(void)
{
Serial.begin(115200);
SPI.begin();
radio.begin();
network.begin(/*channel*/ 92, /*node address*/ home_node);
}
void loop(void)
{
RF24NetworkHeader header;
payload_t payload;
network.update();
while ( network.available() ) { // Is there anything ready for us?
bool ok = network.read(header, &payload, sizeof(payload));
if (ok) // Non-blocking
{
count++;
Serial.println ("count=");
Serial.println (count);
}
else
Serial.println ("Failed");
}
}

AVR USART transmitting only 2 chars

Below is a code ran on an ATmega328P. It's supposed to send "abcdef" to my computer every second. However, it sent me only "ab" every second. What is wrong here?
#include <avr/io.h>
#include <util/delay.h>
void USART_transmit(unsigned char data);
void print(const unsigned char *buffer, size_t n);
void print(const char* str);
int main(void) {
// Serial.begin(115200)
UCSR0B |= (1<<TXEN0);
UBRR0L = 8;
while(1){
// Serial.write(i)
print("abcdef");
_delay_ms(1000);
}
}
void USART_transmit(const uint8_t data) {
/* wait for empty transmit buffer */
while (!UCSR0A & (1<<UDRE0));
UDR0 = data;
}
void print(const uint8_t *buffer, size_t n) {
while(n--){
USART_transmit(*buffer++); //**
}
}
void print(const char *str) {
if(strlen(str) != 0) print((const uint8_t *) str, strlen(str));
}
The code resulted in:
ababababababababababab...
Changing from USART_transmit(*buffer++); to USART_transmit(n + 48); (+48 to convert to char) resulted in:
5454545454545454545454545454...
So I guess the loop shouldn't be stopping?
The "data register empty" check is wrong.
while (!UCSR0A & (1<<UDRE0));
should be
while (!(UCSR0A & (1 << UDRE0)));
In your case, the check is not blocking until the buffer is empty. I think one byte is buffered in the USART output buffer and one byte is pending in UDR. Every additional byte is then discarded, this is why you see only "ab".

Publish onto Mosquitto broker running on laptop

I have followed the steps outlined in the following link to install and run the Mosquitto broker on my laptop.
https://sivatechworld.wordpress.com/2015/06/11/step-by-step-installing-and-configuring-mosquitto-with-windows-7/
I have tested the publishing and subscription events using command line i.e, the mosquitto_pub and mosquitto_sub commands and they worked fine!
I want to publish to this broker running on my laptop (listening to port 1883 - confirmed using the netstat-an command) using an Arduino which is using the functions from "PubSubClient" library.
For the server's IP address in the Arduino Publish sketch, I have given the IP address of my laptop itself which it gets when it connects to my home network. Following is the publisher code:
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#define CLIENTID "Arsen"
const char topic[] = "tem";
#define POLLINTERVAL 120000
void callback(char* topic, byte* payload, unsigned int length){
//Do nothing as we are publishing ONLY.
}
byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} ;
IPAddress server(192, 168, 0, 10);
EthernetClient ethClient;
PubSubClient arduinoClient(server,1883, callback, ethClient) ;
unsigned long boardTime = 0 ;
float sensedTemperature = 0;
char charTemp [6];
int connRC = 0;
void setup(void) {
Serial.begin(9600);
//Connect to the MQTT server - test.mosquitto.org
beginConnection() ;
}
//Initialise MQTT connection
void beginConnection() {
Serial.begin(9600);
Ethernet.begin(mac) ; //using the address assigned through DHCP
do{
int connRC = arduinoClient.connect(CLIENTID);
}while(connRC==1);
Serial.println("We are connected finally");
delay(5000);
Serial.println(arduinoClient.state());
}
void loop(void) {
boardTime = millis();
if ((boardTime % POLLINTERVAL) == 0) {
getTemp();
dtostrf(sensedTemperature,5,2,charTemp);
boolean rec = arduinoClient.publish(topic, charTemp);
Serial.println(rec);
Serial.println("Successfully published");
}
arduinoClient.loop();
}
void getTemp() {
// Send the command to get temperatures
delay(100);
sensedTemperature = analogRead(2); //temperature sensor at analog pin 2 on Arduino.
Serial.println(sensedTemperature);
delay(150);
}
The subscriber code is:
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EthernetServer.h>
#include <EthernetClient.h>
#include <Dns.h>
#include <Dhcp.h>
#include <SPI.h>
#include <PubSubClient.h>
#define PIN 13
//MQTT Definition
#define CLIENTID "Aract"
#define TOPICNAME "tem"
byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} ;
IPAddress server(192,168,0,5); // Mosquitto Server
int connRC = 0;
//Handle message from mosquitto and set LEDs
void callback(char* topic, byte* payload, unsigned int length) {
int i=0; char buffer [length];
for(i=0;i<length;i++) {
buffer [i] = char((payload[i]));
}
String payLoadData = buffer;
Serial.println(payLoadData);
setled(payLoadData);
}
//Our MQTT client
EthernetClient ethclient;
PubSubClient arduinoClient(server, 1883, callback, ethclient) ;
void setup() {
//Connect to the MQTT server - test.mosquitto.org
beginConnection() ;
}
//Initialise MQTT connection
void beginConnection() {
Serial.begin(9600);
Ethernet.begin(mac) ;
do{
connRC = arduinoClient.connect(CLIENTID) ;
delay(2000);
}while(connRC==1);
delay(3000);
Serial.println(arduinoClient.state());
if (connRC==1) {
boolean rec = arduinoClient.subscribe(TOPICNAME) ;
Serial.println(rec);
Serial.println("Successfully Subscribed");
} else {
Serial.println("connRC") ;
}
}
void loop()
{
arduinoClient.loop() ;
}
void setled(String s)
{
int temperature;
temperature = s.toInt();
Serial.println(temperature);
if (temperature >= 200){
digitalWrite(PIN, HIGH);
}
else{
digitalWrite(PIN, LOW);
}
}
The Ethernet shield is working well as it successfully executed the example programs. The state function returns "zero" which implies that the board is connecting to the network. However, the publish command returns a boolean value "rec" which is giving false. I want to know as to why this process is being unsuccessful all the time?
The verbose option (-v) along with Mosquitto on command line throws up an unknown error instead of showing the logged details. How to overcome this problem or what exactly is wrong in my technique?
I had faced the same problem and I got the solution as this...
Just go to the services running in your PC and stop the mosquitto-broker, now open the cmd and change the directory to the place where mosuitto files were located (in my case it is "cd C:\Program Files (x86)\mosquitto") and then enter this command mosquitto -v, now just left that window like that (If u closed then it won't work)...
It really worked and the reason why its not working in before case is, when we started moquitto-broker service then its not opening ports for IPv4 and here is the reference "Mosquitto Error"

Resources