-
Notifications
You must be signed in to change notification settings - Fork 171
Description
I'm developing a system in which an Atmega328 acts as a gateway, receiving data from a sensor via NRF24L01 and sending it to an HTTP server in Spring via 4G.
Well, everything was working perfectly until yesterday, I edited some things in the data reception EndPoint in Spring, and now my Gateway returns the HTTP code -2 (Which doesn't exist in standard HTTP, I looked it up and this is a code from the ArduinoHTTPClient library itself [You are receiving an HTTP response code of -2, which means there was an error sending the request header]).
Well, I tested the 4G modem and it has a signal and is connected to the mobile internet network.
I tested another test endpoint and the same thing, code -2.
I don't know how to proceed, I've tried several ways to get around the problem but I always get the same answer.
#include <SoftwareSerial.h>
#include "RF24.h"
RF24 radio(7, 8);
const byte radio_address[6] = "00006";
#define TINY_GSM_MODEM_SIM7600
#define TINY_GSM_USE_WIFI false
#define GSM_PIN ""
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 3
#define PIN_RX 2
#define PWR_PIN 7
#define RESET_ATMEGA 6
SoftwareSerial SerialAT(PIN_RX, PIN_TX);
const char apn[] = "zap.vivo.com.br";
const char gprsUser[] = "vivo";
const char gprsPass[] = "vivo";
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#define TINY_GSM_USE_GPRS true
//#include <StreamDebugger.h>
//StreamDebugger debugger(SerialAT, Serial);
//TinyGsm modem(debugger);
TinyGsm modem(Serial);
const char resource[] = "/data/register";
const uint8_t cid = 1;
const int port = 8080;
TinyGsmClient client(modem);
int g = 0;
bool connectedToNetwork = false;
#define TRANSMISSION_DATA_PACKAGE 5
struct __attribute__((__packed__)) Data {
char type;
char key[6];
float rms[3];
float temperature;
} data;
struct __attribute__((__packed__)) VibrationPackage {
char type;
char key[6];
float dataPackage[TRANSMISSION_DATA_PACKAGE];
int start;
int end;
} vibrationPackage;
void initGSM() {
if (!connectedToNetwork) {
SerialAT.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialAT.println(" fail");
delay(10000);
return;
}
SerialAT.println(" success");
connectedToNetwork = true;
if (modem.isNetworkConnected()) {
SerialAT.println("Network connected");
}
}
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialAT.println("fail");
return;
}
g = 1;
}
void setup() {
pinMode(RESET_ATMEGA, OUTPUT);
digitalWrite(RESET_ATMEGA, HIGH);
Serial.begin(115200);
SerialAT.begin(UART_BAUD);
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(100);
digitalWrite(PWR_PIN, HIGH);
delay(1000);
digitalWrite(PWR_PIN, LOW);
SerialAT.println("Wait...");
delay(11000);
SerialAT.println("Initializing modem...");
if (!modem.init()) {
SerialAT.println("Failed to restart modem, delaying 10s and retrying");
return;
}
if (GSM_PIN && modem.getSimStatus() != 3) {
modem.simUnlock(GSM_PIN);
}
if (!radio.begin()) {
SerialAT.println(F("[ERROR] Radio hardware is not responding..."));
while (1) {}
}
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS);
radio.setChannel(76);
radio.setCRCLength(RF24_CRC_16);
radio.openReadingPipe(1, radio_address);
radio.startListening();
}
void httpRequest(String json) {
HttpClient http(client, "ec2-**-**-***-***.us-west-2.compute.amazonaws.com", port);
http.beginRequest();
http.post(resource);
if (http.connected()) {
SerialAT.println("Connected to server");
} else {
SerialAT.println("Connection to server failed");
return;
}
http.sendHeader("Content-Type", "application/json");
http.sendHeader("Content-Length", json.length());
http.beginBody();
http.print(json);
http.endRequest();
http.stop();
}
void (*resetFunc)(void) = 0;
void loop() {
if (g == 0)
initGSM();
uint8_t pipe;
if (radio.available(&pipe)) {
SerialAT.println("Res ");
SerialAT.print("Recieved ");
int size = radio.getDynamicPayloadSize();
SerialAT.print(size);
SerialAT.println(" bytes on pipe");
byte packet[size];
radio.read(&packet, size);
if (packet[0] == 'D') {
memcpy(&data, &packet, sizeof(data)); //0 | Type 1-6 | Key
String dataJson = "";
dataJson += "{\"type\":\"";
dataJson += data.type;
dataJson += "\",\"key\":\"";
dataJson += data.key;
dataJson += "\",\"rms\":[";
dataJson += data.rms[0];
dataJson += ",";
dataJson += data.rms[1];
dataJson += ",";
dataJson += data.rms[2];
dataJson += "],\"temperature\":";
dataJson += data.temperature;
dataJson += "}";
SerialAT.println(dataJson);
httpRequest(dataJson);
} else {
memcpy(&vibrationPackage, &packet, sizeof(vibrationPackage));
String vibrationJson = "";
vibrationJson += "{\"type\":\"";
vibrationJson += vibrationPackage.type;
vibrationJson += "\",\"key\":\"";
vibrationJson += vibrationPackage.key;
vibrationJson += "\",\"dataPackage\":[";
for (int i = 0; i < TRANSMISSION_DATA_PACKAGE; i++) {
vibrationJson += vibrationPackage.dataPackage[i];
if (i < TRANSMISSION_DATA_PACKAGE - 1) {
vibrationJson += ",";
}
}
vibrationJson += "],\"start\":";
vibrationJson += vibrationPackage.start;
vibrationJson += ",\"end\":";
vibrationJson += vibrationPackage.end;
vibrationJson += "}";
SerialAT.println(vibrationJson);
httpRequest(vibrationJson);
}
}
}