Hey everyone
I am a newbie working with the Particle and have managed to publish a series of messages to mosquito but actually need the values of the sensor not just a message …some help would be greatly appreciated
the code
#include "MQTT/MQTT.h"
#include "math.h"
const int ledPin1 = D1 ;
const int ledPin2 = D2 ;
const int analogInPin1 = A0;
const int analogInPin2 = A1;
#define ThermisterPin 2
char message_buff[100];
char* tempC;
double Thermistor(int RawADC) {
double Temp;
Temp = log(12000.0 * ((1024.0 / RawADC - 1)));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
Temp = Temp -273.15;//Convert Kelvin to Celcius
//Temp = (Temp * 9.0) / 5.0 + 32.0;//Convert Celcius to Fahrenheit
return Temp;
}
//*******************//
//*******************//
int sensorValue = 0;
int outputValue = 0;
int ledState = LOW;
int ledState2 = LOW;
void callback(char* topic, byte* payload, unsigned int length);
// if want to use IP address,
byte server[] = { 192,168, 1,108 };
MQTT client(server, 1883, callback);
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message.equals("RED"))
RGB.color(255, 0, 0);
else if (message.equals("GREEN"))
RGB.color(0, 255, 0);
else if (message.equals("BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}
//**********************//
//*********************//
void setup() {
RGB.control(true);
Serial.begin(9600);
// connect to the server
client.connect("sparkclient");
// publish/subscribe
if (client.isConnected()) {
client.publish("outTopic/message","hello world");
client.subscribe("inTopic/message");
}
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
Serial.println(int(Thermistor(analogRead(0))));//display Fahrenheit
delay(1000);
float t1 = analogRead(analogInPin1);
sensorValue = analogRead(analogInPin1);
outputValue = map(sensorValue, 0, 5000, 0, 255);
Serial.println(sensorValue);
if (sensorValue < 3000) {
digitalWrite (ledPin2, HIGH);
digitalWrite (ledPin1, LOW);
client.publish("mymosquitto/Particle", "Larry Happy");
delay(3000);
}
else if (sensorValue > 3000) {
digitalWrite (ledPin1, HIGH);
digitalWrite (ledPin2, LOW);
client.publish("mymosquitto/Particle", "Larry needs Water");
delay(3000);
}
if (client.isConnected())
Serial.println("connected");
client.loop();
}