Hi
the post is some day ago but this "theme" is everlasting i guess - this is how i do this currently.
in my setup i have running severals ESP8266 ( node mcu lua ).. the mcu´s are collecting lots of data.
Target is was to have this data in an MS SQL Database...This way other applications ( e.g. Desktop, Android APP,.. ) easily can retrieve or access those data.
esp8266 - how to send this data to the implemented IIs Rest API:
here is the significant esp code ( sniplets ) to send Data to the rest API :
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include "WiFiManager.h"
#include <osapi.h>
......
const char* mySSID = "csfcdcc";
const char* myLANpw = "mypojawoll;
String hostIP = "192.14.xxx7111.69";
String myRESTapiCALL = "http://192.00.1.000:80/api/scanner";
.....
int sendDataPack()
{
...
wifi_set_channel(myWifiChannel);
if (WiFi.status() !=
WL_CONNECTED) { // FIX FOR USING 2.3.0 CORE (only .begin if not connected)
WiFi.begin(mySSID, myLANpw); // connect to the network
Serial.println("login into AP...");
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Blink the LED
digitalWrite(2, ledStatus); // Write LED high/low
ledStatus = (ledStatus == HIGH) ? LOW : HIGH;
Serial.print("setup()/500erLoop/Waiting for connection/ new Wifi-State: ");
Serial.println(WiFi.status());
}
String jsonPayload = "";
int httpCode = 0;
restArray[ri] = data4RestService;
// chunks are now ALL in the array and we loop through it
if (WiFi.status() == WL_CONNECTED) { // Check WiFi connection status
Serial.println("WiFi.status(): WL_CONNECTED - OK");
HTTPClient http; // Declare object of class HTTPClient
// now we need to chunk the payload into 1000 byte chunks
for (int i = 0; i <= ri; ++i) {
Serial.print("for(int i = 0; i <= ri; ++i) / LOOP / i = ");
Serial.println(i);
jsonPayload = "{"data":"" + restArray + ""}";
_ Serial.println(restArray*);_
_ delay(10);_
_ http.begin(myRESTapiCALL); // Specify request destination*_
* http.addHeader("Content-Type", "application/json");*
* httpCode = http.POST(jsonPayload);*
* if (httpCode >= 200 && httpCode <= 300) {*
* Serial.print("sendDataPack()/MobileDeviceData ok: OK../ httpCode = ");*
* Serial.println(httpCode);*
* } else {*
* Serial.print("sendDataPack(): recursive Loop..call again sendDataPack() E R R O R - httpCode: ");*
* Serial.println(httpCode);*
* if (recursiveLoopCounter < 3) {*
* recursiveLoopCounter++;*
* ri = 0;*
* data4RestService = "";*
* memset(restArray, 0, sizeof(restArray));*
* Serial.print( "sendDataPack(): recursive Loop..: recursiveLoopCounter= ");*
* Serial.println(recursiveLoopCounter);*
* delay(500);*
* sendDataPack();*
* } else {*
* ESP.restart();*
* }*
* }*
* http.end();*
* }*
* ri = 0;*
* data4RestService = "";*
* memset(restArray, 0, sizeof(restArray));*
* } else {*
* Serial.print("Error in WiFi connection: ");*
* Serial.println(WiFi.status());*
* returnResult = WiFi.status(); // in case of failure ...quick led flashing*
* }*
RESTapi written in VS ... there are many option on how to setup a restService ...Code from above with minimal adaption also can be unsed with a normal webService!
1. We set up the IIS on windows 10 ..i followed just this video
*https://www.youtube.com/watch?v=fTMPq3Ti1OU*_
2. I create this simple hallo world REST-API application / Rest1 - WAS so much helpful for me !!::
*https://www.youtube.com/watch?v=qg4z7uEx8Dg#t=9.998062*_
Now to your Question on How to access ESP8266 Data from an Android APP. Once the MCU-Data is Stored vis REST in the MS SQL-DB via REST - Access from Android is very easy and flexible:
Android APP / js + bootstrap + cordova
some important in index.html:
*
* *
* *
* *
* *
* *
..this is the snipplet on how to RETRIEVE DATA from the Rest-API-Service
// get Data from the WebPage from the iCounterBOX-AP....java-script...
**// Make the actual CORS request. OK!! Arsch gerettet !! - https://www.html5rocks.com/en/tutorials/cors/**_
// Create the XHR object.
function createCORSRequest(method, url) {
** var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest !== "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
function makeCorsRequest4WebPageData() {
url = "http://" + "1xx.1xx.4.1/read"; //
var xhr = createCORSRequest('GET', url);
if (!xhr) {
console.log("CORS not supported");
return;
}
// Response handlers.
xhr.onload = function () {
var text = xhr.responseText;
// empfangenen String etwas säubern CLEANING.._
text = fkt.replaceAll(text, "\n", ""); // !! REPLACE ersetzt immer NUR den zuerst gefundenen !! **
** text = text.replace("", "");
text = text.replace("", "");
text = text.replace("", "");
text = text.replace("{", "");_
text = fkt.getStringTillSubstr(text, "}");**
** var index = text.indexOf('WAIT');
if (index !== -1) {
// Noch keine Daten Da - Nichts machen..Nichts Speichern..
} else {_
fkt.setLoadMessage(text); // Bring this data into the RAW data TextAREA-LOG **
** //data is ok and we bring this into an ARRAY - would do this today with other? JSON functions!!
currentMACdevices = text.split("|");
var totalNrdeliveredCIs = currentMACdevices.length; // for some console.log and statistics
//document.getElementById("apCounter").innerHTML = totalNrdeliveredCIs; _
writeCounterBoxDataIntoDB_consolitated(currentMACdevices); // write 2 sqLite (Android )
_ }
// VALUE DATA**
** console.log("xhr.responseText: \n" + text);
};**_
** xhr.onerror = function () {**
** console.log("Woops, there was an error making the request (AP)."); **
** fkt.setLoadMessage("xhr.onerror / AP request Fails / CORS(xHrsScan) /(AP) - Err getting data from iCounterBOX"); **
** };
xhr.send();
return 0;
}**_
This was ESP 8266 --- > REST ----> Android App
As always there are so many trails going to rome...
Your other question concerning GSM instead of wifi...have a look to www.quectel.com
regards
kina