Unexpected String when using BlueMix's Node-RED editor and MQTT->Debug Node - node.js

I'm following this tutorial,
http://energia.nu/creating-an-iot-connected-sensor-with-energia-mqtt/
I see the pushed data, but the Node-RED editor constantly prints 'Hello World #XX'. I don't see anything in the code that would suggest where its coming from:
#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h> //only required if using an MCU LaunchPad + CC3100 BoosterPack. Not needed for CC3200 LaunchPad
WiFiClient wclient;
byte server[] = { 198, 41, 30, 241 }; // Public MQTT Brokers: http://mqtt.org/wiki/doku.php/public_brokers
byte ip[] = { 172, 16, 0, 100 };
char sensorRead[4];
#define WIFI_SSID "SSID"
#define WIFI_PWD "WIFIPASSWORD"
PubSubClient client(server, 1883, callback, wclient);
void callback(char* inTopic, byte* payload, unsigned int length){
// Handle callback here
}
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(115200);
Serial.println("Start WiFi");
WiFi.begin(WIFI_SSID, WIFI_PWD);
while(WiFi.localIP() == INADDR_NONE) {
Serial.print(".");
delay(300);
}
Serial.println("");
printWifiStatus();
}
void loop()
{
// read the input on analog pin:
int sensorValue = analogRead(24);
Serial.println(sensorValue);
// convert into to char array
String str = (String)sensorValue;
int str_len = str.length() + 1; // Length (with one extra character for the null terminator)
char char_array[str_len]; // Prepare the character array (the buffer)
str.toCharArray(char_array, str_len); // Copy it over
// publish data to MQTT broker
if (client.connect("LaunchPadClient")) {
client.publish("outTopic", char_array);
//client.subscribe("inTopic");
Serial.println("Publishing successful!");
client.disconnect();
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
Is this because this is a free/trial account? Incidentally, it shows I'm using 512MB/2GB, which seems high... does it include the data sent, or is 512MB just the application size?

You are using a MQTT broker that is public to the world, anybody can publish data to any topic on that broker. The messages are probably coming from somebody else doing similar experiments to yourself.
outTopic is the sort of topic name that many people could be using to test, try changing it to some random string in both the publishing code and the MQTT In node in Node-RED.
As for the size in Bluemix, this is how much memory is assigned to your application, it is unlikely to be actually using anything near that amount at the moment.

Related

sensor esp8266 with arduino wont loop anymore

Do forgive me as i am new to programming.
I used a sample code I found online for dht22 and have added other sensors in my esp8266. However, when i added the code necessary for the latest sensor (waterproof temp sensor), it doesn't activate the loop. the serial monitor prints out one sensor reading but it stops there
12:03:50.208 -> WiFi connected - ESP IP address: ****
12:03:50.208 -> Attempting MQTT connection...connected
12:03:55.499 -> { "temperature": " 23.72", "humidity" : " 56.70", "soilmoisture" : "99", "soiltemperature" : " 22.00"}
I looked around and deleted the code for the temp sensor one by one and i have discovered that when i changed the
char data[80] to char data[100]
the code starts working again but now it is looping every milisecond or something instead of 10s that i have assigned
// Loading the ESP8266WiFi library and the PubSubClient library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <OneWire.h>
#include <DallasTemperature.h>
// Uncomment one of the lines bellow for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//For capacitive soil sensor
const int AirValue = 865; //you need to replace this value with Value_1
const int WaterValue = 590; //you need to replace this value with Value_2
int soilMoistureValue = 0;
int soilmoisturepercent= 0;
// GPIO where the DS18B20 (soil temperature) is connected to
const int oneWireBus = D3;
// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "****";
const char* password = "****";
// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "****";
// Initializes the espClient
WiFiClient espClient;
PubSubClient client(espClient);
// DHT Sensor
const int DHTPin = 5;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;
char data[100];
// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
}
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
/*
YOU NEED TO CHANGE THIS NEXT LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
To change the ESP device ID, you will have to give a unique name to the ESP8266.
Here's how it looks like now:
if (client.connect("ESP8266Client")) {
If you want more devices connected to the MQTT broker, you can do it like this:
if (client.connect("ESPOffice")) {
Then, for the other ESP:
if (client.connect("ESPGarage")) {
That should solve your MQTT multiple connections problem
THE SECTION IN loop() function should match your device name
*/
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// The setup function sets your DHT sensor, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
dht.begin();
// Start the DS18B20 sensor
sensors.begin();
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
// For this project, you don't need to change anything in the loop function.
// Basically it ensures that you ESP is connected to your broker
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop())
/*
YOU NEED TO CHANGE THIS NEXT LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
To change the ESP device ID, you will have to give a unique name to the ESP8266.
Here's how it looks like now:
client.connect("ESP8266Client");
If you want more devices connected to the MQTT broker, you can do it like this:
client.connect("ESPOffice");
Then, for the other ESP:
client.connect("ESPGarage");
That should solve your MQTT multiple connections problem
THE SECTION IN recionnect() function should match your device name
*/
client.connect("ESP8266Client");
now = millis();
// Publishes new temperature and humidity every 30 seconds
if (now - lastMeasure > 10000)
{
lastMeasure = now;
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
//put Sensor insert into soil
float sensorValue = analogRead(A0);
soilmoisturepercent = map(sensorValue, AirValue, WaterValue, 0, 100);
float s = soilmoisturepercent;
//sensor soil temperature code
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
//float temperatureF = sensors.getTempFByIndex(0);
// Check if any reads failed and exit early (to try again).
//
if (isnan(h) || isnan(t) || isnan(f) || isnan(soilmoisturepercent) || isnan(temperatureC))
{
Serial.println("Failed to read from sensors!");
return;
}
// Computes temperature values in Celsius
float hic = dht.computeHeatIndex(t, h, false);
static char temperatureTemp[7];
dtostrf(hic, 6, 2, temperatureTemp);
static char humidityTemp[7];
dtostrf(h, 6, 2, humidityTemp);
static char soilmoist[7];
dtostrf(soilmoisturepercent, 6, 2, soilmoist);
static char soiltemp[7];
dtostrf(temperatureC, 6, 2, soiltemp);
String dhtReadings = "{ \"temperature\": \"" + String(temperatureTemp) + "\", \"humidity\" : \"" + String(humidityTemp) + "\", \"soilmoisture\" : \"" + String(soilmoisturepercent) + "\", \"soiltemperature\" : \"" + String(soiltemp) + "\"}";
dhtReadings.toCharArray(data, (dhtReadings.length() + 1));
client.publish("room/temperature", temperatureTemp);
client.publish("room/humidity", humidityTemp);
client.publish("room/soilmoisture", soilmoist);
client.publish("room/soiltemperature", soiltemp);
// Publishes Temperature and Humidity values
client.publish("/esp8266/dhtreadings", data);
Serial.println(data);
}
}

How to send float values from one bluetooth module to other(HC 05)

I am doing a project where I need to send data from ultrasonic sensor wirelessly present in one arduino to other arduino where I need these values in Serial monitor. But the problem is I cannot able to send these values through bluetooth. I tried to send one character, it is appearing in serial monitor.. But when I tried to the same for integer values it is not appearing in serial monitor.
I have configured Master and Slave modes for the Bluetooth. I have uploaded the image of the code which I am using to send these values. Please help me on this. Thanks in advance .
code
//# transmitting end
#define trigPin 12
#define echoPin 11
void setup() {
Serial.begin(38400); // Default communication rate of the Bluetooth module
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration;
float distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.println(distance,2); // Sends floatValue
delay(500);
}
//# receving end
#include <SoftwareSerial.h>
#define led 13
SoftwareSerial BTSerial(10, 11);
int data=0;
void setup() {
pinMode(led,OUTPUT);
Serial.begin(38400);
BTSerial.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
int number;
if(Serial.available() > 0){ // Checks data is from the serial port
data = BTSerial.read(); // Reads the data from the serial port
//analogWrite(led,data);
delay(10);
//Serial.println(data);
}
Serial.println(data);
}
I need integer values at the serial monitor. But there I am getting some symbols like ?/<>..
From the Arduino reference, Serial.read() only reads the first available byte available in the Serial buffer. As an int is coded on 8 bytes, I would say that you need to read the incoming bytes sequentially in order to get the full value.
Maybe you can implement this by putting (Serial.available() > 0) in a while loop, concatenate the values you get in a char[8] for instance and then convert this char to a integer value.
Also, beware that you are sending floats and not int.
Thanks for the help..!
I modified the code in the receiver end to get the float values from the transmitter.. Here is my modified code
#include <SoftwareSerial.h>
int bluetoothTx = 10;
int bluetoothRx = 11;
String content; //content buffer to concatenate characters
char character; //To store single character
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup(){
bluetooth.begin(38400);
Serial.begin(9600);
}
void loop(){
bluetooth();
}
void bluetooth(){ //
while(bluetooth.available()){
character = bluetooth.read();
content.concat(character);
if(character == '\r'){ // find if there is carriage return
Serial.print(content); //display content (Use baud rate 9600)
content = ""; //clear buffer
Serial.println();
}
}
}

ESP8266 / Arduino modbus RTU Buffer data conversion

I am using ESP8266 and ModbusMaster.h library to communicate with RS485 enabled power meter. Communication works fine but responses are the ones are confusing me and I can not get correct values. My power meter shows 1.49 kWh but response from Modbus is 16318. Here is my code:
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ModbusMaster.h>
#include <ESP8266WiFi.h>
/*
Debug. Change to 0 when you are finished debugging.
*/
const int debug = 1;
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
int timerTask1, timerTask2, timerTask3;
float battBhargeCurrent, bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
float stats_today_pv_volt_min, stats_today_pv_volt_max;
uint8_t result;
// this is to check if we can write since rs485 is half duplex
bool rs485DataReceived = true;
float data[100];
ModbusMaster node;
SimpleTimer timer;
// tracer requires no handshaking
void preTransmission() {}
void postTransmission() {}
// a list of the regisities to query in order
typedef void (*RegistryList[])();
RegistryList Registries = {
AddressRegistry_0001 // samo potrosnju
};
// keep log of where we are
uint8_t currentRegistryNumber = 0;
// function to switch to next registry
void nextRegistryNumber() {
currentRegistryNumber = (currentRegistryNumber + 1) % ARRAY_SIZE( Registries);
}
void setup()
{
// Serial.begin(115200);
Serial.begin(9600, SERIAL_8E1); //, SERIAL_8E1
// Modbus slave ID 1
node.begin(1, Serial);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
// WiFi.mode(WIFI_STA);
while (Blynk.connect() == false) {}
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
timerTask1 = timer.setInterval(9000, updateBlynk);
timerTask2 = timer.setInterval(9000, doRegistryNumber);
timerTask3 = timer.setInterval(9000, nextRegistryNumber);
}
// --------------------------------------------------------------------------------
void doRegistryNumber() {
Registries[currentRegistryNumber]();
}
void AddressRegistry_0001() {
uint8_t j;
uint16_t dataval[2];
result = node.readHoldingRegisters(0x00, 2);
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 2; j++) // set to 0,1 for two
datablocks
{
dataval[j] = node.getResponseBuffer(j);
}
terminal.println("---------- Show power---------");
terminal.println("kWh: ");
terminal.println(dataval[0]);
terminal.println("crc: ");
terminal.println(dataval[1]);
terminal.println("-----------------------");
terminal.flush();
node.clearResponseBuffer();
node.clearTransmitBuffer();
} else {
rs485DataReceived = false;
}
}
void loop()
{
Blynk.run();
// ArduinoOTA.handle();
timer.run();
}
I have tried similar thing but with Raspberry Pi and USB-RS485 and it works.
Sample of NodeJS code is below. It looks similar to Arduino code.
// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
// open connection to a serial port
client.connectRTUBuffered("/dev/ttyUSB0", { baudRate: 9600, parity: 'even' }, read);
function write() {
client.setID(1);
// write the values 0, 0xffff to registers starting at address 5
// on device number 1.
client.writeRegisters(5, [0 , 0xffff])
.then(read);
}
function read() {
// read the 2 registers starting at address 5
// on device number 1.
console.log("Ocitavanje registra 0000: ");
client.readHoldingRegisters(0000, 12)
.then(function(d) {
var floatA = d.buffer.readFloatBE(0);
// var floatB = d.buffer.readFloatBE(4);
// var floatC = d.buffer.readFloatBE(8);
// console.log("Receive:", floatA, floatB, floatC); })
console.log("Potrosnja u kWh: ", floatA); })
.catch(function(e) {
console.log(e.message); })
.then(close);
}
function close() {
client.close();
}
This code displays 1.493748298302 in console.
How can I implement this var floatA = d.buffer.readFloatBE(0); in Arduino? Looks like that readFloatBE(0) does the trick, but available only in NodeJS / javascript.
Here i part of datasheet for my device
Here is what I am getting as result from original software that came with device:
If someone could point me in better direction I would be thenkfull.
UPDATE:
I found ShortBus Modbus Scanner software and tested readings.
Library read result as Unsigned integer, but need Floating Point and Word Order swapped. It is shown on image below.
Can someone tell how to set proper conversion please.
Right, so indeed the issue is with the part done by var floatA = d.buffer.readFloatBE(0);Modbus returns an array of bytes, and the client has to interpret those bytes, ideally done by the library you're using, but if not available on Arduino, you may try manually with byte decoding functions, with the following considerattions:
Modbus registers are 16 bit in length, so length 1 = 16 bits, length
2 = 32 bits, hence the data type noted on the docs as float32 means
"2 registers used for this value, interpret as float".
Therefore, on client.readHoldingRegisters(0000, 12)you're asking to read the register with address 00, and size 12... so this makes no sense, you only need 2 registers.
On your sample Node code, first you're writing
2 registers to address 5 in client.writeRegisters(5, [0 , 0xffff])
register 5 = 0, and register 6 = 0xFFFF, why? Then you go and read
from address 0, in read(), which is the address for Total KwH per
your docs.
So, you should get an array of bytes, and you need to
decode them as a float. Modbus is Big Endian for words and bytes, so
you need to use those in the decoding functions. I don't know exactly
what is available in Arduino, but hopefully you can figure it out
with this extra info.
I suppose that if you just send the buffer to print, you'll get an integer interpretation of the value, hence the problem

How to store string received with Serial.read() in Arduino?

I manage to send data from NodeJs runtime with serialport library.
The goal is storing a string received from Serial.read() in Arduino . What is correct:
int string = Serial.read()
or:
char string[20] = Serial.read()
There are many documentations on Internet and they are not the same
P/s: I send string from nodejs in buffer form but i dont think it matters because arduino still implement it as a string anyway.
From the Arduino documentation:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
https://www.arduino.cc/en/Serial/Read
.read():
Returns the first byte of incoming serial data available (or -1 if no
data is available) - int
FYI: I've used this code many times and it works on 100%.

SIM800L string concatenation

I have this code from a website that I'm using as a guide to send SMS message from a SIM800L connected to my Arduino Mega.
#include <Sim800l.h>
#include <SoftwareSerial.h>
Sim800l Sim800l; //declare the library
char* text;
char* number;
bool error;
void setup(){
Sim800l.begin();
text="Testing Sms";
number="+542926556644";
error=Sim800l.sendSms(number,text);
// OR
//error=Sim800l.sendSms("+540111111111","the text go here");
}
void loop(){
//do nothing
}
I added some bits of code in the middle so that it will receive a string input from a user in my Python GUI via serial connection.
#include <Sim800l.h>
#include <SoftwareSerial.h>
Sim800l Sim800l; //declare the library
char* text;
char* number;
bool error;
String data;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available() > 0)
{
data = Serial.readString();
Serial.print(data);
sendmess();
}
}
void sendmess()
{
Sim800l.begin();
text="Power Outage occured in area of account #: ";
number="+639164384650";
error=Sim800l.sendSms(number,text);
// OR
//error=Sim800l.sendSms("+540111111111","the text go here");
}
I am trying to concatenate the data from my serial.readString() to the end of the text. Conventional methods like the + and %s don't work.
In Arduino IDE I'm getting this error:
error: cannot convert ‘StringSumHelper’ to ‘char*’ in assignment
If I'm correct char* is a pointer that points to an address. Is there anyway to add the string from the serial monitor to the text?
You have to convert the Arduino String object to a standard C string. You can do this by using the c_str() method of the String class. It will return a char* pointer.
Now you can concatenate the two string with the strncat function from C library, string.h and also using strncpy as well.
#include <string.h>
char message[160]; // max size of an SMS
char* text = "Power Outage occured in area of account #: ";
String data;
/*
* populate <String data> with data from serial port
*/
/* Copy <text> to message buffer */
strncpy(message, text, strlen(text));
/* Calculating remaining space in the message buffer */
int num = sizeof(message) - strlen(message) - 1;
/* Concatenate the data from serial port */
strncat(message, data.c_str(), num);
/* ... */
error=Sim800l.sendSms(number, message);
Note it will simply chop off the remaining data if there is not enough space in the buffer.

Resources