hello all, I have this idea. We can use sim900 + Blynk to make our boards more flexible than Wifi and ethernet are. Maybe, we can select one of them. It mean that we have got wifi and gprs, we use wifi and we havent got wifi we use gprs. I try to modify some function in library to do this. I wana get some advice from you. Did you ?
You can use User_Defined_Connection example to achieve this and code all the needed logics…
I am also trying to do the same for a while now and I am failing wonderfully. I keep coming back to the few pages that talk about gprs and blynk and there is no updates. This is what I have so far. I can get to where it’s connected to my blynk server by tcp.
I run netstat -an 1 | find “8442” and it shows when the tcp goes from listening to established. All I see after that is blynk consistently sending my auth code in serial.
Any help would be greatly appreciated.
Serial output:
Powering on GPRS Shield.
SIM900 started
Connected to the home cell network
Attaching to Packet Service
Attached to Packet Service
Setting APN
APN Set
Starting GPRS Connection
GPRS Connection Established
Establishing TCP Connection
Connected to server
60ca34441b83ee6cc96eded52dade860ca34441b83ee6cc96eded52dfade860ca34441b83ee6cc96eded52d
Here is what I’m working with right now.
#include <BlynkSimpleUserDefined.h>
#include <SoftwareSerial.h>
// SIM900 Shield Serial Pins
SoftwareSerial SIM900(7, 8);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "4fadeMYTOKENee6cc96eded52d";
#define ip_address "AT+CIPSTART=\"TCP\",\"8.8.8.8\",\"8442\""
bool connected = false;
// This function is used by Blynk to receive data
size_t BlynkStreamRead(void* buf, size_t len)
{
return Serial.readBytes((byte*)buf, len);
}
// This function is used by Blynk to send data
size_t BlynkStreamWrite(const void* buf, size_t len)
{
return Serial.write((byte*)buf, len);
}
void setup()
{
// Setup your connection here.
pinMode(9, OUTPUT);
Serial.begin(19200); // for serial monitor
Serial.println("Powering on GPRS Shield.");
SIM900.begin(19200); // Serial for GSM shield
SIM900poweron(); // turn on shield
sendCommand("AT", "OK", 1000); //Wake up modem with an AT command
if(sendCommand("AT", "OK", 1000) == 1){
Serial.println("SIM900 started");
}
gprs();
Blynk.begin(auth);
do {
connected = Blynk.connect();
} while (!connected);
}
void loop()
{
Blynk.run();
// Make sure that Blynk.run() is called
// only when the connection is established
if (connected) {
// Okay, handle Blynk protocol
bool hasIncomingData = (Serial.available() > 0);
// Tell Blynk if it has incoming data
// (this allows to skip unneeded BlynkStreamRead calls)
if (!Blynk.run(hasIncomingData)) {
// Error happened. No action for serial.
}
}
}
void gprs()
{
//Wake up modem with two AT commands
sendCommand("AT", "OK", 1000);
sendCommand("AT", "OK", 2000);
sendCommand("AT+CIPSHUT", "SHUT OK", 2000);
// Check if it's currently registered network
if(sendCommand("AT+CREG?","+CREG: 0,1",2000)){
Serial.println("Connected to the home cell network");
}
else{
Serial.println("Not connected to home cell network");
delay(10000);
}
Serial.println("Attaching to Packet Service");
if(sendCommand("AT+CGATT=1","OK",3000)){
Serial.println("Attached to Packet Service");
}
else{
Serial.println("Unable to attach to Packet Service");
delay(10000);
}
Serial.println("Setting APN");
if(sendCommand("AT+CSTT=\"epc.tmobile.com\"","OK",3000)){
Serial.println("APN Set");
}
else{
Serial.println("Unable to set APN");
delay(10000);
}
Serial.println("Starting GPRS Connection");
if(sendCommand("AT+CIICR","OK",5000)){
Serial.println("GPRS Connection Established");
delay(1000);
sendCommand("AT+CIFSR", "OK", 1000);
}
else{
Serial.println("Unable to start GPRS connection");
delay(10000);
}
Serial.println("Establishing TCP Connection");
SIM900.println(ip_address);
delay(1000);
Serial.println("Connected to server");
}
void SIM900poweron()
// Software equivalent of pressing the GSM shield "power" button
{
//Wake up modem with an AT command
sendCommand("AT", "OK", 1000);
if(sendCommand("AT", "OK", 2000) == 0){
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}
}
int sendCommand(char* ATcommand, char* expected_answer, unsigned int timeout){
int answer=0;
int responsePos=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Clears array
delay(100);
while( SIM900.available() > 0) SIM900.read(); // Clean the input buffer
SIM900.println(ATcommand); // Send the AT command
responsePos = 0;
previous = millis();
// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(SIM900.available() != 0){
response[responsePos] = SIM900.read();
responsePos++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
I’m tring to get your code woring…
Don’t we have to add some AT+CIPSEND commands into BlynkStreamWrite and BlynkStreamRead fnctions, so Blynk lib knows how to talk?
Any help from @vshymanskyy?
For GSM/GPRS we recommend using HTTP API - it consumes much less data plan, and is easier to implement low power modes…