#include <SoftwareSerial.h>
#include <stdio.h>
#include <string.h>
//Change the API key to yours
String Apikey = "FOONITF25Z929EAN";
#define DEBUG true
#define rx 2
#define tx 3
SoftwareSerial Serial1(tx,rx);
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
sendData("AT+CCID", 3000);
sendData("AT+CREG?", 3000);
sendData("AT+CGDCONT=1,IP,m9-itelecom", 1000);
sendData("AT+CGACT=1,1", 1000);
sendData("AT+CGATT=1", 1000);
Serial.println("4G HTTP Test Begin!");
delay(1000);
}
void loop()
{
//--------Get temperature and humidity-------------
float t = analogRead(A0);
Serial.print("Temperature: ");
Serial.print(t);
Serial.println("*C");
delay(1000);
//-----------HTTP---------------------
String http_str = "AT+HTTPPARA=\"URL\",\"https://api.thingspeak.com/update?api_key=" + Apikey + "&field1=" + (String)t + "\"\r\n";
Serial.println(http_str);
sendData("AT+HTTPINIT\r\n", 2000);
sendData(http_str, 2000);
sendData("AT+HTTPACTION=0\r\n", 3000);
sendData("AT+HTTPTERM\r\n", 3000);
delay(5000);
}
String sendData(String toSend, unsigned long milliseconds)
{
String result;
Serial.print("Sending: ");
Serial.println(toSend);
Serial1.println(toSend);
unsigned long startTime = millis();
Serial.print("Received: ");
while (millis() - startTime < milliseconds) {
if (Serial1.available()) {
char c = Serial1.read();
Serial.write(c);
result += c; // append to the result string
}
}
Serial.println(result);
return result;
}
