Skip to content

Instantly share code, notes, and snippets.

@mabbotts9797
Created May 26, 2018 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mabbotts9797/1d60d8cad4d50eaaf3c9b6d82eb400b0 to your computer and use it in GitHub Desktop.
Save mabbotts9797/1d60d8cad4d50eaaf3c9b6d82eb400b0 to your computer and use it in GitHub Desktop.
Arduino code for posting GPS and sensor reading to Azure:
#include <b64.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
static const int RXPin = 12, TXPin = 13; //RX and TX pins for GPS
TinyGPS gps;
SoftwareSerial ss(RXPin, TXPin); //Emulated using SoftwareSerial Library
char ssid[] = "ssid";
char pass[] = "password";
char server[] = "http://yourazurewebsitename.azurewebsites.net"; //Azure function base request URL
char resource[] = "/api/YourTrigger"; //Azure function resource
char code[] = "Azure Function code (part of request url)"; // Azure function code
int status = WL_IDLE_STATUS;
void setup()
{
Serial.begin(9600);
while (!Serial)
;
Serial.print( F("GPSLogger: GPSLogger started\n") );
ss.begin(9600);
Serial.print( F("GPSLogger: connecting to SSID ") );
Serial.println( ssid );
Serial.println();
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
//Delay until connected
delay(500);
Serial.print(".");
}
Serial.print(F("GPSLogger: network module init and connection successful\n"));
IPAddress ip = WiFi.localIP();
Serial.print(F("GPSLogger: network IP address is "));
Serial.println(ip);
}
//--------------------------
void loop()
{
while (ss.available()) {
char c = ss.read();
if ( gps.encode(c) )
{
float flat, flon;
gps.f_get_position(&flat, &flon);
Serial.print( F("Location: ") );
Serial.print( flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6 );
Serial.print( ',' );
Serial.print( flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6 );
Serial.println();
// If connected post readings.
if ((WiFi.status() == WL_CONNECTED)) {
int ppmReading = analogRead(A0);
// Build request URL using above details and readings.
String requestUrl = String(server) + String(resource) + "?code=" + String(code)
+ "&latitude=" + String(flat, 6) + "&longitude=" + String(flon, 6)
+ "&ppm=" + String(ppmReading);
Serial.print("GPSLogger: Posting readings to: ");
Serial.println(requestUrl);
HTTPClient http;
http.begin(requestUrl); //Specify the URL
int httpCode = http.GET(); /Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.print("GPSLogger: status ");
Serial.println(httpCode);
Serial.println(payload);
}
else {
//Could be good here to possibly hold unposted data to EEPROM.
Serial.println("Error on HTTP request");
}
http.end();
}
//Delay for two minutes, cleaner to use millis() to prevent blocking.
delay(120000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment