MEGA and SIM900 ERROR when trying to post using "AT+HTTPACTION=1" - post

I have a SIM900 module connected to an Arduino MEGA, everything works fine except when I try to make a post request, it keeps showing ERROR after the AT+HTTPACTION=1 command is executed, by the SIM900 manual that ERROR message is related to "Mobile Equipment functionality". It worked perfectly but now it keeps showing that annoying ERROR.
This is my code: (I put a fake server address but mine works fine, I have tested it with postman)
void gprsSetup()
{
Serial.println(F("Initializing GPRS module"));
delay(1000);
Serial1.flush();
Serial.flush();
Serial1.println("AT+CGATT?");
delay(1000);
toSerial();
delay(1000);
Serial1.println("AT+CBAND?");
delay(1000);
toSerial();
delay(1000);
Serial1.println("AT+CBAND=\"ALL_BAND\"\r\n");
delay(1000);
toSerial();
delay(1000);
Serial1.print("AT+CGDCONT=1,\"IP\",\"internet.movistar.ve\",\"\",0,0\r\n");
delay(1000);
toSerial();
delay(1000);
Serial1.println("AT+CGDCONT?");
delay(1000);
toSerial();
delay(1000);
Serial1.println("AT+IPR?\r\n");
delay(1000);
toSerial();
delay(1000);
Serial1.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n");
delay(1000);
toSerial();
delay(1000);
Serial1.println("AT+SAPBR=3,1,\"APN\",\"internet.movistar.ve\"\r\n");
delay(1000);
toSerial();
Serial1.println("AT+SAPBR=1,1\r\n");
delay(1000);
toSerial();
Serial1.println("AT+SAPBR=2,1\r\n");
delay(1000);
toSerial();
Serial1.println("AT+CSQ\r\n");
delay(1000);
toSerial();
delay(1000);
Serial1.println("AT+SAPBR=2,1\r\n");
delay(1000);
toSerial();
Serial1.println("AT+HTTPINIT");
delay(1000);
toSerial();
Serial1.println("AT+HTTPPARA=\"CID\",1");
delay(1000);
toSerial();
jsonWriting();
delay(3000);
Serial1.println("AT+HTTPPARA=\"URL\",\"http://myserverthatworks\""); // not my real server address
delay(1000);
toSerial();
Serial1.println("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
delay(1000);
toSerial();
Serial1.println("AT+HTTPDATA=" + String(sendtoserver.length()) + ",10000"); // Server
Serial.println(sendtoserver);
delay(3000);
toSerial();
Serial1.println(sendtoserver);
delay(1000);
toSerial();
Serial1.println("AT+HTTPACTION=1");
delay(3000);
toSerial();
Serial1.println("AT+HTTPREAD");
delay(2000);
toSerial();
delay(2000);
Serial1.println("AT+HTTPTERM");
delay(3000);
toSerial();
}
And here's the output
code output

Use this command
Serial1.println("AT+CMEE=2\r"); // Enable +CME ERROR: <err> result code
delay(1000);
toSerial();
and use verbose values so your module won't respond with a simple "ERROR" string but will tell what is the exact problem. The do:
Serial1.println("AT&W\r"); // save to be persistent after a reset
delay(1000);
toSerial();
Then when calling
Serial1.println("AT+HTTPACTION=1");
delay(3000);
toSerial();
Youwill get the error - most likely with sim900 this will be
CME ERROR: 4 Operation not supported
meaning you have to send the strings via TCP/IP and using
Serial1.println("AT+HTTPACTION=0");
If you get
CME ERROR: 3 Operation not allowed
your network operator does not allow this same as with not supported if
CME ERROR: 30 No network service
up to 40 are network issues you can not influence and errors between 41 and 50 are missing pins,puks, passwords etc.
If you have any othererror message edit your question and I'll try to help

Related

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds problem

A month ago, I created a code and uploaded it to the NodeMCU (ESP8266) in which the NodeMCU establishes connection with the aREST.io MQTT broker. It worked properly.
The code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>
WiFiClient espClient;
PubSubClient client(espClient);
aREST rest = aREST(client);
char* key = "the api key in aREST account";
const char* ssid = "SSID";
const char* password = "Pass";
#define trigger 5
#define echo 4
float distance;
void callback(char* topic, byte* payload, unsigned int length);
void setup(void)
{
Serial.begin(115200);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
rest.setKey(key);
client.setCallback(callback);
rest.variable("distance",&distance);
rest.set_name("esp8266");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
rest.handle(client);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
int timin = pulseIn(echo,HIGH);
distance = (171.5*((timin)))*10E-5;
delay(60);
rest.publish(client, "distance", distance, 60000);
}
void callback(char* topic, byte* payload, unsigned int length) {
rest.handle_callback(client, topic, payload, length);
}
At the moment, the ESP8266 can't connect to the broker. It prints the following message after it connects to WiFi on the serial monitor:
"Attempting MQTT connection...failed, rc=-2 try again in 5 seconds"
I checked the API key about 5 times. it's the right one
I analyzed the network with wireshark and obtained the following:
As shown in the photo, the ESP8266 sends a CONNECT data packet to the aREST broker. The broker responds with the CONNACK packet. It's repeated every 5 seconds the content in the CONNECT packet:
the content of the CONNACK:
I believe the issue may be that the broker is down.
Your code looks like a modified version of the 'ESP8266_cloud.ino' code found in the ArduionIDE examples. I have an ESP8266 D1 and ran that example code with my own API plugin from aREST.io and get the same error message as you.
Running example code 'mqtt_esp8266.ino' I see interaction with my board to the broker, so I don't have reason to believe there's a general hardware or connectivity issue outside of the aREST broker.
Digging into aREST.h, the MQTT broker/server is 'mqtt.arest.io'
Checking on https://downforeveryoneorjustme.com/mqtt.arest.io, they say it's down.
Sorry that's not more helpful :/
I tried contacting the developer/the aREST Instagram page and have yet to hear back. Will keep you posted - please update your findings as well!

ESP8266 (Adafruit Huzzah) disconnects immediately from WiFi, STA disconnect: 203

I have a PM sensor, made for the initiative "sensor community", outside the window, attached to an ESP8266 which connected to a repeater that repeats my home network. Yesterday morning I noticed that suddenly it wasn't publishing the values anymore.
Today I reflashed the board, which is an Adafruit Huzzah with an ESP8266 on board, with the basic example WiFiClientBasic from the ESP82666 library switching on the serial debug of the WiFi.
void setup() {
Serial.begin(115200);
// We start by connecting to a WiFi network
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(ssid, password);
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
The debug yields continuously this error:
[WIFI] Connecting BSSID: SSID: Channel: 6 (-39)
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 0 (12)
wifi evt: 1
STA disconnect: 203
reconnect
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 0 (12)
wifi evt: 1
STA disconnect: 203
reconnect
Another ESP8266 board (a LOLIN Wemos) that had the same configuration refuses to connect. Other devices connected to the same network (my 2 laptops, an Android tablet, a Raspberry Pi) don't have any troubles.
The repeater has a DHCP running and has no problem in releasing IP to other devices. Assigning the ESP8266 a static IP, both on the board and/or on the repeater, has no effect.
I'm not a network expert, but those are the main configs of the repeater (a rather old Digicom REW300).
WLAN STATUS Infrastructure Client --- (Connected)
Signal Strenght 54%
Channel-Band 2.4GHz (G+N) channel 6
Rate 13Mbps (MCS1)
Encryption WPA2-PSK
Repeater Status
WLAN STATUS AP --- (Enabled)
Rate auto
Encryption WPA2-PSK
I also looked at the log on the repeater:
Mar 19 16:01:12 DIGICOM-REW300-Z01 user.warn kernel: wlan0-vxd: A wireless client is deauthenticated - "MAC address of the ESP8266"
Mar 19 16:01:13 DIGICOM-REW300-Z01 user.warn kernel: wlan0-vxd: A wireless client is deauthenticated - "MAC address of the ESP8266"
Mar 19 16:01:15 DIGICOM-REW300-Z01 user.warn kernel: wlan0-vxd: A wireless client is deauthenticated - "MAC address of the ESP8266"
I didn't change the settings of the repeater recently, however yesterday morning the ISP changed the main router with a newer one. I think that could be the problem, but anyway the ESP8266 isn't connecting directly to it (it's too far away) but to the repeater which didn't change at all. Moreover: if I take the ESP8266 inside, it can connect to the main router without any troubles. It seems that the combination of new router and old repeater caused the problem, and just only for the ESP8266s. The only thing that changed from the old router is that the new one has only band G+N only, while the old one had B+G+N, I don't think it matters anyway, as it can be connected directly, and the old repeater is B+G+N.
The official docs from Espressif says that error 203 is ASSOC_FAIL, which is a rather generic error.
Further test I did: if the repeater is tethered with my mobile, the ESP8266 connects.
So:
ESP8266 to repeater to WAN (broken)
ESP8266 to WAN (OK)
ESP8266 to repeater to Mobile Phone to WAN (OK)
Maybe add these lines can help...
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
Here an example with reconection and status connection

Mqtt Client disconnects immediately after connect

when I try to test to connect to my broker and publish it keeps connecting but then failing to stay connected an publish a test. does anyone see a problem in my code?
import paho.mqtt.client as mqtt
import time
broker = "*************"
port = ****
def on_log(client, userdata, level, buf):
print(buf)
def on_connect(client,usedata,flags,rc):
if rc == 0:
client.connected_flag=True ##set flag
print("client is connected")
global connected
else:
print("connection failed")
client.loop_stop()
def on_disconnect(client, userdata, rc):
print("client disconnected ok")
def on_publish(client, userdata, mid):
print("In on_pub callback mid= ", mid)
mqtt.Client.connected_flag=False #create flag in class
client = mqtt.Client("MyClient-01") #create new instance
client.on_log=on_log
client.on_connect=on_connect
client.on_disconnect=on_disconnect
client.on_publish=on_publish
client.connect(broker,port) #establish connection
client.loop_start()
while not client.connected_flag:
print("in wait loop")
time.sleep(1)
time.sleep(3)
print("publishing")
#client.loop()
ret=client.publish("house/bulb1","Test message 0",0)
time.sleep(3)
#client.loop()
ret=client.publish("house/bulb1","Test message 1",1)
time.sleep(3)
#client.loop()
ret=client.publish("house/bulb1","Test message 2",2)
time.sleep(3)
client.loop_stop()
client.disconnect()
and I get this log:
Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b'MyClient-01'
in wait loop
Received CONNACK (0, 5)
connection failed
client disconnected ok
in wait loop
in wait loop
in wait loop
in wait loop
in wait loop
in wait loop
in wait loop
It just stays in the loop to try and connect , broker is found with an ip and port should be the correct one.
The MQTT specification may be some of the clearest documentation I've ever had the pleasure to read, porbably because it is so simple. For version 3.1.1 and the CONNACK message you can find it here:
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718033
You configured the library to log and got this message printed:
Received CONNACK (0, 5)
CONNACK is your message type (response to your CONNECT message). 0 and 5 refer to the Conneck Acknowledge Flags and Connect Return code variables from the CONNACK variable header. 0 means that this is the beginning of a new session and 5 means that you are not authorized, as you figured out.
Fixed with adding username and password arguments and adding
client.username_pw_set(user,password=password)

ESP8266 cannot reconnect to last saved WiFi network

I'm using WifiManager library to manually add user's network on configuration portal. Already it works fine, but every time I power off and power on again, it does not connect to network I have established previous. To connect I have to pin out the 'Vcc' of ESP8266 and pin in again and then again connect with ESP8266 network and go to configure portal.
For now I have two lines in code with 'WifiManager';
Good news is that connects to Wifi by configurationPortal.
ESP8266WebServer server; //server variable
void setup() {
initializePin(); //call function
Serial.begin(74880);
delay(500);
//Connect to network
WiFiManager wifiManager;
wifiManager.autoConnect("ESP8266","password");
Serial.println("Connected.");
if (!MDNS.begin("esp8266")) { Serial.println("Error setting up MDNS responder!"); }
else { Serial.println("mDNS responder started"); }
serverSection();
server.begin();
Serial.println("Server started");
}
I need to connect to previous established network,
Also it would be fine to run Configuration Portal if there wont be connection to that network ( for example if device would be transfer to other place)
Your code is supposed to connect to the internet. What happens usually when you use auto connect and it fails is that WL_IDLE_STATUS occurs. Read more about it here: https://www.arduino.cc/en/Reference/WiFiStatus . What I would do is to check if I got that status then I would try to reconnect to the wifi with a delay of 2 seconds. Here is an example:
if (WiFi.status() == WL_IDLE_STATUS) {
delay(2000);
WiFi.begin("yourssid", "password");
}

ESP8266: Wifi gets connected, but no internet access

I am trying to connect my ESP8266 NodeMCU to my Mobile Hotspot Wifi
When I try to connect to the ESP Wifi using laptop, It says Wifi Connected, no Internet access.
Which means, The Wifi_SSID and Password are fetched by ESP correctly.
Why is the ESP not able to connect to the Internet?
I have used Arduino IDE for uploading code to ESP8266.
I have uploaded the below code to the ESP8266.
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
// Set these to run example.
#define WIFI_SSID "OnePlus3"
#define WIFI_PASSWORD ""
#define FIREBASE_DB_URL "https://my_db_url.firebaseio.com/"
#define FIREBASE_DB_SECRET_KEY "fakezaSyDsdadasddwGClaAy8ltYgywwo6i_VzXgY"
void setup() {
Serial.begin(115200);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Firebase.begin(FIREBASE_DB_URL, FIREBASE_DB_SECRET_KEY);
Firebase.stream("/automation");
}
void loop() {
if (Firebase.failed()) {
Serial.println("streaming error");
Serial.println(Firebase.error());
}
if (Firebase.available()) {
FirebaseObject event = Firebase.readEvent();
String eventType = event.getString("type");
eventType.toLowerCase();
Serial.print(eventType);
if (eventType == "put") {
String path = event.getString("path");
String data = event.getString("data");
if (path.equals("/fan/value")) {
if (data.equals("off")) {
digitalWrite(D1, HIGH);
} else {
digitalWrite(D1, LOW);
}
} else if (path.equals("/light/value")) {
if (data.equals("off")) {
digitalWrite(D2, HIGH);
} else {
digitalWrite(D2, LOW);
}
}
}
}
}
Troubleshooting ESP8266 not connecting to WiFi
Failure to connect to the WiFi network can be caused by some factors. Among them I highlight:
1 - The ESP8266 has a particularity of the initial state of its GPIOs during an initialization. As the intention is to make a connection to the Internet, I suggest that you leave the ESP8266 only with the power and communication connections. If you have a NodeMCU you can use only the USB cable.
2 - Wrong filling in of the access data of the router will cause an error in the connection. Please note that you have to put in your code the WLAN SSID and WLAN PASS information, which are, respectively, the name and password of your network. It is worth remembering that the capture of these characters is case sensitive.
3 - If your router is not connected to the Internet, then you will have feedback via the serial monitor of your local IP, but you will not have confirmation of access with the Google server. Test it with your smartphone or computer and check if everything is fine with your Internet connection.
4 - It is difficult, but I have already seen incompatibility with the internet provider. To solve, test on different networks. I suggest that we carry out tests with your smartphone functioning as a WiFi router. You can even check the connected devices quickly and easily.
You can learn more about it in this link (Eduardo Castro) in Brazilian Portuguese:
https://www.filipeflop.com/blog/como-conectar-o-esp8266-a-internet/

Resources