UbiMakerMaka Hernandez
Published © CC BY-SA

Connect Your Adafruit Feather 32u4 FONA To Ubidots

Display and control your environment with the Feather 32u4 FONA and Ubidots.

BeginnerProtip2 hours865
Connect Your Adafruit Feather 32u4 FONA To Ubidots

Story

Read more

Code

Code snippet #3

Plain text
/********************************
 * Libraries included
 *******************************/
#include <SoftwareSerial.h>
#include <stdlib.h>
#include "Arduino.h"
 
/********************************
 * Constants and objects
 *******************************/
 
#define DEFAULT_BUFFER_SIZE      64

#define FONA_RX  9
#define FONA_TX  8
#define FONA_RST 4

#define SERVER "translate.ubidots.com"
#define PORT "9012"

#define APN  "...."  // The APN of your operator
#define USER ""  // if your apn doesnt have username just leave it ""
#define PASS ""  // if your apn doesnt have password just leave it ""
#define TOKEN "...."  // Replace it with your Ubidots token
#define METHOD "POST"
#define USER_AGENT  "adafruit"
#define VERSION  "1.0"
#define DEVICE_LABEL  "feather-fona"
#define VARIABLE_LABEL  "temperature"


bool _debug = true; 
char buffer[DEFAULT_BUFFER_SIZE];
 
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);

/********************************
 * Main Functions
 *******************************/
void setup() {
    Serial.begin(115200);
    pinMode(A0, INPUT);
    delay(2000);
    while(!setApn(APN, USER, PASS));
}

void loop() {
    char* request = (char *) malloc(sizeof(char) * 300);
    char str_val[15];
    
    float sensor_value = analogRead(A0);

    dtostrf(sensor_value, 4, 2, str_val);
    sprintf(request, "%s/%s|%s|%s|%s=>%s:%s|end", USER_AGENT, VERSION, METHOD, TOKEN, DEVICE_LABEL, VARIABLE_LABEL, str_val);
    //sprintf(request, "%s|%s|%s","adafruit/1.0|POST", TOKEN, "fona=>test:17.00|end");
    sendToUbidots(request);
    free(request);
}

/********************************
 * Auxiliar Functions
 *******************************/
bool sendMessageAndwaitForOK(char* message, uint16_t timeout = 4000) {
    fonaSS.println(message);
    if (strstr(readData(timeout), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error"));
        }
            return false;
        }
    return true;
}

bool setApn(char* apn, char* user, char* pwd) {
    checkFona();
    fonaSS.println(F("AT"));
    if (strstr(readData(2000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT"));
        }
        return false;
    }
    fonaSS.println(F("AT+CREG?"));
    if (strstr(readData(2000), "+CREG:") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT"));
        }
        return false;
    }
    fonaSS.println(F("AT+CSQ"));
    if (strstr(readData(2000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CSQ"));
        }
        return false;
    }
    fonaSS.println(F("AT+CGATT?"));
    if (strstr(readData(10000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CGATT"));
        }
        return false;
    }
    fonaSS.println(F("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""));
    if (strstr(readData(10000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR CONTYPE"));
        }
        return false;
    }
    fonaSS.print(F("AT+SAPBR=3,1,\"APN\",\""));
    fonaSS.print(apn);
    fonaSS.println(F("\""));
    if (strstr(readData(3000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR APN"));
        }
        return false;
    }
    fonaSS.print(F("AT+SAPBR=3,1,\"USER\",\""));
    fonaSS.print(user);
    fonaSS.println(F("\""));
    if (strstr(readData(10000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR USER"));
        }
        return false;
    }
    fonaSS.print(F("AT+SAPBR=3,1,\"PWD\",\""));
    fonaSS.print(pwd);
    fonaSS.println("\"");
    if (strstr(readData(3000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR PASSWORD"));
        }
        return false;
    }
    fonaSS.println(F("AT+SAPBR=1,1"));
    if (strstr(readData(4000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR=1,1 Connection ip"));
        }
        return false;
    }
    fonaSS.println(F("AT+SAPBR=2,1"));
    if (strstr(readData(4000), "+SAPBR:") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR=2,1 no IP to show"));
        }
        return false;
    }
    return true;
}

bool module_begin() {
    pinMode(FONA_RST, OUTPUT);
    digitalWrite(FONA_RST, HIGH);
    delay(500);
    digitalWrite(FONA_RST, LOW);
    delay(500);
    digitalWrite(FONA_RST, HIGH);
    delay(500);
    Serial.println(F("Attempting to open comm with ATs"));
    int16_t timeout = 10000;
    while (timeout > 0) {
        if (sendMessageAndwaitForOK("AT"))
            break;
        delay(500);
        timeout -= 500;
    }
    if (timeout <= 0) {
        Serial.println(F("Timeout: No response to AT... last ditch attempt."));
        sendMessageAndwaitForOK("AT");
        delay(100);
        sendMessageAndwaitForOK("AT");
        delay(100);
        sendMessageAndwaitForOK("AT");
        delay(100);
        return false;
    }
    sendMessageAndwaitForOK("ATE0");
    delay(100);
    return true;
}

bool sendToUbidots(char* request) {
  
    fonaSS.println(F("AT+CIPMUX=0"));
    if (strstr(readData(4000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPMUX"));
        }
        return false;
    }
    fonaSS.print(F("AT+CIPSTART=\"TCP\",\""));
    fonaSS.print(SERVER);
    fonaSS.print(F("\",\""));
    fonaSS.print(PORT);
    fonaSS.println(F("\""));
    if (strstr(readData(4000), "CONNECT OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPSTART"));
        }
        return false;
    }
    fonaSS.print(F("AT+CIPSEND="));
    fonaSS.println(dataLen(request));
    if (strstr(readData(4000), ">") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPSEND"));
        }
        return false;
    }
    fonaSS.write(request);
    if (strstr(readData(4000), "SEND OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error sending variables"));
        }
        return false;
    }
    fonaSS.println(F("AT+CIPCLOSE"));
    if (strstr(readData(4000), "CLOSE OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPCLOSE"));
        }
        return false;
    }
    fonaSS.println(F("AT+CIPSHUT"));
    if (strstr(readData(4000), "SHUT OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPSHUT"));
        }
        return false;
    }
    return true;
}

int dataLen(char* variable) {
  uint8_t dataLen = 0;
  for (int i = 0; i <= 250; i++) {
    if (variable[i] != '\0') {
      dataLen++;
    } else {
      break;
    }
  }
  return dataLen;
}

bool checkFona() {
    fonaSS.begin(4800);
    delay(2000);
    module_begin();
    if (!sendMessageAndwaitForOK("ATE0", 6000)) {
        Serial.print("Couldn't find FONA");
        while (1) {
            Serial.println("ERROR");
        }
        return false;
    }
    return true;
}

char* readData(uint16_t timeout) {
    uint16_t replyidx = 0;
    char replybuffer[254];
    int secconds = 0;
    while (!fonaSS.available() && secconds < timeout) {
        secconds++;
    }
    while (timeout--) {
        if (replyidx >= 254) {
            break;
        }
        while (fonaSS.available()) {
            char c =  fonaSS.read();
            if (c == '\r') continue;
            if (c == 0xA) {
                if (replyidx == 0)   // the first 0x0A is ignored
                    continue;
            }
            replybuffer[replyidx] = c;
            replyidx++;
        }
        while (!fonaSS.available() && timeout > 0) {
            timeout--;
            delay(1);
        }

        if (timeout == 0) {
            if (fonaSS.available()) {
                timeout = 1000;
            } else {
                break;
            }
        }
    }
    replybuffer[replyidx] = '\0';  // null term
    if (_debug) {
        Serial.println("Response of FONA:");
        Serial.println(replybuffer);
    }
    return replybuffer;
}

Code snippet #4

Plain text
/********************************
 * Libraries included
 *******************************/
#include <SoftwareSerial.h>
#include <stdlib.h>
#include "Arduino.h"
 
/********************************
 * Constants and objects
 *******************************/
 
#define DEFAULT_BUFFER_SIZE      64

#define FONA_RX  9
#define FONA_TX  8
#define FONA_RST 4

#define SERVER "translate.ubidots.com"
#define PORT "9012"

#define APN  "...."  // The APN of your operator
#define USER ""  // if your apn doesnt have username just leave it ""
#define PASS ""  // if your apn doesnt have password just leave it ""
#define TOKEN "...."  // Replace it with your Ubidots token
#define METHOD "POST"
#define USER_AGENT  "adafruit"
#define VERSION  "1.0"
#define DEVICE_LABEL  "feather-fona"
#define VARIABLE_LABEL  "temperature"


bool _debug = true; 
char buffer[DEFAULT_BUFFER_SIZE];
 
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);

/********************************
 * Main Functions
 *******************************/
void setup() {
    Serial.begin(115200);
    pinMode(A0, INPUT);
    delay(2000);
    while(!setApn(APN, USER, PASS));
}

void loop() {
    char* request = (char *) malloc(sizeof(char) * 300);
    char str_val[15];
    
    float sensor_value = analogRead(A0);

    dtostrf(sensor_value, 4, 2, str_val);
    sprintf(request, "%s/%s|%s|%s|%s=>%s:%s|end", USER_AGENT, VERSION, METHOD, TOKEN, DEVICE_LABEL, VARIABLE_LABEL, str_val);
    //sprintf(request, "%s|%s|%s","adafruit/1.0|POST", TOKEN, "fona=>test:17.00|end");
    sendToUbidots(request);
    free(request);
}

/********************************
 * Auxiliar Functions
 *******************************/
bool sendMessageAndwaitForOK(char* message, uint16_t timeout = 4000) {
    fonaSS.println(message);
    if (strstr(readData(timeout), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error"));
        }
            return false;
        }
    return true;
}

bool setApn(char* apn, char* user, char* pwd) {
    checkFona();
    fonaSS.println(F("AT"));
    if (strstr(readData(2000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT"));
        }
        return false;
    }
    fonaSS.println(F("AT+CREG?"));
    if (strstr(readData(2000), "+CREG:") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT"));
        }
        return false;
    }
    fonaSS.println(F("AT+CSQ"));
    if (strstr(readData(2000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CSQ"));
        }
        return false;
    }
    fonaSS.println(F("AT+CGATT?"));
    if (strstr(readData(10000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CGATT"));
        }
        return false;
    }
    fonaSS.println(F("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""));
    if (strstr(readData(10000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR CONTYPE"));
        }
        return false;
    }
    fonaSS.print(F("AT+SAPBR=3,1,\"APN\",\""));
    fonaSS.print(apn);
    fonaSS.println(F("\""));
    if (strstr(readData(3000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR APN"));
        }
        return false;
    }
    fonaSS.print(F("AT+SAPBR=3,1,\"USER\",\""));
    fonaSS.print(user);
    fonaSS.println(F("\""));
    if (strstr(readData(10000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR USER"));
        }
        return false;
    }
    fonaSS.print(F("AT+SAPBR=3,1,\"PWD\",\""));
    fonaSS.print(pwd);
    fonaSS.println("\"");
    if (strstr(readData(3000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR PASSWORD"));
        }
        return false;
    }
    fonaSS.println(F("AT+SAPBR=1,1"));
    if (strstr(readData(4000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR=1,1 Connection ip"));
        }
        return false;
    }
    fonaSS.println(F("AT+SAPBR=2,1"));
    if (strstr(readData(4000), "+SAPBR:") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+SAPBR=2,1 no IP to show"));
        }
        return false;
    }
    return true;
}

bool module_begin() {
    pinMode(FONA_RST, OUTPUT);
    digitalWrite(FONA_RST, HIGH);
    delay(500);
    digitalWrite(FONA_RST, LOW);
    delay(500);
    digitalWrite(FONA_RST, HIGH);
    delay(500);
    Serial.println(F("Attempting to open comm with ATs"));
    int16_t timeout = 10000;
    while (timeout > 0) {
        if (sendMessageAndwaitForOK("AT"))
            break;
        delay(500);
        timeout -= 500;
    }
    if (timeout <= 0) {
        Serial.println(F("Timeout: No response to AT... last ditch attempt."));
        sendMessageAndwaitForOK("AT");
        delay(100);
        sendMessageAndwaitForOK("AT");
        delay(100);
        sendMessageAndwaitForOK("AT");
        delay(100);
        return false;
    }
    sendMessageAndwaitForOK("ATE0");
    delay(100);
    return true;
}

bool sendToUbidots(char* request) {
  
    fonaSS.println(F("AT+CIPMUX=0"));
    if (strstr(readData(4000), "OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPMUX"));
        }
        return false;
    }
    fonaSS.print(F("AT+CIPSTART=\"TCP\",\""));
    fonaSS.print(SERVER);
    fonaSS.print(F("\",\""));
    fonaSS.print(PORT);
    fonaSS.println(F("\""));
    if (strstr(readData(4000), "CONNECT OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPSTART"));
        }
        return false;
    }
    fonaSS.print(F("AT+CIPSEND="));
    fonaSS.println(dataLen(request));
    if (strstr(readData(4000), ">") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPSEND"));
        }
        return false;
    }
    fonaSS.write(request);
    if (strstr(readData(4000), "SEND OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error sending variables"));
        }
        return false;
    }
    fonaSS.println(F("AT+CIPCLOSE"));
    if (strstr(readData(4000), "CLOSE OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPCLOSE"));
        }
        return false;
    }
    fonaSS.println(F("AT+CIPSHUT"));
    if (strstr(readData(4000), "SHUT OK") == NULL) {
        if (_debug) {
            Serial.println(F("Error with AT+CIPSHUT"));
        }
        return false;
    }
    return true;
}

int dataLen(char* variable) {
  uint8_t dataLen = 0;
  for (int i = 0; i <= 250; i++) {
    if (variable[i] != '\0') {
      dataLen++;
    } else {
      break;
    }
  }
  return dataLen;
}

bool checkFona() {
    fonaSS.begin(4800);
    delay(2000);
    module_begin();
    if (!sendMessageAndwaitForOK("ATE0", 6000)) {
        Serial.print("Couldn't find FONA");
        while (1) {
            Serial.println("ERROR");
        }
        return false;
    }
    return true;
}

char* readData(uint16_t timeout) {
    uint16_t replyidx = 0;
    char replybuffer[254];
    int secconds = 0;
    while (!fonaSS.available() && secconds < timeout) {
        secconds++;
    }
    while (timeout--) {
        if (replyidx >= 254) {
            break;
        }
        while (fonaSS.available()) {
            char c =  fonaSS.read();
            if (c == '\r') continue;
            if (c == 0xA) {
                if (replyidx == 0)   // the first 0x0A is ignored
                    continue;
            }
            replybuffer[replyidx] = c;
            replyidx++;
        }
        while (!fonaSS.available() && timeout > 0) {
            timeout--;
            delay(1);
        }

        if (timeout == 0) {
            if (fonaSS.available()) {
                timeout = 1000;
            } else {
                break;
            }
        }
    }
    replybuffer[replyidx] = '\0';  // null term
    if (_debug) {
        Serial.println("Response of FONA:");
        Serial.println(replybuffer);
    }
    return replybuffer;
}

Github

https://github.com/adafruit/Adafruit_FONA

Credits

UbiMaker

UbiMaker

53 projects • 229 followers
Maker @ ubidots.com
Maka Hernandez

Maka Hernandez

29 projects • 123 followers

Comments