Created
September 21, 2017 23:41
-
-
Save jotathebest/d080d026f5eac1ffc976d3dbec5efe4f to your computer and use it in GitHub Desktop.
This script is intended to explain how to send data using a Finite State Machine, FSM, to Ubidots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************** | |
* 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