Difference between revisions of "WeMOS: MQTT send A0"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 58: | Line 58: | ||
void loop() { | void loop() { | ||
− | int sensorReading = analogRead(A0); | + | int sensorReading = analogRead(A0); |
− | client.publish("test", sensorReading); | + | char sensorChar[5]; |
+ | client.publish("test", itoa(sensorReading, sensorChar, 10) ); | ||
delay(1000); | delay(1000); | ||
} | } |
Latest revision as of 11:18, 14 June 2022
Code
#include <ESP8266WiFi.h> #include <PubSubClient.h> const char* ssid = "Kampus ITTS"; const char* password = "itts2020"; const char* mqttServer = "192.168.17.25"; const int mqttPort = 1883; const char* mqttUser = ""; const char* mqttPassword = ""; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Connecting to WiFi.."); } Serial.println("Connected to the WiFi network"); client.setServer(mqttServer, mqttPort); client.setCallback(callback); while (!client.connected()) { Serial.println("Connecting to MQTT..."); // if (client.connect("ESP8266Client", mqttUser, mqttPassword )) { if (client.connect("ESP8266Client" )) { Serial.println("connected"); } else { Serial.print("failed with state "); Serial.print(client.state()); delay(2000); } } client.publish("test", "Hello from ESP8266"); client.subscribe("test"); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived in topic: "); Serial.println(topic); Serial.print("Message:"); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); Serial.println("-----------------------"); } void loop() { int sensorReading = analogRead(A0); char sensorChar[5]; client.publish("test", itoa(sensorReading, sensorChar, 10) ); delay(1000); }