0% found this document useful (0 votes)
29 views4 pages

Sim HTTP

This document contains code for an Arduino project that collects sensor data from an RF24 radio, converts it to JSON format, and posts it to a server using GPRS. It initializes the RF24 radio in receiver mode and listens for sensor data packets. It defines functions to configure the GPRS connection, format the data as JSON, and make HTTP POST requests to upload the data. The main loop checks for received sensor packets and uploads the data every 15 seconds.

Uploaded by

Thinh Hoang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Sim HTTP

This document contains code for an Arduino project that collects sensor data from an RF24 radio, converts it to JSON format, and posts it to a server using GPRS. It initializes the RF24 radio in receiver mode and listens for sensor data packets. It defines functions to configure the GPRS connection, format the data as JSON, and make HTTP POST requests to upload the data. The main loop checks for received sensor packets and uploads the data every 15 seconds.

Uploaded by

Thinh Hoang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include "SoftwareSerial.

h"
#include <TinyGPSPlus.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define csn 15
#define mosi 13 //hspi
#define miso 12
#define clk 14
#define ce 2
float giatri[5]; // gia tri cua cam bien
RF24 radio(2, 15, clk, miso, mosi); // CE, CSN
const uint64_t address = 0xF0F0F0F0E1LL;
////
TinyGPSPlus gps; // create gps object
SoftwareSerial sim_(22, 23); // rx tx
//
String timestamp;
float lat = 28.5458, lon = 77.1703;
float temp, hum, acc;
///

///
String apn = "v-connect"; //APN
String apn_u = ""; //APN-Username
String apn_p = ""; //APN-Password
String url_post = "http://scube.loca.lt/box/upload-data/mother01"; //URL of Server
String url_get = "api.thingspeak.com/channels/119922/feeds/last.txt";
String data_get = "";
/////////
void gsm_send_serial(String command, uint32_t time_delay = 3000, bool cvt_data = 0)
{
Serial.println("Send ->: " + command);
sim_.println(command);
long wtimer = millis();
while (wtimer + time_delay > millis()) {
while (sim_.available()) {
char c = sim_.read();
Serial.write(c);
}
}
Serial.println();
}
void gsm_config_gprs() {
Serial.println(" --- CONFIG GPRS --- ");
gsm_send_serial("AT+SAPBR=3,1,Contype,GPRS");
gsm_send_serial("AT+SAPBR=3,1,APN," + apn);
if (apn_u != "") {
gsm_send_serial("AT+SAPBR=3,1,USER," + apn_u);
}
if (apn_p != "") {
gsm_send_serial("AT+SAPBR=3,1,PWD," + apn_p);
}
}

String str_cvt(String str) {


String c = "\"" + str + "\"";
return c;
}
void data_json() {
StaticJsonDocument<400> doc;
doc["timeStamp"] = "1672483819";
doc["longitude"] = lon;
doc["latitude"] = lat;
JsonArray slave = doc.createNestedArray("slaves");
StaticJsonDocument<200> slaves;
slaves["id"] = "box-01";
slaves["temperature"] = giatri[2];
slaves["humidity"] = giatri[1];
slaves["acceleration"] = giatri[0];
// StaticJsonDocument<200> slaves2;
// slaves2["id"] = "sla";
// slaves2["temperature"] = temp ;
// slaves2["humidity"] = hum ;
// slaves2["acceleration"] = acc;
// StaticJsonDocument<200> slaves3;
// slaves3["id"] = "sla";
// slaves3["temperature"] = temp;
// slaves3["humidity"] = hum;
// slaves3["acceleration"] = acc;
slave.add(slaves);
// slave.add(slaves2);
// slave.add(slaves3);
String c;
serializeJsonPretty(doc, c);
//Serial.println(c);
}
void gsm_http_post(String postdata) {
Serial.println(" --- Start GPRS & HTTP --- ");
gsm_send_serial("AT+SAPBR=1,1");
gsm_send_serial("AT+SAPBR=2,1");
gsm_send_serial("AT+HTTPINIT");
gsm_send_serial("AT+HTTPPARA=\"CID\",1");

StaticJsonDocument<400> doc;
doc["timeStamp"] = "1672483819";
doc["longitude"] = lon;
doc["latitude"] = lat;
JsonArray slave = doc.createNestedArray("slaves");
StaticJsonDocument<200> slaves;
slaves["id"] = "box01";
slaves["temperature"] = giatri[2];
slaves["humidity"] = giatri[1];
slaves["acceleration"] = giatri[0];
// StaticJsonDocument<200> slaves2;
// slaves2["id"] = "sla";
// slaves2["temperature"] = temp ;
// slaves2["humidity"] = hum ;
// slaves2["acceleration"] = acc;
// StaticJsonDocument<200> slaves3;
// slaves3["id"] = "sla";
// slaves3["temperature"] = temp;
// slaves3["humidity"] = hum;
// slaves3["acceleration"] = acc;
slave.add(slaves);
// slave.add(slaves2);
// slave.add(slaves3);
String c;
serializeJson(doc, c);
gsm_send_serial("AT+HTTPPARA=\"URL\",\"http://scube.loca.lt/box/upload-data/
mother01\"");
gsm_send_serial("AT+HTTPPARA=\"CONTENT\",\"application/json\""); /// thay bang
json
gsm_send_serial("AT+HTTPDATA="+String(c.length())+",10000");
gsm_send_serial(c);
gsm_send_serial("AT+HTTPACTION=1", 10000);
gsm_send_serial("AT+HTTPREAD", 7000);
//gsm_send_serial("AT+HTTPTERM",10000);
// gsm_send_serial("AT+SAPBR=0,1");
}
void gsm_http_get() {
gsm_send_serial("AT+SAPBR=1,1");
gsm_send_serial("AT+SAPBR=2,1");
gsm_send_serial("AT+HTTPINIT");
gsm_send_serial("AT+HTTPPARA=CID,1");
gsm_send_serial("AT+HTTPPARA=URL," + url_get);
gsm_send_serial("AT+HTTPACTION=0");
gsm_send_serial("AT+HTTPREAD", 5000, 1);
}
//////
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
sim_.begin(9600);
Serial2.begin(9600); // gps
delay(10000);
SPI.begin(14, 12, 13, 15);
radio.begin();
radio.openReadingPipe(0, address); //Setting the address at which we will
receive the data
radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum
depending on the distance between the transmitter and receiver.
radio.startListening(); //This sets the module as receiver
// gsm_http_get();
gsm_config_gprs();
//data_json();
}
int recvData() {
if (radio.available()) {
radio.read(&giatri, sizeof(giatri));
return 1;
}
return 0;
}
void loop() {
// put your main code here, to run repeatedly:
static unsigned long time_ = millis();
if (millis() - time_ > 15000) {
gsm_http_post(" ");
time_ = millis();
}
static unsigned long time_2 = millis();
if (millis() - time_2 >1000) {
if (recvData()) {
Serial.println(giatri[0]);
time_2=millis();
}
}
}

You might also like