Error while sending AT command from stm32 to sim 800 - stm32

I am trying to send AT command to stm32 but I am getting error while sending AT command
#include "mbed.h"
#include <string>
Serial pc(SERIAL_TX, SERIAL_RX); // pc comunication
Serial SIM900(PA_9, PA_10); //tx, rx SIM 900
string result;
char x;
void clearString()
{
result.clear();
}
}
void controlAT()
{
clearString();
wait_ms(100);
SIM900.printf("AT\r");
wait_ms(1000);
if(result=="\r\nOK\r\n") {
pc.printf("\r\n----OK AT----\r\n");
}else {
pc.printf("-- ERROR AT --"); //this is a error
}
pc.printf("%s",result.c_str());
}
void sendSMS()
{
pc.printf("\r\n----sendSMS2----\r\n");
clearString();
SIM900.printf("AT+CMGS=");
SIM900.printf("\"");
SIM900.printf("074xxxxx");
SIM900.printf("\"");
SIM900.printf("\r");
wait_ms(200); // this create the problem
SIM900.printf("Hello");
SIM900.printf("\r");
SIM900.putc(0x1A);
wait_ms(200);
pc.printf("%s",result.c_str());
clearString();
}
int main() {
pc.printf("\r\n GSM 900 TEST\n");
SIM900.baud(9600)
wait_ms(200);
controlAT();
wait_ms(200);
sendSMS(); // SEND SMS
wait_ms(200);
}
I am getting error in controlAT() function and it always printing
pc.printf("-- ERROR AT --"); //this is a error
Please help me to resolve this issue

Your code looks wrong, u clear "result" but u never set it.
There should be a line like...
result=SIM900.print...

Related

ESP32 ModBus master half duplex read coil input registers

I'm trying to read a MODBUS sensor via an ESP32.
I'm using the following library: https://github.com/emelianov/modbus-esp8266
I have the following code:
#include <ModbusRTU.h>
#include <SoftwareSerial.h>
SoftwareSerial modBusSerial;
ModbusRTU modbus;
#define startReg 100
#define endReg 123
uint16_t res[endReg - startReg + 1];
// Callback to monitor errors in the modbus
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) {
if (event != Modbus::EX_SUCCESS) {
Serial.print("Request result: 0x");
Serial.print(event, HEX);
}
return true;
}
void setup() {
Serial.begin(115200); // Default serial port (Hardware serial)
modBusSerial.begin(9600, SWSERIAL_8E1, MB_RX, MB_TX); // modbus configuration SWSERIAL_8E1 = 8 bits data, even parity and 1 stop-bit
modbus.begin(&modBusSerial);
modbus.master();
Serial.println("starting modbus...");
while (true) {
Serial.println(modBusSerial.read());
res[endReg - startReg] = 0; // string terminator to allow to use res as char*
if (!modbus.slave()) {
modbus.readIreg(16, startReg, res, endReg - startReg, cb);
}
modbus.task();
Serial.print("result: ");
Serial.println((char*) res);
delay(1000); // every second
}
}
Response I get;
When I do the exact same in QModMaster, I do get the correct output. Anyone any idea what I'm doing wrong here?
These are the settings I use;
I am aware of the "wrong" address in my code. I have 2 identical sensors but one is connected to my computer while the other one is connected to my ESP32.
Thanks in advance!

Arduino mkr1000 does not Serial print upon GET using aRest library

I'm trying to create a REST server on an arduino mkr1000. upon searching google i came upon the aRest library which handles most of the stuff i need.
aRest library documentation
So i created a sample sketch based on the guides. Here is the code:
#include < SPI.h >
#include < WiFi101.h >
#include < aREST.h >
aREST rest = aREST();
int status = WL_IDLE_STATUS;
WiFiServer restServer(80);
char ssid[] = "user"; // not actual username
char pass[] = "pass"; // not actual password
int clapMode(String data){
Serial.println("Request Recieved: " + data);
}
void setup() {
Serial.begin(115200);
rest.set_id("000");
rest.set_name("MKR1000");
rest.function("test",clapMode);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP
network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println();
// you're connected now, so print out the status:
printWifiStatus();
// Start server
restServer.begin();
Serial.println(F("Listening for connections..."));
}
void loop() {
WiFiClient client = restServer.available();
if (!client) {
return;
}
while(!client.available()){
delay(1);
}
rest.handle(client);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
IPAddress subnet = WiFi.subnetMask();
Serial.print("Netmask: ");
Serial.println(subnet);
IPAddress gateway = WiFi.gatewayIP();
Serial.print("Gateway: ");
Serial.println(gateway);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
The code works mostly. Upon using POSTMAN to perform a GET the arduino is able to give the appropriate response.
Now for the part that is not working is the endpoint i just have created using this code
rest.function("test",clapMode);
upon performing a GET in postman, the arduino is able to give a response, but it should perform this code
int clapMode(String data){
Serial.println("Request Recieved: " + data);
}
but on my serial Monitor i am not getting anything.
Also i could not find on how to tailor the response of the arduino from the request. How do i it ?
Thank you very much
You have to send the request to <host>/test?params=0, not <host>/clapMode.
Furthermore, you can modify the ID and name with
rest.set_id("device ID");
rest.set_name("device name");
and add variables with
rest.variable("variable name", &variable);
The response is hard-coded into the library, therefore if you want to add/remove other stuff, you have to edit the library by yourself.

How to get the MAC adress of an access point when esp8266 is in client (station) mode?

I would like my esp8266 to retrieve the mac address of the AP it`s connected to as a client (station), following this discussion on How to get Access Point MAC adress.
Here is my code:
#include <ESP8266WiFi.h>
const char* ssid = "somrmthing";
const char* password = "somrmthing"; //
const char* host = "aubs.gear.host"; //create webserver & correct address
uint8_t MAC_array[6];
char MAC_char[18];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(100);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
int value = 0;
void loop() {
// put your main code here, to run repeatedly:
/*
* http://stackoverflow.com/questions/34078497/esp8266-wificlient-simple-http-get
*/
delay(30000);
++value;
/*
* https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino
*/
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// getting the mac address http://bbs.espressif.com/viewtopic.php?f=15&t=3102&p=10569&hilit=Access+Point+MAC+adress&sid=a68dcff311ea05ece032126d6f93902f#p10569
void wifi_handle_event_cb(System_Event_t *evt)
{
os_printf("event %x\n", evt->event);
switch (evt->event){
case EVENT_STAMODE_CONNECTED:
os_printf("connect to ssid %s, channel %d\n", evt->event_info.connected.ssid, evt->event_info.connected.channel);
os_printf("AP MAC address is %s\n", evt->event_info.connected.bssid);
break;
case ....
....
}
}
//old wrong MAC ADDRESS
// getting the mac address //Serial.println(MAC_char); - See more at: http://www.esp8266.com/viewtopic.php?f=29&t=3587#sthash.bwWPqcc6.dpuf
WiFi.macAddress(MAC_array);
for (int i = 0; i < sizeof(MAC_array); ++i){
sprintf(MAC_char,"%s%02x:",MAC_char,MAC_array[i]);
}
// We now create a URI for the request
String url = "/store.php"; // String url = "/input/";
url += "?dev_id=";
url += "BikeShare9";
url += "&hoster=";
url += MAC_char;
url += "&ip_add=";
url += WiFi.localIP();
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
For void wifi_handle_event_cb(System_Event_t *evt) I get the following error:
C:\Users\Tinotenda\Desktop\ver1.0\ver1.0.ino: In function 'void setup()':
ver1.0:48: error: a function-definition is not allowed here before '{' token
void loop() {
^
ver1.0:129: error: expected '}' at end of input
}
^
exit status 1
a function-definition is not allowed here before '{' token
How can I fix that?
Your setup()-function doesn't have it's closing bracket.
You should also place all the global variables at the top, or make them static inside the function that uses them. (ref: int value = 0 ,although I don't know what you're using the variable for)
Furthermore, avoid the user of very long delays. delay(30000)causes the ESP8266 IP-stack to behave strangely. You should better use the millis-structure:
static unsigned long lastMillis = 0;
if (millis() - lastMillis < 30 * 1000UL) {
lastMillis = millis();
//do your stuff
}
This answer should help other beginners:
Copy and pasting code without understanding the basics is never a good idea.
Basic structure of an Arduino program (=sketch)
Definitions, global vars
int value = 0;
should be placed there
SETUP
void setup(){
All one time functions, initalization routines, etc
}
LOOP
void loop(){
recuring tasks call functions from here in the above example
Call the example function like this
wifi_handle_event_cb(SomeParamToHandOver);
NEVER use delay - it stops processing for the given time
Not a good idea in server client scenarios ->
look for blink without delay in the Arduino examples for more
Never use while in here it may lead to dead locks
}
functions/modules you call from the setup or loop
void wifi_handle_event_cb(System_Event_t *evt) {
Exmple from the question
}
plus all remarks in the answer above
All beginners should start with https://www.arduino.cc/en/Tutorial/BuiltInExamples

TCP Client with select , after connect waits for input

I've ran into a problem with a simple TCP Client implemented using select.
The problem is that,at the second printf it only displays before it gets to the connect() function then waits for user input. Does connect block the rest of the program until i send something? (The TCP server is also implemented using select but i didn't find anything wrong with it)
I've searched on the web and couldn't find a cause or maybe i didn't search for the right thing..
#include <includes.h>
int main()
{
int sfd;
fd_set rset;
char buff[1024]=" ";
char playerName[20]="";
int nameSet=0;
struct sockaddr_in server;
sfd= socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{ printf("socket not created\n"); return 0; }
bzero(&server,sizeof(struct sockaddr_in));
server.sin_family=AF_INET;
server.sin_port=htons(2020);
inet_aton("127.0.0.1",&server.sin_addr);
//here is the problem after %d which calls the connect() function
printf("Conexion returned:%d \n Name:",connect(sfd,(struct sockaddr *)&server,sizeof(server)));
for(;;)
{
bzero(buff,1024);
FD_ZERO(&rset);
FD_SET(0,&rset);
FD_SET(sfd,&rset);
if(select(sfd+1,&rset,NULL,NULL,NULL)<0)
{
printf("con-lost!\n");
break;
}
if(FD_ISSET(0,&rset))
{
printf("Talk: \n");
scanf("%s",buff);
if(nameSet==0)
{
strcpy(playerName,buff);
nameSet=1;
printf("Hi:%s\n",playerName);
}
if(write(sfd,buff,strlen(buff)+10)<0)
{
break;
}
}
if(FD_ISSET(sfd,&rset)>0)
{
if(read(sfd,buff,1024)<=0)
{
printf("con is off!\n");
break;
}
printf("msg rcd %s\n",buff);
}
} //endfor
close(sfd);
return 0;
} //endmain
The connect function, on a blocking socket, blocks until the connect operation succeeds or fails.
You should be warned that using select with a blocking socket, which is what your program does, does not ensure that your program will not block. When you get a select hit, that does not guarantee that a future operation will not block.
strlen(buff)+10
What's the reasoning behind the +10?

Flex - Socket.close() issues (#2031: Socket Error)

I'm having an issue using sockets in flash builder 4. The code below sends a set of bytes to a receiving c# sockerServer. If I dismiss the error I get in flash builder manually, the bytes are sent fine and all comes across as it should on 127.0.0.1:10. Now if I could just get the same results without an error being displayed in Flex.
So, I have two questions:
1) Why does it return an error when I try to close the socket? See closeConnection() below for context. I tried flushing it just before which didn't help.
2) Why is nothing sent when I use socket.flush()?
package
{
import flash.events.IOErrorEvent;
import flash.net.Socket;
import flash.utils.ByteArray;
public class socketClient
{
private var socket:Socket;
public function openConnection(address:String, port:int):void
{
if (socket != null && socket.connected)
socket.close();
socket = new Socket();
try {
socket.connect( address, port );
}
catch( e:Error ) { }
}
public function sendProtocol(p:socketProtocol):void {
//p.serialize() gets me a bunch of bytes in a ByteArray
var buffer:ByteArray = p.serialize();
socket.writeBytes(buffer, 0, buffer.length);
//Nothing happens when I flush
socket.flush();
}
public function closeConnection():void {
//As soon as I get to socket.close(), I get this
//"Unhandled IOErrorEvent:. text=Error #2031: Socket Error."
socket.close();
}
}
}
I use the class like this:
var socket:socketClient = new socketClient();
//works fine, I see the connection on the server
socket.openConnection("127.0.0.1", 10);
//no errors, but nothing sent
socket.sendProtocol(protocol);
//returns the error. (if manually dismissed, data is sent)
socket.closeConnection();
I finally solved it after hammering this one since I posted the question.
I had to add a
socket.addEventListener(flash.events.Event.CLOSE, closeHandler)
and do the socket.close() from there.