Sending datas using HTTP request using SIM 900 - http-post

I'm trying to post some manual data from Arduino to a server. I'm using SIM 900 for data connection.. The data are not reaching the server.. The SIM is been initialized without error but the data are not been posted to the server.. Please help me to fix that.. The code is below..
#include<SoftwareSerial.h>
SoftwareSerial client(2,3);
String testReading="{ \"testID\" : 1, \"testLevel\" : 1, }";
void setup()
{
Serial.begin(9600);
client.begin(9600);
delay(1000);
if(client.available())
{
Serial.print("Connected");
}
else
{
Serial.print("Not Connected");
}
checkSignal();
connectGPRS();
postData();
delay(1000);
}
void loop()
{
if(client.available())
{
checkSignal();
connectGPRS();
postData();
delay(1000);
}
}
void connectGPRS()
{
client.println("AT+CGATT?");
delay(1000);
ShowSerialData();
client.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(1000);
ShowSerialData();
client.println("AT+SAPBR=3,1,\"APN\",\"www\"");//Using Indian Standard Vodafone Sim and so APN is www
delay(1000);
ShowSerialData();
client.println("AT+SAPBR=1,1");
delay(1000);
ShowSerialData();
client.println("AT+SAPBR=2,1");
delay(1000);
ShowSerialData();
}
void checkSignal()
{
client.println("AT+CSQ");
delay(1000);
ShowSerialData();
}
void ShowSerialData()
{
while(client.available()!=0)
Serial.write(client.read());
}
void postData()
{
client.println("AT+HTTPINIT");
delay(1000);
ShowSerialData();
client.println("AT+HTTPPARA=\"CID\",1");
delay(1000);
ShowSerialData();
client.println("AT+HTTPPARA=\"URL\",\"http://test.server/insert-path");
delay(1000);
ShowSerialData();
client.println("AT+HTTPPARA=\"CONTENT\",\"application/json");
delay(1000);
ShowSerialData();
client.println("AT+HTTPDATA=99,10000");
delay(1000);
ShowSerialData;
client.println(testReading);
delay(1000);
ShowSerialData;
client.println("AT+HTTPACTION=1");
delay(10000);
while(!client.available());
ShowSerialData();
}

You need to add another '\"' to complete the URL and content type.
client.println("AT+HTTPPARA=\"URL\",\"http://test.server/insert-path\"");
client.println("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
Also, trying to increase the client.println("AT+HTTPDATA=99,10000");
data input delay 10s to 20s.

Related

Send string on serial monitor Arduino tinkercad

I have problem with my code. I wanna send alphanumeric on serial monitor Arduino. I hope someone can help
void setup()
{
Serial.begin(9600);
}
void loop()
{
if ( Serial.available())
{
char datachar = Serial.read();
}
delay(1000)
serial.print(datachar)
}
Your code will not compile because of the following errors:
The statements delay(1000) and serial.print(datachar) are missing the ; at the end.
The variable datachar is declared inside the if block, so it will not be available outside when you call serial.print(datachar).
You misspelled the word serial in serial.print(datachar), it should start with an uppercase letter.
I believe this is what you are looking for:
void setup()
{
Serial.begin(9600);
}
void loop()
{
char datachar;
if(Serial.available())
{
datachar = Serial.read();
}
delay(1000);
Serial.print(datachar);
}

how to solve meaningless characters on serial montior Arduino

Hi i am new at Arduino Uno , i am trying to use imu sensor and read datas from it so i installed mpu9500 lib from arduino website and run one example from its lib. However as i open the serial monitor, it displays strange characters . I check the pin configuration many times and load different libs and examples but i could not fix it.
Here is the codes i run.
#include "MPU9250.h"
MPU9250 mpu;
void setup() {
Serial.begin(115200);
Wire.begin();
delay(2000);
if (!mpu.setup(0x68)) { // change to your own address
while (1) {
Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
delay(5000);
}
}
}
void loop() {
if (mpu.update()) {
static uint32_t prev_ms = millis();
if (millis() > prev_ms + 25) {
print_roll_pitch_yaw();
prev_ms = millis();
}
}
}
void print_roll_pitch_yaw() {
Serial.print("Yaw, Pitch, Roll: ");
Serial.print(mpu.getYaw(), 2);
Serial.print(", ");
Serial.print(mpu.getPitch(), 2);
Serial.print(", ");
Serial.println(mpu.getRoll(), 2);
}
And this is what i get also i changed value of boud but nothing changed
01:10:23.688 -> ⸮7в⸮⸮⸮⸮⸮⸮`f⸮⸮⸮⸮⸮f⸮⸮⸮~⸮⸮xxf⸮~⸮⸮fx⸮⸮⸮⸮⸮⸮怘⸮
Try running simple Serial print code at the same baud rate(115200) and check if your board is alright. Then run the following code once:
#include "MPU9250.h"
MPU9250 mpu; // You can also use MPU9255 as is
void setup() {
Serial.begin(115200);
Wire.begin();
delay(2000);
mpu.setup(0x68); // change to your own address
}
void loop() {
if (mpu.update()) {
Serial.print(mpu.getYaw()); Serial.print(", ");
Serial.print(mpu.getPitch()); Serial.print(", ");
Serial.println(mpu.getRoll());
}
}

ESP32 unable to detect interrupt

The code below is suppose to detect a pulse from a rain gauge. It is working in esp8266 but not in esp32 nodemcu. The logic seems fine I suppose I did something wrong while define the pin?
const int interruptPin = 18;
volatile boolean interrupt = false;
const int led = 2 ;
void setup() {
Serial.begin(115200);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), ISR, FALLING);
pinMode(led,OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
if (interrupt == true) {
Serial.println("An interrupt occurred");
delay(500);
digitalWrite(led, LOW);
interrupt = false;
}
delay(1000);
}
IRAM_ATTR void ISR () {
digitalWrite(led, HIGH);
interrupt = true;
}

Unicast image transmission using Xbee

community
I've been working on image transmission using xbee s2b pro modules and Arduino Mega. Main task is to transmit an jpg image taken by a jpeg serial camera at the transmitter and send it to a microSD memory at the receiver, but I've been dealing with one unique trouble, I need a delay of 2 seconds to send and receive succesfully 1 byte, if I set less seconds I lose information and receipt image get corrupted. Here you will see my codes:
Transmitter code:
byte ZERO = 0x00;
byte incomingbyte;
long int a=0x0000,j=0,k=0,count=0,i=0;
uint8_t MH,ML;
boolean EndFlag=0;
void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial1.begin(38400);
}
void loop()
{
byte a[32];
int ii;
SendResetCmd();
delay(4000);
SendTakePhotoCmd();
delay(1000);
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
}
while(!EndFlag)
{
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(20);
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
k++;
if((k>5)&&(j<32)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-1]==0xFF)&&(a[j]==0xD9)) //tell if the picture is finished
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++)
{
Serial.write(a[j]);
delay(2000);// observe the image through serial port
}
i++;
}
while(1);
}
void SendResetCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x26);
Serial1.write(ZERO);
}
void SetImageSizeCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x31);
Serial1.write(0x05);
Serial1.write(0x04);
Serial1.write(0x01);
Serial1.write(ZERO);
Serial1.write(0x19);
Serial1.write(0x11);
}
void SetBaudRateCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x24);
Serial1.write(0x03);
Serial1.write(0x01);
Serial1.write(0x2A);
Serial1.write(0xC8);
}
void SendTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(ZERO);
}
void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x32);
Serial1.write(0x0c);
Serial1.write(ZERO);
Serial1.write(0x0a);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(MH);
Serial1.write(ML);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(0x20);
Serial1.write(ZERO);
Serial1.write(0x0a);
a+=0x20;
}
void StopTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(0x03);
}
Receiver code:
#include <SD.h>
#include <SPI.h>
File myFile;
int pinCS = 53; // Pin 10 on Arduino Uno
void setup()
{
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
if (!SD.begin()) {
return;
}
}
void loop() {
byte buf[5000];
if (Serial.available()>0)
{
myFile = SD.open("imgrx.jpg", FILE_WRITE);
Serial.readBytes(buf,sizeof(buf));
Serial.println((byte)*buf);
myFile.write((byte)*buf);
while(Serial.available()>0) Serial.read();
}
else{
myFile.close();
}
}
I've tried a MicroSD -> XBEE -> MicroSD also and that trouble continued there, maybe I've passed through something and I'd like to tell me where. Let me add these methods give me images identical to the ones I send, so even slowly, it's very effective. I changed baudrates without any success. Xbee adresses are connected as unicast. I also tried using only Serial.readBytes and I got Bytes faster but as decimal values and is unreadable.
I would really appreciate any information or experience approach to solve this problem. Ask me anything you don't understand about my question. Thanks
Some starting recommendations:
Increase your baud rate to 115200 to increase throughput to/from the XBee.
Add hardware flow control to your setup so you know when the XBee module is "full" of queued data and not receiving.
Switch to API mode on your XBee modules, and generate API frames using the maximum payload size.
Wait for the Transmit Status response on your outbound Transmit frames before sending your next packet.
XBee 802.15.4 modules (including Zigbee and DigiMesh) weren't designed for high throughput. They're low-power, long range devices where you'll be lucky to get 10kbytes/second.

Why am I getting the same performance from Hardware Serial and Software Serial?

I'm trying to interface (1) LinkSprite JPEG Color Camera TTL Interface - Infrared and (2) Arduino Mega 2560 connected to my laptop. While I am able to print the HEX values of the images, it takes about 30 seconds to print 1 image to the monitor. I thought that it was because I was using SoftwareSerial, so I tried HardwareSerial, but still, 30 seconds per image. Shouldn't it be faster using HardwareSerial? Just wondering, do I need a special cable connecting the arduino to my laptop?
I tried different combinations of baud rates for Serial and Serial1. (Serial.begin(9600), Serial1.begin(38400)), (Serial.begin(38400), Serial1.begin(38400)) etc... This doesn't work when I set Serial1 anything higher than 38400. (It should be able to go higher..) Also, do I have to increase baud rate by a certain interval namely, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 250000?
#include <SoftwareSerial.h>
byte incomingbyte;
//Configure pin 2 and 3 as soft serial port
//SoftwareSerial Serial1 = SoftwareSerial(2, 3);
int a=0x0000, //Read Starting address
j=0,
k=0,
count=0;
uint8_t MH,ML;
boolean EndFlag=0;
void setup() {
Serial.begin(115200);
Serial1.begin(38400); //made changes in ChangeBaudRate()
ChangeBaudRate();[enter image description here][1]
SendResetCmd();
delay(3000);
}
void loop() {
SendTakePhotoCmd();
Serial.println("Start pic");
delay(100);
while(Serial1.available()>0) {
incomingbyte=Serial1.read();
}
byte b[32];
while(!EndFlag) {
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(75); //try going up
while(Serial1.available()>0) {
incomingbyte=Serial1.read();
k++;
if((k>5)&&(j<32)&&(!EndFlag)) {
b[j]=incomingbyte;
if((b[j-1]==0xFF)&&(b[j]==0xD9))
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++) {
if(b[j]<0x10)
Serial.print("0");
Serial.print(b[j], HEX);
}
Serial.println();
}
delay(3000);
StopTakePhotoCmd(); //stop this picture so another one can be taken
EndFlag = 0; //reset flag to allow another picture to be read
Serial.println("End of pic");
Serial.println();
while(1);
}
//Send Reset command
void SendResetCmd() {
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x26);
Serial1.write((byte)0x00);
}
//Send take picture command
void SendTakePhotoCmd() {
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x36);
Serial1.write((byte)0x01);
Serial1.write((byte)0x00);
a = 0x0000; //reset so that another picture can taken
}
void FrameSize() {
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x34);
Serial1.write((byte)0x01);
Serial1.write((byte)0x00);
}
//Read data
void SendReadDataCmd() {
MH=a/0x100;
ML=a%0x100;
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x32);
Serial1.write((byte)0x0c);
Serial1.write((byte)0x00);
Serial1.write((byte)0x0a);
Serial1.write((byte)0x00);
Serial1.write((byte)0x00);
Serial1.write((byte)MH);
Serial1.write((byte)ML);
Serial1.write((byte)0x00);
Serial1.write((byte)0x00);
Serial1.write((byte)0x00);
Serial1.write((byte)0x20);
Serial1.write((byte)0x00);
Serial1.write((byte)0x0a);
a+=0x20;
}
void StopTakePhotoCmd() {
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x36);
Serial1.write((byte)0x01);
Serial1.write((byte)0x03);
}
void ChangeBaudRate(){
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x24);
Serial1.write((byte)0x03);
Serial1.write((byte)0x01);
Serial1.write((byte)0x0D); // 115200 = 0x0D 0xA6
Serial1.write((byte)0xA6);
Serial1.end(); // Not really necessary
Serial1.begin( 115200 ); // change to match the camera's new baud rate
}
Yes, the baud rate between the Arduino and the camera is the determining factor. The baud rate between the Arduino and the PC (i.e., the Serial Monitor window) is 9600, but you can change that to 115200 in the code with Serial.begin(115200); and in the Serial Monitor window (pulldown in corner).
The default baud rate between the Arduino and the camera is 38400. While at that baud rate, you can send the "Change Baud Rate" command (section 9 on page 6/10 of the spec). That seems to be the purpose of this routine:
void ChangeBaudRate(){
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x24);
Serial1.write((byte)0x03);
Serial1.write((byte)0x01);
Serial1.write((byte)0x1c);
Serial1.write((byte)0x4c);
}
According to the spec, this sets the new baud rate to 57600. You could choose 115200 by changing the last two bytes to 0x0D and 0xA6. Then you have to set Serial1 to the new baud rate:
void ChangeBaudRate(){
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x24);
Serial1.write((byte)0x03);
Serial1.write((byte)0x01);
Serial1.write((byte)0x0D); // 115200 = 0x0D 0xA6
Serial1.write((byte)0xA6);
Serial1.end(); // Not really necessary
Serial1.begin( 115200 ); // change to match the camera's new baud rate
}

Resources