Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This script is intended to explain how to send data using a Finite State Machine, FSM, to Ubidots
/****************************************
* Include Libraries
****************************************/
#include "UbidotsESPMQTT.h"
/****************************************
* Define Constants
****************************************/
#define TOKEN "...." // Your Ubidots TOKEN
#define WIFINAME "...." //Your SSID
#define WIFIPASS "...." // Your Wifi Pass
#define WAIT 0
#define READ_SEND 1
uint8_t fsm_state = WAIT; // Initial state
int msCount = 0; // time counter
float value; // memory space for the value to be read
Ubidots client(TOKEN);
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
/****************************************
* Main Functions
****************************************/
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(A0, INPUT);
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
}
// the loop function runs over and over again forever
void loop() {
switch(fsm_state) {
case WAIT:
if (msCount == 1000){
msCount = 0;
fsm_state = READ_SEND;
}
break;
case READ_SEND:
value = analogRead(A0);
if(!client.connected()){
client.reconnect();
}
/* Routine for sending data */
client.add("stuff", value);
client.ubidotsPublish("source1");
client.loop();
fsm_state = WAIT;
break;
default:
break;
}
// Increments the counter
msCount += 1;
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.