Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created November 4, 2020 11:13
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 jenschr/237b16aeffa33e22df60af16a94ed3d7 to your computer and use it in GitHub Desktop.
Save jenschr/237b16aeffa33e22df60af16a94ed3d7 to your computer and use it in GitHub Desktop.
Basic sketch for an TTGO LoRa ESP-32 to send a message to Dweet.io using GET. Written specifically for the SIM7600E-H, but may work well on other similar modules.
/*
* Basic sketch to send a message to Dweet.io using GET
* This sketch is written specifically for the SIM7600E-H
* This specific version is for TTGO LoRa with Oled. Note
* that you cannot use the OLED with Serial since the OLED
* Reset pin conflicts with RX2.
*
* Connect the R pin on the module to pin 16 on the ESP32
* Connect the T pin on the module to pin 17 on the ESP32
*
* Also enter your own deviceName below (replace "8675840")
* and you can then go and see the result at
* http://dweet.io/get/latest/dweet/for/8675840
*
* You will also have to change the PIN and APN for this
* to work with YOUR telcom provider.
*
* Written by https://github.com/jenschr
*/
//Note that you cannot use the builtin OLED Display since the Reset pin will crash with RX2
#define RXD2 16
#define TXD2 17
String deviceName = "8675840";
int counter = 0;
// Declare metod with defaults here. Implementation is further down
void sendAndReadResponse( String command, int extraWaitInMillisecondsForResponse = 200 );
void setup() {
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
delay(1000); // The ESP32 is slow at start, so wait a little extra...
// Wait for SIM to come online
Serial.println("Wait 8 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 7 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 6 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 5 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 4 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 3 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 2 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 1 boring second for the green LED to turn on");
delay(1000);
// Change speed from 115200 to 19200 to prevent errors
//Serial2.println("AT+IPR=19200");
//delay(1000);
//Serial2.begin(19200);
//delay(1000);
// Setup the module - start by just saying "hi" with an AT
sendAndReadResponse("AT");
// Enter the PIN for your SIM card in the command below (unless it's 1234)
//sendAndReadResponse("AT+CPIN=1234");
sendAndReadResponse("AT+CFUN=1");
sendAndReadResponse("AT+CGACT=1,1");
// Last parameter here ('internet') is your telco's APN. Google it unless you know it?
sendAndReadResponse("AT+CGDCONT=1,\"IP\",\"internet\"");
sendAndReadResponse("AT+CGPS=1");
sendAndReadResponse("AT+CGREG?");
sendAndReadResponse("AT+NETOPEN");
sendAndReadResponse("AT+IPADDR");
// Send request to Dweet.io
sendAndReadResponse("AT+CHTTPACT=\"dweet.io\",80");
Serial2.print("GET http://dweet.io/dweet/for/");
Serial2.print(deviceName);
Serial2.println("?temperature=5.2&humidity=77.7&bananas=TheCrappiestThing HTTP/1.1\n");
Serial2.println("Host: dweet.io\n");
Serial2.println("User-Agent: mozilly\n");
Serial2.println("Content-Length: 0\n");
Serial2.print(char(26)); delay(20);
Serial2.print(char(26));
}
void loop() {
if (Serial.available() > 0) {
Serial.print(">");
delay(100);
while ( Serial.available() ) {
char ch = Serial.read();
Serial.print(ch);
Serial2.print(ch);
}
}
if (Serial2.available() > 0) {
Serial.print(":");
delay(10);
while ( Serial2.available() ) {
char ch = Serial2.read();
if ( ch ) {
Serial.print(ch);
}
}
}
}
void sendAndReadResponse( String command, int extraWaitInMillisecondsForResponse )
{
// Send the command
Serial.print( "Sending: " );
Serial.println( command );
Serial2.println( command );
// Setup a timeout
int requestStartTime = millis();
int millisecondsSinceRequestStarted = 0;
bool wegotResponse = false;
// Wait until we get a response (or timeout)
while ( !wegotResponse || millisecondsSinceRequestStarted < 2000 )
{
millisecondsSinceRequestStarted = millis() - requestStartTime;
if ( Serial2.available() > 0 )
{
wegotResponse = true;
}
}
// Print out the results
if ( millisecondsSinceRequestStarted < 2000 )
{
Serial.print("\nCommand: '");
Serial.print(command);
Serial.println(" timed out?\n");
}
else if (Serial2.available() > 0)
{
Serial.print("<\n");
// wait a little longer so we get the whole response
delay(extraWaitInMillisecondsForResponse);
// Print out the response to Serial monitor
while ( Serial2.available() )
{
char ch = Serial2.read();
if ( ch ) {
Serial.print(ch);
}
}
Serial.println("\n>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment