gfvalvo:
I'll take the easy way out and let strtok() do the work:
void someFunction(byte *);
void setup() {
const char dog[] = "A0:433.33";
Serial.begin(115200);
delay(2000);
someFunction((byte *)dog);
}
void loop() {
}
void someFunction(byte *payload) {
char *ptr, tempBuffer[25];
strcpy(tempBuffer, (const char *)payload); // strtok changes the string, so make a local copy
ptr = strtok(tempBuffer, ":");
Serial.print("First Token = ");Serial.println(ptr);
ptr = strtok(NULL, ":");
Serial.print("Second Token = ");Serial.println(ptr);
}
I'm getting weird trailing 0's and S's now. Here is my full code:
/*
This code connects the Wemos D1 Mini Pro to Cloud MQTT
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "ESP8266Ping.h"
#include "config.h"
/* WiFi and PubSub */
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
/* Serial Read Parsing for recvWithStartEndMarkers() */
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
/* Relay Pins */
//int relayPin1 = D0;
//int relayPin2 = D1;
//int relayPin3 = D2;
//int relayPin4 = D3;
//int relayPin5 = D4;
//int relayPin6 = D5;
//int relayPin7 = D6;
//int relayPin8 = D7;
void setup() {
Serial.begin(115200);
// setting pinMode and initial states
// static const uint8_t digitalPins[] = {D0, D1, D2, D3, D4, D5, D6, D7};
// for (int i = 0; i < 8; i++) { //or i <= 4
// pinMode(digitalPins[i], OUTPUT);
// digitalWrite(digitalPins[i], HIGH);
// }
delay(10);
setup_wifi();
client.setServer(MQTT_SERVER, 15272);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
recvWithStartEndMarkers(); //adds characters to receivedChars array
Serial.print("payload (receivedChars) on the Arduino to be published to MQTT: ");
Serial.println(receivedChars);
client.publish("hydro1", receivedChars);
newData = false;
delay(2000);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// pings a server to make sure internet is alive
bool ret = Ping.ping("www.google.com");
Serial.print("Pinging Google result: ");
Serial.println(ret);
delay(2000);
}
void callback(const char* topic, byte* payload, unsigned int length) {
char buf1[10]; // payload before colon
char buf2[10]; // payload after colon
if (strcmp(topic, "hydro1") == 0) {
Serial.print("payload received in callback from MQTT server: ");
Serial.println((const char*) payload);
char *ptr, tempBuffer[25];
strcpy(tempBuffer, (const char *)payload); // strtok changes the string, so make a local copy
ptr = strtok(tempBuffer, ":");
Serial.print("First Token = ");
Serial.println(ptr);
ptr = strtok(NULL, ":");
Serial.print("Second Token = ");
Serial.println(ptr);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ajxfcvag", MQTT_USER, MQTT_PASS)) {
Serial.println("connected");
// ... and resubscribe
client.subscribe("hydro1");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/* Serial Read Parsing */
// adds characters to receivedChars
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
Here is the output:
.
.
Pinging Google result: 1
Attempting MQTT connection...connected
payload (receivedChars) on the Arduino to be published to MQTT: A3:0.00
payload received in callback from MQTT server: A3:0.00.00fcvag
First Token = A3
Second Token = 0.00.00fcvag
payload (receivedChars) on the Arduino to be published to MQTT: A4:0.00
payload received in callback from MQTT server: A4:0.00.00fcvag
First Token = A4
Second Token = 0.00.00fcvag
payload (receivedChars) on the Arduino to be published to MQTT: A5:433.00
payload received in callback from MQTT server: A5:433.00.00vag
First Token = A5
Second Token = 433.00.00vag