Related
I have a problem with my code. When I test it without ThingSpeak library, my sensor values (especially for analogRead on fireLevel dan gasLevel) are there (not 0). But when I use ThingSpeak library for send the data, the fireLevel dan gasLevel show 0 for the values. Any solutions for my code, thanks
I'm still learning
The sensor that I used:
DHT11
KY-026
MQ-135
and for the NodeMCU I used ESP32 type
#include <WiFi.h>
#include "ThingSpeak.h"
#include <SPI.h>
#include <Wire.h>
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const char* ssid = "SECRET_SSID";
const char* password = "SECRET_PASSWORD";
WiFiClient client;
unsigned long channelNum = X;
const char* APIKey = "SECRET_KEY";
#define AIRQ_SENSOR 2 // ESP32 pin GIP2 connected to mq135 sensor
#define DHT_SENSOR_PIN 13 // ESP32 pin GIOP13 connected to DHT11 sensor
#define DHT_SENSOR_TYPE DHT11
#define FIRE_SENSOR_ANALOG 14 // ESP32 pin GIP14 connected to Fire sensor
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
float humi, tempC, gasLevel, fireLevel;
String quality = "";
const uint8_t bitmap19[] = {0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x3f, 0x40, 0x00, 0x7f, 0x60, 0x00, 0xff, 0xe0, 0x00, 0xff, 0xf0, 0x06, 0xff, 0xf0, 0x07, 0xff, 0xf8, 0x07, 0xff, 0xf8, 0x0f, 0xf7, 0xf8, 0x0f, 0xe7, 0xf8, 0x1f, 0xe3, 0xfc, 0x1f, 0xc1, 0xfc, 0x3f, 0x80, 0xfc, 0x3f, 0x80, 0xf8, 0x1f, 0x00, 0xf8, 0x1f, 0x00, 0xf0, 0x0f, 0x00, 0xe0, 0x07, 0x80, 0xc0, 0x01, 0x81, 0x00};
void setup() {
Serial.begin(9600);
dht_sensor.begin(); // initialize the DHT sensor
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(50, 0);
display.println("Air");
display.setTextSize(1);
display.setCursor(23, 20);
display.println("Quality Monitor");
display.display();
delay(1200);
display.clearDisplay();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void fireSensor() {
fireLevel = analogRead(FIRE_SENSOR_ANALOG);
Serial.println("fire level: " + String(fireLevel));
if(fireLevel < 2000) {
display.drawBitmap(90, 35, bitmap19, 24, 24, 1);
}
else {
display.clearDisplay();
}
}
void temphSensor() {
// read humidity
humi = dht_sensor.readHumidity();
// read temperature in Celsius
tempC = dht_sensor.readTemperature();
// check whether the reading is successful or not
if ( isnan(tempC) || isnan(humi)) {
Serial.println("Failed to read from DHT sensor!");
} else {
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 35);
display.println("Tmp:");
display.setCursor(30, 35);
display.println(tempC);
display.setCursor(64, 35);
display.println("C");
display.setCursor(0, 50);
display.println("RH:");
display.setCursor(30, 50);
display.println(humi);
display.setCursor(64, 50);
display.println("%");
}
}
void airqSensor() {
gasLevel = analogRead(AIRQ_SENSOR);
Serial.println("gas Level: " + String(gasLevel));
if(gasLevel < 500) {
quality = "Good";
}
else if(gasLevel < 750) {
// avg 750
quality = "Moderate";
}
else if(gasLevel < 1500) {
// avg 1500
quality = "Unhealty";
}
else {
// 2000 >
quality = "Hazardous";
}
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(30, 5);
display.println("Air Quality");
display.setCursor(35, 20);
display.println(quality);
}
void writeThingSpeak() {
ThingSpeak.setField(1, tempC);
ThingSpeak.setField(2, humi);
ThingSpeak.setField(3, gasLevel);
ThingSpeak.setField(4, fireLevel);
int x = ThingSpeak.writeFields(channelNum, APIKey);
if(x == 200) {
Serial.println("Channel update successful.");
}
else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(15000);
}
void loop() {
display.clearDisplay();
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
fireSensor();
airqSensor();
temphSensor();
display.display();
writeThingSpeak();
}
After further search I found a solution about my question. The problem is I used ADC2 for my sensor (ex: MQ-135 and KY-026) which is used to read an analog. In this case I put my sensor on GPIO2 and GPIO4, which is among the ADC2.
List of ADC2s are: GPIO pins 0, 2, 4, 12, 13, 14, 15, 25, 26 and 27.
So, instead use ADC2 that originally if you used a WiFi connection on your ESP32, just use ADC1.
List of ADC1s are: GPIO pins 32, 33, 34, 35, 36, 37, 38 and 39.
When I changed my AO pins for both of my sensors (MQ-135 and KY-026) to GPIO34 and 35 it worked!
reference: esp32 pinout reference
Hello everyone I'm working for the first time with SHA256 and I'm trying to follow a tutorial on this my problem is to write the equivalence in Objective C of SHA 256. I'm trying to understand the function that I show you below but I still have problems on how to find the equivalence in Objective C of this Swift function
let rsa2048Asn1Header:[UInt8] = [
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00
]
private func sha256(data : Data) -> String {
var keyWithHeader = Data(bytes: rsa2048Asn1Header)
keyWithHeader.append(data)
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
keyWithHeader.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(keyWithHeader.count), &hash)
}
return Data(hash).base64EncodedString()
}
Can you help me ?
Working with raw bytes in Objective-C is generally a little more straightforward than Swift. An implementation like this should be equivalent.
#define RSA_2048_ASN1_HDR_LEN 24
- (NSString *)sha256:(NSData *)data {
NSMutableData *keyWithHeader = [NSMutableData dataWithBytes:rsa2048Asn1Header length:RSA_2048_ASN1_HDR_LEN];
[keyWithHeader appendData:data];
UInt8 hash[CC_SHA256_DIGEST_LENGTH] = { 0 };
CC_SHA256(keyWithHeader.bytes, (CC_LONG) keyWithHeader.length, hash);
return [[NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH] base64EncodedStringWithOptions:0];
}
Note that you'll also need to import the common crypto library into your Objective-C file as well:
#import <CommonCrypto/CommonDigest.h>
I need a clarification. I'm following a tutorial on Hash sha256 in Swift and my app is in Objective-C. I have trouble translating this UInt8 in Objective-C. What is the equivalent in Objective-C?
let rsa2048Asn1Header:[UInt8] = [
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00
]
I tried this but I'm not sure it's right I'd like to have confirmation
UInt8 rsa2048AsnlHeader[] = {
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00};
Yes what you posted above would work if you're just looking to use a primitive array in Objective-C.
Another option would be to use NSArray, shown below:
NSArray<NSNumber *> *rsa2048AsnlHeader = #[
#0x30, #0x82, #0x01, #0x22, #0x30, #0x0d, #0x06, #0x09, #0x2a, #0x86, #0x48, #0x86,
#0xf7, #0x0d, #0x01, #0x01, #0x01, #0x05, #0x00, #0x03, #0x82, #0x01, #0x0f, #0x00
];
To get the equivalent UInt8 value from the NSNumber elements, use the unsignedCharValue property of the NSNumber class (which is equivalent to uint8Value in Swift). For example:
for (NSNumber *value in rsa2048AsnlHeader) {
UInt8 uint8Value = value.unsignedCharValue;
// ...
}
Source
let arr: [UInt8] = [0x14, 0x00, 0xAB, 0x45, 0x49, 0x1F, 0xEF, 0x15, 0xA8, 0x89, 0x78, 0x0F, 0x09, 0xA9, 0x07, 0xB0, 0x01, 0x20, 0x01, 0x4E, 0x38, 0x32, 0x35, 0x56, 0x20, 0x20, 0x20, 0x00]
How can i store in sqlite3 or in NSUserDefaults
i have tried like this
let arrData = NSData(bytes: &arr, length: (arr?.count)!)
let d = NSUserDefaults.standardUserDefaults()
d.setObject(arrData, forKey: "mydata")
d.synchronize()
let obj = d.objectForKey("mydata")
let objData = obj as! NSData
let resultArr = NSKeyedUnarchiver.unarchiveObjectWithData(objData) as! [UInt8]
print(resultArr.count)
For NSUserDefaults:
let integersToStore: [UInt8] = [0x14, 0x00, 0xAB, 0x45, 0x49, 0x1F, 0xEF, 0x15, 0xA8, 0x89, 0x78, 0x0F, 0x09, 0xA9, 0x07, 0xB0, 0x01, 0x20, 0x01, 0x4E, 0x38, 0x32, 0x35, 0x56, 0x20, 0x20, 0x20, 0x00]
let dataToStore = NSData(bytes: integersToStore, length: integersToStore.count)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(dataToStore, forKey: "mydata")
defaults.synchronize()
// get the data we stored in NSUserDefaults
if let readData = defaults.dataForKey("mydata") {
// determine the number of UInt8 to read from this data
let count = readData.length / sizeof(UInt8)
// create a new UInt8 array with the correct count
var readArray = [UInt8](count: count, repeatedValue: 0)
// copy data into array
readData.getBytes(&readArray, length: count * sizeof(UInt8))
// readArray has what you need
print(readArray)
}
Note: Don't expect the correct result when testing this in a playground.
I am trying to conver the following code to swift:
static unsigned char rsa2048Asn1Header[] = {
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00
};
static unsigned char rsa4096Asn1Header[] = {
0x30, 0x82, 0x02, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x02, 0x0f, 0x00
};
static unsigned char ecDsaSecp256r1Asn1Header[] = {
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
0x42, 0x00
};
static unsigned char *asn1HeaderBytes[3] = { rsa2048Asn1Header, rsa4096Asn1Header, ecDsaSecp256r1Asn1Header };
static unsigned int asn1HeaderSizes[3] = { sizeof(rsa2048Asn1Header), sizeof(rsa4096Asn1Header), sizeof(ecDsaSecp256r1Asn1Header) };
My swift code looks like this:
let rsa2048Asn1Header:[CUnsignedChar] = [0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00]
let rsa4096Asn1Header:[CUnsignedChar] = [0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00]
let ecDsaSecp256r1Asn1Header:[CUnsignedChar] = [0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00]
let asn1HeaderBytes:[[CUnsignedChar]] = [rsa2048Asn1Header, rsa4096Asn1Header, ecDsaSecp256r1Asn1Header]
let asn1HeaderSizes:[UInt] = [sizeof(rsa2048Asn1Header.dynamicType).toUInt, sizeof(rsa4096Asn1Header.dynamicType).toUInt, sizeof(ecDsaSecp256r1Asn1Header.dynamicType).toUInt]
However i failing at his badly, per example using rsa2048Asn1Header and converting it to NSData:
let data:NSData = NSData(bytes: rsa2048Asn1Header, length:strideofValue(rsa2048Asn1Header))
swift prints with length 8:
<30820122 300d0609>
objc for the following code:
[NSData dataWithBytes:rsa2048Asn1Header length:sizeof(rsa2048Asn1Header)];
prints
<30820122 300d0609 2a864886 f70d0101 01050003 82010f00> size: 24
looking at apple documentation, strideOfvalue should return the right size for each one of those [CUnsignedChar] but that doesnt seem to be the case, my question is shouldnt a [CUnsignedChar] have same size as usigned char [] in objc, if not what could i use to change?
also [[CUnsignedChar]] makes no sense whatsoever in my head, how would the code below convert to swift, should i convert those arrays to an NSString and extract the CString when required, if so what encoding should I use?
static unsigned char *asn1HeaderBytes[3] = { rsa2048Asn1Header, rsa4096Asn1Header, ecDsaSecp256r1Asn1Header };
I don't think strideofValue is the function you are looking for. I haven't heard of it before, but the documentation states:
Returns the least possible interval between distinct instances of T in memory. The result is always positive.
If this is returning 8, that means that the minimum distance between [CUnsignedChar]s in memory is 8 bytes.
To use NSData(data:length:) properly, the second parameter needs to be the size of the array. You can use the count property for this:
let data: NSData = NSData(bytes: rsa2048Asn1Header, length:rsa2048Asn1Header.count)
[[CUnsignedChar]] is an array of arrays (two-dimensional array) of CUnsignedChar.
Your headers are raw bytes, so technically no encoding is 'correct' to convert to NSString. The cryptographic headers you have don't appear to be strings, and contain unprintable characters.
You should not use strideofValue function. 'strideofValue' it's not a replacement for 'sizeof'. Just go for ".count" property.